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

request.js 16KB

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