index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <div class="cart-pages">
  3. <div class="cart-bos">
  4. <div class="cart-head flex-row-between">
  5. <div class="flex-row-start head1">
  6. <img src="@/assets/images/cart1.png" alt="" />
  7. <div>我的购物车</div>
  8. </div>
  9. <div class="head2" @click="onExport">导出订单</div>
  10. </div>
  11. <el-table
  12. ref="multipleTableRef"
  13. :data="tableData"
  14. style="width: 100%"
  15. :header-cell-style="{
  16. color: '#1D2129',
  17. backgroundColor: '#F2F3F5',
  18. fontWeight: 'normal'
  19. }"
  20. @selection-change="handleSelectionChange1"
  21. >
  22. <el-table-column type="selection" width="55" />
  23. <el-table-column label="商品信息" minWidth="490">
  24. <template #default="scope">
  25. <div class="cart-info" @click="onPath('/item?id=' + scope.row.shoppingCartId + '&productNo=' + scope.row.productNo)">
  26. <img class="cart-img" :src="scope.row.productImage" alt="" />
  27. <div class="cart-text">
  28. <div class="text1">{{ scope.row.itemName }}</div>
  29. <div class="text2">
  30. <span>单位:{{ scope.row.unitName }}</span>
  31. </div>
  32. <div class="text2">
  33. <span>编号:{{ scope.row.productNo }}</span>
  34. </div>
  35. </div>
  36. </div>
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="单价" minWidth="130">
  40. <template #default="scope"> ¥{{ scope.row.memberPrice }} </template>
  41. </el-table-column>
  42. <!-- @blur="(res) => handleChange(res, scope.row)" -->
  43. <el-table-column label="数量" minWidth="200">
  44. <template #default="scope">
  45. <el-input-number
  46. v-model="scope.row.productNum"
  47. :min="scope.row.minOrderQuantity"
  48. :max="scope.row.allStock"
  49. @change="handleChange(scope.row)"
  50. />
  51. </template>
  52. </el-table-column>
  53. <el-table-column label="小计" minWidth="140">
  54. <template #default="scope"> ¥{{ (scope.row.memberPrice * scope.row.productNum).toFixed(2) }} </template>
  55. </el-table-column>
  56. <el-table-column label="操作" width="200">
  57. <template #default="scope">
  58. <div>
  59. <el-button @click="editCollection(scope, 1)" link>{{ scope.row.isCollect == 1 ? '取消收藏' : '移入收藏' }} </el-button>
  60. </div>
  61. <div>
  62. <el-button link @click="onDel(scope, 1)"> 删除 </el-button>
  63. </div>
  64. </template>
  65. </el-table-column>
  66. </el-table>
  67. <div class="cart-shi">失效商品</div>
  68. <el-table
  69. :data="noTableData"
  70. style="width: 100%"
  71. :header-cell-style="{
  72. color: '#1D2129',
  73. backgroundColor: '#F2F3F5',
  74. fontWeight: 'normal'
  75. }"
  76. :row-style="{
  77. backgroundColor: '#F9F9F9'
  78. }"
  79. >
  80. <el-table-column type="selection" width="55" />
  81. <el-table-column label="商品信息" minWidth="490">
  82. <template #default="scope">
  83. <div class="cart-info" @click="onPath('/item?id=' + scope.row.shoppingCartId + '&productNo=' + scope.row.productNo)">
  84. <img class="cart-img" :src="scope.row.productImage" alt="" />
  85. <div class="cart-text">
  86. <div class="text1">{{ scope.row.itemName }}</div>
  87. <div class="text2">
  88. <span>单位:{{ scope.row.unitName }}</span>
  89. </div>
  90. <div class="text2">
  91. <span>编号:{{ scope.row.productNo }}</span>
  92. </div>
  93. <!-- <div class="text3">当前商品库存不足,当前库存量:0</div> -->
  94. </div>
  95. </div>
  96. </template>
  97. </el-table-column>
  98. <el-table-column label="单价" minWidth="130">
  99. <template #default="scope"> ¥{{ scope.row.memberPrice }} </template>
  100. </el-table-column>
  101. <el-table-column label="数量" minWidth="200">
  102. <template #default="scope">
  103. <el-input-number disabled v-model="scope.row.productNum" :controls="false" :min="1" :max="10" style="width: 130px" />
  104. </template>
  105. </el-table-column>
  106. <el-table-column label="小计" minWidth="140">
  107. <template #default="scope"> ¥{{ (scope.row.memberPrice * scope.row.productNum).toFixed(2) }} </template>
  108. </el-table-column>
  109. <el-table-column label="操作" width="200">
  110. <template #default="scope">
  111. <div>
  112. <el-button @click="editCollection(scope, 2)" link>{{ scope.row.isCollect == 1 ? '取消收藏' : '移入收藏' }} </el-button>
  113. </div>
  114. <div>
  115. <el-button link @click="onDel(scope, 2)"> 删除 </el-button>
  116. </div>
  117. </template>
  118. </el-table-column>
  119. </el-table>
  120. </div>
  121. <div class="cart-bnt">
  122. <div class="bnt-bos flex-row-between">
  123. <div></div>
  124. <!-- <div class="flex-row-start">
  125. <el-checkbox v-model="checked1" label="全选" @change="handleSelectAll" />
  126. <el-button style="margin-left: 10px">删除</el-button>
  127. <el-button>移入收藏</el-button>
  128. </div> -->
  129. <div class="bnt-box flex-row-start">
  130. <span>共</span>
  131. <span class="hig">{{ selectedList.length }}</span>
  132. <span>种商品,已选择</span>
  133. <span class="hig">{{ onNun }}</span>
  134. <span>件,商品合计:</span>
  135. <span class="hig">¥{{ onMoney }}</span>
  136. <span>,活动优惠:0.00</span>
  137. <span class="span1">合计:</span>
  138. <span class="span2">¥{{ onMoney }}</span>
  139. <el-button class="bnt" type="primary" @click="goTrad">去结算</el-button>
  140. </div>
  141. </div>
  142. </div>
  143. <!-- 取消收藏 -->
  144. <el-dialog
  145. v-model="dialogVisible"
  146. title="选择取消的收藏夹"
  147. width="500"
  148. :before-close="
  149. () => {
  150. dialogVisible = false;
  151. }
  152. "
  153. >
  154. <div>
  155. <el-radio-group v-model="radio">
  156. <el-radio v-for="(item, index) in favorites" :key="index" :value="item.id">{{ item.title }}</el-radio>
  157. </el-radio-group>
  158. </div>
  159. <template #footer>
  160. <div class="dialog-footer">
  161. <el-button @click="dialogVisible = false">取消</el-button>
  162. <el-button type="primary" @click="onCancel"> 确认 </el-button>
  163. </div>
  164. </template>
  165. </el-dialog>
  166. </div>
  167. </template>
  168. <script setup lang="ts">
  169. import type { TableInstance } from 'element-plus';
  170. const tableData = ref<any>([]);
  171. const selectedList = ref<any>([]);
  172. const noTableData = ref<any>([]);
  173. const multipleTableRef = ref<TableInstance>();
  174. const checked1 = ref(false);
  175. const dialogVisible = ref<any>(false);
  176. const favorites = ref<any>([]);
  177. const radio = ref<any>(null);
  178. const radioRow = ref<any>({});
  179. import {
  180. shoppingCartList,
  181. deleteProductShoppingCart,
  182. isProductInDefaultCollect,
  183. cancelProductCollect,
  184. addProductCollect,
  185. favoritesList,
  186. exportProductShoppingCart,
  187. updateProductShoppingCart
  188. } from '@/api/goods/index';
  189. import FileSaver from 'file-saver';
  190. import { onPath } from '@/utils/siteConfig';
  191. onMounted(() => {
  192. getInfo();
  193. });
  194. const getInfo = () => {
  195. shoppingCartList({
  196. pageNum: 1,
  197. pageSize: 999
  198. }).then((res) => {
  199. if (res.code == 200) {
  200. tableData.value = [];
  201. noTableData.value = [];
  202. if (res.rows && res.rows.length > 0) {
  203. res.rows.forEach((item: any) => {
  204. item.allStock = Number(item.totalInventory || 0) + Number(item.nowInventory || 0) + Number(item.virtualInventory || 0);
  205. item.minOrderQuantity = Number(item.minOrderQuantity || 1);
  206. if (item.productStatus == 1) {
  207. tableData.value.push(item);
  208. } else {
  209. noTableData.value.push(item);
  210. }
  211. });
  212. }
  213. }
  214. });
  215. };
  216. //购物车选中
  217. const handleSelectionChange1 = (val: any) => {
  218. selectedList.value = val;
  219. };
  220. //全选
  221. const handleSelectAll = (val: any) => {
  222. if (val) {
  223. multipleTableRef.value?.toggleAllSelection();
  224. } else {
  225. multipleTableRef.value?.clearSelection();
  226. }
  227. };
  228. import { cartStore } from '@/store/modules/cart';
  229. const cart = cartStore();
  230. const onDel = (scope: any, type: any) => {
  231. ElMessageBox.confirm(`确定要删除吗?`, '提示', {
  232. confirmButtonText: '确定',
  233. cancelButtonText: '取消',
  234. type: 'warning'
  235. }).then(() => {
  236. deleteProductShoppingCart(scope.row.shoppingCartId).then((res) => {
  237. if (res.code == 200) {
  238. ElMessage.success('删除成功');
  239. if (type == 1) {
  240. tableData.value.splice(scope.$index, 1);
  241. } else {
  242. noTableData.value.splice(scope.$index, 1);
  243. }
  244. cart.onCartCount();
  245. }
  246. });
  247. });
  248. };
  249. const goTrad = () => {
  250. if (selectedList.value.length == 0) {
  251. ElMessage.warning('请选择商品');
  252. return;
  253. }
  254. const ids = selectedList.value.map((item: any) => item.shoppingCartId).join(',');
  255. onPath('/trad?ids=' + ids);
  256. };
  257. const onMoney = computed(() => {
  258. let money = 0;
  259. let price = '';
  260. selectedList.value.forEach((item: any) => {
  261. money = money + Number(item.memberPrice * item.productNum);
  262. });
  263. price = Number(money).toFixed(2);
  264. return price;
  265. });
  266. const onNun = computed(() => {
  267. let money = 0;
  268. selectedList.value.forEach((item: any) => {
  269. money = money + Number(item.productNum);
  270. });
  271. return money;
  272. });
  273. //修改收藏
  274. const editCollection = (scope: any, type: any) => {
  275. if (scope.row.isCollect == 1) {
  276. dialogVisible.value = true;
  277. favoritesList(scope.row.id).then((res) => {
  278. if (res.code == 200) {
  279. if (res.rows.length > 0) {
  280. radio.value = res.rows[0].id;
  281. }
  282. favorites.value = res.rows;
  283. radioRow.value = scope.row;
  284. }
  285. });
  286. } else {
  287. // 添加
  288. addProductCollect({ productId: scope.row.id }).then((res) => {
  289. if (res.code == 200) {
  290. getInfo();
  291. }
  292. });
  293. }
  294. };
  295. // 取消收藏
  296. const onCancel = () => {
  297. cancelProductCollect({ productId: radioRow.value.id, favoritesId: radio.value }).then((res) => {
  298. if (res.code == 200) {
  299. dialogVisible.value = false;
  300. getInfo();
  301. }
  302. });
  303. };
  304. // 导出订单
  305. const onExport = () => {
  306. exportProductShoppingCart()
  307. .then((res: any) => {
  308. const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
  309. FileSaver.saveAs(blob, '购物车商品.xlsx');
  310. })
  311. .catch(() => {
  312. ElMessage.error('导出失败,请稍后重试');
  313. });
  314. };
  315. const handleChange = (row: any) => {
  316. updateProductShoppingCart({ productId: row.id, productNum: row.productNum }).then((res) => {});
  317. };
  318. </script>
  319. <style lang="scss" scoped>
  320. .cart-pages {
  321. width: 100%;
  322. background: #ffffff;
  323. .cart-bos {
  324. width: 100%;
  325. min-width: 1200px;
  326. max-width: 1500px;
  327. margin: 0 auto;
  328. }
  329. .cart-head {
  330. margin-bottom: 20px;
  331. padding-top: 20px;
  332. .head1 {
  333. font-weight: 600;
  334. font-size: 16px;
  335. color: #101828;
  336. img {
  337. width: 18px;
  338. height: 18px;
  339. margin-right: 6px;
  340. }
  341. }
  342. .head2 {
  343. width: 88px;
  344. height: 25px;
  345. border-radius: 4px 4px 4px 4px;
  346. border: 1px solid #e7000b;
  347. font-size: 12px;
  348. color: #e7000b;
  349. line-height: 25px;
  350. text-align: center;
  351. cursor: pointer;
  352. &:hover {
  353. background-color: #fff0f0;
  354. }
  355. }
  356. }
  357. .cart-info {
  358. width: 490px;
  359. height: 94px;
  360. display: flex;
  361. cursor: pointer;
  362. .cart-img {
  363. width: 94px;
  364. height: 94px;
  365. border-radius: 6px;
  366. }
  367. .cart-text {
  368. flex: 1;
  369. width: 0;
  370. padding-left: 10px;
  371. .text1 {
  372. font-size: 14px;
  373. color: #000000;
  374. height: 50px;
  375. }
  376. .text2 {
  377. font-size: 14px;
  378. color: #364153;
  379. span {
  380. margin-right: 10px;
  381. }
  382. }
  383. .text3 {
  384. font-size: 14px;
  385. color: #e7000b;
  386. }
  387. }
  388. }
  389. .cart-shi {
  390. font-weight: 600;
  391. font-size: 16px;
  392. color: #101828;
  393. padding: 20px 0;
  394. }
  395. .cart-bnt {
  396. width: 100%;
  397. height: 82px;
  398. background: #ffffff;
  399. box-shadow: 0px -2px 13px 0px rgba(0, 0, 0, 0.05);
  400. margin-top: 48px;
  401. .bnt-bos {
  402. width: 100%;
  403. min-width: 1200px;
  404. max-width: 1500px;
  405. margin: 0 auto;
  406. height: 82px;
  407. .bnt-box {
  408. font-size: 14px;
  409. color: #000000;
  410. .span1 {
  411. font-weight: 600;
  412. color: #101828;
  413. margin-left: 20px;
  414. }
  415. .span2 {
  416. font-weight: 600;
  417. color: #e7000b;
  418. font-size: 16px;
  419. margin-top: 2px;
  420. margin-left: 2px;
  421. }
  422. .hig {
  423. color: #e7000b;
  424. }
  425. .bnt {
  426. width: 142px;
  427. height: 50px;
  428. font-size: 16px;
  429. margin-left: 20px;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. </style>