import Vue from 'vue' export default function getCategoryList (payload = {}, option = {}) { return new Promise(async (resolve, reject) => { // 筛选项 const filters = { name: { type: 'input', title: '分类名称', key: 'name', props: { placeholder: '输入分类名称', clearable: true, }, }, } // 操作项 const actions = [ { title: '添加分类', action: 'goCategoryDetail', props: { type: 'primary', }, }, ] const sortActions = [ { title: '保存', action: 'editCategorySort', props: { type: 'success', }, }, ] // 表格项 const columns = { name: { title: '分类名称', key: 'name', align: 'center', width: 100, }, level: { title: '级别', key: 'level', align: 'center', width: 80, format: { type: 'config', }, }, ico: { nodeType: 'image', payload: { key: 'ico', title: '分类图标', width: 80, height: 80, }, }, desc: { title: '分类描述', key: 'desc', align: 'center', }, is_open: { nodeType: 'iSwitch', payload: { title: '开启', key: 'is_open', action: 'switchCategory', }, }, } const sortColumns = { name: { title: '分类名称', key: 'name', align: 'center', }, level: { title: '级别', key: 'level', align: 'center', format: { type: 'config', }, }, sort: { nodeType: 'input', payload: { title: '排序', key: 'sort', props: { type: 'number', number: true, }, }, }, } // 表格扩展(数据操作) const columnsExtra = { nodeType: 'action', payload: { title: '操作', list: [ { title: '编辑', action: 'goCategoryDetail', props: { loading: false, }, }, { title: '删除', action: 'deleteCategory', }, ], }, } const sortColumnsExtra = { nodeType: 'action', payload: { title: '操作', list: [ { title: '商品排序', action: 'goCategoryProductSort', props: { loading: false, type: 'primary', }, }, ], }, } try { const isSort = option.type === 'sort' if (isSort) payload.per_page = 1000 const { data, extra, meta } = await Vue.http.get('/product/category', { params: payload, }) // 处理级别功能和操作 const handleLevel = { level1 () { actions.push({ title: '排序', action: 'goCategorySort', actionOption: { parent_id: 0, type: 'sort', }, props: { type: 'primary', }, }) this.level2() }, level2 () { columnsExtra.payload.list.unshift({ title: '查看子分类', action: 'goCategoryList', props: { loading: false, type: 'primary', }, },) }, level3 () {}, } if (data.length) handleLevel[`level${data[0].level}`]() // 返回数据 const response = { filters: isSort ? [] : extra.filters.map(key => filters[key]).filter(Boolean), actions: isSort ? sortActions : actions, data, columns: extra.columns .map(key => (isSort ? sortColumns : columns)[key]) .filter(Boolean) .concat(isSort ? sortColumnsExtra : columnsExtra), page: isSort ? {} : meta.pagination, } resolve(response) } catch (e) { reject(e) } }) }