check-res.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 Plugin = {
  9. install(Vue, options) {
  10. // 4. 添加实例方法
  11. Vue.prototype.$checkRes = (res, okText, errText) => {
  12. let _okText = okText;
  13. let _errText = errText;
  14. if (!_.isFunction(okText) && _.isObject(okText) && okText != null) {
  15. ({ okText: _okText, errText: _errText } = okText);
  16. }
  17. const { errcode = 0, errmsg } = res || {};
  18. if (errcode === 0) {
  19. if (_.isFunction(_okText)) {
  20. return _okText();
  21. }
  22. if (_okText) {
  23. // Vue.prototype.$u.toast({
  24. // text: _okText,
  25. // type: 'success',
  26. // });
  27. }
  28. return true;
  29. }
  30. if (_.isFunction(_errText)) {
  31. return _errText();
  32. }
  33. // Vue.prototype.$u.toast({
  34. // text: _errText || errmsg,
  35. // type: 'error',
  36. // });
  37. return false;
  38. };
  39. },
  40. };
  41. Vue.use(Plugin);