Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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