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.2KB

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