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

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