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

1 rok temu
2 lat temu
1 rok temu
1 rok temu
2 lat temu
2 lat temu
2 lat temu
1 rok temu
2 lat temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. import SM4Util from '../util/sm4.js'
  18. import {
  19. agentId,
  20. channelId
  21. } from "@/utils/network/difference";
  22. import {
  23. updateToken
  24. } from "@/utils/network/api";
  25. const s4 = new SM4Util()
  26. /* 刷新token */
  27. function updateGetToken() {
  28. const options = {
  29. type: 2,
  30. data: {
  31. openId: getItem("openId"),
  32. accessToken: getItem(StorageKeys.Token)
  33. },
  34. method: "POST",
  35. showLoading: false,
  36. };
  37. //刷新token
  38. return new Promise(async (resolve, reject) => {
  39. const res = await request(updateToken, options);
  40. const data = JSON.parse(res.bizContent);
  41. console.log("data", data)
  42. setItem('accessToken', data.accessToken);
  43. setItem(StorageKeys.Token, data.accessToken);
  44. setItem('openId', data.openId);
  45. resolve(data);
  46. }).catch((error) => {
  47. reject(error);
  48. });
  49. }
  50. //请求
  51. export function request(code, options = {}, start = false) {
  52. //公参
  53. const Common = {
  54. agentId: agentId,
  55. channelId: channelId,
  56. channelType: "1",
  57. staffId: "54623263cb4d4a289dccbc983b22a4af",
  58. terminalId: "999999999999",
  59. loginSource: getItem("loginSource"),
  60. rbacSource: 'MINI_PROGRAM',
  61. accessToken: getItem(StorageKeys.Token)
  62. }
  63. options.url = envs[process.env.NODE_ENV].baseUrl + '/api/interfaceMidGroundIn'
  64. // options.url = envs[process.env.NODE_ENV].baseUrl + '/ifzt/api/interfaceMidGroundIn'
  65. //默认json数据格式提交`
  66. let contentType = 'application/x-www-form-urlencoded'
  67. //判断baseUri是否为空
  68. if (options.baseUrl) {
  69. options.url = options.baseUrl
  70. }
  71. //根据type判断数据传输格式
  72. if (options.type && options.type === 2) {
  73. contentType = 'application/json;charset=UTF-8'
  74. }
  75. //默认POST提交
  76. options.method = options.method ? options.method : 'POST'
  77. //设置请求超时时间
  78. options.timeout = 60000
  79. options.header = {
  80. 'content-type': contentType,
  81. 'Access-Token': getItem(StorageKeys.Token)
  82. }
  83. console.log('code', code, options.data)
  84. if (!start) {
  85. //判断code不为空
  86. if (code) {
  87. options.data = encryption(code, Object.assign(Common, {
  88. ...options.data,
  89. 'appId': appId
  90. }), 2);
  91. }
  92. }
  93. //是否显示加载中
  94. if (options.showLoading) {
  95. uni.showLoading({
  96. title: '请稍后',
  97. mask: true
  98. });
  99. }
  100. //参数返回
  101. return new Promise((resolve, reject) => {
  102. options.success = (res) => {
  103. console.log('请求成功返回参数:', code, res)
  104. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  105. if (options.showLoading) {
  106. uni.hideLoading()
  107. }
  108. if (res.data.statusCode !== 0) {
  109. if (res.data.statusCode == 600) {
  110. resolve(res.data)
  111. } else if (res.data.statusCode == 401) {
  112. uni.showModal({
  113. title: '提示',
  114. content: res.data.errorMsg,
  115. confirmText: '去登录',
  116. showCancel: false,
  117. success: function(res) {
  118. if (res.confirm) {
  119. removeItem(StorageKeys.Token);
  120. removeItem(StorageKeys.OpenId);
  121. removeItem(StorageKeys.QYorder);
  122. uni.reLaunch({
  123. url: '/login/login',
  124. })
  125. }
  126. }
  127. });
  128. } else if (res.data.statusCode == 99999) {
  129. if (!start) {
  130. updateGetToken().then((data) => {
  131. console.log("token刷新", data);
  132. request(code, options, true)
  133. })
  134. } else {
  135. uni.showModal({
  136. title: '提示',
  137. content: res.data.errorMsg,
  138. success: function(res) {
  139. if (res.confirm) {
  140. console.log('用户点击确定1');
  141. } else if (res.cancel) {
  142. console.log('用户点击取消1');
  143. }
  144. }
  145. });
  146. }
  147. } else {
  148. // 当前车辆已存在订单,无法再次创建订单
  149. if (code == 6 && res.data.statusCode == 704) {
  150. console.log(code == 6, res.data.statusCode == 704)
  151. uni.showModal({
  152. title: '提示',
  153. content: res.data.errorMsg,
  154. cancelText: '解除车牌', //前往解除车牌占用
  155. success: function(res) {
  156. if (res.confirm) {
  157. console.log('用户点击确定2');
  158. } else if (res.cancel) {
  159. uni.redirectTo({
  160. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  161. })
  162. }
  163. }
  164. });
  165. } else {
  166. uni.showModal({
  167. title: '提示',
  168. content: res.data.errorMsg,
  169. success: function(res) {
  170. if (res.confirm) {
  171. console.log('用户点击确定2');
  172. } else if (res.cancel) {
  173. console.log('用户点击取消2');
  174. }
  175. }
  176. });
  177. }
  178. }
  179. reject(res.data.errorMsg)
  180. return
  181. } else {
  182. let content = s4.decryptData_CBC(res, sm4Key)
  183. console.log('请求成功返回参数:', code, content, res)
  184. resolve(content.data)
  185. }
  186. }
  187. options.fail = (err) => {
  188. // console.log('请求失败返回参数:', res)
  189. uni.hideLoading()
  190. console.log('请求错误', err)
  191. // if (err == 'openId无效,请核实传参!') {
  192. // setItem(StorageKeys.OpenId, openId);
  193. // }
  194. //处理请求错误
  195. reject(err)
  196. }
  197. uni.getNetworkType({
  198. success: function(res) {
  199. if (res.networkType == 'none') {
  200. uni.showModal({
  201. title: '提示',
  202. content: "网络异常",
  203. success: function(res) {
  204. if (res.confirm) {
  205. console.log('用户点击确定');
  206. } else if (res.cancel) {
  207. console.log('用户点击取消');
  208. }
  209. }
  210. });
  211. } else {
  212. uni.request(options)
  213. }
  214. console.log("res.networkType", res.networkType);
  215. }
  216. });
  217. });
  218. }