request.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import config from "@/config";
  2. import store from "../store";
  3. import chat from '@/uitls/chat.js'
  4. const logout = () => {
  5. chat.disConnect();
  6. uni.removeStorageSync('token');
  7. uni.removeStorageSync('chatToken');
  8. uni.removeStorageSync('vuex_state');
  9. store.dispatch('app/changeOrganization', {});
  10. store.dispatch('app/changeUser', {});
  11. uni.removeTabBarBadge({
  12. index: 1
  13. })
  14. setTimeout(() => {
  15. uni.switchTab({
  16. url: '/pages/workark/workark'
  17. });
  18. }, 400)
  19. }
  20. /* 消息提示 */
  21. const tip = (msg) => {
  22. let types = 'warning';
  23. if (msg == 'RET_INVALID_PASSWORD') msg = '账号密码有误'
  24. if (msg == 'RET_INVALID_CODE') msg = '短信验证码有误'
  25. if (msg == 'RET_AUTH_FAILED') msg = '登录已过期,请重新登录'
  26. uni.showToast({
  27. title: msg,
  28. icon: 'none'
  29. })
  30. }
  31. const errorCallBack = (resolve) => {
  32. // 请求失败处理
  33. tip('出错了,请联系系统管理员~');
  34. resolve({
  35. state: false,
  36. data: null,
  37. msg: 'error'
  38. });
  39. uni.hideLoading();
  40. }
  41. export function request(options) {
  42. return new Promise((resolve, reject) => {
  43. let header = options.header || {
  44. 'Content-Type': 'application/json'
  45. }
  46. if (uni.getStorageSync('token')) header['token'] = uni.getStorageSync('token');
  47. uni.request({
  48. url: config.baseUrl + options.url, // 服务器url
  49. method: options.method || 'GET', // 请求方法,默认为GET
  50. data: options.data || {}, // 请求参数
  51. header: header, // 设置请求的 header
  52. success: (res) => {
  53. // 请求成功
  54. if (res.statusCode === 200) {
  55. let data = res.data;
  56. if (data.code != '200') {
  57. tip(data.message || data.msg);
  58. if (data.code == '20005') logout();
  59. uni.hideLoading();
  60. }
  61. resolve({
  62. state: data.code == '200',
  63. data: data.data,
  64. msg: data.code == '200' ? 'success' : 'error'
  65. });
  66. } else {
  67. errorCallBack(resolve);
  68. }
  69. },
  70. fail: () => errorCallBack(resolve)
  71. });
  72. });
  73. };