Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

request.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // 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.navigateTo({
  117. url: '/login/login',
  118. })
  119. }
  120. }
  121. });
  122. } else if (res.data.statusCode == 99999) {
  123. if (!start) {
  124. updateGetToken().then((data) => {
  125. console.log("token刷新", data);
  126. request(code, options, true)
  127. })
  128. } else {
  129. uni.showModal({
  130. title: '提示',
  131. content: res.data.errorMsg,
  132. success: function(res) {
  133. if (res.confirm) {
  134. console.log('用户点击确定');
  135. } else if (res.cancel) {
  136. console.log('用户点击取消');
  137. }
  138. }
  139. });
  140. }
  141. } else {
  142. uni.showModal({
  143. title: '提示',
  144. content: res.data.errorMsg,
  145. success: function(res) {
  146. if (res.confirm) {
  147. console.log('用户点击确定');
  148. } else if (res.cancel) {
  149. console.log('用户点击取消');
  150. }
  151. }
  152. });
  153. }
  154. reject(res.data.errorMsg)
  155. return
  156. } else {
  157. let content = res
  158. // let content = s4.decryptData_CBC(res, sm4Key)
  159. // console.log(content);
  160. // content.data.bizContent = JSON.stringify(content.data.bizContent)
  161. // console.log(content.data);
  162. resolve(content.data)
  163. }
  164. }
  165. options.fail = (err) => {
  166. console.log('请求失败返回参数:', res)
  167. uni.hideLoading()
  168. console.log('请求错误', err)
  169. // if (err == 'openId无效,请核实传参!') {
  170. // setItem(StorageKeys.OpenId, openId);
  171. // }
  172. //处理请求错误
  173. reject(err)
  174. }
  175. uni.getNetworkType({
  176. success: function(res) {
  177. if (res.networkType == 'none') {
  178. uni.showModal({
  179. title: '提示',
  180. content: "网络异常",
  181. success: function(res) {
  182. if (res.confirm) {
  183. console.log('用户点击确定');
  184. } else if (res.cancel) {
  185. console.log('用户点击取消');
  186. }
  187. }
  188. });
  189. } else {
  190. uni.request(options)
  191. }
  192. console.log("res.networkType", res.networkType);
  193. }
  194. });
  195. });
  196. }