You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import {
  2. appId,
  3. envs
  4. } from "./api";
  5. import {
  6. encryption
  7. } from "./encryption";
  8. import {
  9. setItem,
  10. getItem,
  11. StorageKeys,
  12. removeItem
  13. } from "../storage";
  14. import {
  15. sm4Key
  16. } from "../network/api.js";
  17. import SM4Util from '../util/sm4.js'
  18. import {
  19. agentId,
  20. channelId
  21. } from "@/utils/network/difference";
  22. import {
  23. updateToken
  24. } from "@/utils/network/api";
  25. import orderJump from "@/composables/order/orderJump";
  26. const {
  27. getOrderList
  28. } = orderJump();
  29. import { jump } from "@/datas/9901Jump.js";
  30. const s4 = new SM4Util()
  31. /* 刷新token */
  32. function updateGetToken() {
  33. const options = {
  34. type: 2,
  35. data: {
  36. openId: getItem("openId"),
  37. accessToken: getItem(StorageKeys.Token)
  38. },
  39. method: "POST",
  40. showLoading: false,
  41. };
  42. //刷新token
  43. return new Promise(async (resolve, reject) => {
  44. const res = await request(updateToken, options);
  45. const data = JSON.parse(res.bizContent);
  46. console.log("data", data)
  47. setItem('accessToken', data.accessToken);
  48. setItem(StorageKeys.Token, data.accessToken);
  49. setItem('openId', data.openId);
  50. resolve(data);
  51. }).catch((error) => {
  52. reject(error);
  53. });
  54. }
  55. //请求
  56. export function request(code, options = {}, start = false) {
  57. //公参
  58. const Common = {
  59. agentId: agentId,
  60. channelId: channelId,
  61. channelType: "1",
  62. staffId: "54623263cb4d4a289dccbc983b22a4af",
  63. terminalId: "999999999999",
  64. loginSource: getItem("loginSource"),
  65. rbacSource: 'MINI_PROGRAM',
  66. accessToken: getItem(StorageKeys.Token),
  67. openId: getItem(StorageKeys.OpenId),
  68. opId: getItem(StorageKeys.OpenId),
  69. }
  70. // options.url = envs[process.env.NODE_ENV].baseUrl + '/api/interfaceMidGroundIn'
  71. options.url = envs[process.env.NODE_ENV].baseUrl + '/dev/api/interfaceMidGroundIn'
  72. //默认json数据格式提交`
  73. let contentType = 'application/x-www-form-urlencoded'
  74. //判断baseUri是否为空
  75. if (options.baseUrl) {
  76. options.url = options.baseUrl
  77. }
  78. //根据type判断数据传输格式
  79. if (options.type && options.type === 2) {
  80. contentType = 'application/json;charset=UTF-8'
  81. }
  82. //默认POST提交
  83. options.method = options.method ? options.method : 'POST'
  84. //设置请求超时时间
  85. options.timeout = 60000
  86. options.header = {
  87. 'content-type': contentType,
  88. 'Access-Token': getItem(StorageKeys.Token)
  89. }
  90. if (!start) {
  91. //判断code不为空
  92. if (code) {
  93. options.data = encryption(code, Object.assign(Common, {
  94. ...options.data,
  95. 'appId': appId
  96. }), 2);
  97. }
  98. }
  99. console.log('code', code, options.data)
  100. //是否显示加载中
  101. if (options.showLoading) {
  102. uni.showLoading({
  103. title: '请稍后',
  104. mask: true
  105. });
  106. }
  107. //参数返回
  108. return new Promise((resolve, reject) => {
  109. options.success = (res) => {
  110. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  111. if (options.showLoading) {
  112. uni.hideLoading()
  113. }
  114. if (res.data.statusCode !== 0) {
  115. if (res.data.statusCode == 600) {
  116. resolve(res.data)
  117. } else if (res.data.statusCode == 401) {
  118. uni.showModal({
  119. title: '提示',
  120. content: res.data.errorMsg,
  121. confirmText: '去登录',
  122. showCancel: false,
  123. success: function(res) {
  124. if (res.confirm) {
  125. removeItem(StorageKeys.Token);
  126. removeItem(StorageKeys.OpenId);
  127. removeItem(StorageKeys.QYorder);
  128. uni.reLaunch({
  129. url: '/login/login',
  130. })
  131. }
  132. }
  133. });
  134. }else if (res.data.statusCode == 99999 && res.data.errorMsg=='客户端登录凭证为空') {
  135. console.log("11====",res.data.errorMsg)
  136. jump("17","")
  137. }else if (res.data.statusCode == 99999) {
  138. if (!start) {
  139. updateGetToken().then((data) => {
  140. console.log("token刷新", data);
  141. request(code, options, true)
  142. })
  143. } else {
  144. uni.showModal({
  145. title: '提示',
  146. content: res.data.errorMsg,
  147. success: function(res) {
  148. if (res.confirm) {
  149. console.log('用户点击确定1');
  150. } else if (res.cancel) {
  151. console.log('用户点击取消1');
  152. }
  153. }
  154. });
  155. }
  156. } else if (res.data.statusCode == 7041) {
  157. let orderId = res.data.errorMsg
  158. uni.showModal({
  159. title: '提示',
  160. content: "继续申办",
  161. // cancelText: '解除车牌', //前往解除车牌占用
  162. success: function(res) {
  163. if (res.confirm) {
  164. console.log('继续申办2', orderId);
  165. getOrderList(orderId)
  166. } else if (res.cancel) {
  167. }
  168. }
  169. });
  170. } else if (res.data.statusCode == 7042) {
  171. uni.showModal({
  172. title: '提示',
  173. content: res.data.errorMsg,
  174. confirmText: "去激活",
  175. success: function(res) {
  176. if (res.confirm) {
  177. uni.redirectTo({
  178. url: "/subpackage/personal-center/install-activation-order"
  179. })
  180. } else if (res.cancel) {
  181. }
  182. }
  183. });
  184. } else if (res.data.statusCode == 7043) {
  185. // 非小程序 非当前用户 提示内容即可
  186. uni.showModal({
  187. title: '提示',
  188. content: res.data.errorMsg,
  189. success: function(res) {
  190. if (res.confirm) {
  191. uni.redirectTo({
  192. url: "/subpackage/after-sale/onlineService"
  193. })
  194. } else if (res.cancel) {
  195. }
  196. }
  197. });
  198. } else {
  199. // 当前车辆已存在订单,无法再次创建订单
  200. if (code == 6 && res.data.statusCode == 704) {
  201. console.log(code == 6, res.data.statusCode == 704)
  202. uni.showModal({
  203. title: '提示',
  204. content: res.data.errorMsg,
  205. cancelText: '解除车牌', //前往解除车牌占用
  206. success: function(res) {
  207. if (res.confirm) {
  208. console.log('用户点击确定2');
  209. } else if (res.cancel) {
  210. uni.redirectTo({
  211. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  212. })
  213. }
  214. }
  215. });
  216. } else if ((code == 'abaf0013caa24dafad12b0f571e8ee40' && res.data.statusCode == 704) ||
  217. (code == '36' && res.data.statusCode == 200)) {
  218. // 从九州过来的无感登录
  219. console.log(code == 6, res.data.statusCode == 704)
  220. uni.showModal({
  221. title: '提示',
  222. content: res.data.errorMsg,
  223. confirmText: '去登录',
  224. showCancel: false,
  225. success: function(res) {
  226. if (res.confirm) {
  227. uni.navigateTo({
  228. url: `/login/login`,
  229. })
  230. }
  231. }
  232. });
  233. } else {
  234. console.log("请求失败返回参数", code, res)
  235. uni.showModal({
  236. title: '提示',
  237. content: res.data.errorMsg,
  238. success: function(res) {
  239. if (res.confirm) {
  240. console.log('用户点击确定2');
  241. } else if (res.cancel) {
  242. console.log('用户点击取消2');
  243. }
  244. }
  245. });
  246. }
  247. }
  248. reject(res.data.errorMsg)
  249. return
  250. } else {
  251. let content = s4.decryptData_CBC(res, sm4Key)
  252. console.log('请求成功返回参数:', code, content.data)
  253. resolve(content.data)
  254. }
  255. }
  256. options.fail = (err) => {
  257. // console.log('请求失败返回参数:', res)
  258. uni.hideLoading()
  259. console.log('请求错误', err)
  260. //处理请求错误
  261. reject(err)
  262. }
  263. uni.getNetworkType({
  264. success: function(res) {
  265. if (res.networkType == 'none') {
  266. uni.showModal({
  267. title: '提示',
  268. content: "网络异常",
  269. success: function(res) {
  270. if (res.confirm) {
  271. console.log('用户点击确定');
  272. } else if (res.cancel) {
  273. console.log('用户点击取消');
  274. }
  275. }
  276. });
  277. } else {
  278. uni.request(options)
  279. }
  280. console.log("res.networkType", res.networkType);
  281. }
  282. });
  283. });
  284. }