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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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,updateTokenNew
  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 + '/ndev/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. uni.reLaunch({
  129. url: '/login/login',
  130. })
  131. }
  132. }
  133. });
  134. }else if (res.data.statusCode == 99999 && res.data.errorMsg=='客户端登录凭证为空') {
  135. console.log("11====",res.data.errorMsg)
  136. jump("17","")
  137. }else if (res.data.statusCode == 99999) {
  138. if (!start) {
  139. updateGetToken().then((data) => {
  140. console.log("token刷新", data);
  141. request(code, options, true)
  142. })
  143. } else {
  144. uni.showModal({
  145. title: '提示',
  146. content: res.data.errorMsg,
  147. success: function(res) {
  148. if (res.confirm) {
  149. console.log('用户点击确定1');
  150. } else if (res.cancel) {
  151. console.log('用户点击取消1');
  152. }
  153. }
  154. });
  155. }
  156. } else if (res.data.statusCode == 7041) {
  157. let orderId = res.data.errorMsg
  158. uni.showModal({
  159. title: '提示',
  160. content: "继续申办",
  161. // cancelText: '解除车牌', //前往解除车牌占用
  162. success: function(res) {
  163. if (res.confirm) {
  164. console.log('继续申办2', orderId);
  165. getOrderList(orderId)
  166. } else if (res.cancel) {
  167. }
  168. }
  169. });
  170. } else if (res.data.statusCode == 7042) {
  171. uni.showModal({
  172. title: '提示',
  173. content: res.data.errorMsg,
  174. confirmText: "去激活",
  175. success: function(res) {
  176. if (res.confirm) {
  177. uni.redirectTo({
  178. url: "/subpackage/personal-center/install-activation-order"
  179. })
  180. } else if (res.cancel) {
  181. }
  182. }
  183. });
  184. } else if (res.data.statusCode == 7043) {
  185. // 非小程序 非当前用户 提示内容即可
  186. uni.showModal({
  187. title: '提示',
  188. content: res.data.errorMsg,
  189. success: function(res) {
  190. if (res.confirm) {
  191. uni.redirectTo({
  192. url: "/subpackage/after-sale/onlineService"
  193. })
  194. } else if (res.cancel) {
  195. }
  196. }
  197. });
  198. } else {
  199. // 当前车辆已存在订单,无法再次创建订单
  200. if (code == 6 && res.data.statusCode == 704) {
  201. console.log(code == 6, res.data.statusCode == 704)
  202. uni.showModal({
  203. title: '提示',
  204. content: res.data.errorMsg,
  205. cancelText: '解除车牌', //前往解除车牌占用
  206. success: function(res) {
  207. if (res.confirm) {
  208. console.log('用户点击确定2');
  209. } else if (res.cancel) {
  210. uni.redirectTo({
  211. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  212. })
  213. }
  214. }
  215. });
  216. } else if ((code == 'abaf0013caa24dafad12b0f571e8ee40' && res.data.statusCode == 704) ||
  217. (code == '36' && res.data.statusCode == 200)) {
  218. // 从九州过来的无感登录
  219. console.log(code == 6, res.data.statusCode == 704)
  220. uni.showModal({
  221. title: '提示',
  222. content: res.data.errorMsg,
  223. confirmText: '去登录',
  224. showCancel: false,
  225. success: function(res) {
  226. if (res.confirm) {
  227. uni.navigateTo({
  228. url: `/login/login`,
  229. })
  230. }
  231. }
  232. });
  233. } else {
  234. if (options.showLoading) {
  235. tools.hideLoadingAlert();
  236. uni.hideToast()
  237. }
  238. console.log("请求失败返回参数", code, res)
  239. uni.showModal({
  240. title: '提示',
  241. content: res.data.errorMsg,
  242. success: function(res) {
  243. if (res.confirm) {
  244. console.log('用户点击确定2');
  245. } else if (res.cancel) {
  246. console.log('用户点击取消2');
  247. }
  248. }
  249. });
  250. }
  251. }
  252. reject(res.data.errorMsg)
  253. return
  254. } else {
  255. console.log("res===",res)
  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. }
  289. /* 刷新token */
  290. function updateGetTokenNew() {
  291. const options = {
  292. type: 2,
  293. data: {
  294. openId: getItem("openId"),
  295. accessToken: getItem(StorageKeys.Token)
  296. },
  297. method: "POST",
  298. showLoading: false,
  299. };
  300. //刷新token
  301. return new Promise(async (resolve, reject) => {
  302. const res = await requestNew(updateTokenNew, options);
  303. const data = res;
  304. console.log("data", data)
  305. setItem('accessToken', data.accessToken);
  306. setItem(StorageKeys.Token, data.accessToken);
  307. setItem('openId', data.openId);
  308. resolve(data);
  309. }).catch((error) => {
  310. reject(error);
  311. });
  312. }
  313. //请求
  314. export function requestNew(code, options = {}, start = false,clie=()=>{}) {
  315. //公参
  316. const Common = {
  317. loginSource: getItem("loginSource"),
  318. accessToken: getItem(StorageKeys.Token)
  319. }
  320. // // #ifdef MP-ALIPAY
  321. // Common['orderSource'] = "ALI"
  322. // // #endif
  323. // // #ifdef MP-WEIXIN
  324. // Common['orderSource'] = "WECHAT"
  325. // // #endif
  326. // options.url = envs[process.env.NODE_ENV].baseUrl+'/prod' + code
  327. options.url = envs[process.env.NODE_ENV].baseUrl+'/newDev/ndev' + code
  328. // options.url = envs[process.env.NODE_ENV].baseUrl+'/ndev' + code
  329. //默认json数据格式提交`
  330. let contentType = 'application/json'
  331. //判断baseUri是否为空
  332. if (options.baseUrlNew) {
  333. options.url = options.baseUrlNew
  334. }
  335. //根据type判断数据传输格式
  336. if (options.type && options.type === 2) {
  337. contentType = 'application/json;charset=UTF-8'
  338. }
  339. //默认POST提交
  340. options.method = options.method ? options.method : 'POST'
  341. //设置请求超时时间
  342. options.timeout = 60000
  343. console.log("getItem(StorageKeys.Token)",getItem(StorageKeys.Token))
  344. options.header = {
  345. 'content-type': contentType,
  346. 'Access-Token': getItem(StorageKeys.Token)
  347. }
  348. if (!start) {
  349. //判断code不为空
  350. if (code) {
  351. options.data =Object.assign(Common,{...options.data});
  352. }
  353. }
  354. console.log('code', code, options.data)
  355. //是否显示加载中
  356. if (options.showLoading) {
  357. uni.showLoading({
  358. title: '请稍后',
  359. mask: true
  360. });
  361. }
  362. //参数返回
  363. return new Promise((resolve, reject) => {
  364. options.success = (res) => {
  365. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  366. if (options.showLoading) {
  367. uni.hideLoading()
  368. }
  369. console.log("返回结果"+code,res,res.statusCode,res.data.code,res.statusCode == 200 && res.data.code == 0)
  370. if (res.statusCode == 200 && res.data.code == 0) {
  371. resolve(res.data.data)
  372. } else {
  373. if(res.data.code == 401002){
  374. // updateGetTokenNew().then((data) => {
  375. // console.log("token刷新", data);
  376. // clie(data)
  377. // requestNew(code, options, true)
  378. // })
  379. // uni.showModal({
  380. // title: '提示',
  381. // content: res.data.message,
  382. // confirmText: '去登录',
  383. // showCancel: false,
  384. // success: function(res) {
  385. // if (res.confirm) {
  386. // uni.navigateTo({
  387. // url: `/login/login`,
  388. // })
  389. // }
  390. // }
  391. // });
  392. }else if(res.data.code == 403001 || res.data.code == 403004 || res.data.code == 403003){
  393. // 会详细列出具体字段的错误信息。
  394. uni.showModal({
  395. title: '提示',
  396. content: res.data.message,
  397. success: function(res) {
  398. if (res.confirm) {
  399. console.log('用户点击确定2');
  400. } else if (res.cancel) {
  401. console.log('用户点击取消2');
  402. }
  403. }
  404. });
  405. }else{
  406. uni.showModal({
  407. title: '提示',
  408. content: res.data.message,
  409. success: function(res) {
  410. if (res.confirm) {
  411. console.log('用户点击确定2');
  412. } else if (res.cancel) {
  413. console.log('用户点击取消2');
  414. }
  415. }
  416. });
  417. }
  418. reject(res.data.message)
  419. return
  420. }
  421. }
  422. options.fail = (err) => {
  423. uni.hideLoading()
  424. console.log('请求错误', err)
  425. //处理请求错误
  426. reject(err)
  427. }
  428. uni.getNetworkType({
  429. success: function(res) {
  430. if (res.networkType == 'none') {
  431. uni.showModal({
  432. title: '提示',
  433. content: "网络异常",
  434. success: function(res) {
  435. if (res.confirm) {
  436. console.log('用户点击确定');
  437. } else if (res.cancel) {
  438. console.log('用户点击取消');
  439. }
  440. }
  441. });
  442. } else {
  443. uni.request(options)
  444. }
  445. console.log("res.networkType", res.networkType);
  446. }
  447. });
  448. });
  449. }