index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <!-- 商品中心 - 商品列表 -->
  2. <template>
  3. <!-- 搜索工作栏 -->
  4. <ContentWrap>
  5. <el-form
  6. ref="queryFormRef"
  7. :inline="true"
  8. :model="queryParams"
  9. class="-mb-15px"
  10. label-width="68px"
  11. >
  12. <el-form-item label="商品名称" prop="name">
  13. <el-input
  14. v-model="queryParams.name"
  15. class="!w-240px"
  16. clearable
  17. placeholder="请输入商品名称"
  18. @keyup.enter="handleQuery"
  19. />
  20. </el-form-item>
  21. <el-form-item label="商品分类" prop="categoryId">
  22. <el-cascader
  23. v-model="queryParams.categoryId"
  24. :options="categoryList"
  25. :props="defaultProps"
  26. class="w-1/1"
  27. clearable
  28. filterable
  29. placeholder="请选择商品分类"
  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
  53. v-hasPermi="['product:spu:create']"
  54. plain
  55. type="primary"
  56. @click="openForm(undefined)"
  57. >
  58. <Icon class="mr-5px" icon="ep:plus" />
  59. 新增
  60. </el-button>
  61. <el-button
  62. v-hasPermi="['product:spu:export']"
  63. :loading="exportLoading"
  64. plain
  65. type="success"
  66. @click="handleExport"
  67. >
  68. <Icon class="mr-5px" icon="ep:download" />
  69. 导出
  70. </el-button>
  71. </el-form-item>
  72. </el-form>
  73. </ContentWrap>
  74. <!-- 列表 -->
  75. <ContentWrap>
  76. <el-tabs v-model="queryParams.tabType" @tab-click="handleTabClick">
  77. <el-tab-pane
  78. v-for="item in tabsData"
  79. :key="item.type"
  80. :label="item.name + '(' + item.count + ')'"
  81. :name="item.type"
  82. />
  83. </el-tabs>
  84. <el-table v-loading="loading" :data="list">
  85. <el-table-column type="expand">
  86. <template #default="{ row }">
  87. <el-form class="spu-table-expand" label-position="left">
  88. <el-row>
  89. <el-col :span="24">
  90. <el-row>
  91. <el-col :span="8">
  92. <el-form-item label="商品分类:">
  93. <span>{{ formatCategoryName(row.categoryId) }}</span>
  94. </el-form-item>
  95. </el-col>
  96. <el-col :span="8">
  97. <el-form-item label="市场价:">
  98. <span>{{ fenToYuan(row.marketPrice) }}</span>
  99. </el-form-item>
  100. </el-col>
  101. <el-col :span="8">
  102. <el-form-item label="成本价:">
  103. <span>{{ fenToYuan(row.costPrice) }}</span>
  104. </el-form-item>
  105. </el-col>
  106. </el-row>
  107. </el-col>
  108. </el-row>
  109. <el-row>
  110. <el-col :span="24">
  111. <el-row>
  112. <el-col :span="8">
  113. <el-form-item label="浏览量:">
  114. <span>{{ row.browseCount }}</span>
  115. </el-form-item>
  116. </el-col>
  117. <el-col :span="8">
  118. <el-form-item label="虚拟销量:">
  119. <span>{{ row.virtualSalesCount }}</span>
  120. </el-form-item>
  121. </el-col>
  122. </el-row>
  123. </el-col>
  124. </el-row>
  125. </el-form>
  126. </template>
  127. </el-table-column>
  128. <el-table-column label="商品编号" min-width="140" prop="id" />
  129. <el-table-column label="商品信息" min-width="300">
  130. <template #default="{ row }">
  131. <div class="flex">
  132. <el-image
  133. fit="cover"
  134. :src="row.picUrl"
  135. class="flex-none w-50px h-50px"
  136. @click="imagePreview(row.picUrl)"
  137. />
  138. <div class="ml-4 overflow-hidden">
  139. <el-tooltip effect="dark" :content="row.name" placement="top">
  140. <div>
  141. {{ row.name }}
  142. </div>
  143. </el-tooltip>
  144. </div>
  145. </div>
  146. </template>
  147. </el-table-column>
  148. <el-table-column align="center" label="价格" min-width="160" prop="price">
  149. <template #default="{ row }"> ¥ {{ fenToYuan(row.price) }}</template>
  150. </el-table-column>
  151. <el-table-column align="center" label="销量" min-width="90" prop="salesCount" />
  152. <el-table-column align="center" label="库存" min-width="90" prop="stock" />
  153. <el-table-column align="center" label="排序" min-width="70" prop="sort" />
  154. <el-table-column align="center" label="销售状态" min-width="80">
  155. <template #default="{ row }">
  156. <template v-if="row.status >= 0">
  157. <el-switch
  158. v-model="row.status"
  159. :active-value="1"
  160. :inactive-value="0"
  161. active-text="上架"
  162. inactive-text="下架"
  163. inline-prompt
  164. @change="handleStatusChange(row)"
  165. />
  166. </template>
  167. <template v-else>
  168. <el-tag type="info">回收站</el-tag>
  169. </template>
  170. </template>
  171. </el-table-column>
  172. <el-table-column
  173. :formatter="dateFormatter"
  174. align="center"
  175. label="创建时间"
  176. prop="createTime"
  177. width="180"
  178. />
  179. <el-table-column align="center" fixed="right" label="操作" min-width="200">
  180. <template #default="{ row }">
  181. <el-button link type="primary" @click="openDetail(row.id)"> 详情 </el-button>
  182. <el-button
  183. v-hasPermi="['product:spu:update']"
  184. link
  185. type="primary"
  186. @click="openForm(row.id)"
  187. >
  188. 修改
  189. </el-button>
  190. <template v-if="queryParams.tabType === 4">
  191. <el-button
  192. v-hasPermi="['product:spu:delete']"
  193. link
  194. type="danger"
  195. @click="handleDelete(row.id)"
  196. >
  197. 删除
  198. </el-button>
  199. <el-button
  200. v-hasPermi="['product:spu:update']"
  201. link
  202. type="primary"
  203. @click="handleStatus02Change(row, ProductSpuStatusEnum.DISABLE.status)"
  204. >
  205. 恢复
  206. </el-button>
  207. </template>
  208. <template v-else>
  209. <el-button
  210. v-hasPermi="['product:spu:update']"
  211. link
  212. type="danger"
  213. @click="handleStatus02Change(row, ProductSpuStatusEnum.RECYCLE.status)"
  214. >
  215. 回收
  216. </el-button>
  217. </template>
  218. </template>
  219. </el-table-column>
  220. </el-table>
  221. <!-- 分页 -->
  222. <Pagination
  223. v-model:limit="queryParams.pageSize"
  224. v-model:page="queryParams.pageNo"
  225. :total="total"
  226. @pagination="getList"
  227. />
  228. </ContentWrap>
  229. </template>
  230. <script lang="ts" setup>
  231. import { TabsPaneContext } from 'element-plus'
  232. import { createImageViewer } from '@/components/ImageViewer'
  233. import { dateFormatter } from '@/utils/formatTime'
  234. import { defaultProps, handleTree, treeToString } from '@/utils/tree'
  235. import { ProductSpuStatusEnum } from '@/utils/constants'
  236. import { fenToYuan } from '@/utils'
  237. import download from '@/utils/download'
  238. import * as ProductSpuApi from '@/api/mall/product/spu'
  239. import * as ProductCategoryApi from '@/api/mall/product/category'
  240. defineOptions({ name: 'ProductSpu' })
  241. const message = useMessage() // 消息弹窗
  242. const { t } = useI18n() // 国际化
  243. const { push } = useRouter() // 路由跳转
  244. const loading = ref(false) // 列表的加载中
  245. const exportLoading = ref(false) // 导出的加载中
  246. const total = ref(0) // 列表的总页数
  247. const list = ref<ProductSpuApi.Spu[]>([]) // 列表的数据
  248. // tabs 数据
  249. const tabsData = ref([
  250. {
  251. name: '出售中',
  252. type: 0,
  253. count: 0
  254. },
  255. {
  256. name: '仓库中',
  257. type: 1,
  258. count: 0
  259. },
  260. {
  261. name: '已售罄',
  262. type: 2,
  263. count: 0
  264. },
  265. {
  266. name: '警戒库存',
  267. type: 3,
  268. count: 0
  269. },
  270. {
  271. name: '回收站',
  272. type: 4,
  273. count: 0
  274. }
  275. ])
  276. const queryParams = ref({
  277. pageNo: 1,
  278. pageSize: 10,
  279. tabType: 0,
  280. name: '',
  281. categoryId: undefined,
  282. createTime: undefined
  283. }) // 查询参数
  284. const queryFormRef = ref() // 搜索的表单Ref
  285. /** 查询列表 */
  286. const getList = async () => {
  287. loading.value = true
  288. try {
  289. const data = await ProductSpuApi.getSpuPage(queryParams.value)
  290. list.value = data.list
  291. total.value = data.total
  292. } finally {
  293. loading.value = false
  294. }
  295. }
  296. /** 切换 Tab */
  297. const handleTabClick = (tab: TabsPaneContext) => {
  298. queryParams.value.tabType = tab.paneName as number
  299. getList()
  300. }
  301. /** 获得每个 Tab 的数量 */
  302. const getTabsCount = async () => {
  303. const res = await ProductSpuApi.getTabsCount()
  304. for (let objName in res) {
  305. tabsData.value[Number(objName)].count = res[objName]
  306. }
  307. }
  308. /** 添加到仓库 / 回收站的状态 */
  309. const handleStatus02Change = async (row: any, newStatus: number) => {
  310. try {
  311. // 二次确认
  312. const text = newStatus === ProductSpuStatusEnum.RECYCLE.status ? '加入到回收站' : '恢复到仓库'
  313. await message.confirm(`确认要"${row.name}"${text}吗?`)
  314. // 发起修改
  315. await ProductSpuApi.updateStatus({ id: row.id, status: newStatus })
  316. message.success(text + '成功')
  317. // 刷新 tabs 数据
  318. await getTabsCount()
  319. // 刷新列表
  320. await getList()
  321. } catch {}
  322. }
  323. /** 更新上架/下架状态 */
  324. const handleStatusChange = async (row: any) => {
  325. try {
  326. // 二次确认
  327. const text = row.status ? '上架' : '下架'
  328. await message.confirm(`确认要${text}"${row.name}"吗?`)
  329. // 发起修改
  330. await ProductSpuApi.updateStatus({ id: row.id, status: row.status })
  331. message.success(text + '成功')
  332. // 刷新 tabs 数据
  333. await getTabsCount()
  334. // 刷新列表
  335. await getList()
  336. } catch {
  337. // 异常时,需要重置回之前的值
  338. row.status =
  339. row.status === ProductSpuStatusEnum.DISABLE.status
  340. ? ProductSpuStatusEnum.ENABLE.status
  341. : ProductSpuStatusEnum.DISABLE.status
  342. }
  343. }
  344. /** 删除按钮操作 */
  345. const handleDelete = async (id: number) => {
  346. try {
  347. // 删除的二次确认
  348. await message.delConfirm()
  349. // 发起删除
  350. await ProductSpuApi.deleteSpu(id)
  351. message.success(t('common.delSuccess'))
  352. // 刷新tabs数据
  353. await getTabsCount()
  354. // 刷新列表
  355. await getList()
  356. } catch {}
  357. }
  358. /** 商品图预览 */
  359. const imagePreview = (imgUrl: string) => {
  360. createImageViewer({
  361. urlList: [imgUrl]
  362. })
  363. }
  364. /** 搜索按钮操作 */
  365. const handleQuery = () => {
  366. getList()
  367. }
  368. /** 重置按钮操作 */
  369. const resetQuery = () => {
  370. queryFormRef.value.resetFields()
  371. handleQuery()
  372. }
  373. /** 新增或修改 */
  374. const openForm = (id?: number) => {
  375. // 修改
  376. if (typeof id === 'number') {
  377. push({ name: 'ProductSpuEdit', params: { id } })
  378. return
  379. }
  380. // 新增
  381. push({ name: 'ProductSpuAdd' })
  382. }
  383. /** 查看商品详情 */
  384. const openDetail = (id: number) => {
  385. push({ name: 'ProductSpuDetail', params: { id } })
  386. }
  387. /** 导出按钮操作 */
  388. const handleExport = async () => {
  389. try {
  390. // 导出的二次确认
  391. await message.exportConfirm()
  392. // 发起导出
  393. exportLoading.value = true
  394. const data = await ProductSpuApi.exportSpu(queryParams)
  395. download.excel(data, '商品列表.xls')
  396. } catch {
  397. } finally {
  398. exportLoading.value = false
  399. }
  400. }
  401. /** 获取分类的节点的完整结构 */
  402. const categoryList = ref() // 分类树
  403. const formatCategoryName = (categoryId: number) => {
  404. return treeToString(categoryList.value, categoryId)
  405. }
  406. /** 激活时 */
  407. onActivated(() => {
  408. getList()
  409. })
  410. /** 初始化 **/
  411. onMounted(async () => {
  412. await getTabsCount()
  413. await getList()
  414. // 获得分类树
  415. const data = await ProductCategoryApi.getCategoryList({})
  416. categoryList.value = handleTree(data, 'id', 'parentId')
  417. })
  418. </script>
  419. <style lang="scss" scoped>
  420. .spu-table-expand {
  421. padding-left: 42px;
  422. :deep(.el-form-item__label) {
  423. width: 82px;
  424. font-weight: bold;
  425. color: #99a9bf;
  426. }
  427. }
  428. </style>