request.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* wx.request */
  2. //const BASE_API_URL = 'https://api.itthealth.com/api'
  3. const BASE_API_URL = 'https://itt.chl6zk.store/api'
  4. const BASE_API_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 + url + BASE_API_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. if (response.statusCode !== 200) {
  22. reject(response.data.msg || '请求错误')
  23. } else {
  24. if (response.data.code !== '000' && response.data.code !== '102') {
  25. reject(response.data.msg || '请求错误')
  26. } else if (response.data.code == '102') {
  27. wx.hideLoading()
  28. // 重新登录
  29. wx.showModal({
  30. content: response.data.msg,
  31. confirmColor: '#333',
  32. showCancel: false,
  33. success (res) {
  34. if (res.confirm) {
  35. wx.clearStorageSync()
  36. app.globalData.isNeedHeadImg = false
  37. app.globalData.isNeedPhone = false
  38. app.globalData.userInfo.login = false
  39. app.globalData.accessToken = ''
  40. app.globalData.userInfo.headImg= ''
  41. app.globalData.userInfo.userName = ''
  42. app.globalData.userInfo.phoneNumber = ''
  43. app.globalData.selectedInex = 0
  44. wx.reLaunch({
  45. url: '/pages/index/index',
  46. })
  47. }
  48. }
  49. })
  50. } else {
  51. resolve(response.data)
  52. }
  53. }
  54. },
  55. fail (e) {
  56. reject('网络错误')
  57. }
  58. })
  59. })
  60. }
  61. export const wxUpload = (url, data) => {
  62. return new Promise((resolve, reject) => {
  63. wx.uploadFile({
  64. url: BASE_API_URL + url + BASE_API_VERSION,
  65. filePath: data.filePath,
  66. name: 'file',
  67. formData: {
  68. 'file': data.filePath
  69. },
  70. success (res){
  71. const data = res.data
  72. resolve(data)
  73. },
  74. fail () {
  75. reject('上传失败')
  76. }
  77. })
  78. })
  79. }