feat 添加商品评论对接

This commit is contained in:
lwh 2023-07-19 12:20:35 +08:00
parent 8f5533ef66
commit e7976cf7de
8 changed files with 135 additions and 122 deletions

View File

@ -5,9 +5,9 @@
wx:for-item="resource"
wx:key="*this"
>
<t-image wx:if="{{resource.type === 'image'}}" t-class="resource-item-{{classType}}" src="{{resource.src}}" />
<image wx:if="{{resource.type === 'image'}}" class="resource-item-{{classType}}" src="{{resource.src}}" />
<my-video wx:else videoSrc="{{resource.src}} " my-video="resource-item-{{classType}}">
<t-image t-class="resource-item resource-item-{{classType}}" slot="cover-img" src="{{resource.coverSrc}}" />
<image class="resource-item resource-item-{{classType}}" slot="cover-img" src="{{resource.coverSrc}}" />
<image class="play-icon" slot="play-icon" src="./assets/play.png" />
</my-video>
</view>

View File

@ -1,5 +1,9 @@
import { fetchComments } from '../../../services/comments/fetchComments';
import { fetchCommentsCount } from '../../../services/comments/fetchCommentsCount';
import {
fetchComments
} from '~/services/comments/fetchComments';
import {
fetchCommentsCount
} from '~/services/comments/fetchCommentsCount';
import dayjs from 'dayjs';
const layoutMap = {
0: 'vertical',
@ -17,11 +21,11 @@ Page({
layoutText: layoutMap[0],
loadMoreStatus: 0,
myLoadStatus: 0,
spuId: '1060004',
spuId: '',
commentLevel: '',
hasImage: '',
commentType: '',
totalCount: 0,
totalNum: 0,
countObj: {
badCount: '0',
commentCount: '0',
@ -37,14 +41,7 @@ Page({
},
async getCount(options) {
try {
const result = await fetchCommentsCount(
{
spuId: options.spuId,
},
{
method: 'POST',
},
);
const result = await fetchCommentsCount(options.spuId);
this.setData({
countObj: result,
});
@ -64,25 +61,31 @@ Page({
} catch (error) {}
},
generalQueryData(reset) {
const { hasImage, pageNum, pageSize, spuId, commentLevel } = this.data;
const {
hasImage,
pageNum,
pageSize,
spuId,
commentLevel
} = this.data;
const params = {
pageNum: 1,
pageSize: 30,
queryParameter: {
spuId,
},
pageSize: 10,
spuId: spuId,
commentLevel: 0,
hasImage: false
};
if (
Number(commentLevel) === 3 ||
Number(commentLevel) === 2 ||
Number(commentLevel) === 1
) {
params.queryParameter.commentLevel = Number(commentLevel);
params.commentLevel = Number(commentLevel);
}
if (hasImage && hasImage === '1') {
params.queryParameter.hasImage = true;
params.hasImage = true;
} else {
delete params.queryParameter.hasImage;
delete params.hasImage;
}
// 重置请求
if (reset) return params;
@ -94,7 +97,10 @@ Page({
};
},
async init(reset = true) {
const { loadMoreStatus, commentList = [] } = this.data;
const {
loadMoreStatus,
commentList = []
} = this.data;
const params = this.generalQueryData(reset);
// 在加载中或者无更多数据,直接返回
@ -106,34 +112,37 @@ Page({
try {
const data = await fetchComments(params, {
method: 'POST',
method: 'GET',
});
const code = 'SUCCESS';
if (code.toUpperCase() === 'SUCCESS') {
const { pageList, totalCount = 0 } = data;
pageList.forEach((item) => {
const {
result,
totalNum = 0
} = data;
result.forEach((item) => {
// eslint-disable-next-line no-param-reassign
item.commentTime = dayjs(Number(item.commentTime)).format(
'YYYY/MM/DD HH:mm',
);
// item.commentTime = dayjs(Number(item.commentTime)).format(
// 'YYYY/MM/DD HH:mm',
// );
});
if (Number(totalCount) === 0 && reset) {
if (Number(totalNum) === 0 && reset) {
this.setData({
commentList: [],
hasLoaded: true,
total: totalCount,
total: totalNum,
loadMoreStatus: 2,
});
return;
}
const _commentList = reset ? pageList : commentList.concat(pageList);
const _commentList = reset ? result : commentList.concat(result);
const _loadMoreStatus =
_commentList.length === Number(totalCount) ? 2 : 0;
_commentList.length === Number(totalNum) ? 2 : 0;
this.setData({
commentList: _commentList,
pageNum: params.pageNum || 1,
totalCount: Number(totalCount),
totalNum: Number(totalNum),
loadMoreStatus: _loadMoreStatus,
});
} else {
@ -158,7 +167,9 @@ Page({
return array;
},
getComments(options) {
const { commentLevel = -1, spuId, hasImage = '' } = options;
const {
commentLevel = -1, spuId, hasImage = ''
} = options;
if (commentLevel !== -1) {
this.setData({
commentLevel: commentLevel,
@ -172,8 +183,12 @@ Page({
this.init(true);
},
changeTag(e) {
var { commenttype } = e.currentTarget.dataset;
var { commentType } = this.data;
var {
commenttype
} = e.currentTarget.dataset;
var {
commentType
} = this.data;
if (commentType === commenttype) return;
this.setData({
loadMoreStatus: 0,
@ -214,7 +229,9 @@ Page({
}
},
onReachBottom() {
const { total = 0, commentList } = this.data;
const {
total = 0, commentList
} = this.data;
if (commentList.length === total) {
this.setData({
loadMoreStatus: 2,

View File

@ -2,24 +2,24 @@
<t-tag t-class="comments-header-tag {{commentType === '' ? 'comments-header-active' : ''}}" data-commentType="" bindtap="changeTag">
全部({{countObj.commentCount}})
</t-tag>
<t-tag
<!-- <t-tag
t-class="comments-header-tag {{commentType === '5' ? 'comments-header-active' : ''}}"
wx:if="{{countObj.uidCount !== '0'}}"
data-commentType="5"
bindtap="changeTag"
>
自己({{countObj.uidCount}})
</t-tag>
</t-tag> -->
<t-tag t-class="comments-header-tag {{commentType === '4' ? 'comments-header-active' : ''}}" data-commentType="4" bindtap="changeTag">
带图({{countObj.hasImageCount}})
</t-tag>
<t-tag t-class="comments-header-tag {{commentType === '3' ? 'comments-header-active' : ''}}" data-commentType="3" bindtap="changeTag">
<t-tag t-class="comments-header-tag {{commentType === '1' ? 'comments-header-active' : ''}}" data-commentType="1" bindtap="changeTag">
好评({{countObj.goodCount}})
</t-tag>
<t-tag t-class="comments-header-tag {{commentType === '2' ? 'comments-header-active' : ''}}" data-commentType="2" bindtap="changeTag">
中评({{countObj.middleCount}})
</t-tag>
<t-tag t-class="comments-header-tag {{commentType === '1' ? 'comments-header-active' : ''}}" data-commentType="1" bindtap="changeTag">
<t-tag t-class="comments-header-tag {{commentType === '3' ? 'comments-header-active' : ''}}" data-commentType="3" bindtap="changeTag">
差评({{countObj.badCount}})
</t-tag>
</view>

View File

@ -412,13 +412,13 @@ Page({
async getCommentsList() {
try {
const code = 'Success';
const data = await getGoodsDetailsCommentList();
const {
homePageComments
} = data;
const data = await getGoodsDetailsCommentList(this.data.spuId);
// const {
// homePageComments
// } = data;
if (code.toUpperCase() === 'SUCCESS') {
const nextState = {
commentsList: homePageComments.map((item) => {
commentsList: data.map((item) => {
return {
goodsSpu: item.spuId,
userName: item.userName || '',
@ -458,7 +458,7 @@ Page({
async getCommentsStatistics() {
try {
const code = 'Success';
const data = await getGoodsDetailsCommentsCount();
const data = await getGoodsDetailsCommentsCount(this.data.spuId);
if (code.toUpperCase() === 'SUCCESS') {
const {
badCount,
@ -497,7 +497,7 @@ Page({
const {
spuId
} = query;
// const spuId = "1673671239077597184";
// const spuId = "1680467515018448896";
this.setData({
spuId: spuId,

View File

@ -1,18 +1,23 @@
import { config } from '../../config/index';
/** 获取商品评论 */
function mockFetchComments(parmas) {
const { delay } = require('../_utils/delay');
const { getGoodsAllComments } = require('../../model/comments');
return delay().then(() => getGoodsAllComments(parmas));
}
import {
request
} from '../_utils/request';
/** 获取商品评论 */
export function fetchComments(parmas) {
if (config.useMock) {
return mockFetchComments(parmas);
return new Promise((resolve, reject) => {
request({
url: `GoodsCommentApi/getGoodsCommentList`,
data: parmas,
method: 'GET',
success: function (res) {
let data = res.data;
resolve(data);
},
fail: function (error) {
reject(error);
}
return new Promise((resolve) => {
resolve('real api');
});
});
}

View File

@ -1,18 +1,22 @@
import { config } from '../../config/index';
/** 获取商品评论数 */
function mockFetchCommentsCount(ID = 0) {
const { delay } = require('../_utils/delay');
const { getGoodsCommentsCount } = require('../../model/comments');
return delay().then(() => getGoodsCommentsCount(ID));
}
import {
request
} from '../_utils/request';
/** 获取商品评论数 */
export function fetchCommentsCount(ID = 0) {
if (config.useMock) {
return mockFetchCommentsCount(ID);
return new Promise((resolve, reject) => {
request({
url: `GoodsCommentApi/GetGoodsDetailsCommentsCount`,
data: {SpuId: ID},
method: 'GET',
success: function (res) {
let data = res.data;
resolve(data);
},
fail: function (error) {
reject(error);
}
return new Promise((resolve) => {
resolve('real api');
});
});
}

View File

@ -1,25 +1,6 @@
import {
config
} from '../../config/index';
import {
request
} from '../_utils/request';
/** 获取商品详情 */
// function mockFetchGood(ID = 0) {
// const { delay } = require('../_utils/delay');
// const { genGood } = require('../../model/good');
// return delay().then(() => genGood(ID));
// }
// /** 获取商品列表 */
// export function fetchGood(ID = 0) {
// if (config.useMock) {
// return mockFetchGood(ID);
// }
// return new Promise((resolve) => {
// resolve('real api');
// });
// }
/** 获取商品详情 */
export function fetchGood(ID) {

View File

@ -1,37 +1,43 @@
import { config } from '../../config/index';
import {
request
} from '../_utils/request';
/** 获取商品详情页评论数 */
function mockFetchGoodDetailsCommentsCount(spuId = 0) {
const { delay } = require('../_utils/delay');
const {
getGoodsDetailsCommentsCount,
} = require('../../model/detailsComments');
return delay().then(() => getGoodsDetailsCommentsCount(spuId));
}
export function getGoodsDetailsCommentsCount(spuId) {
return new Promise((resolve, reject) => {
request({
url: `GoodsCommentApi/GetGoodsDetailsCommentsCount`,
data: {SpuId: spuId},
method: 'GET',
success: function (res) {
let data = res.data;
/** 获取商品详情页评论数 */
export function getGoodsDetailsCommentsCount(spuId = 0) {
if (config.useMock) {
return mockFetchGoodDetailsCommentsCount(spuId);
resolve(data);
},
fail: function (error) {
reject(error);
}
return new Promise((resolve) => {
resolve('real api');
});
});
}
/** 获取商品详情页评论 */
function mockFetchGoodDetailsCommentList(spuId = 0) {
const { delay } = require('../_utils/delay');
const { getGoodsDetailsComments } = require('../../model/detailsComments');
return delay().then(() => getGoodsDetailsComments(spuId));
}
export function getGoodsDetailsCommentList(spuId) {
return new Promise((resolve, reject) => {
request({
url: `GoodsCommentApi/getGoodsDetailsComments`,
data: {SpuId: spuId},
method: 'GET',
success: function (res) {
let data = res.data;
/** 获取商品详情页评论 */
export function getGoodsDetailsCommentList(spuId = 0) {
if (config.useMock) {
return mockFetchGoodDetailsCommentList(spuId);
resolve(data);
},
fail: function (error) {
reject(error);
}
return new Promise((resolve) => {
resolve('real api');
});
});
}