request.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* wx.request */
  2. const BASE_API_URL = 'https://itt.chl6zk.store/api'
  3. const BASE_API_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 + url + BASE_API_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. app.globalData.selectedInex = 2
  43. wx.reLaunch({
  44. url: '/pages/my/my',
  45. })
  46. }
  47. }
  48. })
  49. } else {
  50. resolve(response.data)
  51. }
  52. }
  53. },
  54. fail (e) {
  55. reject('网络错误')
  56. }
  57. })
  58. })
  59. }
  60. export const wxUpload = (url, data) => {
  61. return new Promise((resolve, reject) => {
  62. wx.uploadFile({
  63. url: BASE_API_URL + url + BASE_API_VERSION,
  64. filePath: data.filePath,
  65. name: 'file',
  66. formData: {
  67. 'file': data.filePath
  68. },
  69. success (res){
  70. const data = res.data
  71. resolve(data)
  72. },
  73. fail () {
  74. reject('上传失败')
  75. }
  76. })
  77. })
  78. }