Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

request.js 5.8KB

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