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.3KB

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