Authored by 邱骏

点赞功能

@@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
22 <img :src="item.authorHeadIco"> 22 <img :src="item.authorHeadIco">
23 </div> 23 </div>
24 <p class="user-name">{{item.authorName}}</p> 24 <p class="user-name">{{item.authorName}}</p>
25 - <div class="zan-container"> 25 + <div class="zan-container" :data-praised="item.hasPraised" :data-index="index" :data-id="item.articleId" @click="updatePraise">
26 <i :class="[item.hasPraised === 'Y' ? 'iconzan-fill' : 'iconzan_n', 'iconfont', 'btn-zan']"></i> 26 <i :class="[item.hasPraised === 'Y' ? 'iconzan-fill' : 'iconzan_n', 'iconfont', 'btn-zan']"></i>
27 <p class="zan-count">{{item.praiseCount ? item.praiseCount : '赞'}}</p> 27 <p class="zan-count">{{item.praiseCount ? item.praiseCount : '赞'}}</p>
28 </div> 28 </div>
@@ -45,11 +45,11 @@ export default { @@ -45,11 +45,11 @@ export default {
45 name: 'waterFallList', 45 name: 'waterFallList',
46 components: {WaterFall}, 46 components: {WaterFall},
47 props: { 47 props: {
48 - listData: { 48 + listData: { // 列表数据
49 type: Array, 49 type: Array,
50 default: [] 50 default: []
51 }, 51 },
52 - listInfo: { 52 + listInfo: { // 列表基础信息
53 type: Object, 53 type: Object,
54 default: {} 54 default: {}
55 }, 55 },
@@ -61,7 +61,7 @@ export default { @@ -61,7 +61,7 @@ export default {
61 type: Number, 61 type: Number,
62 default: 18 62 default: 18
63 }, 63 },
64 - isLoading: { 64 + isLoading: { // 是否正在调用列表接口
65 type: Boolean, 65 type: Boolean,
66 default: false 66 default: false
67 } 67 }
@@ -69,9 +69,10 @@ export default { @@ -69,9 +69,10 @@ export default {
69 data() { 69 data() {
70 return { 70 return {
71 col: 2, 71 col: 2,
72 - scrollTop: 0,  
73 - isLogin: false,  
74 - loadText: '' 72 + scrollTop: 0, // 页面滚动高度
  73 + isLogin: false, // 是否登录
  74 + loadText: '', // 加载新列表提示文字
  75 + isUpdatingPraise: false, // 是否正在调用点赞接口
75 }; 76 };
76 }, 77 },
77 computed: { 78 computed: {
@@ -104,17 +105,15 @@ export default { @@ -104,17 +105,15 @@ export default {
104 this.isLogin = val; 105 this.isLogin = val;
105 }, 106 },
106 isLoading(newVal) { 107 isLoading(newVal) {
107 - console.log('isLoading', newVal);  
108 if (newVal === true) { 108 if (newVal === true) {
109 this.loadText = '加载中...'; 109 this.loadText = '加载中...';
110 } else { 110 } else {
111 this.loadText = ''; 111 this.loadText = '';
112 } 112 }
113 - console.log(this.loadText);  
114 } 113 }
115 }, 114 },
116 methods: { 115 methods: {
117 - ...mapActions(['articleAddLike']), 116 + ...mapActions(['updateArticlePraise']),
118 loadMore() { 117 loadMore() {
119 this.$emit('load-more'); 118 this.$emit('load-more');
120 }, 119 },
@@ -141,6 +140,16 @@ export default { @@ -141,6 +140,16 @@ export default {
141 } 140 }
142 }); 141 });
143 } 142 }
  143 + },
  144 + updatePraise(e) {
  145 + let articleId = e.currentTarget.dataset.id;
  146 + let index = e.currentTarget.dataset.index;
  147 + let status = e.currentTarget.dataset.praised === 'Y' ? 1 : 0;
  148 +
  149 + console.log(articleId,index, status);
  150 + this.updateArticlePraise({articleId, status, index}).then(result => {
  151 + console.log(result);
  152 + });
144 } 153 }
145 } 154 }
146 }; 155 };
@@ -45,7 +45,18 @@ export default { @@ -45,7 +45,18 @@ export default {
45 commit(Types.FETCH_ARTICLE_LIST_FAILED); 45 commit(Types.FETCH_ARTICLE_LIST_FAILED);
46 } 46 }
47 }, 47 },
48 - async articleAddLike({commit}, {articleId}) {  
49 48
  49 + // 点赞
  50 + async updateArticlePraise({commit}, {articleId, status, index}) {
  51 + let result = await this.$api.get('/api/grass/updateArticlePraise', {
  52 + articleId,
  53 + status
  54 + });
  55 +
  56 + if (result.code === 200) {
  57 + commit(Types.UPDATE_ARTICLE_PRAISE, {articleId, status, index});
  58 + }
  59 +
  60 + return result;
50 } 61 }
51 }; 62 };
@@ -23,5 +23,25 @@ export default { @@ -23,5 +23,25 @@ export default {
23 } 23 }
24 delete result.data.list; 24 delete result.data.list;
25 state.articleListInfo = result.data; 25 state.articleListInfo = result.data;
  26 + },
  27 +
  28 + // 更新点赞
  29 + [Types.UPDATE_ARTICLE_PRAISE](state, {articleId, status, index}) {
  30 + let article = state.articleList[index];
  31 +
  32 + if (article.articleId === parseInt(articleId, 10)) {
  33 + if (article.praiseCount === 0 && status === 0) {
  34 + article.praiseCount += 1;
  35 + article.hasPraised = 'Y';
  36 + } else if (article.praiseCount > 0) {
  37 + if (status === 0) {
  38 + article.praiseCount += 1;
  39 + article.hasPraised = 'Y';
  40 + } else {
  41 + article.praiseCount -= 1;
  42 + article.hasPraised = 'N';
  43 + }
  44 + }
  45 + }
26 } 46 }
27 }; 47 };
1 export const FETCH_ARTICLE_LIST_SUCCESS = 'FETCH_ARTICLE_LIST_SUCCESS'; 1 export const FETCH_ARTICLE_LIST_SUCCESS = 'FETCH_ARTICLE_LIST_SUCCESS';
2 export const FETCH_ARTICLE_LIST_REQUEST = 'FETCH_ARTICLE_LIST_REQUEST'; 2 export const FETCH_ARTICLE_LIST_REQUEST = 'FETCH_ARTICLE_LIST_REQUEST';
3 export const FETCH_ARTICLE_LIST_FAILED = 'FETCH_ARTICLE_LIST_FAILED'; 3 export const FETCH_ARTICLE_LIST_FAILED = 'FETCH_ARTICLE_LIST_FAILED';
  4 +export const UPDATE_ARTICLE_PRAISE = 'UPDATE_ARTICLE_PRAISE';