Type

  1. export type UseMethodItem = { name: string; args?: any[]; fallback?: (() => any) | string };

Mixin

  1. import Vue from 'vue';
  2. Vue.mixin({
  3. methods: {
  4. useMethod(methodItem): void {
  5. if (!methodItem) {
  6. return;
  7. }
  8. const { name: methodName, args = [], fallback: fallback }: UseMethodItem = methodItem;
  9. if (!methodName) {
  10. return;
  11. }
  12. if (methodName in this) {
  13. //@ts-ignore
  14. this[methodName].apply(this, [...args]);
  15. return;
  16. }
  17. if (fallback) {
  18. try {
  19. if (typeof fallback === 'string') {
  20. // @ts-ignore
  21. this[fallback].apply(this);
  22. return;
  23. } else {
  24. fallback.apply(this);
  25. }
  26. } catch (e) {
  27. this.$logger(`[ UseMethod ] No method ${methodName}() and fallback ${fallback}() on instance.`);
  28. return;
  29. }
  30. }
  31. this.$logger(`[ UseMethod ] No method ${methodName}() on instance.`);
  32. }
  33. }
  34. });

ComponmentPage

  1. export default Vue.extend({
  2. data(): DataType {
  3. return {
  4. config: {
  5. featuredButtons: [
  6. { name: '每日签到', icon: 'gift' },
  7. { name: '金币商城', icon: 'shop' }
  8. ],
  9. settings: [
  10. {
  11. name: '账号管理',
  12. icon: 'user',
  13. options: [
  14. { name: '修改昵称', path: '/pages/user/nickname' },
  15. { name: '修改密码', path: '/pages/user/password' },
  16. { name: '修改头像', path: '' },
  17. { name: '删除账号', path: '' },
  18. { name: '认证指纹', path: '' }
  19. ]
  20. },
  21. {
  22. name: '身份管理',
  23. icon: 'identity',
  24. options: [
  25. { name: '切换身份', path: '' },
  26. { name: '新增身份', path: '' },
  27. { name: '上传人脸识别照片', path: '' }
  28. ]
  29. },
  30. {
  31. name: '服务管理',
  32. icon: 'list',
  33. options: [
  34. { name: '查看订单', path: '' },
  35. { name: '查看用户协议', path: '' },
  36. { name: '查看隐私协议', path: '' },
  37. { name: '设置隐私权限', path: '' }
  38. ]
  39. },
  40. {
  41. name: '邀请好友',
  42. icon: 'share'
  43. },
  44. {
  45. name: '退出登录',
  46. icon: 'exit',
  47. method: {
  48. name: 'toLogout',
  49. fallback: 'toPromptUnderConstruction'
  50. }
  51. }
  52. ]
  53. }
  54. };
  55. },
  56. methods: {
  57. toPromptUnderConstruction() {
  58. this.$toast('暂不可用');
  59. },
  60. gotoSubPage(path: string) {
  61. uni.navigateTo({
  62. url: path,
  63. fail: () => this.$toast('暂不可用')
  64. });
  65. return;
  66. },
  67. toLogout() {
  68. uni.showModal({
  69. title: '确认提示',
  70. content: '退出登录当前账号?',
  71. cancelText: '取消',
  72. confirmText: '确认',
  73. success: (res) => {
  74. if (res.confirm) {
  75. this.$ewsedu().Auth.logout();
  76. }
  77. }
  78. });
  79. }
  80. }
  81. });