request.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import config from "@/config";
  2. export function request(options) {
  3. return new Promise((resolve, reject) => {
  4. let header = options.header || {
  5. 'Content-Type': 'application/json'
  6. }
  7. if (uni.getStorageSync('token')) header['token'] = uni.getStorageSync('token');
  8. if (options.url === '/im/message/save') header['token'] = uni.getStorageSync('systemChatToken');
  9. uni.request({
  10. url: config.baseUrl + options.url, // 服务器url
  11. method: options.method || 'GET', // 请求方法,默认为GET
  12. data: options.data || {}, // 请求参数
  13. header: header, // 设置请求的 header
  14. success: (res) => {
  15. // 请求成功
  16. if (res.statusCode === 200) {
  17. resolve(res.data);
  18. if (res.data.code != 200) uni.showToast({
  19. title: res.data.message,
  20. icon: 'none'
  21. })
  22. } else {
  23. // 可以根据项目要求修改错误处理
  24. reject(res.data);
  25. uni.hideLoading();
  26. }
  27. },
  28. fail: (err) => {
  29. // 请求失败处理
  30. uni.showToast({
  31. title: '出错了,请联系管理员~',
  32. icon: 'none'
  33. })
  34. reject(err);
  35. uni.hideLoading();
  36. },
  37. });
  38. });
  39. };