index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div id="index">
  3. <el-row>
  4. <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
  5. <el-col :span="24" class="one">
  6. <cSearch :is_title="false" :is_search="true" :fields="fields" @search="toSearch"> </cSearch>
  7. </el-col>
  8. <el-col :span="24" class="two">
  9. <cButton @toAdd="toAdd()">
  10. <template v-slot:custom>
  11. <el-button type="primary" @click="toBack">返回</el-button>
  12. </template>
  13. </cButton>
  14. </el-col>
  15. <el-col :span="24" class="thr">
  16. <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @edit="toEdit" @del="toDel"> </cTable>
  17. </el-col>
  18. </el-col>
  19. </el-row>
  20. <cDialog :dialog="dialog" @toClose="toClose">
  21. <template v-slot:info>
  22. <el-col :span="24" class="dialog_one" v-if="dialog.type == '1'">
  23. <cForm :span="24" :fields="formFields" :form="form" :rules="rules" @save="toSave">
  24. <template #file>
  25. <cUpload
  26. :model="`${'file'}`"
  27. :limit="6"
  28. listType="picture"
  29. url="/files/material/specs/upload"
  30. accept="*"
  31. :list="form.file"
  32. @change="onUpload"
  33. ></cUpload>
  34. </template>
  35. <template #is_use>
  36. <el-radio v-for="i in is_useList" :key="i._id" :label="i.value">{{ i.label }}</el-radio>
  37. </template>
  38. </cForm>
  39. </el-col>
  40. </template>
  41. </cDialog>
  42. </div>
  43. </template>
  44. <script setup lang="ts">
  45. // 基础
  46. import _ from 'lodash';
  47. import type { FormRules } from 'element-plus';
  48. import type { Ref } from 'vue';
  49. import { ref, onMounted, getCurrentInstance, reactive } from 'vue';
  50. import { ElMessage } from 'element-plus';
  51. import { useRoute, useRouter } from 'vue-router';
  52. // 接口
  53. import { SpecsStore } from '@/stores/good/spec'; //
  54. import { DictDataStore } from '@/stores/dict/dictData';
  55. import type { IQueryResult } from '@/util/types.util';
  56. const dictData = DictDataStore();
  57. const specAxios = SpecsStore();
  58. // 路由
  59. const route = useRoute();
  60. const router = useRouter();
  61. const { proxy } = getCurrentInstance() as any;
  62. // 加载中
  63. const loading: Ref<any> = ref(false);
  64. // 列表数据
  65. let list: Ref<any> = ref([]);
  66. let total: Ref<number> = ref(0);
  67. let skip = 0;
  68. let limit: number = proxy.$limit;
  69. let fields: Ref<any[]> = ref([
  70. { label: '规格名称', model: 'name', isSearch: true },
  71. { label: '销售价格', model: 'money' },
  72. { label: '库存', model: 'num' },
  73. { label: '是否使用', model: 'is_use', format: (i: any) => getDict(i, 'is_use') }
  74. ]);
  75. // 操作
  76. let opera: Ref<any[]> = ref([
  77. { label: '修改', method: 'edit' },
  78. { label: '删除', method: 'del', confirm: true, type: 'danger' }
  79. ]);
  80. // 弹框
  81. const dialog: Ref<{ type: string; show: boolean; title: string }> = ref({ type: '1', show: false, title: '信息管理' });
  82. let form: Ref<{ file: Array<any>; type: string }> = ref({ file: [], type: '' });
  83. // 表单
  84. let formFields: Ref<any[]> = ref([
  85. { label: '规格名称', model: 'name' },
  86. { label: '销售价格', model: 'money', type: 'number' },
  87. { label: '库存', model: 'num', type: 'number' },
  88. { label: '图片', model: 'file', custom: true },
  89. { label: '是否使用', model: 'is_use', type: 'radio' }
  90. ]);
  91. const rules = reactive<FormRules>({
  92. name: [{ required: true, message: '规格名称', trigger: 'blur' }],
  93. money: [{ required: true, message: '销售价格', trigger: 'blur' }],
  94. num: [{ required: true, message: '库存', trigger: 'blur' }]
  95. });
  96. // 查询数据
  97. let searchForm: Ref<any> = ref({});
  98. // 字典表
  99. let is_useList: Ref<any> = ref([]);
  100. // 请求
  101. onMounted(async () => {
  102. loading.value = true;
  103. await searchOther();
  104. await search({ skip, limit });
  105. loading.value = false;
  106. });
  107. const search = async (e: { skip: number; limit: number }) => {
  108. const { skip, limit } = e;
  109. const condition = _.cloneDeep(searchForm.value);
  110. let goods = route.query.id;
  111. let info = { limit: limit, skip: skip, ...condition, goods: goods };
  112. let res: IQueryResult = await specAxios.query(info);
  113. if (res.errcode == 0) {
  114. list.value = res.data;
  115. total.value = res.total;
  116. }
  117. };
  118. const toSearch = (query: any) => {
  119. searchForm.value = query;
  120. search({ skip, limit });
  121. };
  122. const getDict = (value: any, model: any) => {
  123. if (model == 'is_use') {
  124. if (value) {
  125. let data = is_useList.value.find((i: any) => i.value == value);
  126. if (data) return data.label;
  127. else return '暂无';
  128. }
  129. }
  130. };
  131. // 新增
  132. const toAdd = () => {
  133. let type: any = route.query.type;
  134. form.value = { file: [], type: type };
  135. dialog.value = { title: '信息管理', show: true, type: '1' };
  136. };
  137. // 修改
  138. const toEdit = async (data: any) => {
  139. form.value = data;
  140. dialog.value = { title: '信息管理', show: true, type: '1' };
  141. };
  142. // 上传图片
  143. const onUpload = (e: { model: string; value: Array<[]> }) => {
  144. const { model, value } = e;
  145. form.value[model] = value;
  146. };
  147. // 提交保存
  148. const toSave = async (data: any) => {
  149. let res: IQueryResult;
  150. data.goods = route.query.id;
  151. if (data._id) res = await specAxios.update(data);
  152. else res = await specAxios.create(data);
  153. if (res.errcode == 0) {
  154. ElMessage({ type: `success`, message: `维护信息成功` });
  155. toClose();
  156. }
  157. };
  158. // 删除
  159. const toDel = async (data: any) => {
  160. let res: IQueryResult = await specAxios.del(data._id);
  161. if (res.errcode == 0) {
  162. ElMessage({ type: `success`, message: `刪除信息成功` });
  163. search({ skip, limit });
  164. }
  165. };
  166. // 弹框关闭
  167. const toClose = () => {
  168. form.value = { file: [], type: '' };
  169. dialog.value = { title: '信息管理', show: false, type: '1' };
  170. search({ skip, limit });
  171. };
  172. // 查询其他信息
  173. const searchOther = async () => {
  174. let res: IQueryResult;
  175. // 是否使用
  176. res = await dictData.query({ type: 'is_use' });
  177. if (res.errcode == '0') is_useList.value = res.data;
  178. };
  179. // 返回上一页
  180. const toBack = () => {
  181. router.push({ path: '/goods/index' });
  182. };
  183. </script>
  184. <style scoped lang="scss">
  185. .main {
  186. .two {
  187. margin: 0 0 10px 0;
  188. }
  189. }
  190. </style>