request.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.showLoading({
  9. title: '加载中...',
  10. mask: true
  11. })
  12. wx.request({
  13. url: BASE_API + url + BASE_VERSION,
  14. data: data,
  15. header: {
  16. systemData:JSON.stringify({
  17. requestId: itt.getUUID(),
  18. loginToken: app.globalData.accessToken,
  19. timestamp: new Date().getTime()
  20. })
  21. },
  22. method: 'POST',
  23. success (response) {
  24. console.log('当前请求地址:', url);
  25. console.log('当前请求参数:', data);
  26. console.log('当前请求返回:', response.data);
  27. if (response.statusCode !== 200) {
  28. wx.showToast({
  29. title: response.errMsg || '请求错误',
  30. icon:'none',
  31. duration: 2500,
  32. mask: true
  33. })
  34. } else {
  35. if (response.data.code !== '000') {
  36. wx.showToast({
  37. title: response.data.msg || '请求错误',
  38. icon:'none',
  39. duration: 2500,
  40. mask: true
  41. })
  42. } else {
  43. resolve(response.data)
  44. }
  45. }
  46. },
  47. fail (e) {
  48. wx.showToast({
  49. title: '网络错误',
  50. icon: 'error',
  51. duration: 2500,
  52. mask: true
  53. })
  54. reject(e)
  55. },
  56. complete () {
  57. wx.hideLoading()
  58. }
  59. })
  60. })
  61. }