feat: 秒杀CURD

feature/task1.0.0__0514__ch
向文可 2 years ago
parent bb05072e3e
commit 797b7f1215

@ -0,0 +1,35 @@
import request from '@/utils/request.js';
export const search = (params) => {
return request({
url: '/mall/marketing/activity',
method: 'get',
params,
});
};
export const detail = (id) => {
return request({
url: '/mall/marketing/activity/' + id,
method: 'get',
});
};
export const create = (data) => {
return request({
url: '/mall/marketing/activity',
method: 'post',
data,
});
};
export const update = (data) => {
return request({
url: '/mall/marketing/activity/' + data.id,
method: 'put',
data,
});
};
export const remove = (idList) => {
return request({
url: '/mall/marketing/activity',
method: 'delete',
params: { idList },
});
};

@ -11,6 +11,9 @@
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>
.el-button { .el-button {
&.el-button--text {
background-color: transparent;
}
:deep(.el-icon) { :deep(.el-icon) {
position: relative; position: relative;
top: -2px; top: -2px;

@ -1,4 +1,4 @@
import * as api from '@/api/sales/category.js'; import * as api from '@/api/operation/limit.js';
import { ElMessage, ElMessageBox } from '@/plugins/element-plus'; import { ElMessage, ElMessageBox } from '@/plugins/element-plus';
const state = () => ({ const state = () => ({
code: 'LimitActivity', code: 'LimitActivity',
@ -25,11 +25,13 @@ const mutations = {
setOpts: (state, data) => (state.opts = data), setOpts: (state, data) => (state.opts = data),
}; };
const actions = { const actions = {
search: async ({ state, commit }) => { search: async ({ state, commit, rootGetters }) => {
let res = await api.search(state.condition); let data = { ...state.condition };
commit('setList', res); let res = await api.search({ ...rootGetters['local/page'](state.code), ...data });
commit('setList', res?.records || []);
commit('setTotal', res?.total || 0);
if (!res) { if (!res) {
ElMessage.error('查询商品分类列表失败'); ElMessage.error('查询活动列表失败');
} }
return res; return res;
}, },
@ -39,11 +41,8 @@ const actions = {
init: true, init: true,
}); });
}, },
detail: async ({ state, dispatch }, id) => { detail: async (context, id) => {
if (!state.list.length) { let res = await api.detail(id);
await dispatch('search');
}
let res = state.list.find((item) => item.id === id);
if (!res) { if (!res) {
ElMessage.error('加载详情失败'); ElMessage.error('加载详情失败');
} }
@ -51,6 +50,9 @@ const actions = {
}, },
save: async ({ dispatch }, data) => { save: async ({ dispatch }, data) => {
let save = data.id ? api.update : api.create; let save = data.id ? api.update : api.create;
data = _.cloneDeep(data);
data.activityStartTime = data.activityStartTime.split(' ')[0];
data.activityEndTime = data.activityEndTime.split(' ')[0];
let res = await save(data); let res = await save(data);
if (res) { if (res) {
ElMessage.success('保存成功'); ElMessage.success('保存成功');
@ -60,22 +62,13 @@ const actions = {
} }
return res; return res;
}, },
sort: async (context, data) => {
let res = await api.sort(data);
if (res) {
ElMessage.success(`移动前序号:${data.oldSort + 1};移动后序号:${data.currentSort + 1}`);
} else {
ElMessage.error('保存排序失败');
}
return res;
},
remove: async ({ dispatch }, ids) => { remove: async ({ dispatch }, ids) => {
if (!ids.length) { if (!ids.length) {
ElMessage.warning('请选择要删除的数据'); ElMessage.warning('请选择要删除的数据');
} else { } else {
try { try {
await ElMessageBox.confirm('数据删除后无法恢复,确定要删除吗?', '危险操作'); await ElMessageBox.confirm('数据删除后无法恢复,确定要删除吗?', '危险操作');
let res = await api.remove({ id: ids.join(',') }); let res = await api.remove(ids.join(','));
if (res) { if (res) {
ElMessage.success('删除成功'); ElMessage.success('删除成功');
dispatch('search'); dispatch('search');

@ -9,16 +9,16 @@
:reset="handleReset" :reset="handleReset"
title="活动" title="活动"
:total="total" :total="total"
@create="handleCreate" @create="handleCreate()"
@search="handleSearch" @search="handleSearch"
> >
<template #search> <template #search>
<el-form inline> <el-form inline>
<el-form-item label="活动名称" prop="name"> <el-form-item label="活动名称" prop="activityName">
<el-input v-model="state.condition.name" /> <el-input v-model="state.condition.activityName" />
</el-form-item> </el-form-item>
<el-form-item label="活动状态" prop="status"> <el-form-item label="活动状态" prop="activityStatus">
<el-select v-model="state.condition.status" :opts="opts.status" /> <el-select v-model="state.condition.activityStatus" :opts="opts.status" />
</el-form-item> </el-form-item>
</el-form> </el-form>
</template> </template>
@ -29,16 +29,12 @@
<el-input v-model="formState.form.name" /> <el-input v-model="formState.form.name" />
</el-form-item> </el-form-item>
<el-form-item label="活动名称" prop="time"> <el-form-item label="活动名称" prop="time">
<el-date-picker <el-date-picker v-model="formState.form.time" type="daterange" value-format="YYYY-MM-DD" />
v-model="formState.form.time"
type="datetimerange"
value-format="YYYY-MM-DD HH:mm:ss"
/>
</el-form-item> </el-form-item>
</el-form> </el-form>
<template #footer> <template #footer>
<el-button @click="formState.formVisible = false">取消</el-button> <el-button @click="formState.formVisible = false">取消</el-button>
<el-button type="primary" @click="handleSave"></el-button> <el-button :loading="formState.submitting" type="primary" @click="handleSave"></el-button>
</template> </template>
</el-dialog> </el-dialog>
</div> </div>
@ -52,7 +48,7 @@
const store = useStore(); const store = useStore();
const loading = ref(false); const loading = ref(false);
const code = computed(() => store.state.limitActivity.code); const code = computed(() => store.state.limitActivity.code);
const list = computed(() => store.state.limitActivity.list); const list = computed(() => _.cloneDeep(store.state.limitActivity.list));
const total = computed(() => store.state.limitActivity.total); const total = computed(() => store.state.limitActivity.total);
const opts = computed(() => store.state.limitActivity.opts); const opts = computed(() => store.state.limitActivity.opts);
if (!unref(opts).init) { if (!unref(opts).init) {
@ -62,8 +58,8 @@
/* 列表查询 */ /* 列表查询 */
const state = reactive({ const state = reactive({
condition: { condition: {
name: null, activityName: null,
status: null, activityStatus: null,
}, },
}); });
watch( watch(
@ -75,8 +71,8 @@
); );
const handleReset = () => { const handleReset = () => {
state.condition = { state.condition = {
name: null, activityName: null,
status: null, activityStatus: null,
}; };
}; };
const handleSearch = async () => { const handleSearch = async () => {
@ -86,12 +82,14 @@
}; };
/* 表单 */ /* 表单 */
const refsForm = ref(null);
const formState = reactive({ const formState = reactive({
formVisible: false, formVisible: false,
refsForm: null, submitting: false,
form: { form: {
name: null, name: null,
time: [], time: [],
isOnline: false,
}, },
rules: { rules: {
name: [{ required: true, message: '活动名称不能为空' }], name: [{ required: true, message: '活动名称不能为空' }],
@ -107,16 +105,23 @@
time: [], time: [],
} }
); );
formState.formVisible.value = true; formState.formVisible = true;
}; };
// //
const handleSave = async () => { const handleSave = async () => {
formState.submitting = true;
try { try {
await unref(formState.refsForm).validate(); await unref(refsForm).validate();
let data = _.cloneDeep(formState.form);
data.activityStartTime = data.time[0];
data.activityEndTime = data.time[1];
delete data.time;
await store.dispatch('limitActivity/save', data);
formState.formVisible = false; formState.formVisible = false;
} catch (e) { } catch (e) {
console.info('取消保存', e); console.info('取消保存', e);
} }
formState.submitting = false;
}; };
/* 操作 */ /* 操作 */
@ -129,8 +134,10 @@
router.push({ name: 'UpdateLimitProduct', params: { id: row.id } }); router.push({ name: 'UpdateLimitProduct', params: { id: row.id } });
}; };
// //
const handleEnable = (row, value) => { const handleEnable = async (row) => {
row.enabled = value; loading.value = true;
await store.dispatch('limitActivity/save', row);
loading.value = false;
}; };
// //
const handleRemove = async (rows) => { const handleRemove = async (rows) => {
@ -160,23 +167,21 @@
label: '活动状态', label: '活动状态',
width: 160, width: 160,
slots: { slots: {
default: ({ row }) => proxy.$dict(unref(opts).status, row.status), default: ({ row }) => proxy.$dict(unref(opts).status, row.activityState),
}, },
}, },
{ {
label: '活动时间', label: '活动时间',
width: 360, width: 360,
slots: { slots: {
default: ({ row }) => row.startTime + ' - ' + row.endTime, default: ({ row }) => row.activityStartTime + ' - ' + row.activityEndTime,
}, },
}, },
{ {
label: '上线/下线', label: '上线/下线',
width: 120, width: 120,
slots: { slots: {
default: ({ row }) => ( default: ({ row }) => <ElSwitch v-model={row.isOnline} onChange={() => handleEnable(row)} />,
<ElSwitch value={row.isEnable} onChange={(visible) => handleEnable(row, visible)} />
),
}, },
}, },
{ {
@ -192,10 +197,14 @@
<ElButton type="text" onClick={() => handleUpdateProduct(row)}> <ElButton type="text" onClick={() => handleUpdateProduct(row)}>
设置商品 设置商品
</ElButton> </ElButton>
<ElButton type="text" onClick={() => handleCreate(row)}> <ElButton type="text" disabled={row.activityState !== 1} onClick={() => handleCreate(row)}>
编辑 编辑
</ElButton> </ElButton>
<ElButton type="text" onClick={() => handleRemove([row])}> <ElButton
type="text"
disabled={row.activityState !== 1}
onClick={() => handleRemove([row])}
>
删除 删除
</ElButton> </ElButton>
</div> </div>

Loading…
Cancel
Save