favorite.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view class="wrap">
  3. <u-navbar :custom-back="back" title="喜欢的商品">
  4. <view class="navbar-right" slot="right" v-if="favList.length!=0">
  5. <view class="link-text" @click="onClickRight">
  6. {{rightText}}
  7. </view>
  8. </view>
  9. </u-navbar>
  10. <view class="no-data" v-if="favList.length ===0 ">
  11. <view class="content">
  12. <u-icon name="heart" size="80" /><br>
  13. 收藏列表还是空的
  14. </view>
  15. <u-button shape="circle" @click="toHome">去逛逛</u-button>
  16. </view>
  17. <view class="gl-body">
  18. <u-row v-for="(item,index) in favList" :key="index">
  19. <u-col :span="2" v-if="showEdit">
  20. <u-checkbox @change="changeCheckBox" v-model="checkList[index]"></u-checkbox>
  21. </u-col>
  22. <u-col :span="4" class="gl-img">
  23. <u-image width="170rpx" height="170rpx" :src="item.goods.img"
  24. @click="toDetail(item.goods.id)"></u-image>
  25. </u-col>
  26. <u-col :span="showEdit?6:8" @click="toDetail(item.goods.id)">
  27. <view class="gl-name">{{item.goods.name}}</view>
  28. <view class="gl-descript">{{item.goods.descript}}</view>
  29. <view class="gl-price">¥{{formatPrice(item.goods.price)}}</view>
  30. </u-col>
  31. </u-row>
  32. </view>
  33. <view class="navigation" v-if="showEdit && favList.length>0">
  34. <view class="left">
  35. <view class="item">
  36. <u-checkbox @change="checkAll" v-model="checkedAll">全选</u-checkbox>
  37. </view>
  38. </view>
  39. <view class="right">
  40. <u-button type="error" :disabled="delDisabled" @click="del">删除</u-button>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. export default {
  47. data() {
  48. return {
  49. test: true,
  50. checkList: [],
  51. favList: [],
  52. rightText: '管理',
  53. showEdit: false,
  54. delDisabled: false,
  55. checkedAll: false
  56. }
  57. },
  58. onLoad() {
  59. this.init();
  60. },
  61. methods: {
  62. init() {
  63. const baseApi = this.baseApi;
  64. this.$u.get('user/favorite/list').then(res => {
  65. let favList = res;
  66. let checkList = new Array()
  67. for (var index in favList) {
  68. const item = favList[index];
  69. checkList[index] = true
  70. item.goods.img = baseApi + '/file/getImgStream?idFile=' + item.goods.pic;
  71. }
  72. this.checkList = checkList
  73. this.checkedAll = true
  74. this.favList = favList
  75. });
  76. },
  77. formatPrice(price) {
  78. return (price / 100).toFixed(2);
  79. },
  80. back() {
  81. this.$u.route({
  82. type: 'navigateBack',
  83. delta: 1
  84. })
  85. },
  86. onClickRight() {
  87. this.showEdit = !this.showEdit
  88. this.rightText = this.rightText === '管理' ? '完成' : '管理'
  89. },
  90. changeCheckBox(e) {
  91. this.delDisabled = false;
  92. },
  93. checkAll() {
  94. console.log('checkedAll', this.checkedAll)
  95. if (this.checkedAll) {
  96. this.delDisabled = true;
  97. for (var i in this.checkList) {
  98. this.checkList[i] = false;
  99. }
  100. } else {
  101. this.delDisabled = false;
  102. let checkList = this.checkList
  103. for (var i in checkList) {
  104. checkList[i] = true;
  105. }
  106. this.checkList = checkList
  107. console.log("checkList", checkList)
  108. }
  109. this.checkedAll = !this.checkedAll;
  110. },
  111. del() {
  112. let idChechkedArr = new Array();
  113. for (const i in this.checkList) {
  114. if (this.checkList[i]) {
  115. idChechkedArr.push(this.favList[i].id);
  116. }
  117. }
  118. if (idChechkedArr.length == 0) {
  119. this.$u.toast('请选择要移出收藏的商品');
  120. return;
  121. }
  122. this.$u.post('user/favorite/dislikeBatch', idChechkedArr).then(res => {
  123. this.$u.toast('已取消收藏');
  124. this.init();
  125. });
  126. },
  127. toDetail(id) {
  128. this.$u.route({
  129. url: '/pages/goods/goods',
  130. params: {
  131. id: id
  132. }
  133. })
  134. },
  135. toHome() {
  136. this.$u.route({
  137. type: 'switchTab',
  138. url: '/pages/shop/index'
  139. });
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. .wrap {}
  146. .gl-body {
  147. padding: 0rpx 30rpx;
  148. padding-bottom: 130rpx;
  149. }
  150. .gl-title {
  151. padding: 20rpx;
  152. }
  153. .gl-img {
  154. padding: 30rpx;
  155. }
  156. .gl-name {
  157. font-size: 26rpx;
  158. }
  159. .gl-descript {
  160. font-size: 20rpx;
  161. }
  162. .gl-price {
  163. font-size: 24rpx;
  164. color: #FA3534;
  165. }
  166. .navbar-right {
  167. margin-right: 24rpx;
  168. display: flex;
  169. }
  170. .link-text {
  171. font-size: 28rpx;
  172. }
  173. .navigation {
  174. width: 100%;
  175. display: flex;
  176. position: fixed;
  177. bottom: 0;
  178. padding: 0rpx 30rpx;
  179. border: solid 2rpx #f2f2f2;
  180. background-color: #ffffff;
  181. justify-content: space-between;
  182. .left {
  183. display: flex;
  184. font-size: 20rpx;
  185. .item {
  186. margin: 0 8rpx;
  187. }
  188. }
  189. .right {
  190. display: flex;
  191. font-size: 28rpx;
  192. align-items: center;
  193. }
  194. }
  195. .no-data {
  196. padding: 0rpx 30rpx;
  197. margin-top: 50%;
  198. .content {
  199. text-align: center;
  200. color: lightgray;
  201. margin-bottom: 30rpx;
  202. }
  203. }
  204. </style>