request.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import util from './util.js';
  2. import authLogin from './autuLogin.js';
  3. import { HEADER , TOKENNAME} from './../config.js';
  4. /**
  5. * 发送请求
  6. */
  7. export default function request(api, method, data, {noAuth = false, noVerify = false})
  8. {
  9. let Url = getApp().globalData.url, header = HEADER;
  10. if (!noAuth) {
  11. //登录过期自动登录
  12. if (!util.checkLogin()) return authLogin().then(res => { return request(api, method, data, { noAuth, noVerify}); });
  13. }
  14. if (getApp().globalData.token) header[TOKENNAME] = 'Bearer ' + getApp().globalData.token;
  15. return new Promise((reslove, reject) => {
  16. wx.request({
  17. url: Url + '/api/' + api,
  18. method: method || 'GET',
  19. header: header,
  20. data: data || {},
  21. success: (res) => {
  22. if (noVerify)
  23. reslove(res.data, res);
  24. else if (res.data.status == 200)
  25. reslove(res.data, res);
  26. else if (res.data.status == 402)
  27. reslove(res.data, res);
  28. else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
  29. util.logout()
  30. return authLogin().then(res => { return request(api, method, data, { noAuth, noVerify }); });
  31. } else
  32. reject(res.data.msg || '系统错误');
  33. },
  34. fail: (msg) => {
  35. reject('请求失败');
  36. }
  37. })
  38. });
  39. }
  40. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  41. request[method] = (api, data, opt) => request(api, method, data, opt || {})
  42. });