Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

request.js 7.5KB

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