index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. <el-button
  174. v-hasPermi="['product:spu:update']"
  175. link
  176. type="primary"
  177. @click="openDetail(row.id)"
  178. >
  179. 详情
  180. </el-button>
  181. <template v-if="queryParams.tabType === 4">
  182. <el-button
  183. v-hasPermi="['product:spu:delete']"
  184. link
  185. type="danger"
  186. @click="handleDelete(row.id)"
  187. >
  188. 删除
  189. </el-button>
  190. <el-button
  191. v-hasPermi="['product:spu:update']"
  192. link
  193. type="primary"
  194. @click="changeStatus(row, ProductSpuStatusEnum.DISABLE.status)"
  195. >
  196. 恢复到仓库
  197. </el-button>
  198. </template>
  199. <template v-else>
  200. <!-- 只有不是上架和回收站的商品可以编辑 -->
  201. <el-button
  202. v-if="queryParams.tabType !== 0"
  203. v-hasPermi="['product:spu:update']"
  204. link
  205. type="primary"
  206. @click="openForm(row.id)"
  207. >
  208. 修改
  209. </el-button>
  210. <el-button
  211. v-hasPermi="['product:spu:update']"
  212. link
  213. type="primary"
  214. @click="changeStatus(row, ProductSpuStatusEnum.RECYCLE.status)"
  215. >
  216. 加入回收站
  217. </el-button>
  218. </template>
  219. </template>
  220. </el-table-column>
  221. </el-table>
  222. <!-- 分页 -->
  223. <Pagination
  224. v-model:limit="queryParams.pageSize"
  225. v-model:page="queryParams.pageNo"
  226. :total="total"
  227. @pagination="getList"
  228. />
  229. </ContentWrap>
  230. </template>
  231. <script lang="ts" name="ProductSpu" setup>
  232. import { TabsPaneContext } from 'element-plus'
  233. import { cloneDeep } from 'lodash-es'
  234. import { createImageViewer } from '@/components/ImageViewer'
  235. import { dateFormatter } from '@/utils/formatTime'
  236. import { checkSelectedNode, defaultProps, handleTree, treeToString } from '@/utils/tree'
  237. import { ProductSpuStatusEnum } from '@/utils/constants'
  238. import { formatToFraction } from '@/utils'
  239. import download from '@/utils/download'
  240. import * as ProductSpuApi from '@/api/mall/product/spu'
  241. import * as ProductCategoryApi from '@/api/mall/product/category'
  242. const message = useMessage() // 消息弹窗
  243. const { t } = useI18n() // 国际化
  244. const { currentRoute, push } = useRouter() // 路由跳转
  245. const loading = ref(false) // 列表的加载中
  246. const exportLoading = ref(false) // 导出的加载中
  247. const total = ref(0) // 列表的总页数
  248. const list = ref<any[]>([]) // 列表的数据
  249. // tabs 数据
  250. const tabsData = ref([
  251. {
  252. count: 0,
  253. name: '出售中商品',
  254. type: 0
  255. },
  256. {
  257. count: 0,
  258. name: '仓库中商品',
  259. type: 1
  260. },
  261. {
  262. count: 0,
  263. name: '已经售空商品',
  264. type: 2
  265. },
  266. {
  267. count: 0,
  268. name: '警戒库存',
  269. type: 3
  270. },
  271. {
  272. count: 0,
  273. name: '商品回收站',
  274. type: 4
  275. }
  276. ])
  277. /** 获得每个 Tab 的数量 */
  278. const getTabsCount = async () => {
  279. const res = await ProductSpuApi.getTabsCount()
  280. for (let objName in res) {
  281. tabsData.value[Number(objName)].count = res[objName]
  282. }
  283. }
  284. const queryParams = ref({
  285. pageNo: 1,
  286. pageSize: 10,
  287. tabType: 0,
  288. name: '',
  289. categoryId: null
  290. }) // 查询参数
  291. const queryFormRef = ref() // 搜索的表单Ref
  292. const handleTabClick = (tab: TabsPaneContext) => {
  293. queryParams.value.tabType = tab.paneName as number
  294. getList()
  295. }
  296. /** 查询列表 */
  297. const getList = async () => {
  298. loading.value = true
  299. try {
  300. const data = await ProductSpuApi.getSpuPage(queryParams.value)
  301. list.value = data.list
  302. total.value = data.total
  303. } finally {
  304. loading.value = false
  305. }
  306. }
  307. /**
  308. * 更改 SPU 状态
  309. *
  310. * @param row
  311. * @param status 更改前的值
  312. */
  313. const changeStatus = async (row, status?: number) => {
  314. const deepCopyValue = cloneDeep(unref(row))
  315. if (typeof status !== 'undefined') deepCopyValue.status = status
  316. try {
  317. let text = ''
  318. switch (deepCopyValue.status) {
  319. case ProductSpuStatusEnum.DISABLE.status:
  320. text = ProductSpuStatusEnum.DISABLE.name
  321. break
  322. case ProductSpuStatusEnum.ENABLE.status:
  323. text = ProductSpuStatusEnum.ENABLE.name
  324. break
  325. case ProductSpuStatusEnum.RECYCLE.status:
  326. text = `加入${ProductSpuStatusEnum.RECYCLE.name}`
  327. break
  328. }
  329. await message.confirm(
  330. deepCopyValue.status === -1
  331. ? `确认要将[${row.name}]${text}吗?`
  332. : row.status === -1 // 再判断一次原对象是否等于-1,例: 把回收站中的商品恢复到仓库中,事件触发时原对象status为-1 深拷贝对象status被赋值为0
  333. ? `确认要将[${row.name}]恢复到仓库吗?`
  334. : `确认要${text}[${row.name}]吗?`
  335. )
  336. await ProductSpuApi.updateStatus({ id: deepCopyValue.id, status: deepCopyValue.status })
  337. message.success('更新状态成功')
  338. // 刷新 tabs 数据
  339. await getTabsCount()
  340. // 刷新列表
  341. await getList()
  342. } catch {
  343. // 取消更改状态时回显数据
  344. row.status =
  345. row.status === ProductSpuStatusEnum.DISABLE.status
  346. ? ProductSpuStatusEnum.ENABLE.status
  347. : ProductSpuStatusEnum.DISABLE.status
  348. }
  349. }
  350. /** 删除按钮操作 */
  351. const handleDelete = async (id: number) => {
  352. try {
  353. // 删除的二次确认
  354. await message.delConfirm()
  355. // 发起删除
  356. await ProductSpuApi.deleteSpu(id)
  357. message.success(t('common.delSuccess'))
  358. // 刷新tabs数据
  359. await getTabsCount()
  360. // 刷新列表
  361. await getList()
  362. } catch {}
  363. }
  364. /** 商品图预览 */
  365. const imagePreview = (imgUrl: string) => {
  366. createImageViewer({
  367. urlList: [imgUrl]
  368. })
  369. }
  370. /** 搜索按钮操作 */
  371. const handleQuery = () => {
  372. getList()
  373. }
  374. /** 重置按钮操作 */
  375. const resetQuery = () => {
  376. queryFormRef.value.resetFields()
  377. handleQuery()
  378. }
  379. /**
  380. * 新增或修改
  381. *
  382. * @param id 商品 SPU 编号
  383. */
  384. const openForm = (id?: number) => {
  385. // 修改
  386. if (typeof id === 'number') {
  387. push('/product/spu/edit/' + id)
  388. return
  389. }
  390. // 新增
  391. push('/product/spu/add')
  392. }
  393. /**
  394. * 查看商品详情
  395. */
  396. const openDetail = (id?: number) => {
  397. push('/product/spu/detail/' + id)
  398. }
  399. /** 导出按钮操作 */
  400. const handleExport = async () => {
  401. try {
  402. // 导出的二次确认
  403. await message.exportConfirm()
  404. // 发起导出
  405. exportLoading.value = true
  406. const data = await ProductSpuApi.exportSpu(queryParams)
  407. download.excel(data, '商品列表.xls')
  408. } catch {
  409. } finally {
  410. exportLoading.value = false
  411. }
  412. }
  413. // 监听路由变化更新列表,解决商品保存后,列表不刷新的问题。
  414. watch(
  415. () => currentRoute.value,
  416. () => {
  417. getList()
  418. }
  419. )
  420. const categoryList = ref() // 分类树
  421. /**
  422. * 获取分类的节点的完整结构
  423. * @param categoryId 分类id
  424. */
  425. const categoryString = (categoryId) => {
  426. return treeToString(categoryList.value, categoryId)
  427. }
  428. /**
  429. * 校验所选是否为二级及以下节点
  430. */
  431. const nodeClick = () => {
  432. if (!checkSelectedNode(categoryList.value, queryParams.value.categoryId)) {
  433. queryParams.value.categoryId = null
  434. message.warning('必须选择二级及以下节点!!')
  435. }
  436. }
  437. /** 初始化 **/
  438. onMounted(async () => {
  439. await getTabsCount()
  440. await getList()
  441. // 获得分类树
  442. const data = await ProductCategoryApi.getCategoryList({})
  443. categoryList.value = handleTree(data, 'id', 'parentId')
  444. })
  445. </script>
  446. <style lang="scss" scoped>
  447. .demo-table-expand {
  448. padding-left: 42px;
  449. :deep(.el-form-item__label) {
  450. width: 82px;
  451. font-weight: bold;
  452. color: #99a9bf;
  453. }
  454. }
  455. </style>