goods.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <div class="el-container-md">
  3. <div class="selection-toolbar">
  4. <el-checkbox :indeterminate="isIndeterminate" v-model="allChecked" @change="toggleSelectAll">
  5. {{ multipleSelection.length > 0 ? `已选 ${multipleSelection.length} 条` : '选中本页' }}
  6. </el-checkbox>
  7. <el-button plain size="mini" @click="handleShelf">上架</el-button>
  8. <el-button plain size="mini" @click="handleUnshelf">下架</el-button>
  9. <el-button plain size="mini" @click="handleDeleteSelected">删除</el-button>
  10. <el-button plain type="mini" icon="el-icon-plus" @click="handleAddLiveGoods">添加商品</el-button>
  11. </div>
  12. <el-table
  13. ref="goodTable"
  14. :data="goodsLiveList"
  15. style="width: 100%"
  16. v-loading="loading"
  17. @selection-change="handleSelectionChange"
  18. >
  19. <el-table-column type="selection" width="55"></el-table-column>
  20. <!-- 题干列:显示试题的主要内容 -->
  21. <el-table-column
  22. prop="goodsId"
  23. label="商品id"
  24. show-overflow-tooltip
  25. ></el-table-column>
  26. <el-table-column
  27. label="商品图片"
  28. >
  29. <template slot-scope="scope">
  30. <img
  31. :src="scope.row.imgUrl"
  32. style="display: block; max-width: 100%; width: 100px; height: 100px"
  33. />
  34. </template>
  35. </el-table-column>
  36. <el-table-column
  37. prop="productName"
  38. label="商品名称"
  39. ></el-table-column>
  40. <el-table-column
  41. prop="price"
  42. label="价格"
  43. ></el-table-column>
  44. <el-table-column
  45. prop="stock"
  46. label="库存"
  47. ></el-table-column>
  48. <el-table-column
  49. prop="sales"
  50. label="销量"
  51. ></el-table-column>
  52. <el-table-column
  53. prop="status"
  54. label="上下架"
  55. >
  56. <template slot-scope="scope">
  57. <el-tag v-if="scope.row.status == 1">上架</el-tag>
  58. <el-tag v-if="scope.row.status == 0">下架</el-tag>
  59. </template>
  60. </el-table-column>
  61. <!-- 操作列:包含编辑和删除按钮 -->
  62. <el-table-column
  63. label="操作"
  64. width="180"
  65. fixed="right"
  66. >
  67. <template slot-scope="scope">
  68. <el-button
  69. type="text"
  70. size="small"
  71. style="color: #F56C6C;"
  72. @click="handleGoodDelete(scope.row)"
  73. >删除</el-button>
  74. </template>
  75. </el-table-column>
  76. </el-table>
  77. <!-- 分页组件:用于分页展示试题列表 -->
  78. <pagination
  79. v-show="goodsLiveTotal > 0"
  80. :total="goodsLiveTotal"
  81. :page.sync="goodsParams.pageNum"
  82. :limit.sync="goodsParams.pageSize"
  83. @pagination="getLiveGoodsList"
  84. style="margin-top: 20px;"
  85. />
  86. <!-- 添加商品弹窗 -->
  87. <el-dialog
  88. title="添加商品"
  89. :visible.sync="goodsDialogVisible"
  90. width="800px"
  91. :close-on-click-modal="false"
  92. :close-on-press-escape="false"
  93. >
  94. <div class="dialog-content">
  95. <div style="text-align: right; margin-bottom: 20px;">
  96. <el-input
  97. v-model="searchTitle"
  98. placeholder="请输入产品名称"
  99. style="width: 300px;"
  100. @input="handleGoodsSearch"
  101. ></el-input>
  102. </div>
  103. <el-table
  104. :data="goodsList"
  105. style="width: 100%"
  106. v-loading="goodsLoading"
  107. @selection-change="handleGoodsChange"
  108. @row-click="handleGoodsRowClick"
  109. row-key="id"
  110. >
  111. <el-table-column
  112. type="selection"
  113. width="55"
  114. >
  115. </el-table-column>
  116. <el-table-column
  117. prop="storeName"
  118. label="商铺名称"
  119. class-name="clickable-column"
  120. ></el-table-column>
  121. <el-table-column
  122. prop="productName"
  123. label="产品"
  124. class-name="clickable-column"
  125. ></el-table-column>
  126. <el-table-column
  127. prop="price"
  128. label="价格"
  129. class-name="clickable-column"
  130. ></el-table-column>
  131. <el-table-column
  132. prop="stock"
  133. label="库存"
  134. class-name="clickable-column"
  135. ></el-table-column>
  136. </el-table>
  137. <pagination
  138. v-show="goodsTotal > 0"
  139. :total="goodsTotal"
  140. :page.sync="queryGoodParams.pageNum"
  141. :limit.sync="queryGoodParams.pageSize"
  142. @pagination="getStoreProductLists"
  143. style="margin-top: 20px;"
  144. />
  145. </div>
  146. <div slot="footer" class="dialog-footer">
  147. <div style="display: flex; justify-content: space-between; align-items: center;">
  148. <span class="selected-count">当前已选择 <span style="color: #00BFFF; font-style: italic;">{{ selectedGoods.length }}</span> 商品</span>
  149. <div>
  150. <el-button @click="goodsDialogVisible = false">取 消</el-button>
  151. <el-button type="primary" @click="confirmAddGoods">确 定</el-button>
  152. </div>
  153. </div>
  154. </div>
  155. </el-dialog>
  156. </div>
  157. </template>
  158. <script>
  159. import {addLiveGoods, delLiveGoods, listLiveGoods, listStoreProduct, handleShelfOrUn, handleDeleteSelected} from "@/api/live/liveGoods";
  160. export default {
  161. data() {
  162. return {
  163. liveId: '',
  164. loading: true,
  165. searchTitle: '',
  166. queryGoodParams: {
  167. pageNum: 1,
  168. pageSize: 10,
  169. productName: null,
  170. liveId: null
  171. },
  172. goodsLiveList: [],
  173. goodsLiveTotal: 0,
  174. goodsParams: {
  175. pageNum: 1,
  176. pageSize: 10,
  177. liveId: null
  178. },
  179. goodsList: [],
  180. goodsTotal: 0,
  181. selectedGoods: [],
  182. goodsLoading: false,
  183. goodsDialogVisible: false,
  184. multipleSelection: [],
  185. allChecked: false,
  186. isIndeterminate: false,
  187. };
  188. },
  189. created() {
  190. this.liveId = this.$route.params.liveId
  191. this.goodsParams.liveId = this.liveId
  192. this.getLiveGoodsList();
  193. },
  194. methods: {
  195. handleShelf(){
  196. this.handleShelfOrUn(1)
  197. },
  198. handleUnshelf(){
  199. this.handleShelfOrUn(0)
  200. },
  201. handleShelfOrUn(type){
  202. if (this.multipleSelection.length > 0) {
  203. const goodsList = this.getSelectedList();
  204. handleShelfOrUn({"goodsIds":goodsList,"status":type}).then(res=>{
  205. this.dealResult(res)
  206. })
  207. } else {
  208. this.$message.info("请选择下架商品!")
  209. }
  210. },
  211. handleDeleteSelected(){
  212. if (this.multipleSelection.length > 0) {
  213. const goodsList = this.getSelectedList();
  214. handleDeleteSelected({"goodsIds":goodsList}).then(res=>{
  215. this.dealResult(res)
  216. })
  217. } else {
  218. this.$message.info("请选择被删除的商品!")
  219. }
  220. },
  221. dealResult(res){
  222. if (res.code == 200) {
  223. this.getLiveGoodsList();
  224. this.$refs.goodTable.clearSelection();
  225. } else {
  226. this.$message.error(res.msg);
  227. }
  228. },
  229. getSelectedList(){
  230. var goodsList = []
  231. this.multipleSelection.forEach(item => {
  232. goodsList.push(item.goodsId);
  233. })
  234. return goodsList;
  235. },
  236. // 全选或取消全选
  237. toggleSelectAll(val) {
  238. this.checked = val; // 更新 checkbox 的状态
  239. if (val) {
  240. // 如果 checkbox 被选中,则全选
  241. this.toggleSelection(this.goodsLiveList);
  242. } else {
  243. // 如果 checkbox 被取消选中,则取消全选
  244. this.toggleSelection();
  245. }
  246. },
  247. toggleSelection(rows) {
  248. if (rows && !this.isIndeterminate) {
  249. rows.forEach(row => {
  250. this.$refs.goodTable.toggleRowSelection(row);
  251. });
  252. } else {
  253. this.$refs.goodTable.clearSelection();
  254. }
  255. },
  256. // 多选框选中数据
  257. handleSelectionChange(val) {
  258. this.multipleSelection = val;
  259. // 根据选择项的数量更新 checkbox 的状态
  260. this.allChecked = val.length === this.goodsLiveList.length;
  261. this.isIndeterminate = val.length > 0 && val.length < this.goodsLiveList.length;
  262. },
  263. getLiveGoodsList() {
  264. this.loading = true
  265. listLiveGoods(this.goodsParams).then(response => {
  266. this.goodsLiveList = response.rows
  267. this.goodsLiveTotal = response.total
  268. this.loading = false
  269. })
  270. },
  271. handleAddLiveGoods(){
  272. this.goodsDialogVisible = true;
  273. this.getStoreProductLists()
  274. },
  275. handleGoodsSearch(){
  276. this.queryGoodParams.pageNum = 1
  277. this.queryGoodParams.productName = this.searchTitle
  278. this.getStoreProductLists()
  279. },
  280. handleGoodsChange(goods) {
  281. this.selectedGoods = goods
  282. },
  283. confirmAddGoods(){
  284. if (this.selectedGoods.length === 0) {
  285. this.$message({
  286. message: '请选择要添加的商品',
  287. type: 'warning'
  288. })
  289. return
  290. }
  291. addLiveGoods({
  292. liveId: this.liveId,
  293. productsId: this.selectedGoods.map(item => item.productId).join(',')
  294. }).then(response => {
  295. this.goodsDialogVisible = false
  296. this.getLiveGoodsList()
  297. })
  298. },
  299. handleGoodDelete(row){
  300. delLiveGoods(row.goodsId).then(response => {
  301. this.getLiveGoodsList()
  302. })
  303. },
  304. /** 处理行点击事件 */
  305. handleGoodsRowClick(row, column) {
  306. // 如果点击的是复选框列,不进行处理
  307. if (column.type === 'selection') {
  308. return
  309. }
  310. // 获取表格实例
  311. const table = this.$refs.goodsTable[0]
  312. if (!table) {
  313. return
  314. }
  315. // 判断当前行是否已经被选中
  316. const isSelected = this.selectedGoods.some(item => item.id === row.id)
  317. // 切换选中状态
  318. table.toggleRowSelection(row, !isSelected)
  319. },
  320. getStoreProductLists() {
  321. this.queryGoodParams.liveId = this.liveId
  322. listStoreProduct(this.queryGoodParams).then(response => {
  323. this.goodsList = response.rows
  324. this.goodsTotal = response.total
  325. this.loading = false
  326. })
  327. }
  328. }
  329. };
  330. </script>
  331. <style scoped>
  332. .selection-toolbar {
  333. display: flex;
  334. align-items: center;
  335. margin-bottom: 10px;
  336. padding-left: 10px;
  337. }
  338. .selection-toolbar .el-checkbox {
  339. margin-right: 10px;
  340. }
  341. /* 调整 checkbox 内部输入框的对齐 */
  342. .selection-toolbar .el-checkbox .el-checkbox__inner {
  343. top: 8px; /* 根据实际需求调整 */
  344. }
  345. </style>