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.8KB

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
1 rok temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 == 704 && res.data.errorMsg.indexOf('相应的申请单')) {
  109. // // let content = s4.decryptData_CBC(res, sm4Key)
  110. // console.log('请求成功返回参数:---------', code, content, res)
  111. // let aa = "data: {bizContent:{'val':null}}"
  112. // resolve(aa)
  113. // } else
  114. if (res.data.statusCode !== 0) {
  115. if (res.data.statusCode == 600) {
  116. resolve(res.data)
  117. } else if (res.data.statusCode == 401) {
  118. uni.showModal({
  119. title: '提示',
  120. content: res.data.errorMsg,
  121. confirmText: '去登录',
  122. showCancel: false,
  123. success: function(res) {
  124. if (res.confirm) {
  125. removeItem(StorageKeys.Token);
  126. removeItem(StorageKeys.OpenId);
  127. removeItem(StorageKeys.QYorder);
  128. uni.reLaunch({
  129. url: '/login/login',
  130. })
  131. }
  132. }
  133. });
  134. } else if (res.data.statusCode == 99999) {
  135. if (!start) {
  136. updateGetToken().then((data) => {
  137. console.log("token刷新", data);
  138. request(code, options, true)
  139. })
  140. } else {
  141. uni.showModal({
  142. title: '提示',
  143. content: res.data.errorMsg,
  144. success: function(res) {
  145. if (res.confirm) {
  146. console.log('用户点击确定1');
  147. } else if (res.cancel) {
  148. console.log('用户点击取消1');
  149. }
  150. }
  151. });
  152. }
  153. } else {
  154. // 当前车辆已存在订单,无法再次创建订单
  155. if (code == 6 && res.data.statusCode == 704) {
  156. console.log(code == 6, res.data.statusCode == 704)
  157. uni.showModal({
  158. title: '提示',
  159. content: res.data.errorMsg,
  160. cancelText: '解除车牌', //前往解除车牌占用
  161. success: function(res) {
  162. if (res.confirm) {
  163. console.log('用户点击确定2');
  164. } else if (res.cancel) {
  165. uni.redirectTo({
  166. url: "/subpackage/after-sale/rescind-carId/rescind-carId-select"
  167. })
  168. }
  169. }
  170. });
  171. } else {
  172. uni.showModal({
  173. title: '提示',
  174. content: res.data.errorMsg,
  175. success: function(res) {
  176. if (res.confirm) {
  177. console.log('用户点击确定2');
  178. } else if (res.cancel) {
  179. console.log('用户点击取消2');
  180. }
  181. }
  182. });
  183. }
  184. }
  185. reject(res.data.errorMsg)
  186. return
  187. } else {
  188. let content = s4.decryptData_CBC(res, sm4Key)
  189. console.log('请求成功返回参数:', code, content, res)
  190. resolve(content.data)
  191. }
  192. }
  193. options.fail = (err) => {
  194. // console.log('请求失败返回参数:', res)
  195. uni.hideLoading()
  196. console.log('请求错误', err)
  197. // if (err == 'openId无效,请核实传参!') {
  198. // setItem(StorageKeys.OpenId, openId);
  199. // }
  200. //处理请求错误
  201. reject(err)
  202. }
  203. uni.getNetworkType({
  204. success: function(res) {
  205. if (res.networkType == 'none') {
  206. uni.showModal({
  207. title: '提示',
  208. content: "网络异常",
  209. success: function(res) {
  210. if (res.confirm) {
  211. console.log('用户点击确定');
  212. } else if (res.cancel) {
  213. console.log('用户点击取消');
  214. }
  215. }
  216. });
  217. } else {
  218. uni.request(options)
  219. }
  220. console.log("res.networkType", res.networkType);
  221. }
  222. });
  223. });
  224. }