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

339 lines
8.5 KiB

  1. <template>
  2. <view id="favorite-index">
  3. <checkbox-group change="select">
  4. <view class="num-box">
  5. <view class="goods">您收藏了 <text class="num">{{total}}</text> 个商品</view>
  6. <view class="operation" @tap="editF">{{edit ? '完成' : '编辑'}}</view>
  7. </view>
  8. <view class="goods-list">
  9. <view v-for="(item, idx) in dataList" :key="idx">
  10. <view class="goods-item mx-1px-bottom" v-if="!item.remove" v-for="(item, index) in dataList[idx]" :key="index">
  11. <view class="img-box">
  12. <checkbox :hidden="!edit" color="#fff" @tap="fClick" :checked="item.isCheck" :data-ischecked="item.isCheck" :data-findex="idx" :data-index="index" :data-value="item.favoriteable_id" :value="item.favoriteable_id"></checkbox>
  13. <image :src="item.favoriteable.img" :data-id="item.favoriteable_id" @tap="jump"></image>
  14. </view>
  15. <view class="text" :data-id="item.favoriteable.id" @tap="jump">
  16. <view class="names">{{item.favoriteable.name}}</view>
  17. <view class="money">¥{{item.favoriteable.sell_price}}</view>
  18. </view>
  19. </view>
  20. </view>
  21. <view class="loadingbox" :hidden="!show">
  22. 正在加载下一页数据
  23. </view>
  24. </view>
  25. </checkbox-group>
  26. <view class="cancel mx-1px-top" v-if="edit">
  27. <view class="checkbox">
  28. <label>
  29. <checkbox @tap="click" color="#fff" :checked="allCheck"></checkbox> 全选
  30. </label>
  31. </view>
  32. <view class="cancel-button" :style="'background: ' + config.mainColor" @tap="cancel">
  33. 取消收藏
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  40. export default {
  41. data() {
  42. return {
  43. page: 1,
  44. show: false,
  45. edit: false,
  46. meta: '',
  47. total: 0,
  48. dataList: [],
  49. checkList: [],
  50. allCheck: false,
  51. config: ''
  52. };
  53. },
  54. onReachBottom() {
  55. var hasMore = this.meta.pagination.total_pages > this.meta.pagination.current_page;
  56. if (hasMore) {
  57. this.setData({
  58. show: true
  59. });
  60. var page = this.meta.pagination.current_page + 1;
  61. this.queryFavoriteList(0, page);
  62. } else {
  63. wx.showToast({
  64. image: '../../../static/error.png',
  65. title: '再拉也没有啦'
  66. });
  67. }
  68. },
  69. onShow() {
  70. this.queryFavoriteList(); // let app =getApp();
  71. // app.isBirthday().then(()=>{
  72. // if(this.$cookieStorage.get("birthday_gift")){
  73. // var giftData=this.$cookieStorage.get("birthday_gift").data;
  74. // new app.ToastPannel().__page.showText(giftData);
  75. // }
  76. // });
  77. },
  78. onLoad() {
  79. // 第三方平台配置颜色
  80. var config = this.$cookieStorage.get('globalConfig') || '';
  81. this.setData({
  82. config: config
  83. });
  84. },
  85. components: {},
  86. props: {},
  87. methods: {
  88. editF() {
  89. this.setData({
  90. edit: !this.edit
  91. });
  92. },
  93. select(e) {
  94. var value = e.detail.value;
  95. var newList = [];
  96. if (value.length == this.total) {
  97. this.setData({
  98. allCheck: true
  99. });
  100. } else {
  101. this.setData({
  102. allCheck: false
  103. });
  104. }
  105. value.forEach(v => {
  106. v = parseInt(v);
  107. newList.push(v);
  108. });
  109. this.setData({
  110. checkList: newList
  111. });
  112. },
  113. click() {
  114. var newList = this.dataList;
  115. if (this.allCheck) {
  116. newList.forEach(v => {
  117. v.forEach(i => {
  118. i.isCheck = false;
  119. });
  120. });
  121. this.setData({
  122. checkList: []
  123. });
  124. } else {
  125. var checkList = [];
  126. newList.forEach(v => {
  127. v.forEach(i => {
  128. i.isCheck = true;
  129. });
  130. });
  131. newList.forEach(v => {
  132. v.forEach(i => {
  133. checkList.push(i.favoriteable_id);
  134. });
  135. });
  136. this.setData({
  137. checkList: checkList
  138. });
  139. }
  140. this.setData({
  141. dataList: newList,
  142. allCheck: !this.allCheck
  143. });
  144. },
  145. fClick(e) {
  146. var index = e.currentTarget.dataset.index;
  147. var value = e.currentTarget.dataset.value;
  148. var fIndex = e.currentTarget.dataset.findex;
  149. var isCheck = e.currentTarget.dataset.ischecked;
  150. var list = `dataList[${fIndex}]`;
  151. // this.setData({
  152. // [`${list}[${index}].isCheck`]: !this.dataList[fIndex][index].isCheck
  153. // });
  154. this.list[index].isCheck=!this.dataList[fIndex][index].isCheck;
  155. },
  156. cancel() {
  157. var data = {
  158. checkList: this.checkList,
  159. dataList: this.dataList
  160. };
  161. if (this.checkList.length === 0) {
  162. wx.showModal({
  163. title: '',
  164. content: '请选择你要删除的收藏',
  165. showCancel: false
  166. });
  167. } else {
  168. wx.showModal({
  169. title: '',
  170. content: '是否取消收藏',
  171. success: res => {
  172. if (res.confirm) {
  173. this.removeBatchFavorite(data);
  174. }
  175. }
  176. });
  177. }
  178. },
  179. jump(e) {
  180. var id = e.currentTarget.dataset.id;
  181. wx.navigateTo({
  182. url: '/pages/store/detail/detail?id=' + id
  183. });
  184. },
  185. // 查询收藏列表
  186. queryFavoriteList(status = 0, page = 1) {
  187. var token = this.$cookieStorage.get('user_token');
  188. this.$http.get({
  189. api: 'api/favorite',
  190. header: {
  191. Authorization: token
  192. },
  193. data: {
  194. page: page
  195. }
  196. }).then(res => {
  197. if (res.statusCode == 200) {
  198. res = res.data;
  199. if (res.status) {
  200. var pages = res.meta.pagination;
  201. var total = res.meta.pagination.total;
  202. res.data.forEach(v => {
  203. v.remove = false;
  204. v.isCheck = false;
  205. });
  206. this.setData({
  207. total: total,
  208. //[`dataList[${page - 1}]`]: res.data,
  209. meta: res.meta
  210. });
  211. this.$set(this.dataList, page -1, res.data);
  212. } else {
  213. wx.showModal({
  214. title: '',
  215. content: res.message,
  216. showCancel: false
  217. });
  218. }
  219. } else {
  220. wx.showModal({
  221. title: '',
  222. content: "请求失败",
  223. showCancel: false
  224. });
  225. }
  226. this.setData({
  227. show: false
  228. });
  229. }).catch(rej => {
  230. this.setData({
  231. show: false
  232. });
  233. });
  234. },
  235. // 批量删除收藏
  236. removeBatchFavorite(data) {
  237. var token = this.$cookieStorage.get('user_token');
  238. this.$http.post({
  239. api: 'api/favorite/delFavs',
  240. data: {
  241. favoriteable_id: data.checkList,
  242. favoriteable_type: 'goods'
  243. },
  244. header: {
  245. Authorization: token
  246. }
  247. }).then(res => {
  248. if (res.statusCode == 200) {
  249. res = res.data;
  250. if (res.status) {
  251. var dataList = data.dataList;
  252. var checkList = data.checkList;
  253. dataList.forEach((v, idx) => {
  254. v.forEach((i, index) => {
  255. if (checkList.indexOf(i.favoriteable_id) != -1) {
  256. // this.setData({
  257. // [`dataList[${idx}][${index}].remove`]: true
  258. // });
  259. //
  260. this.dataList[idx][index].remove=true;
  261. }
  262. });
  263. });
  264. this.queryFavoriteList();
  265. this.setData({
  266. checkList: []
  267. });
  268. } else {
  269. wx.showModal({
  270. title: '',
  271. content: res.message,
  272. showCancel: false
  273. });
  274. }
  275. } else {
  276. wx.showModal({
  277. title: '',
  278. content: "请求失败",
  279. showCancel: false
  280. });
  281. }
  282. });
  283. },
  284. setData: function (obj) {
  285. let that = this;
  286. let keys = [];
  287. let val, data;
  288. Object.keys(obj).forEach(function (key) {
  289. keys = key.split('.');
  290. val = obj[key];
  291. data = that.$data;
  292. keys.forEach(function (key2, index) {
  293. if (index + 1 == keys.length) {
  294. that.$set(data, key2, val);
  295. } else {
  296. if (!data[key2]) {
  297. that.$set(data, key2, {});
  298. }
  299. }
  300. data = data[key2];
  301. });
  302. });
  303. }
  304. },
  305. computed: {},
  306. watch: {}
  307. };
  308. </script>
  309. <style rel="stylesheet/less" lang="less">
  310. @import "index";
  311. </style>