request.js 2.1 KB

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