check-res.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // import { Notify } from 'vant';
  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. // Notify({ type: 'success', message: _okText });
  27. }
  28. return true;
  29. }
  30. if (_.isFunction(_errText)) {
  31. return _errText();
  32. }
  33. Message.error(_errText || errmsg);
  34. // Notify({ type: 'danger', message: _okText });
  35. // Message({ message: _errText || errmsg, duration: 60000 });
  36. return false;
  37. };
  38. },
  39. };
  40. Vue.use(Plugin);