request.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import config from "@/config";
  2. import store from "../store";
  3. const logout = () => {
  4. }
  5. /* 消息提示 */
  6. const tip = (msg) => {
  7. let types = type || 'warning';
  8. if (msg == 'RET_INVALID_PASSWORD') msg = '账号密码有误'
  9. if (msg == 'RET_INVALID_CODE') msg = '短信验证码有误'
  10. uni.showToast({
  11. title: msg,
  12. icon: 'none'
  13. })
  14. }
  15. const errorCallBack = () => {
  16. // 请求失败处理
  17. tip('出错了,请联系管理员~');
  18. resolve({
  19. state: false,
  20. data: null,
  21. msg: 'error'
  22. });
  23. uni.hideLoading();
  24. }
  25. export function request(options) {
  26. return new Promise((resolve, reject) => {
  27. let header = options.header || {
  28. 'Content-Type': 'application/json'
  29. }
  30. if (uni.getStorageSync('token')) header['token'] = uni.getStorageSync('token');
  31. uni.request({
  32. url: config.baseUrl + options.url, // 服务器url
  33. method: options.method || 'GET', // 请求方法,默认为GET
  34. data: options.data || {}, // 请求参数
  35. header: header, // 设置请求的 header
  36. success: (res) => {
  37. // 请求成功
  38. if (res.statusCode === 200) {
  39. let data = res.data;
  40. if (data.code != '200' && data.code != '20001') tip(data.message || data.msg);
  41. resolve({
  42. state: data.code == '200',
  43. data: data.data,
  44. msg: data.code == '200' ? 'success' : 'error'
  45. });
  46. } else {
  47. errorCallBack(resolve);
  48. }
  49. },
  50. fail: () => errorCallBack(resolve)
  51. });
  52. });
  53. };