選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

request.js 7.3KB

2年前
11ヶ月前
1年前
1年前
1年前
2年前
2年前
2年前
1年前
2年前
1年前
1年前
11ヶ月前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
11ヶ月前
1年前
1年前
11ヶ月前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
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. const tools = require("../../static/etcUtil/tools.js");
  18. import SM4Util from '../util/sm4.js'
  19. import {
  20. agentId,
  21. channelId
  22. } from "@/utils/network/difference";
  23. import {
  24. updateToken
  25. } from "@/utils/network/api";
  26. import orderJump from "@/composables/order/orderJump";
  27. const {
  28. getOrderList
  29. } = orderJump();
  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) {
  135. if (!start) {
  136. updateGetToken().then((data) => {
  137. console.log("token刷新", data);
  138. request(code, options, true)
  139. })
  140. } else {
  141. uni.showModal({
  142. title: '提示',
  143. content: res.data.errorMsg,
  144. success: function(res) {
  145. if (res.confirm) {
  146. console.log('用户点击确定1');
  147. } else if (res.cancel) {
  148. console.log('用户点击取消1');
  149. }
  150. }
  151. });
  152. }
  153. } else if (res.data.statusCode == 7041) {
  154. let orderId = res.data.errorMsg
  155. uni.showModal({
  156. title: '提示',
  157. content: "继续申办",
  158. // cancelText: '解除车牌', //前往解除车牌占用
  159. success: function(res) {
  160. if (res.confirm) {
  161. console.log('继续申办2', orderId);
  162. getOrderList(orderId)
  163. } else if (res.cancel) {
  164. }
  165. }
  166. });
  167. } else if (res.data.statusCode == 7042) {
  168. uni.showModal({
  169. title: '提示',
  170. content: res.data.errorMsg,
  171. confirmText: "去激活",
  172. success: function(res) {
  173. if (res.confirm) {
  174. uni.redirectTo({
  175. url: "/subpackage/personal-center/install-activation-order"
  176. })
  177. } else if (res.cancel) {
  178. }
  179. }
  180. });
  181. } else if (res.data.statusCode == 7043) {
  182. // 非小程序 非当前用户 提示内容即可
  183. uni.showModal({
  184. title: '提示',
  185. content: res.data.errorMsg,
  186. success: function(res) {
  187. if (res.confirm) {
  188. uni.redirectTo({
  189. url: "/subpackage/after-sale/onlineService"
  190. })
  191. } else if (res.cancel) {
  192. }
  193. }
  194. });
  195. } else {
  196. // 当前车辆已存在订单,无法再次创建订单
  197. if (code == 6 && res.data.statusCode == 704) {
  198. console.log(code == 6, res.data.statusCode == 704)
  199. uni.showModal({
  200. title: '提示',
  201. content: res.data.errorMsg,
  202. cancelText: '解除车牌', //前往解除车牌占用
  203. success: function(res) {
  204. if (res.confirm) {
  205. console.log('用户点击确定2');
  206. } else if (res.cancel) {
  207. uni.redirectTo({
  208. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  209. })
  210. }
  211. }
  212. });
  213. } else if ((code == 'abaf0013caa24dafad12b0f571e8ee40' && res.data.statusCode == 704) ||
  214. (code == '36' && res.data.statusCode == 200)) {
  215. // 从九州过来的无感登录
  216. console.log(code == 6, res.data.statusCode == 704)
  217. uni.showModal({
  218. title: '提示',
  219. content: res.data.errorMsg,
  220. confirmText: '去登录',
  221. showCancel: false,
  222. success: function(res) {
  223. if (res.confirm) {
  224. uni.navigateTo({
  225. url: `/login/login`,
  226. })
  227. }
  228. }
  229. });
  230. } else {
  231. tools.hideLoadingAlert();
  232. console.log("请求失败返回参数", code, res)
  233. uni.showModal({
  234. title: '提示',
  235. content: res.data.errorMsg,
  236. success: function(res) {
  237. if (res.confirm) {
  238. console.log('用户点击确定2');
  239. } else if (res.cancel) {
  240. console.log('用户点击取消2');
  241. }
  242. }
  243. });
  244. }
  245. }
  246. reject(res.data.errorMsg)
  247. return
  248. } else {
  249. let content = s4.decryptData_CBC(res, sm4Key)
  250. console.log('请求成功返回参数:', code, content.data)
  251. resolve(content.data)
  252. }
  253. }
  254. options.fail = (err) => {
  255. // console.log('请求失败返回参数:', res)
  256. uni.hideLoading()
  257. console.log('请求错误', err)
  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. }