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

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