index.vue 14 KB

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