request.js 1.9 KB

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