index.vue 12 KB

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