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

request.js 16KB

1週間前
1年前
1週間前
1年前
1週間前
2年前
1週間前
2年前
1週間前
1週間前
2年前
1週間前
1週間前
1年前
1週間前
9ヶ月前
9ヶ月前
1週間前
9ヶ月前
9ヶ月前
1週間前
1週間前
9ヶ月前
1週間前
9ヶ月前
1週間前
1週間前
1週間前
1週間前
1週間前
1週間前
1週間前
1週間前
1週間前
1週間前
1週間前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 + "/newDev/ndev" + code;
  316. //默认json数据格式提交`
  317. let contentType = "application/json";
  318. //判断baseUri是否为空
  319. if (options.baseUrlNew) {
  320. options.url = options.baseUrlNew;
  321. }
  322. //根据type判断数据传输格式
  323. if (options.type && options.type === 2) {
  324. contentType = "application/json;charset=UTF-8";
  325. }
  326. //默认POST提交
  327. options.method = options.method ? options.method : "POST";
  328. //设置请求超时时间
  329. options.timeout = 60000;
  330. console.log("getItem(StorageKeys.Token)", getItem(StorageKeys.Token));
  331. options.header = {
  332. "content-type": contentType,
  333. "Access-Token": getItem(StorageKeys.Token),
  334. };
  335. if (!start) {
  336. //判断code不为空
  337. if (code) {
  338. options.data = Object.assign(Common, {
  339. ...options.data,
  340. });
  341. }
  342. }
  343. console.log("code", code, options.data);
  344. //是否显示加载中
  345. if (options.showLoading) {
  346. uni.showLoading({
  347. title: "请稍后",
  348. mask: true,
  349. });
  350. }
  351. //参数返回
  352. return new Promise((resolve, reject) => {
  353. options.success = (res) => {
  354. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  355. if (options.showLoading) {
  356. uni.hideLoading();
  357. }
  358. console.log(
  359. "返回结果" + code,
  360. res,
  361. res.statusCode,
  362. res.data.code,
  363. res.statusCode == 200 && res.data.code == 0
  364. );
  365. if (res.statusCode == 200 && res.data.code == 0) {
  366. resolve(res.data.data);
  367. } else {
  368. if (res.data.code == 401002) {
  369. // updateGetTokenNew().then((data) => {
  370. // console.log("token刷新", data);
  371. // clie(data)
  372. // requestNew(code, options, true)
  373. // })
  374. if (!getItem("IsLogin") || getItem("IsLogin") == false) {
  375. setItem("IsLogin", true);
  376. uni.showModal({
  377. title: "提示",
  378. content: "登录失效,请重新登录!",
  379. confirmText: "去登录",
  380. showCancel: false,
  381. success: function (res) {
  382. if (res.confirm) {
  383. setTimeout(() => {
  384. setItem("IsLogin", false);
  385. }, 3000);
  386. uni.navigateTo({
  387. url: `/login/login`,
  388. });
  389. }
  390. },
  391. });
  392. }
  393. } else if (
  394. res.data.code == 403001 ||
  395. res.data.code == 403004 ||
  396. res.data.code == 403003
  397. ) {
  398. // 会详细列出具体字段的错误信息。
  399. uni.showModal({
  400. title: "提示",
  401. content: res.data.message,
  402. success: function (res) {
  403. if (res.confirm) {
  404. console.log("用户点击确定2");
  405. } else if (res.cancel) {
  406. console.log("用户点击取消2");
  407. }
  408. },
  409. });
  410. } else {
  411. uni.showModal({
  412. title: "提示",
  413. content: res.data.message,
  414. success: function (res) {
  415. if (res.confirm) {
  416. console.log("用户点击确定2");
  417. } else if (res.cancel) {
  418. console.log("用户点击取消2");
  419. }
  420. },
  421. });
  422. }
  423. reject(res.data.message);
  424. return;
  425. }
  426. };
  427. options.fail = (err) => {
  428. uni.hideLoading();
  429. console.log("请求错误", err);
  430. //处理请求错误
  431. reject(err);
  432. };
  433. uni.getNetworkType({
  434. success: function (res) {
  435. if (res.networkType == "none") {
  436. uni.showModal({
  437. title: "提示",
  438. content: "网络异常",
  439. success: function (res) {
  440. if (res.confirm) {
  441. console.log("用户点击确定");
  442. } else if (res.cancel) {
  443. console.log("用户点击取消");
  444. }
  445. },
  446. });
  447. } else {
  448. uni.request(options);
  449. }
  450. console.log("res.networkType", res.networkType);
  451. },
  452. });
  453. });
  454. }
  455. export function corefn(urlcode, subdata, backfn) {
  456. var requestUrl = "https://zeus.etcjz.cn/api/common/transmit/v1/send/zt";
  457. var requestData = new Array();
  458. requestData["requestId"] = getDate() + generateMixed(10);
  459. requestData["accessCode"] = "etc";
  460. requestData["distinction"] = urlcode;
  461. requestData["data"] = JSON.stringify(subdata);
  462. requestData["sign"] = sign(requestData, "etc123456");
  463. var requestData = arrayToJson(requestData);
  464. sendPostv2(requestUrl, requestData, function (res) {
  465. uni.hideLoading();
  466. backfn(res);
  467. });
  468. }
  469. function sendPostv2(url, requestData, callback, contentType = null) {
  470. console.log("请求入参:", requestData, url);
  471. var type = "";
  472. if (contentType == "1") {
  473. type = "application/json";
  474. } else {
  475. type = "application/x-www-form-urlencoded";
  476. }
  477. wx.request({
  478. url: url, //请求接口的url
  479. method: "POST", //请求方式
  480. data: requestData, //请求参数
  481. header: {
  482. "Content-Type": type,
  483. },
  484. complete: function (response) {
  485. //请求结束后隐藏 loading 提示框
  486. // wx.hideToast();
  487. },
  488. success: function (response) {
  489. if (response.statusCode != "200") {
  490. callback({
  491. rc: "99",
  492. rm: "请求服务器异常_状态" + response.statusCode,
  493. rd: "",
  494. });
  495. return;
  496. }
  497. console.log(response.data);
  498. console.log("返回结果:", response.data);
  499. callback(response.data);
  500. },
  501. fail: function (response) {
  502. //console.log('========发送请求失败_fai',response);
  503. callback({
  504. rc: "98",
  505. rm: "发送请求失败_fail",
  506. rd: "",
  507. });
  508. },
  509. });
  510. }
  511. //获取时间ymdHis
  512. function getDate() {
  513. var date = new Date();
  514. return (
  515. date.getFullYear().toString() +
  516. pad2(date.getMonth() + 1) +
  517. pad2(date.getDate()) +
  518. pad2(date.getHours()) +
  519. pad2(date.getMinutes()) +
  520. pad2(date.getSeconds())
  521. );
  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. }