Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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