request.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* wx.request */
  2. const BASE_API = 'https://worker.itthealth.com/api' // 'https://itt-worker.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: wx.getStorageSync('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 || '请求错误')
  25. } else {
  26. if (response.data.code !== '000' && response.data.code !== '102') {
  27. reject(response.data || '请求错误')
  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. wx.clearStorageSync()
  38. app.globalData.accessToken = ''
  39. app.globalData.userName = ''
  40. app.globalData.headImg = ''
  41. app.globalData.workerId = ''
  42. app.globalData.device = null
  43. wx.reLaunch({
  44. url: '/pages/login/login'
  45. })
  46. }
  47. }
  48. })
  49. } else {
  50. resolve(response.data)
  51. }
  52. }
  53. },
  54. fail (e) {
  55. reject('网络错误')
  56. }
  57. })
  58. })
  59. }