check-res.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* eslint-disable no-underscore-dangle */
  2. /* eslint-disable no-param-reassign */
  3. /* eslint-disable no-unused-vars */
  4. /* eslint-disable no-shadow */
  5. import Vue from 'vue';
  6. import _ from 'lodash';
  7. import { Message } from 'element-ui';
  8. const vm = new Vue({});
  9. const Plugin = {
  10. install(Vue, options) {
  11. // 4. 添加实例方法
  12. Vue.prototype.$checkRes = (res, okText, errText) => {
  13. let _okText = okText;
  14. let _errText = errText;
  15. if (!_.isFunction(okText) && _.isObject(okText) && okText != null) {
  16. ({ okText: _okText, errText: _errText } = okText);
  17. }
  18. const { errcode = 0, errmsg } = res || {};
  19. if (errcode === 0) {
  20. if (_.isFunction(_okText)) {
  21. return _okText();
  22. }
  23. if (_okText) {
  24. Message.success(_okText);
  25. // Message({ message: _okText, type: 'success', duration: 60000 });
  26. }
  27. return true;
  28. }
  29. if (_.isFunction(_errText)) {
  30. return _errText();
  31. }
  32. Message.error(_errText || errmsg);
  33. // Message({ message: _errText || errmsg, duration: 60000 });
  34. return false;
  35. };
  36. },
  37. };
  38. Vue.use(Plugin);