0.2.93 • Published 3 years ago

vue-admin-mixin v0.2.93

Weekly downloads
788
License
-
Repository
github
Last release
3 years ago

Vue Admin Mixin

For the development of admin project, some commonly used mixins are implemented for rapid development, code reuse and code specification.

This library fully depends on vue-property-decorator, so please read its README before using this library.

License

MIT License

Install

npm i -S vue-admin-mixin

Usage

There are several Mixins:

There are several Decorators:

There are several Service:

VdMixin mixin

提供基本方法,所有的Mixin都继承了这个类

VdMixin 属性:

propertiereturn typedescribe
vdLoadingboolea正在加载数据

VdMixin 方法:

methodreturn typedescribe
vdFetch()T发送api基础类(也可以直接用 vdFetch 充httpUtil获取方法)
vdConfirm(info: VdConfirmInfo)void确认提示
vdMessage(fetch,options)void请求处理消息信息
vdRequest(url: string, data?: any, options)Promise<UseResult>发送请求api,实际上是vdConfirm、vdMessage的组合使用

options:
interface VdRequestOptions {
	load?: boolean; // 是否是获取请求
	loading?: boolean; // 是否加载loading框
	message?: VdMessageOptions; // 处理message选项
	confirm?: VdConfirmInfo; // 确认提示消息
}

VdTable.MainMixin<P, R> mixin

用于列表分页查询的插件。

<template>
  <platform-list-search></platform-list-search>
  <cc-table-container :total="vdTotal" v-loading="vdLLoading" @loadByPage="vdRefreshByPage">
    <platform-list-table></platform-list-table>
  </cc-table-container>
</template>
import { Component } from 'vue-property-decorator';
import { VdTable } from 'vue-admin-mixin';

@Component({
	components: { PlatformListSearch, PlatformListTable },
})
export default class PlatformList extends VdTable.MainMixin<PlatformSearch, Platform> {
	
  	/**
	 * 设置默认请求参数
	 */
	public vdDefaultParams(): PlatformSearch {
		return { searchText: '' };
	}

	/**
	 * 加载数据
	 */
	public created() {
		this.vdInitData(API_PLATFORM_LIST).then();
	}
}

VdTable.ParamMixin<P, R> mixin

建议搜索区域是一个组件,如果是这样的话, 可以使用 VdTable.ParamMixin 来同步列表的请求参数和刷新事件。

<template>
 <el-form :inline="true" :model="vdParams" class="demo-form-inline">
  <el-form-item label="筛选">
    <el-input v-model="vdParams.searchText" placeholder="请输入筛选条件" />
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="vdSearch">搜索</el-button>
    <el-button @click="vdClear">清空</el-button>
  </el-form-item>
 </el-form>
</template>
import { Component } from 'vue-property-decorator';
import { VdTable } from 'vue-admin-mixin';

@Component
export default class PlatformListSearch extends VdTable.ParamMixin<PlatformSearch> {
}

VdTable.ListMixin<P, R> mixin

建议表格的结果页面是一个组件, 可以使用 VdTable.ListMixin 来同步列表的请求参数和刷新事件。

<el-table :data="vdList" stripe>
  <el-table-column prop="name" label="XXX" width="168">
  </el-table-column>
  <div slot="vdLEmpty">
    <moy-empty />
  </div>
</el-table>
import { Component } from 'vue-property-decorator';
import { VdTable } from 'vue-admin-mixin';

@Component
export default class PlatformListTable extends VdTable.ListMixin<Platform> {
}

VdTable.MainMixin 属性:

propertiereturn typedescribe
vdTotalnumber数据总条数
vdPagenumber当前页码
vdPageSizenumber每页显示条数
vdSelectedR[]多选内容
vdListR[]数据列表
vdParamsP请求参数
vdLLoadingboolean是否正在加载
vdLEmptyboolean是否当前数据为空数据
vdLHasDataboolean是否有数据
vdIndexnumber当前索引
vdActiveR undefined当前选中的对象 (Only get is supported)
vdIsDefaultSetboolean请求结果是否直接赋值给 vdList

VdTable.MainMixin 方法:

