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 import/no-extraneous-dependencies */
  5. /* eslint-disable no-shadow */
  6. import Vue from 'vue';
  7. import _ from 'lodash';
  8. import { Message } from 'element-ui';
  9. const vm = new Vue({});
  10. const Plugin = {
  11. install(Vue, options) {
  12. // 4. 添加实例方法
  13. Vue.prototype.$checkRes = (res, okText, errText) => {
  14. let _okText = okText;
  15. let _errText = errText;
  16. if (!_.isFunction(okText) && _.isObject(okText) && okText != null) {
  17. ({ okText: _okText, errText: _errText } = okText);
  18. }
  19. const { errcode = 0, errmsg } = res || {};
  20. if (errcode === 0) {
  21. if (_.isFunction(_okText)) {
  22. return _okText();
  23. }
  24. if (_okText) {
  25. Message.success(_okText);
  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);