index.vue 13 KB

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