Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

request.js 4.8KB

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