index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <!-- 搜索工作栏 -->
  3. <ContentWrap>
  4. <el-form
  5. ref="queryFormRef"
  6. :inline="true"
  7. :model="queryParams"
  8. class="-mb-15px"
  9. label-width="68px"
  10. >
  11. <!-- TODO @puhui999:https://admin.java.crmeb.net/store/index,参考,使用分类 + 标题搜索 fix-->
  12. <el-form-item label="品牌名称" prop="name">
  13. <el-input
  14. v-model="queryParams.name"
  15. class="!w-240px"
  16. clearable
  17. placeholder="请输入品牌名称"
  18. @keyup.enter="handleQuery"
  19. />
  20. </el-form-item>
  21. <!-- TODO 分类只能选择二级分类目前还没做,还是先以联调通顺为主 -->
  22. <el-form-item label="商品分类" prop="categoryId">
  23. <el-tree-select
  24. v-model="queryParams.categoryId"
  25. :data="categoryList"
  26. :props="defaultProps"
  27. check-strictly
  28. class="w-1/1"
  29. node-key="id"
  30. placeholder="请选择商品分类"
  31. />
  32. </el-form-item>
  33. <el-form-item label="创建时间" prop="createTime">
  34. <el-date-picker
  35. v-model="queryParams.createTime"
  36. :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
  37. class="!w-240px"
  38. end-placeholder="结束日期"
  39. start-placeholder="开始日期"
  40. type="daterange"
  41. value-format="YYYY-MM-DD HH:mm:ss"
  42. />
  43. </el-form-item>
  44. <el-form-item>
  45. <el-button @click="handleQuery">
  46. <Icon class="mr-5px" icon="ep:search" />
  47. 搜索
  48. </el-button>
  49. <el-button @click="resetQuery">
  50. <Icon class="mr-5px" icon="ep:refresh" />
  51. 重置
  52. </el-button>
  53. <el-button v-hasPermi="['product:spu:create']" plain type="primary" @click="openForm">
  54. <Icon class="mr-5px" icon="ep:plus" />
  55. 新增
  56. </el-button>
  57. <!-- TODO @puhui999:增加一个【导出】操作 -->
  58. <el-button
  59. v-hasPermi="['product:spu:export']"
  60. :loading="exportLoading"
  61. plain
  62. type="success"
  63. @click="handleExport"
  64. >
  65. <Icon class="mr-5px" icon="ep:download" />
  66. 导出
  67. </el-button>
  68. </el-form-item>
  69. </el-form>
  70. </ContentWrap>
  71. <!-- 列表 -->
  72. <ContentWrap>
  73. <el-tabs v-model="queryParams.tabType" @tab-click="handleTabClick">
  74. <el-tab-pane
  75. v-for="item in tabsData"
  76. :key="item.type"
  77. :label="item.name + '(' + item.count + ')'"
  78. :name="item.type"
  79. />
  80. </el-tabs>
  81. <el-table v-loading="loading" :data="list">
  82. <!-- TODO 折叠数据按需增加暂定以下三个 -->
  83. <el-table-column type="expand" width="30">
  84. <template #default="{ row }">
  85. <el-form class="demo-table-expand" inline label-position="left">
  86. <el-form-item label="市场价:">
  87. <span>{{ formatToFraction(row.marketPrice) }}</span>
  88. </el-form-item>
  89. <el-form-item label="成本价:">
  90. <span>{{ formatToFraction(row.costPrice) }}</span>
  91. </el-form-item>
  92. <el-form-item label="虚拟销量:">
  93. <span>{{ row.virtualSalesCount }}</span>
  94. </el-form-item>
  95. </el-form>
  96. </template>
  97. </el-table-column>
  98. <!-- TODO puhui999: ID 编号的展示 fix-->
  99. <el-table-column key="id" align="center" label="商品编号" prop="id" />
  100. <el-table-column label="商品图" min-width="80">
  101. <template #default="{ row }">
  102. <el-image
  103. :src="row.picUrl"
  104. style="width: 30px; height: 30px"
  105. @click="imagePreview(row.picUrl)"
  106. />
  107. </template>
  108. </el-table-column>
  109. <el-table-column :show-overflow-tooltip="true" label="商品名称" min-width="300" prop="name" />
  110. <!-- TODO 价格 / 100.0 -->
  111. <el-table-column align="center" label="商品售价" min-width="90" prop="price">
  112. <template #default="{ row }">
  113. {{ formatToFraction(row.price) }}
  114. </template>
  115. </el-table-column>
  116. <el-table-column align="center" label="销量" min-width="90" prop="salesCount" />
  117. <el-table-column align="center" label="库存" min-width="90" prop="stock" />
  118. <el-table-column align="center" label="排序" min-width="70" prop="sort" />
  119. <el-table-column
  120. :formatter="dateFormatter"
  121. align="center"
  122. label="创建时间"
  123. prop="createTime"
  124. width="180"
  125. />
  126. <el-table-column align="center" label="状态" min-width="80">
  127. <template #default="{ row }">
  128. <!-- fix: 修复打开回收站时商品误触改变商品状态的事件,因为el-switch只用0和1没有-1所以当商品状态为-1时状态使用el-tag显示 -->
  129. <template v-if="row.status >= 0">
  130. <el-switch
  131. v-model="row.status"
  132. :active-value="1"
  133. :inactive-value="0"
  134. active-text="上架"
  135. inactive-text="下架"
  136. inline-prompt
  137. @change="changeStatus(row)"
  138. />
  139. </template>
  140. <template v-else>
  141. <el-tag type="info">回收站</el-tag>
  142. </template>
  143. </template>
  144. </el-table-column>
  145. <el-table-column align="center" fixed="right" label="操作" min-width="200">
  146. <template #default="{ row }">
  147. <!-- TODO @puhui999:【详情】,可以后面点做哈 -->
  148. <el-button v-hasPermi="['product:spu:update']" link type="primary" @click="openDetail">
  149. 详情
  150. </el-button>
  151. <template v-if="queryParams.tabType === 4">
  152. <el-button
  153. v-hasPermi="['product:spu:delete']"
  154. link
  155. type="danger"
  156. @click="handleDelete(row.id)"
  157. >
  158. 删除
  159. </el-button>
  160. <el-button
  161. v-hasPermi="['product:spu:update']"
  162. link
  163. type="primary"
  164. @click="changeStatus(row, ProductSpuStatusEnum.DISABLE.status)"
  165. >
  166. 恢复到仓库
  167. </el-button>
  168. </template>
  169. <template v-else>
  170. <!-- 只有不是上架和回收站的商品可以编辑 -->
  171. <el-button
  172. v-if="queryParams.tabType !== 0"
  173. v-hasPermi="['product:spu:update']"
  174. link
  175. type="primary"
  176. @click="openForm(row.id)"
  177. >
  178. 修改
  179. </el-button>
  180. <el-button
  181. v-hasPermi="['product:spu:update']"
  182. link
  183. type="primary"
  184. @click="changeStatus(row, ProductSpuStatusEnum.RECYCLE.status)"
  185. >
  186. 加入回收站
  187. </el-button>
  188. </template>
  189. </template>
  190. </el-table-column>
  191. </el-table>
  192. <!-- 分页 -->
  193. <Pagination
  194. v-model:limit="queryParams.pageSize"
  195. v-model:page="queryParams.pageNo"
  196. :total="total"
  197. @pagination="getList"
  198. />
  199. </ContentWrap>
  200. </template>
  201. <script lang="ts" name="ProductSpu" setup>
  202. import { TabsPaneContext } from 'element-plus'
  203. import { cloneDeep } from 'lodash-es'
  204. import { createImageViewer } from '@/components/ImageViewer'
  205. import { dateFormatter } from '@/utils/formatTime'
  206. import { defaultProps, handleTree } from '@/utils/tree'
  207. import { ProductSpuStatusEnum } from '@/utils/constants'
  208. import { formatToFraction } from '@/utils'
  209. import download from '@/utils/download'
  210. import * as ProductSpuApi from '@/api/mall/product/spu'
  211. import * as ProductCategoryApi from '@/api/mall/product/category'
  212. const message = useMessage() // 消息弹窗
  213. const { t } = useI18n() // 国际化
  214. const { currentRoute, push } = useRouter() // 路由跳转
  215. const loading = ref(false) // 列表的加载中
  216. const exportLoading = ref(false) // 导出的加载中
  217. const total = ref(0) // 列表的总页数
  218. const list = ref<any[]>([]) // 列表的数据
  219. // tabs 数据
  220. const tabsData = ref([
  221. {
  222. count: 0,
  223. name: '出售中商品',
  224. type: 0
  225. },
  226. {
  227. count: 0,
  228. name: '仓库中商品',
  229. type: 1
  230. },
  231. {
  232. count: 0,
  233. name: '已经售空商品',
  234. type: 2
  235. },
  236. {
  237. count: 0,
  238. name: '警戒库存',
  239. type: 3
  240. },
  241. {
  242. count: 0,
  243. name: '商品回收站',
  244. type: 4
  245. }
  246. ])
  247. /** 获得每个 Tab 的数量 */
  248. const getTabsCount = async () => {
  249. // TODO @puhui999:这里是不是可以不要 try catch 哈 fix
  250. const res = await ProductSpuApi.getTabsCount()
  251. for (let objName in res) {
  252. tabsData.value[Number(objName)].count = res[objName]
  253. }
  254. }
  255. const queryParams = ref({
  256. pageNo: 1,
  257. pageSize: 10,
  258. tabType: 0
  259. }) // 查询参数
  260. const queryFormRef = ref() // 搜索的表单Ref
  261. // TODO @puhui999:可以改成 handleTabClick:更准确一点;fix
  262. const handleTabClick = (tab: TabsPaneContext) => {
  263. queryParams.value.tabType = tab.paneName
  264. getList()
  265. }
  266. /** 查询列表 */
  267. const getList = async () => {
  268. loading.value = true
  269. try {
  270. const data = await ProductSpuApi.getSpuPage(queryParams.value)
  271. list.value = data.list
  272. total.value = data.total
  273. } finally {
  274. loading.value = false
  275. }
  276. }
  277. // TODO @puhui999:是不是 changeStatus 和 addToTrash 调用一个统一的方法,去更新状态。这样逻辑会更干净一些。fix
  278. /**
  279. * 更改 SPU 状态
  280. *
  281. * @param row
  282. * @param status 更改前的值
  283. */
  284. const changeStatus = async (row, status?: number) => {
  285. // fix: 将加入回收站功能合并到changeStatus并优化
  286. const deepCopyValue = cloneDeep(unref(row))
  287. if (typeof status !== 'undefined') deepCopyValue.status = status
  288. try {
  289. let text = ''
  290. switch (deepCopyValue.status) {
  291. case ProductSpuStatusEnum.DISABLE.status:
  292. text = ProductSpuStatusEnum.DISABLE.name
  293. break
  294. case ProductSpuStatusEnum.ENABLE.status:
  295. text = ProductSpuStatusEnum.ENABLE.name
  296. break
  297. case ProductSpuStatusEnum.RECYCLE.status:
  298. text = `加入${ProductSpuStatusEnum.RECYCLE.name}`
  299. break
  300. }
  301. // fix: 修复恢复到仓库的信息提示
  302. await message.confirm(
  303. deepCopyValue.status === -1
  304. ? `确认要将[${row.name}]${text}吗?`
  305. : row.status === -1 // 再判断一次原对象是否等于-1,例: 把回收站中的商品恢复到仓库中,事件触发时原对象status为-1 深拷贝对象status被赋值为0
  306. ? `确认要将[${row.name}]恢复到仓库吗?`
  307. : `确认要${text}[${row.name}]吗?`
  308. )
  309. await ProductSpuApi.updateStatus({ id: deepCopyValue.id, status: deepCopyValue.status })
  310. message.success('更新状态成功')
  311. // 刷新 tabs 数据
  312. await getTabsCount()
  313. // 刷新列表
  314. await getList()
  315. } catch {
  316. // 取消更改状态时回显数据
  317. row.status =
  318. row.status === ProductSpuStatusEnum.DISABLE.status
  319. ? ProductSpuStatusEnum.ENABLE.status
  320. : ProductSpuStatusEnum.DISABLE.status
  321. }
  322. }
  323. /** 删除按钮操作 */
  324. const handleDelete = async (id: number) => {
  325. try {
  326. // 删除的二次确认
  327. await message.delConfirm()
  328. // 发起删除
  329. await ProductSpuApi.deleteSpu(id)
  330. message.success(t('common.delSuccess'))
  331. // 刷新tabs数据
  332. await getTabsCount()
  333. // 刷新列表
  334. await getList()
  335. } catch {}
  336. }
  337. /**
  338. * 商品图预览
  339. * @param imgUrl
  340. */
  341. const imagePreview = (imgUrl: string) => {
  342. // fix: 改用https://kailong110120130.gitee.io/vue-element-plus-admin-doc/components/image-viewer.html 预览图片
  343. createImageViewer({
  344. urlList: [imgUrl]
  345. })
  346. }
  347. /** 搜索按钮操作 */
  348. const handleQuery = () => {
  349. getList()
  350. }
  351. /** 重置按钮操作 */
  352. const resetQuery = () => {
  353. queryFormRef.value.resetFields()
  354. handleQuery()
  355. }
  356. /**
  357. * 新增或修改
  358. *
  359. * @param id 商品 SPU 编号
  360. */
  361. const openForm = (id?: number) => {
  362. // 修改
  363. if (typeof id === 'number') {
  364. push('/product/productSpuEdit/' + id)
  365. return
  366. }
  367. // 新增
  368. push('/product/productSpuAdd')
  369. }
  370. /**
  371. * 查看商品详情
  372. */
  373. const openDetail = () => {
  374. message.alert('查看详情未完善!!!')
  375. }
  376. /** 导出按钮操作 */
  377. const handleExport = async () => {
  378. try {
  379. // 导出的二次确认
  380. await message.exportConfirm()
  381. // 发起导出
  382. exportLoading.value = true
  383. const data = await ProductSpuApi.exportSpu(queryParams)
  384. download.excel(data, '商品spu.xls')
  385. } catch {
  386. } finally {
  387. exportLoading.value = false
  388. }
  389. }
  390. // 监听路由变化更新列表 TODO @puhui999:这个是必须加的么?fix: 因为编辑表单是以路由的方式打开,保存表单后列表不会刷新
  391. watch(
  392. () => currentRoute.value,
  393. () => {
  394. getList()
  395. }
  396. )
  397. const categoryList = ref() // 分类树
  398. /** 初始化 **/
  399. onMounted(async () => {
  400. await getTabsCount()
  401. await getList()
  402. // 获得分类树
  403. const data = await ProductCategoryApi.getCategoryList({})
  404. categoryList.value = handleTree(data, 'id', 'parentId')
  405. })
  406. </script>
  407. <style lang="scss" scoped>
  408. .demo-table-expand {
  409. padding-left: 42px;
  410. :deep(.el-form-item__label) {
  411. width: 82px;
  412. font-weight: bold;
  413. color: #99a9bf;
  414. }
  415. }
  416. </style>