金诚优选前端代码
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.

355 lines
9.5 KiB

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. }
  107. },
  108. watch: {
  109. list: {
  110. deep: true,
  111. handler: function(val){
  112. let total_count = 0;
  113. let total_price = new Bigc(0);
  114. val.map(s => {
  115. s.goods.map(g => {
  116. if(g.checked){
  117. total_count++;
  118. let itemPrice = new Bigc(g.price).times(g.qty);
  119. total_price = total_price.plus(itemPrice);
  120. }
  121. })
  122. })
  123. this.total_count = total_count;
  124. this.total_price = total_price;
  125. }
  126. }
  127. },
  128. onLoad(){
  129. this.token = this.$cookieStorage.get('user_token');
  130. this.getCartList();
  131. },
  132. methods: {
  133. // 购物车商品加减
  134. changeNum(type, parentIndex, childIndex){
  135. let goodsItem = this.list[parentIndex].goods[childIndex];
  136. if(type == 'add'){
  137. if(goodsItem.qty < 10){
  138. goodsItem.qty++;
  139. }else{
  140. return;
  141. }
  142. }else if(type == 'less'){
  143. if(goodsItem.qty > 1){
  144. goodsItem.qty--;
  145. }else{
  146. return;
  147. }
  148. }
  149. let __raw_id = goodsItem.__raw_id;
  150. this.$http.ajax({
  151. api: 'api/shopping/cart/'+ __raw_id,
  152. method: 'PUT',
  153. header: {
  154. Authorization: this.token
  155. },
  156. data: {
  157. attributes: {
  158. qty: goodsItem.qty
  159. }
  160. }
  161. }).then(res => {
  162. if(!res.data.status){
  163. this.$msg('购物车更新失败', {icon: 'error'});
  164. }
  165. })
  166. },
  167. getCartList(){
  168. this.$http.get({
  169. api: 'api/cart',
  170. header: {
  171. Authorization: this.token
  172. }
  173. }).then(res => {
  174. console.log("===", res);
  175. let data = res.data.data;
  176. let list = [];
  177. for(let i in data){
  178. let goods = data[i].goods.map(item => {
  179. item.checked = false;
  180. return item;
  181. })
  182. list.push({
  183. name: data[i].name,
  184. full_minus: '',
  185. goods: goods,
  186. id: data[i].id
  187. })
  188. }
  189. this.list = list;
  190. })
  191. },
  192. // 商品被勾选
  193. goodsCheckChange(event, parentIndex, childIndex) {
  194. this.list[parentIndex].goods[childIndex].checked = event.value;
  195. let check_list = this.list.map(item => {
  196. return item.goods.every(g => g.checked);
  197. });
  198. this.allChecked = check_list.every(a => a);
  199. },
  200. // 全选被改变
  201. allCheckChange(event){
  202. this.allChecked = event.value;
  203. this.list.forEach(s => {
  204. s.goods.forEach(g => {
  205. g.checked = event.value;
  206. })
  207. })
  208. },
  209. // 结算
  210. submit(){
  211. if(this.total_count){
  212. let brand = [];
  213. let cart_ids = [];
  214. this.list.map(item => {
  215. let checked = item.goods.every(g => g.checked);
  216. item.goods.map(g => {
  217. if(g.checked){
  218. cart_ids.push(g.__raw_id);
  219. }
  220. })
  221. if(checked){
  222. brand.push(item.name);
  223. }
  224. })
  225. if(brand.length > 1){
  226. this.$msg('只支持单个店铺结算哦');
  227. }else{
  228. let par = {
  229. cart_ids: cart_ids,
  230. type: "normal",
  231. wechat_group_id: ""
  232. };
  233. this.$cookieStorage.set('order_confirm', par);
  234. this.$url('/pages/order/confirm/confirm');
  235. }
  236. }else{
  237. this.$msg('您未选择需要结算的商品');
  238. }
  239. },
  240. // 滑动组件,按钮被点击
  241. onButton(event, parentIndex, childIndex){
  242. if(event.buttonIndex == 0){
  243. let __raw_id = this.list[parentIndex].goods[childIndex].__raw_id;
  244. uni.showModal({
  245. title: '温馨提示',
  246. content: '确定移除该商品吗?',
  247. success: result => {
  248. if(result.confirm){
  249. uni.showLoading({
  250. title: '正在删除'
  251. })
  252. this.$http.ajax({
  253. api: 'api/shopping/cart/'+ __raw_id,
  254. method: 'DELETE',
  255. header: {
  256. Authorization: this.token
  257. }
  258. }).then(res => {
  259. if(res.data.code == 200){
  260. this.$msg('删除成功', {icon: 'success'}).then(() => {
  261. this.list[parentIndex].goods.splice(childIndex, 1);
  262. if(this.list[parentIndex].goods.length <= 0){
  263. this.list.splice(parentIndex, 1);
  264. }
  265. })
  266. }else{
  267. this.$msg('删除失败', {icon: 'error'});
  268. }
  269. uni.hideLoading();
  270. }).catch(err => uni.hideLoading())
  271. }
  272. }
  273. })
  274. }
  275. }
  276. }
  277. }
  278. </script>
  279. <style>
  280. page {
  281. background-color: #F8F8F8;
  282. }
  283. </style>
  284. <style scoped lang="scss">
  285. .cart-btn {
  286. display: flex;
  287. justify-content: center;
  288. align-items: center;
  289. width: 200rpx;
  290. height: 80rpx;
  291. background: #15716E;
  292. border-radius: 40rpx;
  293. color: #FFFFFF;
  294. font-size: 28rpx;
  295. }
  296. .cart-bottom {
  297. display: flex;
  298. justify-content: space-between;
  299. width: 750rpx;
  300. height: 120rpx;
  301. background: #FFFFFF;
  302. position: fixed;
  303. bottom: 0;
  304. padding: 32rpx;
  305. }
  306. .gray-tag {
  307. border-radius: 2rpx;
  308. border: 1rpx solid #979797;
  309. color: #777;
  310. padding: 0 6rpx;
  311. font-size: 20rpx;
  312. width: max-content;
  313. }
  314. .red-tag {
  315. font-size: 20rpx;
  316. color: #F63434;
  317. }
  318. .content-img {
  319. width: 130rpx;
  320. height: 130rpx;
  321. border-radius: 5rpx;
  322. }
  323. .content-info{
  324. width: 410rpx;
  325. display: flex;
  326. flex-direction: column;
  327. justify-content: space-between;
  328. }
  329. .online-card {
  330. width: 686rpx;
  331. height: auto;
  332. background: #FFFFFF;
  333. border-radius: 20rpx;
  334. // margin-bottom: 30rpx;
  335. padding: 30rpx;
  336. margin: 30rpx auto;
  337. overflow: hidden;
  338. &:last-child {
  339. margin-bottom: 150rpx;
  340. }
  341. }
  342. .num-btn{
  343. width: 36rpx;
  344. }
  345. .input{
  346. width: 40rpx;
  347. height: 45rpx;
  348. border-radius: 2rpx;
  349. background-color: #F3F8F7;
  350. text-align: center;
  351. }
  352. </style>