index.vue 13 KB

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