request.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* wx.request */
  2. const BASE_API = 'https://itt.chl6zk.store/api'
  3. const BASE_VERSION = '/v1'
  4. import itt from '../utils/util'
  5. const app = getApp()
  6. export const wxRequest = (url,data) => {
  7. return new Promise((resolve, reject) => {
  8. wx.request({
  9. url: BASE_API + url + BASE_VERSION,
  10. data: data,
  11. header: {
  12. systemData:JSON.stringify({
  13. requestId: itt.getUUID(),
  14. loginToken: app.globalData.accessToken,
  15. timestamp: new Date().getTime()
  16. })
  17. },
  18. method: 'POST',
  19. success (response) {
  20. console.log('当前请求地址:', url);
  21. console.log('当前请求参数:', data);
  22. console.log('当前请求返回:', response.data);
  23. if (response.statusCode !== 200) {
  24. reject(response.data.msg || '请求错误')
  25. } else {
  26. if (response.data.code !== '000' && response.data.code !== '102') {
  27. reject(response.data.msg || '请求错误')
  28. } else if (response.data.code == '102') {
  29. wx.hideLoading()
  30. // 重新登录
  31. wx.showModal({
  32. content: response.data.msg,
  33. confirmColor: '#333',
  34. showCancel: false,
  35. success (res) {
  36. if (res.confirm) {
  37. app.globalData.userInfo.login = false
  38. app.globalData.accessToken = ''
  39. app.globalData.userInfo.headImg= ''
  40. app.globalData.userInfo.userName = ''
  41. app.globalData.userInfo.phoneNumber = ''
  42. wx.reLaunch({
  43. url: '/pages/my/my',
  44. })
  45. }
  46. }
  47. })
  48. } else {
  49. resolve(response.data)
  50. }
  51. }
  52. },
  53. fail (e) {
  54. reject('网络错误')
  55. }
  56. })
  57. })
  58. }