check-res.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 { Toast } from 'vant';
  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 = 200, errmsg } = res || {};
  19. if (errcode === 200) {
  20. if (_.isFunction(_okText)) {
  21. return _okText();
  22. }
  23. if (_okText) {
  24. Toast({ type: 'success', message: _okText });
  25. }
  26. return true;
  27. }
  28. if (_.isFunction(_errText)) {
  29. return _errText();
  30. }
  31. Toast({ type: 'danger', message: errText });
  32. return false;
  33. };
  34. },
  35. };
  36. Vue.use(Plugin);