您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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