request.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {
  2. publicUrl
  3. } from './config'
  4. export const request = (options) => {
  5. return new Promise((resolve, reject) => {
  6. let Token = uni.getStorageSync('token')
  7. let TenantId = uni.getStorageSync('tenantId')
  8. let userId = uni.getStorageSync('userId')
  9. const isToken = (options.isToken === false)
  10. const isLogin = options.isLogin
  11. console.log("开始提交信息、", options)
  12. console.log("获取到的tokey信息、", Token)
  13. console.log("获取到的租户信息、", TenantId)
  14. console.log("获取到的用户信息、", userId)
  15. console.log("是否拼接请求地址", options.isAddress)
  16. // #ifdef APP-PLUS || MP-WEIXIN
  17. console.log("当前URL、", (publicUrl + options.url))
  18. // #endif
  19. // #ifndef APP-PLUS || MP-WEIXIN
  20. console.log("当前URL、", ( options.url))
  21. // #endif
  22. // if (options.isAddress) {
  23. // options.url = '/admin' + options.url;
  24. // }
  25. let ContentType = '';
  26. let Authorization = '';
  27. if (isToken) {
  28. if (isLogin) {
  29. ContentType = 'application/x-www-form-urlencoded;charset=utf-8',
  30. Authorization = 'Basic cGlnOnBpZw=='
  31. } else {
  32. ContentType = 'application/json;charset=utf-8',
  33. Authorization = 'Basic cGlnOnBpZw=='
  34. }
  35. } else {
  36. if (isLogin) {
  37. ContentType = 'application/x-www-form-urlencoded;charset=utf-8',
  38. Authorization = 'Bearer ' + Token
  39. } else {
  40. ContentType = 'application/json;charset=utf-8',
  41. Authorization = 'Bearer ' + Token
  42. }
  43. }
  44. let header = {
  45. 'Content-Type': ContentType,
  46. 'Authorization': Authorization,
  47. 'TENANT-ID': TenantId || ''
  48. }
  49. if (Token || isToken) {
  50. uni.request({
  51. // #ifdef APP-PLUS || MP-WEIXIN
  52. url: publicUrl + options.url,
  53. // #endif
  54. // #ifndef APP-PLUS || MP-WEIXIN
  55. url: options.url,
  56. // #endif
  57. method: options.method || 'GET',
  58. data: options.data || {},
  59. dataType: 'json',
  60. header: header,
  61. success: (res) => {
  62. const status = Number(res.statusCode) || 200
  63. console.log("返回状态", status);
  64. console.log("返回值", res);
  65. if (status == 424 || status == 401) {
  66. //#ifdef MP-WEIXIN
  67. wx.login({
  68. success(res) {
  69. console.log("开始准备微信登录", res.code);
  70. wx.request({
  71. url: publicUrl +
  72. '/auth/mobile/token/social?grant_type=mobil&mobile=E_MINI@' +
  73. res.code + '&code=' + res.code,
  74. method: 'post',
  75. header: {
  76. Authorization: 'Basic cGlnOnBpZw==',
  77. },
  78. success(res) {
  79. console.log("微信登录返回成功、", res);
  80. if (res.statusCode === 401 || res.statusCode === 404) {
  81. console.log(res.data);
  82. uni.showModal({
  83. // title: '提示',
  84. content: '登录【'+res.statusCode +'】'+(res.statusCode === 401?'已过期':'无法访问接口')+'请重新登录',
  85. showCancel: true,
  86. success: function (res) {
  87. if (res.confirm) {
  88. uni.removeStorageSync('token')
  89. uni.removeStorageSync('tenantId')
  90. uni.removeStorageSync('userId')
  91. uni.removeStorageSync('userName')
  92. uni.removeStorageSync('userInfo')
  93. uni.removeStorageSync('userType')
  94. uni.removeStorageSync('tenantInfo')
  95. uni.removeStorageSync('enterpriseInfo')
  96. uni.reLaunch({
  97. url: "/pages/login/login"
  98. })
  99. console.log('用户点击确定');
  100. } else if (res.cancel) {
  101. console.log('用户点击取消');
  102. }
  103. }
  104. });
  105. } else if (res.statusCode === 200) {
  106. console.log("微信登录后信息、", res);
  107. uni.setStorageSync('userId', res
  108. .data.user_info.id)
  109. uni.setStorageSync('userName', res
  110. .data.user_info.username)
  111. uni.setStorageSync('token', res.data
  112. .access_token)
  113. } else if (res.statusCode != 200 || res
  114. .data.code === 1) {
  115. uni.showToast({
  116. title: res.data.msg,
  117. icon: 'none',
  118. duration: 2000
  119. })
  120. }
  121. },
  122. });
  123. },
  124. });
  125. // #endif
  126. // #ifndef MP-WEIXIN
  127. uni.showModal({
  128. // title: '提示',
  129. content: '登录【401】已过期,请重新登录',
  130. showCancel: true,
  131. success: function (res) {
  132. if (res.confirm) {
  133. uni.removeStorageSync('token')
  134. uni.removeStorageSync('tenantId')
  135. uni.removeStorageSync('userId')
  136. uni.removeStorageSync('userName')
  137. uni.removeStorageSync('userInfo')
  138. uni.removeStorageSync('userType')
  139. uni.removeStorageSync('tenantInfo')
  140. uni.reLaunch({
  141. url: "/pages/login/login"
  142. })
  143. console.log('用户点击确定');
  144. } else if (res.cancel) {
  145. console.log('用户点击取消');
  146. }
  147. }
  148. });
  149. // #endif
  150. } else if (status != 200) {
  151. uni.showModal({
  152. content: res.data.msg || "接口【" + status + "】,没有返回信息",
  153. showCancel: false,
  154. })
  155. }
  156. resolve(res)
  157. },
  158. fail: (err) => {
  159. console.log(err)
  160. uni.showToast({
  161. title: '请求接口失败',
  162. icon: 'none',
  163. duration: 2000
  164. })
  165. reject(err)
  166. }
  167. })
  168. } else {
  169. uni.showModal({
  170. // title: '提示',
  171. content: '暂未登录,请登录',
  172. showCancel: true,
  173. success: function (res) {
  174. if (res.confirm) {
  175. uni.removeStorageSync('token')
  176. uni.removeStorageSync('tenantId')
  177. uni.removeStorageSync('userId')
  178. uni.removeStorageSync('userName')
  179. uni.removeStorageSync('userInfo')
  180. uni.removeStorageSync('userType')
  181. uni.removeStorageSync('tenantInfo')
  182. uni.reLaunch({
  183. url: "/pages/login/login"
  184. })
  185. console.log('用户点击确定');
  186. } else if (res.cancel) {
  187. console.log('用户点击取消');
  188. }
  189. }
  190. });
  191. }
  192. })
  193. }