家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * 网络请求模块
  3. * @author andy
  4. * @param {*} cmd 接口名称
  5. * @param {*} method 请求方法
  6. * @param {*} params 请求参数
  7. * @param {*} handleStatus 是否需要单独处理错误码
  8. */
  9. const request = (cmd, method = 'POST', params = {}, handleStatus = false) => {
  10. return new Promise((resolve, reject) => {
  11. const getHeader = (method) => {
  12. const userInfo = global.tools.memory.getData('USER_INFO') || {};
  13. const header = {
  14. 'token': userInfo.token || '',
  15. 'openid': userInfo.openid || ''
  16. };
  17. if (method && method.toUpperCase() === 'POST') {
  18. // header['content-type'] = 'application/x-www-form-urlencoded';
  19. header['content-type'] = 'application/json';
  20. }
  21. return header;
  22. };
  23. const env = global.config.ENV;
  24. const requestUrl = env.toUpperCase() === 'PRD' ? global.config.REQUEST_URL : global.config.REQUEST_DEV_URL;
  25. wx.request({
  26. url: `${requestUrl}${cmd}`,
  27. header: getHeader(method),
  28. method,
  29. data: params,
  30. success: res => {
  31. res = res.data;
  32. if (res.status && res.status === 105) {
  33. // token失效
  34. global.memory.setData('USER_INFO', {}, {
  35. localSave: true
  36. });
  37. return global.router.goto({
  38. path: global.config.LAUNCH_PAGE,
  39. type: global.ROUTER_TYPE.CLOSE_ALL
  40. });
  41. } else {
  42. if (handleStatus || !res.status) {
  43. resolve(res);
  44. } else if (res.message) {
  45. global.wx.showToast({
  46. title: res.message
  47. });
  48. } else {
  49. global.wx.showToast({
  50. title: '服务不可用,请稍后再试'
  51. });
  52. }
  53. }
  54. },
  55. fail: reject
  56. });
  57. });
  58. };
  59. export default request;