index.vue 13 KB

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