methodreturn typedescribe
vdUsePage()boolean重写可以指定是否使用分页(默认是true)
vdDefaultParams()P设置默认参数,可以重写(默认是{})
vdInitData(path?: string, data?: P)Promise<UseResult<R[]>>初始化数据(vdPage = 1)
vdRefresh(data?: P)Promise<UseResult<R[]>>刷新数据(页码不变)
vdRefreshByPage(data: { page?: number; pageSize?: number })void根据分页参数变化,重新加载数据
vdSetListPath(path?: string)void设置请求参数,vdInitData 时会自动设置到内部变量上

VdTable.ParamMixin 属性:

propertiereturn typedescribe
vdParamsnumber列表请求参数(同步 VdTable.MainMixin里的vdParams)

VdTable.ParamMixin 方法:

methodreturn typedescribe
vdSearchvoid查询数据(实际上调用的是 VdTable.MainMixin里的vdInitData方法)
vdRefreshvoid刷新数据(实际上调用的是 VdTable.MainMixin里的vdRefresh方法)
vdClearvoid清空参数(清空并且同步父组件到vdParams)

VdTable.ListMixin 属性:

propertiereturn typedescribe
vdListArray列表请求返回值(同步 VdTable.MainMixin里的vdList)

VdTable.ListMixin 方法:

methodreturn typedescribe
vdSearchvoid查询数据(实际上调用的是 VdTable.MainMixin里的vdInitData方法)
vdRefreshvoid刷新数据(实际上调用的是 VdTable.MainMixin里的vdRefresh方法)

VdModal.CrlMixin VdModal.TargetMixin VdModal.CallbackMixin mixin

用于控制模态框的打开、隐藏、数据传值和回值。 VdModal.CrlMixin 继承了 VdModal.CallbackMixin 所以可以使用 CallbackMixin 的属性和方法。

<template>
   <div @click="vdOpenModalByAdd()">打开添加modal(打开方式1)</div>
   <div @click="vdOpenModalByUpdate(data)">打开修改modal(打开方式1)</div>
   <div @click="handleOpenModal(data).then()">也可以这样调用(打开方式2)</div>
</template>
import { Component } from 'vue-property-decorator';
import { VdModal } from 'vue-admin-mixin';

@Component({
	components: { Modal1 },
})
export default class ModalTest extends VdModal.CrlMixin {
	/**
	 * 打开模态框方法(打开方式2)
	 */
       public async handleOpenModal(data: any) {
		const result = await this.vdOpenModal(PageMode.UPDATE, data);
	}
	/**
	 * 模态框回调(回调方式1)
	 */
	public vdModalCallback(data: any, pipe?: string) {
		if (this.vdIsUpdate) {
			this.loadDetailById(id).then();
		} else {
			this.loadList().then();
		}
	}
	/**
	 * 如果多个modal回调监听使用pipe区分(回调方式2)
	 * 注意:不能跟 vdModalCallback 重名
	 */
	@ModalCallback('pipeKey')
	public handleModalCallbackByPipeKey(data: any) {
		if (this.vdIsUpdate) {
			this.loadDetailById(id).then();
		} else {
			this.loadList().then();
		}
	}
}

Note that:

如果多个想控制多个modal的话, 使用pipe来解决, 打开时传入pipe, 接受时可以根据pipe来区分。 另一种方式使用@ModalCallback('pipeKey')注解来实现回调监听(推荐使用)。

TargetMixin mixin

VdModal.TargetMixin,模态框组件继承TargetMixin,在vdShowModal回调函数上可以接受到传入的数据,vdCloseModalCallback 方法可以回传数据到CrlMixin或者CallbackMixin上。

<template>
   <el-dialog :title="vdActionText + 'XXX'" :visible.sync="vdVisible">
      <span slot="footer" class="dialog-footer">
	 <el-button @click="vdCloseModal">取 消</el-button>
	 <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
   </el-dialog>
</template>
import { Component } from 'vue-property-decorator';
import { VdModal } from 'vue-admin-mixin';

@Component
export default class Modal1 extends VdModal.TargetMixin {
	/**
	 * 打开模态框回调
	 */
	public vdShowModal(data?: any): void {
	}

