index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <!-- 商品中心 - 商品列表 -->
  2. <template>
  3. <!-- 列表 -->
  4. <ContentWrap style="" class="container">
  5. <div class="search" v-if="!userId">
  6. <!-- 左侧的 -->
  7. <el-tabs v-model="queryParams.tabType" @tab-click="handleTabClick" class="parent-tabs" v-if="!mobile ">
  8. <el-tab-pane v-for="item in tabsData" :key="item.type" :label="item.name + '(' + item.count + ')'"
  9. :name="item.type" />
  10. </el-tabs>
  11. <div style="text-align: right;" class="search-input">
  12. <el-input v-model="queryParams.name" placeholder="搜索商品名称" @keyup.enter="handleQuery" class="!w-240px">
  13. <template #suffix>
  14. <el-icon class="el-input__icon" @click="handleQuery" style="cursor: pointer;">
  15. <Search />
  16. </el-icon>
  17. </template>
  18. <template #append>
  19. <el-button :icon="Operation" @click="showSearchMore" />
  20. </template>
  21. </el-input>
  22. <el-button v-hasPermi="['product:spu:create']" @click="openForm('create', '', 0)" class="ml-5px"
  23. v-if="!mobile && !isDetail && !merchantId">
  24. <Icon class="mr-5px " icon="ep:plus" />
  25. 新增
  26. </el-button>
  27. </div>
  28. </div>
  29. <div class="searchMore" v-show="searchMoreShow">
  30. <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="68px">
  31. <el-form-item label="店铺名称" v-if="!merchantId && !userId">
  32. <el-input v-model="shopName" class="!w-240px" clearable placeholder="请输入店铺名称"
  33. @input="shopNameQuery" />
  34. </el-form-item>
  35. <el-form-item label="商品分类" prop="categoryId">
  36. <el-cascader v-model="queryParams.categoryId" :options="categoryList" :props="defaultProps"
  37. clearable filterable placeholder="请选择商品分类" />
  38. </el-form-item>
  39. <el-form-item label="商品状态" prop="">
  40. <el-select v-model="queryParams.tabType" placeholder="全部">
  41. <el-option v-for="dict in tabsData" :key="dict.type" :label="dict.name" :value="dict.type" />
  42. </el-select>
  43. </el-form-item>
  44. <el-form-item label="创建时间" prop="createTime">
  45. <el-date-picker style="margin-right: unset;width:auto !important" v-model="queryParams.createTime"
  46. :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" end-placeholder="结束日期"
  47. start-placeholder="开始日期" type="daterange" value-format="YYYY-MM-DD HH:mm:ss" />
  48. </el-form-item>
  49. <el-row>
  50. <el-col>
  51. <el-form-item>
  52. <el-button @click="hideSearchMore">
  53. 收起
  54. </el-button>
  55. <el-button @click="resetQuery">
  56. 重置
  57. </el-button>
  58. <el-button @click="handleQuery" plain type="primary">
  59. <Icon class="mr-5px" icon="ep:search" />
  60. 搜索
  61. </el-button>
  62. </el-form-item>
  63. </el-col>
  64. </el-row>
  65. </el-form>
  66. </div>
  67. <el-row>
  68. <el-col v-for="(o, index) in list" class="card" :key="index" :span="7" :xl="7" :lg="7" :md="7" :sm="24"
  69. :xs="24" @mouseover="handleMouseOver(index)" @mouseout="handleMouseOut(index)">
  70. <el-card @click="openDetail('view', o, ProductSpuStatusEnum.RECYCLE.status, queryParams.tabType)">
  71. <div style="display: flex;justify-content: space-between;align-items: center;margin-bottom: 5px;">
  72. <p style="width: 100%;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;font-size: 16px;color:#000"
  73. :title="o.name">{{ o.name }}
  74. </p>
  75. <div @click.stop="openForm('update', o, ProductSpuStatusEnum.RECYCLE.status, queryParams.tabType)"
  76. v-show="o.showSetting && !mobile && !merchantId && !userId" class="setting">
  77. <el-icon size="20" color="rgb(220 223 231)">
  78. <Setting />
  79. </el-icon>
  80. </div>
  81. </div>
  82. <div style="display:flex;align-items: center;">
  83. <div
  84. style="width: 178px;height: 100px;margin-right:10px;border:1px solid rgb(220 223 231);border-radius: 5px;display: flex;align-items: center;justify-content: center;overflow: hidden;">
  85. <img :src="o.picUrl"
  86. style="width: 100%; height: 100%; object-fit: cover; object-position: center;" />
  87. </div>
  88. <div style='line-height: 25px;'>
  89. <p>¥{{ fenToYuan(o.price) }}</p>
  90. <p>销量:{{ o.salesCount }}</p>
  91. <p>库存:{{ o.stock }}</p>
  92. <p>状态:{{ o.status == 1 ? "上架" : "下架" }}</p>
  93. </div>
  94. </div>
  95. </el-card>
  96. </el-col>
  97. </el-row>
  98. <div v-if="list.length == 0">
  99. <p style="text-align: center;margin-top: 100px;">暂无商品</p>
  100. </div>
  101. <!-- 分页 -->
  102. <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
  103. @pagination="getList" />
  104. <!-- 表单弹窗:添加/修改 -->
  105. <SpuIndex ref="formRef" @success="getList" />
  106. </ContentWrap>
  107. </template>
  108. <script lang="ts" setup>
  109. import { TabsPaneContext } from 'element-plus'
  110. import { Setting, Search, Operation } from '@element-plus/icons-vue'
  111. import { createImageViewer } from '@/components/ImageViewer'
  112. import { dateFormatter } from '@/utils/formatTime'
  113. import { defaultProps, handleTree, treeToString } from '@/utils/tree'
  114. import { ProductSpuStatusEnum } from '@/utils/constants'
  115. import { fenToYuan } from '@/utils'
  116. import download from '@/utils/download'
  117. import * as ProductSpuApi from '@/api/mall/product/spu'
  118. import * as AfterSaleApi from '@/api/mall/trade/afterSale/index'
  119. import * as ProductCategoryApi from '@/api/mall/product/category'
  120. import SpuIndex from "./form/index.vue"
  121. // 从路由中引入store中的app仓库
  122. import { useAppStore } from '@/store/modules/app'
  123. const appStore = useAppStore()
  124. const mobile = computed(() => appStore.getMobile)
  125. // 从商户选项卡中传过来的id 如果有的话即证明是从商户中传过来的
  126. const props = defineProps({
  127. merchantId: {
  128. type: Number,
  129. default: undefined
  130. },
  131. userId: {
  132. type: Number,
  133. default: undefined
  134. },
  135. isDetail: {
  136. type: Boolean,
  137. default: false
  138. }
  139. });
  140. defineOptions({ name: 'ProductSpu' })
  141. const message = useMessage() // 消息弹窗
  142. const { t } = useI18n() // 国际化
  143. const { push } = useRouter() // 路由跳转
  144. const loading = ref(false) // 列表的加载中
  145. const exportLoading = ref(false) // 导出的加载中
  146. const total = ref(0) // 列表的总页数
  147. const list = ref<ProductSpuApi.Spu[]>([]) // 列表的数据
  148. // tabs 数据
  149. const tabsData = ref([
  150. {
  151. name: '全部',
  152. type: '',
  153. count: 0
  154. },
  155. {
  156. name: '出售中',
  157. type: 0,
  158. count: 0
  159. },
  160. {
  161. name: '仓库中',
  162. type: 1,
  163. count: 0
  164. },
  165. {
  166. name: '已售罄',
  167. type: 2,
  168. count: 0
  169. },
  170. {
  171. name: '警戒库存',
  172. type: 3,
  173. count: 0
  174. },
  175. {
  176. name: '回收站',
  177. type: 4,
  178. count: 0
  179. }
  180. ])
  181. const shopName = ref("")
  182. const queryParams = ref({
  183. pageNo: 1,
  184. pageSize: 10,
  185. tabType: '',
  186. name: '',
  187. categoryId: undefined,
  188. createTime: undefined,
  189. shopIds: []
  190. }) // 查询参数
  191. /** 查询根据店铺名模糊查询 */
  192. const getShopIds = async (shopName) => {
  193. loading.value = true
  194. const res = await AfterSaleApi.getShopIdsByName(shopName)
  195. queryParams.value.shopIds = res
  196. // console.log(res)
  197. // list.value = res.list
  198. }
  199. let timeout = null;
  200. const shopNameQuery = async (e) => {
  201. clearTimeout(timeout); // 清除之前的定时器
  202. timeout = setTimeout(() => {
  203. getShopIds(e); // 延迟执行查询操作
  204. }, 500);
  205. }
  206. const queryFormRef = ref() // 搜索的表单Ref
  207. const searchMoreShow = ref(false)
  208. // 鼠标移入显示变动图标
  209. function handleMouseOver(index) {
  210. list.value[index].showSetting = true;
  211. }
  212. // 鼠标移出不显示图标
  213. function handleMouseOut(index) {
  214. list.value[index].showSetting = false;
  215. }
  216. // 打开更多搜索条件
  217. const showSearchMore = () => {
  218. searchMoreShow.value = !searchMoreShow.value
  219. }
  220. const hideSearchMore = () => {
  221. searchMoreShow.value = false
  222. }
  223. /** 查询列表 */
  224. const getList = async () => {
  225. loading.value = true
  226. if (props.merchantId) {
  227. queryParams.value.merchantId = props.merchantId
  228. }
  229. try {
  230. let data;
  231. if (props.userId) {
  232. console.log(props.userId)
  233. queryParams.value.userId = props.userId
  234. data = await ProductSpuApi.getFavoriteByUser(queryParams.value)
  235. } else {
  236. data = await ProductSpuApi.getSpuPage(queryParams.value)
  237. }
  238. data.list.forEach(obj => {
  239. obj.showSetting = false;
  240. });
  241. list.value = data.list
  242. // console.log(list.value)
  243. total.value = data.total
  244. } finally {
  245. loading.value = false
  246. searchMoreShow.value = false
  247. getTabsCount()
  248. }
  249. }
  250. /** 切换 Tab */
  251. const handleTabClick = (tab : TabsPaneContext) => {
  252. queryParams.value.tabType = tab.paneName
  253. getList()
  254. }
  255. /** 获得每个 Tab 的数量 */
  256. const getTabsCount = async () => {
  257. const obj = ref({ ...queryParams.value })
  258. let res;
  259. if (props.merchantId) {
  260. obj.value.merId = props.merchantId
  261. res = await ProductSpuApi.getTabsCountByMer(obj.value)
  262. } else {
  263. res = await ProductSpuApi.getTabsCount(obj.value)
  264. }
  265. tabsData.value[0].count = 0
  266. for (let objName in res) {
  267. if (objName <= 1 || objName === '4') {
  268. tabsData.value[0].count += res[objName]
  269. }
  270. tabsData.value[Number(objName) + 1].count = res[objName]
  271. }
  272. }
  273. /** 添加到仓库 / 回收站的状态 */
  274. const handleStatus02Change = async (row : any, newStatus : number) => {
  275. try {
  276. // 二次确认
  277. const text = newStatus === ProductSpuStatusEnum.RECYCLE.status ? '加入到回收站' : '恢复到仓库'
  278. await message.confirm(`确认要"${row.name}"${text}吗?`)
  279. // 发起修改
  280. await ProductSpuApi.updateStatus({ id: row.id, status: newStatus })
  281. message.success(text + '成功')
  282. // 刷新 tabs 数据
  283. await getTabsCount()
  284. // 刷新列表
  285. await getList()
  286. } catch { }
  287. }
  288. /** 更新上架/下架状态 */
  289. const handleStatusChange = async (row : any) => {
  290. try {
  291. // 二次确认
  292. const text = row.status ? '上架' : '下架'
  293. await message.confirm(`确认要${text}"${row.name}"吗?`)
  294. // 发起修改
  295. await ProductSpuApi.updateStatus({ id: row.id, status: row.status })
  296. message.success(text + '成功')
  297. // 刷新 tabs 数据
  298. await getTabsCount()
  299. // 刷新列表
  300. await getList()
  301. } catch {
  302. // 异常时,需要重置回之前的值
  303. row.status =
  304. row.status === ProductSpuStatusEnum.DISABLE.status
  305. ? ProductSpuStatusEnum.ENABLE.status
  306. : ProductSpuStatusEnum.DISABLE.status
  307. }
  308. }
  309. /** 删除按钮操作 */
  310. const handleDelete = async (id : number) => {
  311. try {
  312. // 删除的二次确认
  313. await message.delConfirm()
  314. // 发起删除
  315. await ProductSpuApi.deleteSpu(id)
  316. message.success(t('common.delSuccess'))
  317. // 刷新tabs数据
  318. await getTabsCount()
  319. // 刷新列表
  320. await getList()
  321. } catch { }
  322. }
  323. /** 商品图预览 */
  324. const imagePreview = (imgUrl : string) => {
  325. createImageViewer({
  326. urlList: [imgUrl]
  327. })
  328. }
  329. /** 搜索按钮操作 */
  330. const handleQuery = () => {
  331. getList();
  332. }
  333. /** 重置按钮操作 */
  334. const resetQuery = () => {
  335. queryParams.value.categoryId = undefined;
  336. queryParams.value.createTime = undefined;
  337. queryParams.value.name = "";
  338. queryParams.value.tabType = "";
  339. queryParams.value.shopIds = [];
  340. shopName.value = "";
  341. handleQuery();
  342. }
  343. const formRef = ref()
  344. /** 新增或修改 */
  345. const openForm = (type : string, row : any, newStatus : number, tabType : number) => {
  346. formRef.value.open(type, row, newStatus, tabType)
  347. }
  348. /** 查看商品详情 */
  349. const openDetail = (type : string, row : any, newStatus : number) => {
  350. const id = row.id
  351. if (mobile.value) {
  352. push({ name: 'ProductSpuMobileDetail', params: { id } })
  353. } else {
  354. formRef.value.open(type, row, newStatus)
  355. }
  356. }
  357. /** 导出按钮操作 */
  358. const handleExport = async () => {
  359. try {
  360. // 导出的二次确认
  361. await message.exportConfirm()
  362. // 发起导出
  363. exportLoading.value = true
  364. const data = await ProductSpuApi.exportSpu(queryParams)
  365. download.excel(data, '商品列表.xls')
  366. } catch {
  367. } finally {
  368. exportLoading.value = false
  369. }
  370. }
  371. /** 获取分类的节点的完整结构 */
  372. const categoryList = ref() // 分类树
  373. const formatCategoryName = (categoryId : number) => {
  374. return treeToString(categoryList.value, categoryId)
  375. }
  376. /** 激活时 */
  377. onActivated(() => {
  378. getList()
  379. })
  380. /** 初始化 **/
  381. onMounted(async () => {
  382. await getTabsCount()
  383. await getList()
  384. // 获得分类树
  385. const data = await ProductCategoryApi.getCategoryList({})
  386. categoryList.value = handleTree(data, 'id', 'parentId')
  387. })
  388. </script>
  389. <style src="@/styles/SearchIndex.css"  lang="scss" scoped></style>