您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

request.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. console.log('请求成功返回参数:', code, 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. 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 {
  213. uni.showModal({
  214. title: '提示',
  215. content: res.data.errorMsg,
  216. success: function(res) {
  217. if (res.confirm) {
  218. console.log('用户点击确定2');
  219. } else if (res.cancel) {
  220. console.log('用户点击取消2');
  221. }
  222. }
  223. });
  224. }
  225. }
  226. reject(res.data.errorMsg)
  227. return
  228. } else {
  229. let content = s4.decryptData_CBC(res, sm4Key)
  230. console.log('请求成功返回参数:', code, content)
  231. resolve(content.data)
  232. }
  233. }
  234. options.fail = (err) => {
  235. // console.log('请求失败返回参数:', res)
  236. uni.hideLoading()
  237. console.log('请求错误', err)
  238. // if (err == 'openId无效,请核实传参!') {
  239. // setItem(StorageKeys.OpenId, openId);
  240. // }
  241. //处理请求错误
  242. reject(err)
  243. }
  244. uni.getNetworkType({
  245. success: function(res) {
  246. if (res.networkType == 'none') {
  247. uni.showModal({
  248. title: '提示',
  249. content: "网络异常",
  250. success: function(res) {
  251. if (res.confirm) {
  252. console.log('用户点击确定');
  253. } else if (res.cancel) {
  254. console.log('用户点击取消');
  255. }
  256. }
  257. });
  258. } else {
  259. uni.request(options)
  260. }
  261. console.log("res.networkType", res.networkType);
  262. }
  263. });
  264. });
  265. }