index.vue 12 KB

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