金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
9.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <template>
  2. <view>
  3. <lf-nav :spreadOut="true" :showIcon="true" bgColor="white" title="购物车"></lf-nav>
  4. <view style="height: 1rpx;"></view>
  5. <view>
  6. <view v-for="(s_item, s_index) in list" :key="s_index" class="online-card">
  7. <view class="lf-m-b-20" @click="$url('/pages/shop/shopdetail?id='+ s_item.id)">
  8. <text class="lf-iconfont icon-Group- lf-font-28"></text>
  9. <text class="lf-color-black lf-font-28 lf-font-bold lf-m-l-10">{{ s_item.name }}</text>
  10. <text class="lf-iconfont icon-xiangyou lf-font-24 lf-m-l-10"></text>
  11. </view>
  12. <view class="lf-row-between" v-if="s_item.full_minus">
  13. <view class="lf-flex">
  14. <view class="gray-tag">满减</view>
  15. <view class="lf-m-l-15 lf-font-24 lf-color-333">{{s_item.full_minus}}</view>
  16. </view>
  17. <view class="red-tag" @click="$msg('敬请期待')">去凑单</view>
  18. </view>
  19. <!-- 单个商品信息增加滑动删除 -->
  20. <view v-for="(g_item, g_index) in s_item.goods" :key="g_index">
  21. <lf-swipe-action :options="options" :index="g_index" :show="show" @button="onButton($event, s_index, g_index)">
  22. <view class="lf-row-between">
  23. <u-checkbox-group>
  24. <u-checkbox shape="circle" active-color="#15716E" @change="goodsCheckChange($event, s_index, g_index)" v-model="g_item.checked"></u-checkbox>
  25. </u-checkbox-group>
  26. <view class="lf-m-t-30" style="display: flex;">
  27. <image class="content-img" :src="g_item.img" @click="$url('/pages/shop/goodsdetail?id='+ g_item.com_id)"></image>
  28. <view class="lf-m-l-15 content-info">
  29. <view class="lf-color-333 lf-font-26 lf-line-2" style="max-width: 480rpx;">
  30. <text>{{g_item.name}}</text>
  31. <text class="lf-m-l-10">{{g_item.color ? g_item.color+' ' : ''}}</text>
  32. <text>{{g_item.size ? g_item.size : ''}}</text>
  33. </view>
  34. <!-- <view class="lf-font-24 lf-color-777 lf-m-t-14">
  35. <view>{{g_item.qty ? g_item.qty+'件;' : ''}}{{g_item.color ? g_item.color+';' : ''}}{{g_item.size ? g_item.size : ''}}</view>
  36. </view> -->
  37. <view class="lf-row-between">
  38. <view class="lf-font-32 lf-color-price" style="line-height: 1;">{{ g_item.price }}</view>
  39. <view class="lf-flex">
  40. <view class="num-btn" @click="changeNum('less', s_index, g_index)">
  41. <text class="lf-iconfont icon-jian1 lf-font-30"></text>
  42. </view>
  43. <view>
  44. <input class="input" :value="g_item.qty" :disabled="true" />
  45. </view>
  46. <view class="num-btn lf-text-right" @click="changeNum('add', s_index, g_index)">
  47. <text class="lf-iconfont icon-jia lf-font-30"></text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. </lf-swipe-action>
  55. </view>
  56. </view>
  57. <view class="loading-more" v-if="list.length == 0">
  58. <lf-nocontent src="/static/images/empty-cart.png"></lf-nocontent>
  59. </view>
  60. </view>
  61. <view style="height: 60rpx;"></view>
  62. <view class="cart-bottom">
  63. <u-checkbox-group>
  64. <u-checkbox shape="circle" active-color="#15716E" @change="allCheckChange" v-model="allChecked">
  65. <text class="lf-font-28 lf-color-777">全选</text>
  66. </u-checkbox>
  67. </u-checkbox-group>
  68. <view class="lf-row-between">
  69. <view class="lf-font-32 lf-color-222 lf-font-bold lf-m-r-30">
  70. 合计: {{ total_price || 0 }}
  71. </view>
  72. <view class="cart-btn" hover-class="lf-opacity" @click="submit">
  73. <text>结算</text>
  74. <text v-if="total_count">({{ total_count }})</text>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. import lfSwipeAction from '@/components/lf-swipeAction/lf-swipeAction.vue';
  82. import Bigc from '@/common/js/bigc.js';
  83. export default {
  84. components: {
  85. lfSwipeAction
  86. },
  87. data() {
  88. return {
  89. token: '', // 用户token
  90. allChecked: false, // 全选
  91. list: [], // 购物车列表
  92. total_price: '', // 总金额
  93. total_count: '', // 总数量
  94. options: [{
  95. text: '删除',
  96. style: {
  97. backgroundColor: '#dd524d'
  98. }
  99. },{
  100. text: '取消',
  101. style: {
  102. backgroundColor: '#15716E'
  103. }
  104. }],
  105. show: false,
  106. show_count: 0
  107. }
  108. },
  109. watch: {
  110. list: {
  111. deep: true,
  112. handler: function(val){
  113. let total_count = 0;
  114. let total_price = new Bigc(0);
  115. val.map(s => {
  116. s.goods.map(g => {
  117. if(g.checked){
  118. total_count++;
  119. let itemPrice = new Bigc(g.price).times(g.qty);
  120. total_price = total_price.plus(itemPrice);
  121. }
  122. })
  123. })
  124. this.total_count = total_count;
  125. this.total_price = total_price.round(2, 0);
  126. }
  127. }
  128. },
  129. onLoad(){
  130. this.token = this.$cookieStorage.get('user_token');
  131. this.getCartList();
  132. },
  133. onShow(){
  134. this.show_count++;
  135. if(this.show_count > 1){
  136. this.allChecked = false;
  137. this.total_price = '';
  138. this.total_count = '';
  139. this.getCartList();
  140. }
  141. },
  142. methods: {
  143. // 购物车商品加减
  144. changeNum(type, parentIndex, childIndex){
  145. let goodsItem = this.list[parentIndex].goods[childIndex];
  146. if(type == 'add'){
  147. if(goodsItem.qty < 10){
  148. goodsItem.qty++;
  149. }else{
  150. return;
  151. }
  152. }else if(type == 'less'){
  153. if(goodsItem.qty > 1){
  154. goodsItem.qty--;
  155. }else{
  156. return;
  157. }
  158. }
  159. let __raw_id = goodsItem.__raw_id;
  160. this.$http.ajax({
  161. api: 'api/shopping/cart/'+ __raw_id,
  162. method: 'PUT',
  163. header: {
  164. Authorization: this.token
  165. },
  166. data: {
  167. attributes: {
  168. qty: goodsItem.qty
  169. }
  170. }
  171. }).then(res => {
  172. if(!res.data.status){
  173. this.$msg('购物车更新失败', {icon: 'error'});
  174. }
  175. })
  176. },
  177. getCartList(){
  178. uni.showLoading({
  179. title: '正在查询购物车'
  180. })
  181. this.$http.get({
  182. api: 'api/cart',
  183. header: {
  184. Authorization: this.token
  185. }
  186. }).then(res => {
  187. console.log("===", res);
  188. uni.hideLoading();
  189. let data = res.data.data;
  190. let list = [];
  191. for(let i in data){
  192. let goods = data[i].goods.map(item => {
  193. item.checked = false;
  194. return item;
  195. })
  196. list.push({
  197. name: data[i].name,
  198. full_minus: '',
  199. goods: goods,
  200. id: data[i].id
  201. })
  202. }
  203. this.list = list;
  204. }).catch(err => uni.hideLoading())
  205. },
  206. // 商品被勾选
  207. goodsCheckChange(event, parentIndex, childIndex) {
  208. this.list[parentIndex].goods[childIndex].checked = event.value;
  209. let check_list = this.list.map(item => {
  210. return item.goods.every(g => g.checked);
  211. });
  212. this.allChecked = check_list.every(a => a);
  213. },
  214. // 全选被改变
  215. allCheckChange(event){
  216. this.allChecked = event.value;
  217. this.list.forEach(s => {
  218. s.goods.forEach(g => {
  219. g.checked = event.value;
  220. })
  221. })
  222. },
  223. // 结算
  224. submit(){
  225. if(this.total_count){
  226. let brand = [];
  227. let cart_ids = [];
  228. this.list.map(item => {
  229. let checked = item.goods.every(g => g.checked);
  230. item.goods.map(g => {
  231. if(g.checked){
  232. cart_ids.push(g.__raw_id);
  233. }
  234. })
  235. if(checked){
  236. brand.push(item.name);
  237. }
  238. })
  239. if(brand.length > 1){
  240. this.$msg('只支持单个店铺结算哦');
  241. }else{
  242. let par = {
  243. cart_ids: cart_ids,
  244. type: "normal",
  245. wechat_group_id: ""
  246. };
  247. this.$cookieStorage.set('order_confirm', par);
  248. this.$url('/pages/order/confirm/confirm');
  249. }
  250. }else{
  251. this.$msg('您未选择需要结算的商品');
  252. }
  253. },
  254. // 滑动组件,按钮被点击
  255. onButton(event, parentIndex, childIndex){
  256. if(event.buttonIndex == 0){
  257. let __raw_id = this.list[parentIndex].goods[childIndex].__raw_id;
  258. uni.showModal({
  259. title: '温馨提示',
  260. content: '确定移除该商品吗?',
  261. success: result => {
  262. if(result.confirm){
  263. uni.showLoading({
  264. title: '正在删除'
  265. })
  266. this.$http.ajax({
  267. api: 'api/shopping/cart/'+ __raw_id,
  268. method: 'DELETE',
  269. header: {
  270. Authorization: this.token
  271. }
  272. }).then(res => {
  273. if(res.data.code == 200){
  274. this.$msg('删除成功', {icon: 'success'}).then(() => {
  275. this.list[parentIndex].goods.splice(childIndex, 1);
  276. if(this.list[parentIndex].goods.length <= 0){
  277. this.list.splice(parentIndex, 1);
  278. }
  279. })
  280. }else{
  281. this.$msg('删除失败', {icon: 'error'});
  282. }
  283. uni.hideLoading();
  284. }).catch(err => uni.hideLoading())
  285. }
  286. }
  287. })
  288. }
  289. }
  290. }
  291. }
  292. </script>
  293. <style>
  294. page {
  295. background-color: #F8F8F8;
  296. }
  297. </style>
  298. <style scoped lang="scss">
  299. .cart-btn {
  300. display: flex;
  301. justify-content: center;
  302. align-items: center;
  303. width: 200rpx;
  304. height: 80rpx;
  305. background: #15716E;
  306. border-radius: 40rpx;
  307. color: #FFFFFF;
  308. font-size: 28rpx;
  309. }
  310. .cart-bottom {
  311. display: flex;
  312. justify-content: space-between;
  313. width: 750rpx;
  314. height: 120rpx;
  315. background: #FFFFFF;
  316. position: fixed;
  317. bottom: 0;
  318. padding: 32rpx;
  319. }
  320. .gray-tag {
  321. border-radius: 2rpx;
  322. border: 1rpx solid #979797;
  323. color: #777;
  324. padding: 0 6rpx;
  325. font-size: 20rpx;
  326. width: max-content;
  327. }
  328. .red-tag {
  329. font-size: 20rpx;
  330. color: #F63434;
  331. }
  332. .content-img {
  333. width: 130rpx;
  334. height: 130rpx;
  335. border-radius: 5rpx;
  336. }
  337. .content-info{
  338. width: 410rpx;
  339. display: flex;
  340. flex-direction: column;
  341. justify-content: space-between;
  342. }
  343. .online-card {
  344. width: 686rpx;
  345. height: auto;
  346. background: #FFFFFF;
  347. border-radius: 20rpx;
  348. // margin-bottom: 30rpx;
  349. padding: 30rpx;
  350. margin: 30rpx auto;
  351. overflow: hidden;
  352. &:last-child {
  353. margin-bottom: 150rpx;
  354. }
  355. }
  356. .num-btn{
  357. width: 36rpx;
  358. }
  359. .input{
  360. width: 40rpx;
  361. height: 45rpx;
  362. border-radius: 2rpx;
  363. background-color: #F3F8F7;
  364. text-align: center;
  365. }
  366. </style>