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

request.js 12KB

2年前
11ヶ月前
1年前
1年前
7ヶ月前
1年前
11ヶ月前
2年前
10ヶ月前
2年前
2年前
1年前
2年前
1年前
1年前
9ヶ月前
7ヶ月前
1年前
1年前
1年前
11ヶ月前
11ヶ月前
11ヶ月前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
11ヶ月前
1年前
1年前
10ヶ月前
1年前
1年前
9ヶ月前
1年前
1年前
1年前
1年前
1年前
8ヶ月前
7ヶ月前
8ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
6ヶ月前
7ヶ月前
8ヶ月前
7ヶ月前
8ヶ月前
8ヶ月前
7ヶ月前
7ヶ月前
8ヶ月前
7ヶ月前
7ヶ月前
7ヶ月前
7ヶ月前
7ヶ月前
7ヶ月前
8ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. agentId: agentId,
  318. channelId: channelId,
  319. channelType: "1",
  320. staffId: "54623263cb4d4a289dccbc983b22a4af",
  321. terminalId: "999999999999",
  322. loginSource: getItem("loginSource"),
  323. // rbacSource: 'MINI_PROGRAM',
  324. // accessToken: getItem(StorageKeys.Token),
  325. openId: getItem(StorageKeys.OpenId),
  326. opId: getItem(StorageKeys.OpenId),
  327. }
  328. // #ifdef MP-ALIPAY
  329. Common['orderSource'] = "ALI"
  330. // #endif
  331. // #ifdef MP-WEIXIN
  332. Common['orderSource'] = "WECHAT"
  333. // #endif
  334. // options.url = envs[process.env.NODE_ENV].baseUrl+'/prod' + code
  335. options.url = envs[process.env.NODE_ENV].baseUrl+'/ndev' + code
  336. //默认json数据格式提交`
  337. let contentType = 'application/json'
  338. //判断baseUri是否为空
  339. if (options.baseUrlNew) {
  340. options.url = options.baseUrlNew
  341. }
  342. //根据type判断数据传输格式
  343. if (options.type && options.type === 2) {
  344. contentType = 'application/json;charset=UTF-8'
  345. }
  346. //默认POST提交
  347. options.method = options.method ? options.method : 'POST'
  348. //设置请求超时时间
  349. options.timeout = 60000
  350. console.log("getItem(StorageKeys.Token)",getItem(StorageKeys.Token))
  351. options.header = {
  352. 'content-type': contentType,
  353. 'Access-Token': getItem(StorageKeys.Token)
  354. }
  355. if (!start) {
  356. //判断code不为空
  357. if (code) {
  358. options.data =Object.assign(Common,{...options.data});
  359. }
  360. }
  361. console.log('code', code, options.data)
  362. //是否显示加载中
  363. if (options.showLoading) {
  364. uni.showLoading({
  365. title: '请稍后',
  366. mask: true
  367. });
  368. }
  369. //参数返回
  370. return new Promise((resolve, reject) => {
  371. options.success = (res) => {
  372. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  373. if (options.showLoading) {
  374. uni.hideLoading()
  375. }
  376. console.log("返回结果"+code,res,res.statusCode,res.data.code,res.statusCode == 200 && res.data.code == 0)
  377. if (res.statusCode == 200 && res.data.code == 0) {
  378. resolve(res.data.data)
  379. } else {
  380. // 未知错误:登录凭证失效!
  381. if(res.data.code == 403003){
  382. uni.showModal({
  383. title: '提示',
  384. content: res.data.message,
  385. confirmText: '去登录',
  386. showCancel: false,
  387. success: function(res) {
  388. if (res.confirm) {
  389. uni.navigateTo({
  390. url: `/login/login`,
  391. })
  392. }
  393. }
  394. });
  395. }else if(res.data.code == 401002){
  396. updateGetTokenNew().then((data) => {
  397. console.log("token刷新", data);
  398. clie(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. }