index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <template>
  2. <div class="p-2">
  3. <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
  4. <div v-show="showSearch" class="mb-[10px]">
  5. <el-card shadow="hover">
  6. <el-form ref="queryFormRef" :model="queryParams" :inline="true">
  7. <el-form-item label="属性名称" prop="productAttributesName">
  8. <el-input v-model="queryParams.productAttributesName" placeholder="请输入属性名称" clearable @keyup.enter="handleQuery" />
  9. </el-form-item>
  10. <el-form-item label="商品类别" prop="categoryId">
  11. <el-tree-select
  12. v-model="queryParams.categoryId"
  13. :data="categoryOptions"
  14. :props="{ value: 'id', label: 'label', children: 'children' }"
  15. check-strictly
  16. :render-after-expand="false"
  17. show-checkbox
  18. :check-on-click-node="true"
  19. node-key="id"
  20. clearable
  21. placeholder="请选择商品类别"
  22. >
  23. <template #default="{ data }">
  24. <span>{{ getCategoryFullPath(data.id) }}</span>
  25. </template>
  26. </el-tree-select>
  27. </el-form-item>
  28. <el-form-item>
  29. <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
  30. <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  31. </el-form-item>
  32. </el-form>
  33. </el-card>
  34. </div>
  35. </transition>
  36. <el-card shadow="never">
  37. <template #header>
  38. <el-row :gutter="10" class="mb8">
  39. <el-col :span="1.5">
  40. <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['product:attributes:add']">新增</el-button>
  41. </el-col>
  42. <el-col :span="1.5">
  43. <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['product:attributes:edit']">修改</el-button>
  44. </el-col>
  45. <el-col :span="1.5">
  46. <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['product:attributes:remove']">删除</el-button>
  47. </el-col>
  48. <!-- <el-col :span="1.5">
  49. <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['product:attributes:export']">导出</el-button>
  50. </el-col> -->
  51. <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
  52. </el-row>
  53. </template>
  54. <el-table v-loading="loading" border :data="attributesList" @selection-change="handleSelectionChange">
  55. <el-table-column type="selection" width="55" align="center" />
  56. <el-table-column label="编号" align="center" prop="id" width="120" />
  57. <el-table-column label="属性名称" align="center" prop="productAttributesName" />
  58. <el-table-column label="商品类别" align="center" prop="categoryId" min-width="200">
  59. <template #default="scope">
  60. <span>{{ getCategoryFullPath(scope.row.categoryId) }}</span>
  61. </template>
  62. </el-table-column>
  63. <el-table-column label="属性是否可选" align="center" prop="isOptional" width="120">
  64. <template #default="scope">
  65. <dict-tag :options="[
  66. { label: '是', value: '1' },
  67. { label: '否', value: '0' }
  68. ]" :value="String(scope.row.isOptional)" />
  69. </template>
  70. </el-table-column>
  71. <el-table-column label="属性值的输入方式" align="center" prop="entryMethod" width="140">
  72. <template #default="scope">
  73. <dict-tag :options="[
  74. { label: '唯一属性', value: '1' },
  75. { label: '单选属性', value: '0' },
  76. { label: '复选属性', value: '2' }
  77. ]" :value="String(scope.row.entryMethod)" />
  78. </template>
  79. </el-table-column>
  80. <el-table-column label="可选值列表" align="center" prop="attributesList" show-overflow-tooltip />
  81. <el-table-column label="是否必填" align="center" prop="required" width="100">
  82. <template #default="scope">
  83. <dict-tag :options="[
  84. { label: '是', value: '1' },
  85. { label: '否', value: '0' }
  86. ]" :value="String(scope.row.required)" />
  87. </template>
  88. </el-table-column>
  89. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
  90. <template #default="scope">
  91. <el-tooltip content="编辑" placement="top">
  92. <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['product:attributes:edit']"></el-button>
  93. </el-tooltip>
  94. <el-tooltip content="删除" placement="top">
  95. <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['product:attributes:remove']"></el-button>
  96. </el-tooltip>
  97. </template>
  98. </el-table-column>
  99. </el-table>
  100. <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  101. </el-card>
  102. <!-- 添加或修改产品属性定义(用于动态属性配置)对话框 -->
  103. <el-dialog :title="dialog.title" v-model="dialog.visible" width="650px" append-to-body>
  104. <el-form ref="attributesFormRef" :model="form" :rules="rules" label-width="100px">
  105. <el-form-item label="属性名称" prop="productAttributesName">
  106. <el-input v-model="form.productAttributesName" placeholder="请输入属性名称" />
  107. </el-form-item>
  108. <el-form-item label="关联类别" prop="categoryId">
  109. <el-tree-select
  110. v-model="form.categoryId"
  111. :data="categoryOptions"
  112. :props="{ value: 'id', label: 'label', children: 'children' }"
  113. check-strictly
  114. :render-after-expand="false"
  115. clearable
  116. placeholder="请选择关联类别(必须选择第三级)"
  117. >
  118. <template #default="{ node, data }">
  119. <span :style="{ color: !node.isLeaf && node.level < 3 ? '#999' : '' }">
  120. {{ getCategoryFullPath(data.id) }}
  121. </span>
  122. </template>
  123. </el-tree-select>
  124. </el-form-item>
  125. <el-form-item label="是否可选" prop="isOptional">
  126. <el-radio-group v-model="form.isOptional">
  127. <el-radio :value="1">唯一属性</el-radio>
  128. <el-radio :value="0">单选属性</el-radio>
  129. <el-radio :value="2">复选属性</el-radio>
  130. </el-radio-group>
  131. <div class="text-gray-500 text-sm mt-2">
  132. 选择"单选/复选属性"时,可以对商品属性设置多个值,同时还能对不同属性值设定不同的价格,用户购买商品时需要选择具体的属性值后才能加入购物车。用户购买商品时需要先选择具体的属性值。选择"唯一属性"时,商品的该属性值只能是唯一一个值,用户只能查看具体的属性值。
  133. </div>
  134. </el-form-item>
  135. <el-form-item label="录入方式" prop="entryMethod">
  136. <el-radio-group v-model="form.entryMethod">
  137. <el-radio value="1">手工录入</el-radio>
  138. <el-radio value="2">从下面的列表中选择</el-radio>
  139. </el-radio-group>
  140. </el-form-item>
  141. <el-form-item label="加入筛选" prop="isFilter">
  142. <el-radio-group v-model="form.isFilter">
  143. <el-radio :value="1">是</el-radio>
  144. <el-radio :value="0">否</el-radio>
  145. </el-radio-group>
  146. </el-form-item>
  147. <el-form-item label="是否必填" prop="required">
  148. <el-radio-group v-model="form.required">
  149. <el-radio :value="1">是</el-radio>
  150. <el-radio :value="0">否</el-radio>
  151. </el-radio-group>
  152. </el-form-item>
  153. <el-form-item label="可选值列表" prop="attributesList" v-if="form.entryMethod === 'select'">
  154. <el-input
  155. v-model="form.attributesList"
  156. type="textarea"
  157. :rows="4"
  158. placeholder="请输入属性值,各属性值请用逗号隔开并来示意。例如:A4,70g,100g"
  159. />
  160. </el-form-item>
  161. </el-form>
  162. <template #footer>
  163. <div class="dialog-footer">
  164. <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
  165. <el-button @click="cancel">取 消</el-button>
  166. </div>
  167. </template>
  168. </el-dialog>
  169. </div>
  170. </template>
  171. <script setup name="Attributes" lang="ts">
  172. import { listAttributes, getAttributes, delAttributes, addAttributes, updateAttributes } from '@/api/product/attributes';
  173. import { AttributesVO, AttributesQuery, AttributesForm } from '@/api/product/attributes/types';
  174. import { categoryTree } from '@/api/product/base/index';
  175. import { categoryTreeVO } from '@/api/product/category/types';
  176. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  177. const router = useRouter();
  178. const attributesList = ref<AttributesVO[]>([]);
  179. const buttonLoading = ref(false);
  180. const loading = ref(true);
  181. const showSearch = ref(true);
  182. const ids = ref<Array<string | number>>([]);
  183. const single = ref(true);
  184. const multiple = ref(true);
  185. const total = ref(0);
  186. const categoryOptions = ref<categoryTreeVO[]>([]);
  187. const queryFormRef = ref<ElFormInstance>();
  188. const attributesFormRef = ref<ElFormInstance>();
  189. const dialog = reactive<DialogOption>({
  190. visible: false,
  191. title: ''
  192. });
  193. const initFormData: AttributesForm = {
  194. id: undefined,
  195. categoryId: undefined,
  196. productAttributesCode: undefined,
  197. productAttributesName: undefined,
  198. isOptional: 1,
  199. entryMethod: '1',
  200. isFilter: 1,
  201. attributesList: undefined,
  202. required: 0,
  203. remark: undefined,
  204. }
  205. const data = reactive<PageData<AttributesForm, AttributesQuery>>({
  206. form: {...initFormData},
  207. queryParams: {
  208. pageNum: 1,
  209. pageSize: 10,
  210. categoryId: undefined,
  211. productAttributesCode: undefined,
  212. productAttributesName: undefined,
  213. isOptional: undefined,
  214. entryMethod: undefined,
  215. isFilter: undefined,
  216. attributesList: undefined,
  217. required: undefined,
  218. platformCode: undefined,
  219. params: {
  220. }
  221. },
  222. rules: {
  223. categoryId: [
  224. { required: true, message: "商品类别不能为空", trigger: "change" },
  225. {
  226. validator: (rule: any, value: any, callback: any) => {
  227. if (!value) {
  228. callback();
  229. return;
  230. }
  231. // 检查是否为第三级类别
  232. const isThirdLevel = checkIsThirdLevel(value);
  233. if (!isThirdLevel) {
  234. callback(new Error('请选择第三级类别'));
  235. } else {
  236. callback();
  237. }
  238. },
  239. trigger: "change"
  240. }
  241. ],
  242. productAttributesCode: [
  243. { required: true, message: "属性编码不能为空", trigger: "blur" }
  244. ],
  245. productAttributesName: [
  246. { required: true, message: "属性名称不能为空", trigger: "blur" }
  247. ]
  248. }
  249. });
  250. const { queryParams, form, rules } = toRefs(data);
  251. /** 查询产品属性定义(用于动态属性配置)列表 */
  252. const getList = async () => {
  253. loading.value = true;
  254. const res = await listAttributes(queryParams.value);
  255. attributesList.value = res.rows;
  256. total.value = res.total;
  257. loading.value = false;
  258. }
  259. /** 查询分类树 */
  260. const getCategoryTreeFunc = async () => {
  261. const res = await categoryTree();
  262. categoryOptions.value = res.data || [];
  263. // 设置默认选中第一项
  264. if (categoryOptions.value.length > 0) {
  265. setDefaultCategory();
  266. }
  267. }
  268. /** 设置默认类别(选择第一个一级分类) */
  269. const setDefaultCategory = () => {
  270. if (categoryOptions.value.length > 0) {
  271. const firstCategory = categoryOptions.value[0];
  272. if (!form.value.id && !form.value.categoryId) {
  273. form.value.categoryId = firstCategory.id;
  274. }
  275. }
  276. }
  277. /** 检查是否为第三级类别 */
  278. const checkIsThirdLevel = (categoryId: string | number): boolean => {
  279. const findLevel = (nodes: categoryTreeVO[], targetId: string | number, level: number = 1): number | null => {
  280. for (const node of nodes) {
  281. if (node.id === targetId) {
  282. return level;
  283. }
  284. if (node.children && node.children.length > 0) {
  285. const result = findLevel(node.children, targetId, level + 1);
  286. if (result !== null) return result;
  287. }
  288. }
  289. return null;
  290. };
  291. const level = findLevel(categoryOptions.value, categoryId);
  292. return level === 3;
  293. };
  294. /** 获取分类完整路径 */
  295. const getCategoryFullPath = (categoryId: string | number): string => {
  296. const findPath = (nodes: categoryTreeVO[], targetId: string | number, path: string[] = []): string[] | null => {
  297. for (const node of nodes) {
  298. const currentPath = [...path, node.label];
  299. if (node.id === targetId) {
  300. return currentPath;
  301. }
  302. if (node.children && node.children.length > 0) {
  303. const result = findPath(node.children, targetId, currentPath);
  304. if (result) return result;
  305. }
  306. }
  307. return null;
  308. };
  309. const pathArray = findPath(categoryOptions.value, categoryId);
  310. return pathArray ? pathArray.join(' / ') : '';
  311. }
  312. /** 取消按钮 */
  313. const cancel = () => {
  314. reset();
  315. dialog.visible = false;
  316. }
  317. /** 表单重置 */
  318. const reset = () => {
  319. form.value = {...initFormData};
  320. attributesFormRef.value?.resetFields();
  321. }
  322. /** 搜索按钮操作 */
  323. const handleQuery = () => {
  324. queryParams.value.pageNum = 1;
  325. getList();
  326. }
  327. /** 重置按钮操作 */
  328. const resetQuery = () => {
  329. queryFormRef.value?.resetFields();
  330. handleQuery();
  331. }
  332. /** 多选框选中数据 */
  333. const handleSelectionChange = (selection: AttributesVO[]) => {
  334. ids.value = selection.map(item => item.id);
  335. single.value = selection.length != 1;
  336. multiple.value = !selection.length;
  337. }
  338. /** 新增按钮操作 */
  339. const handleAdd = () => {
  340. router.push({
  341. path: '/product/attributes/edit',
  342. query: {
  343. type: 'add'
  344. }
  345. });
  346. }
  347. /** 修改按钮操作 */
  348. const handleUpdate = async (row?: AttributesVO) => {
  349. const _id = row?.id || ids.value[0];
  350. router.push({
  351. path: '/product/attributes/edit',
  352. query: {
  353. id: _id,
  354. type: 'edit'
  355. }
  356. });
  357. }
  358. /** 提交按钮 */
  359. const submitForm = () => {
  360. attributesFormRef.value?.validate(async (valid: boolean) => {
  361. if (valid) {
  362. buttonLoading.value = true;
  363. if (form.value.id) {
  364. await updateAttributes(form.value).finally(() => buttonLoading.value = false);
  365. } else {
  366. await addAttributes(form.value).finally(() => buttonLoading.value = false);
  367. }
  368. proxy?.$modal.msgSuccess("操作成功");
  369. dialog.visible = false;
  370. await getList();
  371. }
  372. });
  373. }
  374. /** 删除按钮操作 */
  375. const handleDelete = async (row?: AttributesVO) => {
  376. const _ids = row?.id || ids.value;
  377. await proxy?.$modal.confirm('是否确认删除产品属性定义(用于动态属性配置)编号为"' + _ids + '"的数据项?').finally(() => loading.value = false);
  378. await delAttributes(_ids);
  379. proxy?.$modal.msgSuccess("删除成功");
  380. await getList();
  381. }
  382. /** 导出按钮操作 */
  383. const handleExport = () => {
  384. proxy?.download('product/attributes/export', {
  385. ...queryParams.value
  386. }, `attributes_${new Date().getTime()}.xlsx`)
  387. }
  388. onMounted(() => {
  389. getList();
  390. getCategoryTreeFunc();
  391. });
  392. </script>