	/**
	 * 关闭模态框回调
	 */
	public vdHiddenModal(): void {
	}
	
	/**
	 * 提交请求
	 */
	public handleSubmit() {
		// 关闭模态框并且回传值过去
		this.vdCloseModalCallback(this.data.id);
	}
}

VdModal.CrlMixin 方法(继承了CallbackMixin所以拥有CallbackMixin的方法和属性):

methodreturn typedescribe
vdOpenModalByAdd(data?: any, pipe = '')void打开模态框(添加)
vdOpenModalByUpdate(data?: any, pipe = '')void打开模态框(修改)
vdOpenModalByCheck(data?: any, pipe = '')void打开模态框(查看)
vdOpenModal(mode: PageMode, data?: any, pipe = '')Promise打开模态框

VdModal.TargetMixin 属性:

propertiereturn typedescribe
vdVisibleboolean控制模态框显示隐藏
vdInputDataany打开传入模态框数据
vdPageModePageMode当前模式
vdIsUpdateboolean是否是更新 (Only get is supported)
vdIsAddboolean是否是添加 (Only get is supported)
vdActionTextstring显示对应模式的文本 (Only get is supported)

VdModal.TargetMixin 方法:

methodreturn typedescribe
vdSetPipe()string管道,用于匹配打开的模态框(多个modal的时候使用, 或者给modal组件传入pipe属性)
vdShowModal(data?: any)void打开模态框回调
vdHiddenModal()void关闭模态框回调
vdCloseModal()void关闭模态框
vdCloseModalCallback(data?: any)void关闭模态框并且传值触发回调

VdModal.CallbackMixin 属性:

propertiereturn typedescribe
vdIsUpdateboolean是否是更新
vdIsAddboolean是否是添加

VdModal.CallbackMixin 方法:

