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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. if (!start) {
  90. //判断code不为空
  91. if (code) {
  92. options.data = encryption(code, Object.assign(Common, {
  93. ...options.data,
  94. 'appId': appId
  95. }), 2);
  96. }
  97. }
  98. console.log('code', code, options.data)
  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. // 非小程序 非当前用户 提示内容即可
  182. uni.showModal({
  183. title: '提示',
  184. content: res.data.errorMsg,
  185. success: function(res) {
  186. if (res.confirm) {
  187. uni.redirectTo({
  188. url: "/subpackage/after-sale/onlineService"
  189. })
  190. } else if (res.cancel) {
  191. }
  192. }
  193. });
  194. } else {
  195. // 当前车辆已存在订单,无法再次创建订单
  196. if (code == 6 && res.data.statusCode == 704) {
  197. console.log(code == 6, res.data.statusCode == 704)
  198. uni.showModal({
  199. title: '提示',
  200. content: res.data.errorMsg,
  201. cancelText: '解除车牌', //前往解除车牌占用
  202. success: function(res) {
  203. if (res.confirm) {
  204. console.log('用户点击确定2');
  205. } else if (res.cancel) {
  206. uni.redirectTo({
  207. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  208. })
  209. }
  210. }
  211. });
  212. } else if ((code == 'abaf0013caa24dafad12b0f571e8ee40' && res.data.statusCode == 704) ||
  213. (code == '36' && res.data.statusCode == 200)) {
  214. // 从九州过来的无感登录
  215. console.log(code == 6, res.data.statusCode == 704)
  216. uni.showModal({
  217. title: '提示',
  218. content: res.data.errorMsg,
  219. confirmText: '去登录',
  220. showCancel: false,
  221. success: function(res) {
  222. if (res.confirm) {
  223. uni.navigateTo({
  224. url: `/login/login`,
  225. })
  226. }
  227. }
  228. });
  229. } else {
  230. console.log("请求失败返回参数", code, res)
  231. uni.showModal({
  232. title: '提示',
  233. content: res.data.errorMsg,
  234. success: function(res) {
  235. if (res.confirm) {
  236. console.log('用户点击确定2');
  237. } else if (res.cancel) {
  238. console.log('用户点击取消2');
  239. }
  240. }
  241. });
  242. }
  243. }
  244. reject(res.data.errorMsg)
  245. return
  246. } else {
  247. let content = s4.decryptData_CBC(res, sm4Key)
  248. console.log('请求成功返回参数:', code, content.data)
  249. resolve(content.data)
  250. }
  251. }
  252. options.fail = (err) => {
  253. // console.log('请求失败返回参数:', res)
  254. uni.hideLoading()
  255. console.log('请求错误', err)
  256. //处理请求错误
  257. reject(err)
  258. }
  259. uni.getNetworkType({
  260. success: function(res) {
  261. if (res.networkType == 'none') {
  262. uni.showModal({
  263. title: '提示',
  264. content: "网络异常",
  265. success: function(res) {
  266. if (res.confirm) {
  267. console.log('用户点击确定');
  268. } else if (res.cancel) {
  269. console.log('用户点击取消');
  270. }
  271. }
  272. });
  273. } else {
  274. uni.request(options)
  275. }
  276. console.log("res.networkType", res.networkType);
  277. }
  278. });
  279. });
  280. }