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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. 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. console.log("res===",res)
  257. let content = s4.decryptData_CBC(res, sm4Key)
  258. console.log('请求成功返回参数:', code, content.data)
  259. resolve(content.data)
  260. }
  261. }
  262. options.fail = (err) => {
  263. uni.hideLoading()
  264. console.log('请求错误', err)
  265. //处理请求错误
  266. reject(err)
  267. }
  268. uni.getNetworkType({
  269. success: function(res) {
  270. if (res.networkType == 'none') {
  271. uni.showModal({
  272. title: '提示',
  273. content: "网络异常",
  274. success: function(res) {
  275. if (res.confirm) {
  276. console.log('用户点击确定');
  277. } else if (res.cancel) {
  278. console.log('用户点击取消');
  279. }
  280. }
  281. });
  282. } else {
  283. uni.request(options)
  284. }
  285. console.log("res.networkType", res.networkType);
  286. }
  287. });
  288. });
  289. }
  290. /* 刷新token */
  291. function updateGetTokenNew() {
  292. const options = {
  293. type: 2,
  294. data: {
  295. openId: getItem("openId"),
  296. accessToken: getItem(StorageKeys.Token)
  297. },
  298. method: "POST",
  299. showLoading: false,
  300. };
  301. //刷新token
  302. return new Promise(async (resolve, reject) => {
  303. const res = await requestNew(updateTokenNew, options);
  304. const data = res;
  305. console.log("data", data)
  306. setItem('accessToken', data.accessToken);
  307. setItem(StorageKeys.Token, data.accessToken);
  308. setItem('openId', data.openId);
  309. resolve(data);
  310. }).catch((error) => {
  311. reject(error);
  312. });
  313. }
  314. //请求
  315. export function requestNew(code, options = {}, start = false) {
  316. //公参
  317. const Common = {
  318. agentId: agentId,
  319. channelId: channelId,
  320. channelType: "1",
  321. staffId: "54623263cb4d4a289dccbc983b22a4af",
  322. terminalId: "999999999999",
  323. loginSource: getItem("loginSource"),
  324. // rbacSource: 'MINI_PROGRAM',
  325. // accessToken: getItem(StorageKeys.Token),
  326. openId: getItem(StorageKeys.OpenId),
  327. opId: getItem(StorageKeys.OpenId),
  328. }
  329. // #ifdef MP-ALIPAY
  330. Common['orderSource'] = "ALI"
  331. // #endif
  332. // #ifdef MP-WEIXIN
  333. Common['orderSource'] = "WECHAT"
  334. // #endif
  335. // options.url = envs[process.env.NODE_ENV].baseUrl+'/prod' + code
  336. options.url = envs[process.env.NODE_ENV].baseUrl+'/ndev' + code
  337. //默认json数据格式提交`
  338. let contentType = 'application/json'
  339. //判断baseUri是否为空
  340. if (options.baseUrlNew) {
  341. options.url = options.baseUrlNew
  342. }
  343. //根据type判断数据传输格式
  344. if (options.type && options.type === 2) {
  345. contentType = 'application/json;charset=UTF-8'
  346. }
  347. //默认POST提交
  348. options.method = options.method ? options.method : 'POST'
  349. //设置请求超时时间
  350. options.timeout = 60000
  351. console.log("getItem(StorageKeys.Token)",getItem(StorageKeys.Token))
  352. options.header = {
  353. 'content-type': contentType,
  354. 'Access-Token': getItem(StorageKeys.Token)
  355. }
  356. if (!start) {
  357. //判断code不为空
  358. if (code) {
  359. options.data =Object.assign(Common,{...options.data});
  360. }
  361. }
  362. console.log('code', code, options.data)
  363. //是否显示加载中
  364. if (options.showLoading) {
  365. uni.showLoading({
  366. title: '请稍后',
  367. mask: true
  368. });
  369. }
  370. //参数返回
  371. return new Promise((resolve, reject) => {
  372. options.success = (res) => {
  373. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  374. if (options.showLoading) {
  375. uni.hideLoading()
  376. }
  377. console.log("返回结果"+code,res)
  378. if (res.statusCode == 200 && res.data.code == 0) {
  379. resolve(res.data.data)
  380. } else {
  381. // 未知错误:登录凭证失效!
  382. if(res.data.code == 403003 && res.data.message=='未知错误:登录凭证失效!'){
  383. uni.showModal({
  384. title: '提示',
  385. content: res.data.message,
  386. confirmText: '去登录',
  387. showCancel: false,
  388. success: function(res) {
  389. if (res.confirm) {
  390. uni.navigateTo({
  391. url: `/login/login`,
  392. })
  393. }
  394. }
  395. });
  396. }else if(res.data.code == 401002){
  397. updateGetTokenNew().then((data) => {
  398. console.log("token刷新", data);
  399. requestNew(code, options, true)
  400. })
  401. }else if(res.data.code == 403001 || res.data.code == 403004){
  402. // 会详细列出具体字段的错误信息。
  403. uni.showModal({
  404. title: '提示',
  405. content: res.data.message,
  406. success: function(res) {
  407. if (res.confirm) {
  408. console.log('用户点击确定2');
  409. } else if (res.cancel) {
  410. console.log('用户点击取消2');
  411. }
  412. }
  413. });
  414. }else{
  415. uni.showModal({
  416. title: '提示',
  417. content: res.data.message,
  418. success: function(res) {
  419. if (res.confirm) {
  420. console.log('用户点击确定2');
  421. } else if (res.cancel) {
  422. console.log('用户点击取消2');
  423. }
  424. }
  425. });
  426. }
  427. reject(res.data.message)
  428. return
  429. }
  430. }
  431. options.fail = (err) => {
  432. uni.hideLoading()
  433. console.log('请求错误', err)
  434. //处理请求错误
  435. reject(err)
  436. }
  437. uni.getNetworkType({
  438. success: function(res) {
  439. if (res.networkType == 'none') {
  440. uni.showModal({
  441. title: '提示',
  442. content: "网络异常",
  443. success: function(res) {
  444. if (res.confirm) {
  445. console.log('用户点击确定');
  446. } else if (res.cancel) {
  447. console.log('用户点击取消');
  448. }
  449. }
  450. });
  451. } else {
  452. uni.request(options)
  453. }
  454. console.log("res.networkType", res.networkType);
  455. }
  456. });
  457. });
  458. }