Http.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class HTTP {
  2. constructor() {
  3. this.ENV = wx.getAccountInfoSync().miniProgram.envVersion;
  4. this.urls = [{
  5. name: "美大正式",
  6. url: "https://crm.meida.com:16691"
  7. }, {
  8. name: "楚楚",
  9. url: "https://cucu.cnyunl.com:8079"
  10. }, {
  11. name: "开发环境",
  12. url: "http://61.164.207.46:8300"
  13. }]
  14. if (this.ENV === 'release') { // 正式版
  15. this.baseUrl = "https://crm.meida.com:16691";
  16. } else {
  17. this.baseUrl = "http://61.164.207.46:8300";
  18. }
  19. console.log("接口地址:", this.baseUrl)
  20. }
  21. request({
  22. url,
  23. data = {},
  24. method = "POST",
  25. header = {
  26. 'content-type': 'application/json',
  27. "accesstoken": data.accesstoken || wx.getStorageSync('userMsg').token || ''
  28. },
  29. showLoading = '加载中...'
  30. }) {
  31. return new Promise((resolve, reject) => {
  32. this._request(url, resolve, reject, data, method, header, showLoading);
  33. })
  34. }
  35. _request(url, resolve, reject, data, method, header, showLoading) {
  36. /* if (showLoading) wx.showLoading({
  37. title: showLoading,
  38. mask: true
  39. }) */
  40. wx.request({
  41. url: this.baseUrl + '/yos/rest/index' + url,
  42. data,
  43. method,
  44. header,
  45. timeout: 60000,
  46. success: (res) => {
  47. resolve(res.data);
  48. },
  49. fial: (err) => {
  50. reject(err);
  51. },
  52. complete: (res) => {
  53. // if (showLoading) wx.hideLoading()
  54. if (res.errMsg != 'request:ok') {
  55. wx.showToast({
  56. title: '网络异常,请重新进入',
  57. icon: "none"
  58. })
  59. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  60. wx.redirectTo({
  61. url: '/pages/login/phone',
  62. });
  63. wx.showToast({
  64. title: res.msg,
  65. icon: "none"
  66. })
  67. }
  68. }
  69. })
  70. }
  71. }
  72. export {
  73. HTTP
  74. }