fix(components): 产品管理维护功能完善

This commit is contained in:
ronger 2024-03-20 21:43:47 +08:00
parent 68fd037ea8
commit 34588a514c
2 changed files with 122 additions and 130 deletions

View File

@ -1,51 +1,66 @@
<template> <template>
<el-row class="products"> <el-row class="products">
<el-col style="margin-bottom: 1rem;">
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/admin/dashboard' }">首页</el-breadcrumb-item>
<el-breadcrumb-item :to="{ path: '/admin/products' }">产品管理</el-breadcrumb-item>
<el-breadcrumb-item v-if="product.idProduct">编辑</el-breadcrumb-item>
<el-breadcrumb-item v-else>创建</el-breadcrumb-item>
</el-breadcrumb>
</el-col>
<el-col> <el-col>
<el-form ref='formStore' :model='formStore' :rules='rules' label-poionsit='right' label-width='110px'> <el-form ref='productInfo' :model='productInfo' :rules='rules' label-poionsit='right' label-width='110px'>
<div style="margin-bottom:20px"> <div style="margin-bottom:20px">
<img :src="formStore.productImgUrl" style="width: 120px;height: 120px;margin: 0 auto;display: block" <img :src="productInfo.productImgUrl" style="width: 120px;height: 120px;margin: 0 auto;display: block"
@click="cropperVisible=true"> @click="cropperVisible=true">
</div> </div>
<el-form-item label='产品名称' prop='productTitle'> <el-form-item label='产品名称' prop='productTitle'>
<el-input v-model='formStore.productTitle' placeholder='请输入产品名称'/> <el-input v-model='productInfo.productTitle' placeholder='请输入产品名称'/>
</el-form-item> </el-form-item>
<el-form-item label='产品价格' prop='code'> <el-form-item label='产品价格' prop='productPrice'>
<el-input v-model='formStore.productPrice' placeholder='请输入产品价格'/> <el-input v-model='productInfo.productPrice' placeholder='请输入产品价格'/>
</el-form-item> </el-form-item>
<el-form-item label='产品描述' prop='type'> <el-form-item label='产品描述' prop='type'>
<div id="contentEditor"></div> <div id="contentEditor"></div>
</el-form-item> </el-form-item>
<el-form-item label='tags' prop='order'> <el-form-item label='标签' prop='tags'>
<el-input v-model='formStore.tags' placeholder='请输入tags'/> <el-select
style="width: 100%;"
v-model="productTags"
multiple
filterable
allow-create
default-first-option
remote
:remote-method="remoteMethod"
placeholder="请选择文章标签"
:loading="loading"
@change="setLocalstorage('tags', productTags)">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</el-form-item> </el-form-item>
</el-form> </el-form>
<el-col style="margin-top: 1rem;"> <el-col style="margin-top: 1rem;">
</el-col> </el-col>
<el-col v-if="!isEdit" style="margin-top: 1rem;padding-right:3rem;text-align: right;"> <el-col v-if="!isEdit" style="margin-top: 1rem;padding-right:3rem;text-align: right;">
<el-button :loading="doLoading" plain @click="saveArticle">保存草稿</el-button> <el-button :loading="doLoading" plain type="primary" @click="postProduct">发布</el-button>
<el-button :loading="doLoading" plain type="primary" @click="postArticle">发布</el-button>
</el-col> </el-col>
<el-col v-else style="margin-top: 1rem;padding-right:3rem;text-align: right;"> <el-col v-else style="margin-top: 1rem;padding-right:3rem;text-align: right;">
<el-button :loading="doLoading" plain type="danger" @click="deleteArticle">删除</el-button> <el-button v-if="productInfo.status === 0" :loading="doLoading" plain type="danger" @click="updateStatus">下架</el-button>
<el-button v-if="productStatus === '1'" :loading="doLoading" plain @click="saveArticle">保存草稿</el-button> <el-button v-else :loading="doLoading" plain type="danger" @click="updateStatus">上架</el-button>
<el-button v-if="productStatus === '0'" :loading="doLoading" plain type="primary" @click="postArticle">更新 <el-button v-if="productInfo.status === 0" :loading="doLoading" plain type="primary" @click="postProduct">更新
</el-button> </el-button>
<el-button v-else :loading="doLoading" plain type="primary" @click="postArticle">发布</el-button> <el-button v-else :loading="doLoading" plain type="primary" @click="postProduct">发布</el-button>
</el-col> </el-col>
</el-col> </el-col>
<!-- <el-col v-else class="text-center">--> <ImgCropper :avatarUrl="productInfo.productImgUrl||''" :visible.sync='cropperVisible'
<!-- <el-alert--> @onSubmit="updateProductImgUrl"></ImgCropper>
<!-- title="用户无权限"-->
<!-- type="warning"-->
<!-- center-->
<!-- show-icon-->
<!-- :closable="false">-->
<!-- </el-alert>-->
<!-- </el-col>-->
<ImgCropper :avatarUrl="formStore.productImgUrl||''" :visible.sync='cropperVisible'
@onSubmit="updateUser"></ImgCropper>
</el-row> </el-row>
</template> </template>
@ -84,20 +99,7 @@ export default {
computed: { computed: {
...mapState({ ...mapState({
product: state => state.product.detail.data product: state => state.product.detail.data
}), })
// hasPermissions() {
// let account = this.$store.state.auth.user?.nickname;
// if (account) {
// if (this.$route.params.product_id) {
// if (account === this.product.productAuthor.userNickname) {
// return true;
// }
// } else {
// return true;
// }
// }
// return this.$auth.hasScope('blog_admin') || this.$auth.hasScope('admin');
// }
}, },
data() { data() {
return { return {
@ -107,22 +109,18 @@ export default {
linkToImageURL: '', linkToImageURL: '',
token: '' token: ''
}, },
formStore: { productInfo: {
idArticle: 0, idProduct: 0,
productTitle: '', productTitle: '',
productContent: '', productContent: '',
productType: 0, productType: 0,
tags: '', tags: '',
productStatus: '0', status: 0,
productPrice: 0, productPrice: 0,
productImgUrl: '',
productImgType: 0 productImgType: 0
}, },
idArticle: 0,
productTitle: '',
productContent: '',
productType: 0,
productTags: [], productTags: [],
productStatus: '0',
options: [], options: [],
list: [], list: [],
loading: false, loading: false,
@ -134,9 +132,9 @@ export default {
} }
}, },
methods: { methods: {
updateUser(data) { updateProductImgUrl(data) {
this.formStore.productImgUrl = data this.productInfo.productImgUrl = data
this.formStore.productImgType = 1 this.productInfo.productImgType = '1'
this.cropperVisible = false this.cropperVisible = false
}, },
_initEditor(data) { _initEditor(data) {
@ -268,10 +266,10 @@ export default {
this.options = []; this.options = [];
} }
}, },
deleteArticle() { deleteProduct() {
let _ts = this; let _ts = this;
_ts.doLoading = true; _ts.doLoading = true;
this.$confirm('确定删除吗?', '提示', { this.$confirm('确定下架吗?', '提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
cancelButtonText: '取消', cancelButtonText: '取消',
type: 'warning' type: 'warning'
@ -295,24 +293,27 @@ export default {
}); });
}, },
async postArticle() { async postProduct() {
let _ts = this; let _ts = this;
_ts.doLoading = true; _ts.doLoading = true;
let productContent = _ts.contentEditor.getValue(); let productContent = _ts.contentEditor.getValue();
let productContentHtml = await _ts.contentEditor.getHTML(); let productContentHtml = await _ts.contentEditor.getHTML();
if (!(_ts.formStore.productTitle && productContent)) { if (!(_ts.productInfo.productTitle && productContent)) {
_ts.$message("标题/正文不能为空!"); _ts.$message("标题/正文不能为空!");
_ts.doLoading = false; _ts.doLoading = false;
return false; return false;
} }
let product = { let product = {
idProduct: _ts.productInfo.idProduct,
productTitle: _ts.productInfo.productTitle,
productPrice: _ts.productInfo.productPrice,
productImgType: _ts.productInfo.productImgType || '0',
productImgUrl: _ts.productInfo.productImgUrl,
tags: _ts.productTags.join(','),
productContent: productContent, productContent: productContent,
productContentHtml: productContentHtml, productContentHtml: productContentHtml,
productStatus: 0,
..._ts.formStore
}; };
_ts.$axios[_ts.formStore.idProduct ? '$put' : '$post']('/api/product/post', product).then(function (res) { _ts.$axios[_ts.productInfo.idProduct ? '$put' : '$post']('/api/product/post', product).then(function (res) {
if (res) { if (res) {
if (res.message) { if (res.message) {
_ts.$message(res.message); _ts.$message(res.message);
@ -331,13 +332,13 @@ export default {
}) })
}, },
async saveArticle() { async saveProduct() {
let _ts = this; let _ts = this;
_ts.doLoading = true; _ts.doLoading = true;
let id = _ts.$route.params.product_id; let id = _ts.$route.params.product_id;
let productContent = _ts.contentEditor.getValue(); let productContent = _ts.contentEditor.getValue();
let productContentHtml = await _ts.contentEditor.getHTML(); let productContentHtml = await _ts.contentEditor.getHTML();
if (!(_ts.formStore.productTitle && productContent)) { if (!(_ts.productInfo.productTitle && productContent)) {
_ts.$message("标题/正文不能为空!"); _ts.$message("标题/正文不能为空!");
_ts.doLoading = false; _ts.doLoading = false;
return false; return false;
@ -347,10 +348,10 @@ export default {
productContent: productContent, productContent: productContent,
productContentHtml: productContentHtml, productContentHtml: productContentHtml,
productStatus: 1, productStatus: 1,
..._ts.formStore ..._ts.productInfo
}; };
_ts.$axios[_ts.formStore.idProduct ? '$put' : '$post']('/api/product/post', product).then(function (res) { _ts.$axios[_ts.productInfo.idProduct ? '$put' : '$post']('/api/product/post', product).then(function (res) {
if (res) { if (res) {
if (res.message) { if (res.message) {
_ts.$message(res.message); _ts.$message(res.message);
@ -374,6 +375,43 @@ export default {
_ts.$set(_ts, 'list', res); _ts.$set(_ts, 'list', res);
} }
}) })
},
updateStatus() {
let _ts = this;
let product = _ts.productInfo
let title, status;
if (product.status === 0) {
title = '下架';
status = 1;
} else {
title = '上架';
status = 0;
}
_ts.$confirm('确定' + title + '产品 ' + product.productTitle + '?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
_ts.$axios.$patch('/api/admin/product/update-status', {
idProduct: product.idProduct,
status: status
}).then(function (res) {
if (res && res.message) {
_ts.$message.error(res.message);
} else {
_ts.$message({
type: 'success',
message: title + '成功!'
});
}
});
}).catch(() => {
_ts.$message({
type: 'info',
message: '已取消'
});
});
} }
}, },
beforeRouteLeave(to, from, next) { beforeRouteLeave(to, from, next) {
@ -388,7 +426,7 @@ export default {
}).catch(() => { }).catch(() => {
return false return false
}); });
_ts.$store.commit("setActiveMenu", "product-post"); _ts.$store.commit("setActiveMenu", "admin-product-post-product_id");
} else { } else {
next(); next();
} }
@ -405,7 +443,7 @@ export default {
return '关闭提示'; return '关闭提示';
}); });
let _ts = this; let _ts = this;
_ts.$store.commit('setActiveMenu', 'product-post'); _ts.$store.commit('setActiveMenu', 'admin-product-post-product_id');
const responseData = await _ts.$axios.$get('/api/upload/token'); const responseData = await _ts.$axios.$get('/api/upload/token');
if (responseData) { if (responseData) {
_ts.$set(_ts, 'tokenURL', { _ts.$set(_ts, 'tokenURL', {
@ -414,27 +452,17 @@ export default {
linkToImageURL: responseData.linkToImageURL || '' linkToImageURL: responseData.linkToImageURL || ''
}) })
} }
console.log('test')
_ts.getTags(); _ts.getTags();
Vue.nextTick(() => { Vue.nextTick(() => {
console.log('test')
let productContent = ''; let productContent = '';
if (_ts.$route.params.product_id) { if (_ts.$route.params.product_id) {
_ts.$set(_ts, 'isEdit', true); _ts.$set(_ts, 'isEdit', true);
let product = _ts.product; let product = _ts.product;
_ts.formStore = JSON.parse(JSON.stringify(product)) _ts.productInfo = JSON.parse(JSON.stringify(product))
console.log(product)
// _ts.$set(_ts, 'idArticle', product.idArticle);
// _ts.$set(_ts, 'productTitle', product.productTitle);
// _ts.$set(_ts, 'productContent', product.productContent);
// _ts.$set(_ts, 'productStatus', product.productStatus);
// _ts.$set(_ts, 'productTags', (product.tags).split(','));
// localStorage.setItem("product-title", product.productTitle);
// localStorage.setItem("product-tags", (product.tags).split(','));
productContent = product.productContent productContent = product.productContent
let productTags = product.tags?.split(',');
_ts.$set(_ts, 'productTags', productTags || []);
} else { } else {
_ts.$set(_ts, 'isEdit', false); _ts.$set(_ts, 'isEdit', false);
} }

View File

@ -42,20 +42,33 @@
width="180" width="180"
prop="createdTime"> prop="createdTime">
</el-table-column> </el-table-column>
<el-table-column
label="标签">
<template slot-scope="scope">
<el-tag
style="margin-left: 0.5rem;"
v-for="tag in scope.row.tags?.split(',')"
:key="tag"
size="mini"
effect="plain">
# {{ tag }}
</el-tag>
</template>
</el-table-column>
<el-table-column <el-table-column
label="状态" label="状态"
width="180" width="180"
prop="status"> prop="status">
<template slot-scope="scope"> <template slot-scope="scope">
<el-tag type="primary" disable-transitions> <el-tag type="primary" disable-transitions>
{{ scope.row.status === '0' ? '正常' : '禁用' }} {{ scope.row.status === 0 ? '正常' : '禁用' }}
</el-tag> </el-tag>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="操作"> <el-table-column label="操作">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)" plain>编辑</el-button> <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" plain>编辑</el-button>
<el-button v-if="scope.row.status == 1" size="mini" type="primary" <el-button v-if="scope.row.status === 1" size="mini" type="primary"
@click="toggleStatus(scope.$index, scope.row)" plain>上架 @click="toggleStatus(scope.$index, scope.row)" plain>上架
</el-button> </el-button>
<el-button v-else size="mini" type="danger" @click="toggleStatus(scope.$index, scope.row)" plain>下架 <el-button v-else size="mini" type="danger" @click="toggleStatus(scope.$index, scope.row)" plain>下架
@ -77,39 +90,11 @@
</el-pagination> </el-pagination>
</el-col> </el-col>
</el-row> </el-row>
<!-- <el-dialog :title="`物流公司-${isEdit?'修改':'新增'}项目`" :visible.sync='editVisible'-->
<!-- center width='880px'>-->
<!-- <el-form ref='formStore' :model='formStore' :rules='rules' label-poionsit='right' label-width='110px'>-->
<!-- <div style="margin-bottom:20px">-->
<!-- <img @click="cropperVisible=true" :src="formStore.productImgUrl"-->
<!-- style="width: 120px;height: 120px;margin: 0 auto;display: block">-->
<!-- </div>-->
<!-- <el-form-item label='产品名称' prop='productTitle'>-->
<!-- <el-input v-model='formStore.productTitle' placeholder='请输入产品名称'/>-->
<!-- </el-form-item>-->
<!-- <el-form-item label='产品价格' prop='code'>-->
<!-- <el-input v-model='formStore.productPrice' placeholder='请输入产品价格'/>-->
<!-- </el-form-item>-->
<!-- <el-form-item label='产品描述' prop='type'>-->
<!-- <el-input v-model='formStore.productDescription' placeholder='请输入产品描述'/>-->
<!-- </el-form-item>-->
<!-- <el-form-item label='tags' prop='order'>-->
<!-- <el-input v-model='formStore.tags' placeholder='请输入tags'/>-->
<!-- </el-form-item>-->
<!-- </el-form>-->
<!-- <span slot='footer' class='dialog-footer'>-->
<!-- <el-button @click='cropperVisible = false'> </el-button>-->
<!-- <el-button type='primary' @click='handelStore'> </el-button>-->
<!-- </span>-->
<!-- </el-dialog>-->
</div> </div>
</template> </template>
<script> <script>
import {mapState} from 'vuex'; import {mapState} from 'vuex';
// import VueCropper from "vue-cropper";
export default { export default {
name: "productsList", name: "productsList",
@ -143,7 +128,7 @@ export default {
toggleStatus(index, product) { toggleStatus(index, product) {
let _ts = this; let _ts = this;
let title, status; let title, status;
if (product.status == 0) { if (product.status === 0) {
title = '下架'; title = '下架';
status = 1; status = 1;
} else { } else {
@ -191,34 +176,13 @@ export default {
}) })
}, },
handleEdit(index, row) { handleEdit(index, row) {
// this.formStore = {...row}
// this.editVisible = true
this.$router.push({ this.$router.push({
path: `/admin/product/post/${row.idProduct}`, path: `/admin/product/post/${row.idProduct}`,
}) })
}, },
handelStore() {
let _ts = this;
this.formStore.idProduct = this.formStore.idProduct ? this.formStore.idProduct : 0
_ts.$axios[this.formStore.idProduct ? '$put' : '$post']('/api/product/post', this.formStore).then(function (res) {
if (res) {
console.log('res')
// _ts.$router.push({
// path: `/product/${res}`
// })
}
})
},
updateUser(data) {
let _ts = this;
if (data) {
this.formStore.productImgUrl = data
this.formStore.productImgType = 1
} else _ts.$message.error('失败,请重试');
},
handleAdd() { handleAdd() {
this.$router.push({ this.$router.push({
path: `/admin/product/post/`, path: `/admin/product/post`,
}) })
} }
} }