methodreturn typedescribe
vdModalCallback(data?: any, pipe?: string)void模态框关闭时的回调函数(或者可以使用@ModalCallback('pipeKey‘)来区分不同pipe)

VdListMixin<P, R> mixin

用于获取数组对象

VdListMixin 属性:

propertiereturn typedescribe
vdListR[]数据列表
vdParamsP请求参数
vdLLoadingboolean是否正在加载
vdLEmptyboolean是否当前数据为空数据
vdLHasDataboolean是否有数据
vdIndexnumber当前索引
vdActiveR undefined当前选中的对象 (Only get is supported)
vdIsDefaultSetboolean请求结果是否直接赋值给 vdList
vdSubIndexnumber选择二级分类索引
vdSubActivenumber当前选中二级的对象 (Only get is supported)

VdListMixin 方法:

methodreturn typedescribe
vdSetSubAttr()void设置二级属性字段
vdDefaultParams()P设置默认参数,可以重写(默认是{})
vdLoadList(path?: string, data?: P)Promise<UseResult<R[]>>加载数据
vdLoadSuccess(result?: P[])void加载数据回调,可以重写父类方法
vdLoadError(err?: any)void加载数据失败,可以重写父类方法
vdSetListPath(path?: string)void设置请求参数,vdLoadList 时会自动设置到内部变量上

VdObjMixin<P, R> mixin

用于获取数组对象

VdObjMixin 属性:

propertiereturn typedescribe
vdDataR[]返回数据
vdOLoadingboolean是否正在加载
vdOEmptyboolean是否当前数据为空数据
vdOHasDataboolean是否有数据

VdObjMixin 方法:

methodreturn typedescribe
vdDefaultData()T设置默认值,可以重写父类方法
vdResetData()void重置默认值
vdLoadData(path?: string, params?: any)void加载数据
vdLoadSuccess(result?: T)void加载数据回调,可以重写父类方法
vdLoadError(err?: any)void加载数据失败,可以重写父类方法
vdSetListPath(path?: string)void设置请求参数,vdLoadData 时会自动设置到内部变量上

VdSubmitMixin<P, R> mixin

用于表单提交

VdSubmitMixin 属性:

propertiereturn typedescribe
vdDataR[]返回数据
vdSLoadingboolean是否正在加载
vdOEmptyboolean是否当前数据为空数据
vdOHasDataboolean是否有数据

VdSubmitMixin 方法:

methodreturn typedescribe
vdDefaultData()T设置默认值,可以重写父类方法
vdResetData()void重置默认值
vdSubmit(path: string, data?: T, options)void提交数据(options:{ merge: boolean } = {merge: false})如果data不传入则默认使用vdData的值, 如果merge设置为true的话,data的参数会merge到vdData上。
vdSubmitSuccess(result?: T)void提交成功回调,可以重写父类方法
vdSubmitError(err?: any)void提价失败回调,可以重写父类方法
vdValidate($refs?, formName, success, err)void表单验证

VdEditMixin<P, R> mixin

用于编辑页面,加载数据并且提交数据。是VdObjMixin、VdSubmitMixin 混入的结果

@Component
// @ts-ignore
export class VdEditMixin<T> extends Mixins<VdObjMixin<T>, VdSubmitMixin<T, string>>(
	VdObjMixin,
	VdSubmitMixin,
) {
}

@ModalCallback decorators

使用多个modal时,使用@ModalCallback来接受具体回调. 需要混入VdModal.CrlMixin 或者 VdModal.CallbackMixin.

	/**
	 *  pipe key1 回调
	 */
	@ModalCallback(’key1‘)
	public handleModalCallbackByPipeKey1(data?: any) {
	}
	
	/**
	 *  pipe key2 回调
	 */
	@ModalCallback(’key2‘)
	public handleModalCallbackByPipeKey2(data?: any) {
	}

@Confirm decorators

确认提示框注解,使用时传入标题和内容,确认后执行方法。跟 VdMixinthis.vdConfirm 使用方法一样。

	/**
	 * 重置密码
	 */
	@Confirm({title:'确认', content:'确定要重置密码吗?'})
	public handleResetPwd() {
	}
	
	/**
	 * 启用禁用
	 * @param isEnabled 开启关闭
	 */
	@Confirm(isEnabled => confirmAutoEnabled(isEnabled, PLATFORM))  // 可以使用被注释的方法的参数进行回调的入参值
	public toggleEnabled(isEnabled: Enabled) {
	}
	

@Validate decorators

form验证注解,使用时传入被标记 ref的form的refName,验证成功时执行方法。跟 VdMixinthis.vdValidate 使用方法一样。

	/**
	 * 保存提交
	 */
	@Validate(['baseForm', 'userForm']) //  或者单个form使用: @Validate(’userForm‘)
	public vdHandleSubmit(): void {
	}

默认使用VdDefaultConfigService service

可配置选项:一些共通处理内容已经对外暴露接口, 一般情况根据项目需求来实现自定义配置。以下是基于element-ui组件配置。

@Component
export default class App extends Vue {
	public created() {
        		VdConfigService.setup({
        			handleConfirm: (info: VdConfirmInfo) =>
        				this.$confirm(info.content, info.title, {
        					confirmButtonText: '确定',
        					cancelButtonText: '取消',
        					closeOnClickModal: false,
        				}),
        			handle403: () => this.$router.push({ name: 'Login' }).then(),
        			handle401: () => this.$router.push({ name: 'Login' }).then(),
        			showErrorMessage: (message: string) => Message.error(message),
        			showSuccessMessage: (message: string) => Message.success(message),
        			handleStartLoading: () => {
        				loadingCrl = this.$loading({
        					lock: true,
        				});
        			},
        			handleCloseLoading: () => {
        				if (loadingCrl) {
        					loadingCrl.close();
        				}
        			},
        			handleFormValidate: ($refs: { [key: string]: Vue | Element | Vue[] | Element[] }, formNames: string[]) => {
        				let _result = false;
        				try {
        					formNames.forEach(formName => {
        						const target = $refs[formName];
        						if (target) {
        							/* eslint-disable */
        							// @ts-ignore
        							target?.validate((result, item) => {
        								if (!result) {
        									for (const key in item) {
        										setTimeout(() => {
        											if (VdConfigService.config?.showErrorMessage) {
        												VdConfigService.config?.showErrorMessage(item[key][0].message);
        											}
        										}, 1);
        									}
        									_result = true;
        								}
        							});
        						}
        					});
        				} catch (e) {
        					console.log(e);
        				}
        				return _result;
        			},
        			handleHttpResult: <T>(resData: any): UseResult<T> => {
        				const { success, errorCode, errorMessage, payload, count } = resData || {};
        				return { success, errorCode, errorMessage, payload, totalCount: count || 0 };
        			},
        		});
        	}
}
0.2.93

3 years ago

0.2.92

3 years ago

0.2.91

3 years ago

0.2.90

3 years ago

0.2.89

3 years ago

0.2.88

3 years ago

0.2.87

3 years ago

0.2.86

3 years ago

0.2.85

3 years ago

0.2.84

3 years ago

0.2.83

3 years ago

0.2.82

3 years ago

0.2.81

3 years ago

0.2.80

3 years ago

0.2.74

3 years ago

0.2.73

3 years ago

0.2.72

3 years ago

0.2.71

3 years ago

0.2.70

3 years ago

0.2.79

3 years ago

0.2.78

3 years ago

0.2.77

3 years ago

0.2.76

3 years ago

0.2.75

3 years ago

0.2.69

3 years ago

0.2.68

3 years ago

0.2.67

3 years ago

0.2.65

3 years ago

0.2.63

3 years ago

0.2.64

3 years ago

0.2.62

3 years ago

0.2.61

3 years ago

0.2.60

3 years ago

0.2.59

3 years ago

0.2.58

3 years ago

0.2.57

3 years ago

0.2.56

3 years ago

0.2.55

3 years ago

0.2.54

3 years ago

0.2.53

3 years ago

0.2.52

3 years ago

0.2.51

3 years ago

0.2.50

3 years ago

0.2.49

3 years ago

0.2.48

3 years ago

0.2.47

3 years ago

0.2.46

3 years ago

0.2.45

3 years ago

0.2.44

3 years ago

0.2.43

3 years ago

0.2.42

3 years ago

0.2.41

3 years ago

0.2.40

3 years ago

0.2.39

3 years ago

0.2.38

3 years ago

0.2.37

3 years ago

0.2.36

3 years ago

0.2.35

3 years ago

0.2.34

3 years ago

0.2.33

3 years ago

0.2.32

3 years ago

0.2.31

3 years ago

0.2.30

3 years ago

0.2.29

3 years ago

0.2.28

3 years ago

0.2.27

3 years ago

0.2.26

3 years ago

0.2.25

3 years ago

0.2.24

3 years ago

0.2.23

3 years ago

0.2.22

3 years ago

0.2.21

3 years ago

0.2.20

3 years ago

0.2.19

3 years ago

0.2.18

3 years ago

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.1

3 years ago

0.2.2

3 years ago

0.1.69

3 years ago

0.2.0

3 years ago

0.1.65

3 years ago

0.1.66

3 years ago

0.1.67

3 years ago

0.1.68

3 years ago

0.1.52

3 years ago

0.1.53

3 years ago

0.1.54

3 years ago

0.1.55

3 years ago

0.1.56

3 years ago

0.1.57

3 years ago

0.1.58

3 years ago

0.1.59

3 years ago

0.1.63

3 years ago

0.1.64

3 years ago

0.1.60

3 years ago

0.1.61

3 years ago

0.1.62

3 years ago

0.1.50

3 years ago

0.1.51

3 years ago

0.1.49

3 years ago

0.1.41

4 years ago

0.1.42

4 years ago

0.1.43

4 years ago

0.1.44

4 years ago

0.1.45

4 years ago

0.1.46

4 years ago

0.1.47

4 years ago

0.1.48

4 years ago

0.1.40

4 years ago

0.1.38

4 years ago

0.1.39

4 years ago

0.1.30

4 years ago

0.1.31

4 years ago

0.1.32

4 years ago

0.1.33

4 years ago

0.1.34

4 years ago

0.1.35

4 years ago

0.1.36

4 years ago

0.1.37

4 years ago

0.1.29

4 years ago

0.1.27

4 years ago

0.1.28

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.24

4 years ago

0.1.23

4 years ago

0.1.22

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.17

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.14

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.4

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.3

4 years ago

0.1.0

4 years ago