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

2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
11 months ago
9 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
9 months ago
9 months ago
9 months ago
9 months ago
1 week ago
9 months ago
1 week ago
1 month ago
9 months ago
9 months ago
9 months ago
9 months ago
1 month ago
9 months ago
9 months ago
9 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. import {
  2. appId,
  3. envs
  4. } from "./api";
  5. import {
  6. encryption,
  7. sign,
  8. arrayToJson
  9. } from "./encryption";
  10. import {
  11. setItem,
  12. getItem,
  13. StorageKeys,
  14. removeItem
  15. } from "../storage";
  16. import {
  17. sm4Key
  18. } from "../network/api.js";
  19. const tools = require("../../static/etcUtil/tools.js");
  20. import SM4Util from '../util/sm4.js'
  21. import {
  22. agentId,
  23. channelId
  24. } from "@/utils/network/difference";
  25. import {
  26. updateToken,
  27. updateTokenNew
  28. } from "@/utils/network/api";
  29. import orderJump from "@/composables/order/orderJump";
  30. const {
  31. getOrderList
  32. } = orderJump();
  33. import {
  34. jump
  35. } from "@/datas/9901Jump.js";
  36. const s4 = new SM4Util()
  37. /* 刷新token */
  38. function updateGetToken() {
  39. const options = {
  40. type: 2,
  41. data: {
  42. openId: getItem("openId"),
  43. accessToken: getItem(StorageKeys.Token)
  44. },
  45. method: "POST",
  46. showLoading: false,
  47. };
  48. //刷新token
  49. return new Promise(async (resolve, reject) => {
  50. const res = await request(updateToken, options);
  51. const data = JSON.parse(res.bizContent);
  52. console.log("data", data)
  53. setItem('accessToken', data.accessToken);
  54. setItem(StorageKeys.Token, data.accessToken);
  55. setItem('openId', data.openId);
  56. resolve(data);
  57. }).catch((error) => {
  58. reject(error);
  59. });
  60. }
  61. //请求
  62. export function request(code, options = {}, start = false) {
  63. //公参
  64. const Common = {
  65. agentId: agentId,
  66. channelId: channelId,
  67. channelType: "1",
  68. staffId: "54623263cb4d4a289dccbc983b22a4af",
  69. terminalId: "999999999999",
  70. loginSource: getItem("loginSource"),
  71. rbacSource: 'MINI_PROGRAM',
  72. accessToken: getItem(StorageKeys.Token),
  73. openId: getItem(StorageKeys.OpenId),
  74. opId: getItem(StorageKeys.OpenId),
  75. }
  76. // options.url = envs[process.env.NODE_ENV].baseUrl + '/api/interfaceMidGroundIn'
  77. options.url = envs[process.env.NODE_ENV].baseUrl + '/ndev/api/interfaceMidGroundIn'
  78. //默认json数据格式提交`
  79. let contentType = 'application/x-www-form-urlencoded'
  80. //判断baseUri是否为空
  81. if (options.baseUrl) {
  82. options.url = options.baseUrl
  83. }
  84. //根据type判断数据传输格式
  85. if (options.type && options.type === 2) {
  86. contentType = 'application/json;charset=UTF-8'
  87. }
  88. //默认POST提交
  89. options.method = options.method ? options.method : 'POST'
  90. //设置请求超时时间
  91. options.timeout = 60000
  92. options.header = {
  93. 'content-type': contentType,
  94. 'Access-Token': getItem(StorageKeys.Token)
  95. }
  96. if (!start) {
  97. //判断code不为空
  98. if (code) {
  99. options.data = encryption(code, Object.assign(Common, {
  100. ...options.data,
  101. 'appId': appId
  102. }), 2);
  103. }
  104. }
  105. console.log('code', code, options.data)
  106. //是否显示加载中
  107. if (options.showLoading) {
  108. uni.showLoading({
  109. title: '请稍后',
  110. mask: true
  111. });
  112. }
  113. //参数返回
  114. return new Promise((resolve, reject) => {
  115. options.success = (res) => {
  116. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  117. if (options.showLoading) {
  118. uni.hideLoading()
  119. }
  120. if (res.data.statusCode !== 0) {
  121. if (res.data.statusCode == 600) {
  122. resolve(res.data)
  123. } else if (res.data.statusCode == 401) {
  124. uni.showModal({
  125. title: '提示',
  126. content: res.data.errorMsg,
  127. confirmText: '去登录',
  128. showCancel: false,
  129. success: function(res) {
  130. if (res.confirm) {
  131. removeItem(StorageKeys.Token);
  132. removeItem(StorageKeys.OpenId);
  133. uni.reLaunch({
  134. url: '/login/login',
  135. })
  136. }
  137. }
  138. });
  139. } else if (res.data.statusCode == 99999 && res.data.errorMsg == '客户端登录凭证为空') {
  140. console.log("11====", res.data.errorMsg)
  141. jump("17", "")
  142. } else if (res.data.statusCode == 99999) {
  143. if (!start) {
  144. updateGetToken().then((data) => {
  145. console.log("token刷新", data);
  146. request(code, options, true)
  147. })
  148. } else {
  149. uni.showModal({
  150. title: '提示',
  151. content: res.data.errorMsg,
  152. success: function(res) {
  153. if (res.confirm) {
  154. console.log('用户点击确定1');
  155. } else if (res.cancel) {
  156. console.log('用户点击取消1');
  157. }
  158. }
  159. });
  160. }
  161. } else if (res.data.statusCode == 7041) {
  162. let orderId = res.data.errorMsg
  163. uni.showModal({
  164. title: '提示',
  165. content: "继续申办",
  166. // cancelText: '解除车牌', //前往解除车牌占用
  167. success: function(res) {
  168. if (res.confirm) {
  169. console.log('继续申办2', orderId);
  170. getOrderList(orderId)
  171. } else if (res.cancel) {
  172. }
  173. }
  174. });
  175. } else if (res.data.statusCode == 7042) {
  176. uni.showModal({
  177. title: '提示',
  178. content: res.data.errorMsg,
  179. confirmText: "去激活",
  180. success: function(res) {
  181. if (res.confirm) {
  182. uni.redirectTo({
  183. url: "/subpackage/personal-center/install-activation-order"
  184. })
  185. } else if (res.cancel) {
  186. }
  187. }
  188. });
  189. } else if (res.data.statusCode == 7043) {
  190. // 非小程序 非当前用户 提示内容即可
  191. uni.showModal({
  192. title: '提示',
  193. content: res.data.errorMsg,
  194. success: function(res) {
  195. if (res.confirm) {
  196. uni.redirectTo({
  197. url: "/subpackage/after-sale/onlineService"
  198. })
  199. } else if (res.cancel) {
  200. }
  201. }
  202. });
  203. } else {
  204. // 当前车辆已存在订单,无法再次创建订单
  205. if (code == 6 && res.data.statusCode == 704) {
  206. console.log(code == 6, res.data.statusCode == 704)
  207. uni.showModal({
  208. title: '提示',
  209. content: res.data.errorMsg,
  210. cancelText: '解除车牌', //前往解除车牌占用
  211. success: function(res) {
  212. if (res.confirm) {
  213. console.log('用户点击确定2');
  214. } else if (res.cancel) {
  215. uni.redirectTo({
  216. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  217. })
  218. }
  219. }
  220. });
  221. } else if ((code == 'abaf0013caa24dafad12b0f571e8ee40' && res.data.statusCode == 704) ||
  222. (code == '36' && res.data.statusCode == 200)) {
  223. // 从九州过来的无感登录
  224. console.log(code == 6, res.data.statusCode == 704)
  225. uni.showModal({
  226. title: '提示',
  227. content: res.data.errorMsg,
  228. confirmText: '去登录',
  229. showCancel: false,
  230. success: function(res) {
  231. if (res.confirm) {
  232. uni.navigateTo({
  233. url: `/login/login`,
  234. })
  235. }
  236. }
  237. });
  238. } else {
  239. if (options.showLoading) {
  240. tools.hideLoadingAlert();
  241. uni.hideToast()
  242. }
  243. console.log("请求失败返回参数", code, res)
  244. uni.showModal({
  245. title: '提示',
  246. content: res.data.errorMsg,
  247. success: function(res) {
  248. if (res.confirm) {
  249. console.log('用户点击确定2');
  250. } else if (res.cancel) {
  251. console.log('用户点击取消2');
  252. }
  253. }
  254. });
  255. }
  256. }
  257. reject(res.data.errorMsg)
  258. return
  259. } else {
  260. console.log("res===", res)
  261. let content = s4.decryptData_CBC(res, sm4Key)
  262. console.log('请求成功返回参数:', code, content.data)
  263. resolve(content.data)
  264. }
  265. }
  266. options.fail = (err) => {
  267. uni.hideLoading()
  268. console.log('请求错误', err)
  269. //处理请求错误
  270. reject(err)
  271. }
  272. uni.getNetworkType({
  273. success: function(res) {
  274. if (res.networkType == 'none') {
  275. uni.showModal({
  276. title: '提示',
  277. content: "网络异常",
  278. success: function(res) {
  279. if (res.confirm) {
  280. console.log('用户点击确定');
  281. } else if (res.cancel) {
  282. console.log('用户点击取消');
  283. }
  284. }
  285. });
  286. } else {
  287. uni.request(options)
  288. }
  289. console.log("res.networkType", res.networkType);
  290. }
  291. });
  292. });
  293. }
  294. /* 刷新token */
  295. function updateGetTokenNew() {
  296. const options = {
  297. type: 2,
  298. data: {
  299. openId: getItem("openId"),
  300. accessToken: getItem(StorageKeys.Token)
  301. },
  302. method: "POST",
  303. showLoading: false,
  304. };
  305. //刷新token
  306. return new Promise(async (resolve, reject) => {
  307. const res = await requestNew(updateTokenNew, options);
  308. const data = res;
  309. console.log("data", data)
  310. setItem('accessToken', data.accessToken);
  311. setItem(StorageKeys.Token, data.accessToken);
  312. setItem('openId', data.openId);
  313. resolve(data);
  314. }).catch((error) => {
  315. reject(error);
  316. });
  317. }
  318. //请求
  319. export function requestNew(code, options = {}, start = false, clie = () => {}) {
  320. //公参
  321. const Common = {
  322. loginSource: getItem("loginSource"),
  323. // <<<<<<< HEAD
  324. // customerId: getItem("customerObj").customerId,
  325. // accessToken: getItem(StorageKeys.Token)
  326. // =======
  327. accessToken: getItem(StorageKeys.Token),
  328. customerId: getItem("customerObj").customerId,
  329. // >>>>>>> 2bd4fd2288160c9b41c882d376c6debd9051e296
  330. }
  331. console.log('Common',Common, getItem("customerObj"))
  332. // options.url = envs[process.env.NODE_ENV].baseUrl+'/prod' + code
  333. options.url = envs[process.env.NODE_ENV].baseUrl + '/newDev/ndev' + code
  334. //默认json数据格式提交`
  335. let contentType = 'application/json'
  336. //判断baseUri是否为空
  337. if (options.baseUrlNew) {
  338. options.url = options.baseUrlNew
  339. }
  340. //根据type判断数据传输格式
  341. if (options.type && options.type === 2) {
  342. contentType = 'application/json;charset=UTF-8'
  343. }
  344. //默认POST提交
  345. options.method = options.method ? options.method : 'POST'
  346. //设置请求超时时间
  347. options.timeout = 60000
  348. console.log("getItem(StorageKeys.Token)", getItem(StorageKeys.Token))
  349. options.header = {
  350. 'content-type': contentType,
  351. 'Access-Token': getItem(StorageKeys.Token)
  352. }
  353. if (!start) {
  354. //判断code不为空
  355. if (code) {
  356. options.data = Object.assign(Common, {
  357. ...options.data
  358. });
  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
  377. .code == 0)
  378. if (res.statusCode == 200 && res.data.code == 0) {
  379. resolve(res.data.data)
  380. } else {
  381. if (res.data.code == 401002) {
  382. // updateGetTokenNew().then((data) => {
  383. // console.log("token刷新", data);
  384. // clie(data)
  385. // requestNew(code, options, true)
  386. // })
  387. if (!getItem('IsLogin') || getItem('IsLogin') == false) {
  388. setItem('IsLogin', true);
  389. uni.showModal({
  390. title: '提示',
  391. content: '登录失效,请重新登录!',
  392. confirmText: '去登录',
  393. showCancel: false,
  394. success: function(res) {
  395. if (res.confirm) {
  396. setItem('IsLogin', false);
  397. uni.navigateTo({
  398. url: `/login/login`,
  399. })
  400. }
  401. }
  402. });
  403. }
  404. } else if (res.data.code == 403001 || res.data.code == 403004 || res.data.code == 403003) {
  405. // 会详细列出具体字段的错误信息。
  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. } else {
  418. uni.showModal({
  419. title: '提示',
  420. content: res.data.message,
  421. success: function(res) {
  422. if (res.confirm) {
  423. console.log('用户点击确定2');
  424. } else if (res.cancel) {
  425. console.log('用户点击取消2');
  426. }
  427. }
  428. });
  429. }
  430. reject(res.data.message)
  431. return
  432. }
  433. }
  434. options.fail = (err) => {
  435. uni.hideLoading()
  436. console.log('请求错误', err)
  437. //处理请求错误
  438. reject(err)
  439. }
  440. uni.getNetworkType({
  441. success: function(res) {
  442. if (res.networkType == 'none') {
  443. uni.showModal({
  444. title: '提示',
  445. content: "网络异常",
  446. success: function(res) {
  447. if (res.confirm) {
  448. console.log('用户点击确定');
  449. } else if (res.cancel) {
  450. console.log('用户点击取消');
  451. }
  452. }
  453. });
  454. } else {
  455. uni.request(options)
  456. }
  457. console.log("res.networkType", res.networkType);
  458. }
  459. });
  460. });
  461. }
  462. export function corefn(urlcode, subdata, backfn) {
  463. var requestUrl = "https://zeus.etcjz.cn/api/common/transmit/v1/send/zt";
  464. var requestData = new Array();
  465. requestData['requestId'] = getDate() + generateMixed(10);
  466. requestData['accessCode'] = 'etc';
  467. requestData['distinction'] = urlcode;
  468. requestData['data'] = JSON.stringify(subdata);
  469. requestData['sign'] = sign(requestData, 'etc123456');
  470. var requestData = arrayToJson(requestData);
  471. sendPostv2(requestUrl, requestData, function(res) {
  472. uni.hideLoading();
  473. backfn(res);
  474. });
  475. }
  476. function sendPostv2(url, requestData, callback, contentType = null) {
  477. console.log('请求入参:', requestData, url);
  478. var type = "";
  479. if (contentType == "1") {
  480. type = "application/json";
  481. } else {
  482. type = "application/x-www-form-urlencoded";
  483. }
  484. wx.request({
  485. url: url, //请求接口的url
  486. method: 'POST', //请求方式
  487. data: requestData, //请求参数
  488. header: {
  489. "Content-Type": type
  490. },
  491. complete: function(response) { //请求结束后隐藏 loading 提示框
  492. // wx.hideToast();
  493. },
  494. success: function(response) {
  495. if (response.statusCode != "200") {
  496. callback({
  497. "rc": "99",
  498. "rm": "请求服务器异常_状态" + response.statusCode,
  499. "rd": ""
  500. });
  501. return;
  502. }
  503. console.log(response.data);
  504. console.log('返回结果:', response.data);
  505. callback(response.data);
  506. },
  507. fail: function(response) {
  508. //console.log('========发送请求失败_fai',response);
  509. callback({
  510. "rc": "98",
  511. "rm": "发送请求失败_fail",
  512. "rd": ""
  513. });
  514. }
  515. });
  516. }
  517. //获取时间ymdHis
  518. function getDate() {
  519. var date = new Date();
  520. return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2(date.getDate()) + pad2(date.getHours()) +
  521. pad2(date.getMinutes()) + pad2(date.getSeconds());
  522. }
  523. function pad2(n) {
  524. return n < 10 ? "0" + n : n;
  525. }
  526. /**
  527. * 获取随机数
  528. */
  529. function generateMixed(n) {
  530. var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  531. var res = "";
  532. for (var i = 0; i < n; i++) {
  533. var id = Math.ceil(Math.random() * 8);
  534. res += chars[id];
  535. }
  536. return res;
  537. }