|
@@ -112,156 +112,156 @@ import { getMessagePage, sendMessage } from '@/api/mp/message'
|
|
|
import WxMusic from '@/views/mp/components/wx-music/main.vue';
|
|
|
import { getUser } from "@/api/mp/user";
|
|
|
|
|
|
- export default {
|
|
|
- name: "wxMsg",
|
|
|
- components: {
|
|
|
- WxReplySelect,
|
|
|
- WxVideoPlayer,
|
|
|
- WxVoicePlayer,
|
|
|
- WxNews,
|
|
|
- WxLocation,
|
|
|
- WxMusic
|
|
|
+export default {
|
|
|
+ name: "wxMsg",
|
|
|
+ components: {
|
|
|
+ WxReplySelect,
|
|
|
+ WxVideoPlayer,
|
|
|
+ WxVoicePlayer,
|
|
|
+ WxNews,
|
|
|
+ WxLocation,
|
|
|
+ WxMusic
|
|
|
+ },
|
|
|
+ props: {
|
|
|
+ userId: {
|
|
|
+ type: Number,
|
|
|
+ required: true
|
|
|
},
|
|
|
- props: {
|
|
|
- userId: {
|
|
|
- type: Number,
|
|
|
- required: true
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ nowStr: new Date().getTime(), // 当前的时间戳,用于每次消息加载后,回到原位置;具体见 :id="'msg-div' + nowStr" 处
|
|
|
+ loading: false, // 消息列表是否正在加载中
|
|
|
+ loadMore: true, // 是否可以加载更多
|
|
|
+ list: [], // 消息列表
|
|
|
+ queryParams: {
|
|
|
+ pageNo: 1, // 当前页数
|
|
|
+ pageSize: 14, // 每页显示多少条
|
|
|
+ accountId: undefined,
|
|
|
},
|
|
|
- },
|
|
|
- data() {
|
|
|
- return {
|
|
|
- nowStr: new Date().getTime(), // 当前的时间戳,用于每次消息加载后,回到原位置;具体见 :id="'msg-div' + nowStr" 处
|
|
|
- loading: false, // 消息列表是否正在加载中
|
|
|
- loadMore: true, // 是否可以加载更多
|
|
|
- list: [], // 消息列表
|
|
|
- queryParams: {
|
|
|
- pageNo: 1, // 当前页数
|
|
|
- pageSize: 14, // 每页显示多少条
|
|
|
- accountId: undefined,
|
|
|
- },
|
|
|
- user: { // 由于微信不再提供昵称,直接使用“用户”展示
|
|
|
- nickname: '用户',
|
|
|
- avatar: require("@/assets/images/profile.jpg"),
|
|
|
- accountId: 0, // 公众号账号编号
|
|
|
- },
|
|
|
- mp: {
|
|
|
- nickname: '公众号',
|
|
|
- avatar: require("@/assets/images/wechat.png"),
|
|
|
- },
|
|
|
+ user: { // 由于微信不再提供昵称,直接使用“用户”展示
|
|
|
+ nickname: '用户',
|
|
|
+ avatar: require("@/assets/images/profile.jpg"),
|
|
|
+ accountId: 0, // 公众号账号编号
|
|
|
+ },
|
|
|
+ mp: {
|
|
|
+ nickname: '公众号',
|
|
|
+ avatar: require("@/assets/images/wechat.png"),
|
|
|
+ },
|
|
|
+
|
|
|
+ // ========= 消息发送 =========
|
|
|
+ sendLoading: false, // 发送消息是否加载中
|
|
|
+ objData: { // 微信发送消息
|
|
|
+ type: 'text',
|
|
|
+ },
|
|
|
+ }
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ // 获得用户信息
|
|
|
+ getUser(this.userId).then(response => {
|
|
|
+ this.user.nickname = response.data.nickname && response.data.nickname.length > 0 ?
|
|
|
+ response.data.nickname : this.user.nickname;
|
|
|
+ this.user.avatar = response.data.avatar && this.user.avatar.length > 0 ?
|
|
|
+ response.data.avatar : this.user.avatar;
|
|
|
+ this.user.accountId = response.data.accountId;
|
|
|
+ // 设置公众号账号编号
|
|
|
+ this.queryParams.accountId = response.data.accountId;
|
|
|
+ this.objData.accountId = response.data.accountId;
|
|
|
|
|
|
- // ========= 消息发送 =========
|
|
|
- sendLoading: false, // 发送消息是否加载中
|
|
|
- objData: { // 微信发送消息
|
|
|
- type: 'text',
|
|
|
- },
|
|
|
+ // 加载消息
|
|
|
+ console.log(this.queryParams)
|
|
|
+ this.refreshChange()
|
|
|
+ })
|
|
|
+ },
|
|
|
+ methods:{
|
|
|
+ sendMsg(){
|
|
|
+ if (!this.objData) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 公众号限制:客服消息,公众号只允许发送一条
|
|
|
+ if (this.objData.type === 'news'
|
|
|
+ && this.objData.articles.length > 1) {
|
|
|
+ this.objData.articles = [this.objData.articles[0]]
|
|
|
+ this.$message({
|
|
|
+ showClose: true,
|
|
|
+ message: '图文消息条数限制在 1 条以内,已默认发送第一条',
|
|
|
+ type: 'success'
|
|
|
+ })
|
|
|
}
|
|
|
- },
|
|
|
- created() {
|
|
|
- // 获得用户信息
|
|
|
- getUser(this.userId).then(response => {
|
|
|
- this.user.nickname = response.data.nickname && response.data.nickname.length > 0 ?
|
|
|
- response.data.nickname : this.user.nickname;
|
|
|
- this.user.avatar = response.data.avatar && this.user.avatar.length > 0 ?
|
|
|
- response.data.avatar : this.user.avatar;
|
|
|
- this.user.accountId = response.data.accountId;
|
|
|
- // 设置公众号账号编号
|
|
|
- this.queryParams.accountId = response.data.accountId;
|
|
|
- this.objData.accountId = response.data.accountId;
|
|
|
|
|
|
- // 加载消息
|
|
|
- console.log(this.queryParams)
|
|
|
- this.refreshChange()
|
|
|
+ // 执行发送
|
|
|
+ this.sendLoading = true
|
|
|
+ sendMessage(Object.assign({
|
|
|
+ userId: this.userId
|
|
|
+ }, {
|
|
|
+ ...this.objData
|
|
|
+ })).then(response => {
|
|
|
+ this.sendLoading = false
|
|
|
+ // 添加到消息列表,并滚动
|
|
|
+ this.list = [...this.list , ...[response.data] ]
|
|
|
+ this.scrollToBottom()
|
|
|
+ // 重置 objData 状态
|
|
|
+ this.$refs['replySelect'].deleteObj(); // 重置,避免 tab 的数据未清理
|
|
|
+ }).catch(() => {
|
|
|
+ this.sendLoading = false
|
|
|
})
|
|
|
},
|
|
|
- methods:{
|
|
|
- sendMsg(){
|
|
|
- if (!this.objData) {
|
|
|
- return;
|
|
|
+ loadingMore() {
|
|
|
+ this.queryParams.pageNo++
|
|
|
+ this.getPage(this.queryParams)
|
|
|
+ },
|
|
|
+ getPage(page, params) {
|
|
|
+ this.loading = true
|
|
|
+ getMessagePage(Object.assign({
|
|
|
+ pageNo: page.pageNo,
|
|
|
+ pageSize: page.pageSize,
|
|
|
+ userId: this.userId,
|
|
|
+ accountId: page.accountId,
|
|
|
+ }, params)).then(response => {
|
|
|
+ // 计算当前的滚动高度
|
|
|
+ const msgDiv = document.getElementById('msg-div' + this.nowStr);
|
|
|
+ let scrollHeight = 0
|
|
|
+ if(msgDiv){
|
|
|
+ scrollHeight = msgDiv.scrollHeight
|
|
|
}
|
|
|
- // 公众号限制:客服消息,公众号只允许发送一条
|
|
|
- if (this.objData.type === 'news'
|
|
|
- && this.objData.articles.length > 1) {
|
|
|
- this.objData.articles = [this.objData.articles[0]]
|
|
|
- this.$message({
|
|
|
- showClose: true,
|
|
|
- message: '图文消息条数限制在 1 条以内,已默认发送第一条',
|
|
|
- type: 'success'
|
|
|
- })
|
|
|
+
|
|
|
+ // 处理数据
|
|
|
+ const data = response.data.list.reverse();
|
|
|
+ this.list = [...data, ...this.list]
|
|
|
+ this.loading = false
|
|
|
+ if (data.length < this.queryParams.pageSize || data.length === 0){
|
|
|
+ this.loadMore = false
|
|
|
}
|
|
|
+ this.queryParams.pageNo = page.pageNo
|
|
|
+ this.queryParams.pageSize = page.pageSize
|
|
|
|
|
|
- // 执行发送
|
|
|
- this.sendLoading = true
|
|
|
- sendMessage(Object.assign({
|
|
|
- userId: this.userId
|
|
|
- }, {
|
|
|
- ...this.objData
|
|
|
- })).then(response => {
|
|
|
- this.sendLoading = false
|
|
|
- // 添加到消息列表,并滚动
|
|
|
- this.list = [...this.list , ...[response.data] ]
|
|
|
+ // 滚动到原来的位置
|
|
|
+ if(this.queryParams.pageNo === 1) { // 定位到消息底部
|
|
|
this.scrollToBottom()
|
|
|
- // 重置 objData 状态
|
|
|
- this.$refs['replySelect'].deleteObj(); // 重置,避免 tab 的数据未清理
|
|
|
- }).catch(() => {
|
|
|
- this.sendLoading = false
|
|
|
- })
|
|
|
- },
|
|
|
- loadingMore() {
|
|
|
- this.queryParams.pageNo++
|
|
|
- this.getPage(this.queryParams)
|
|
|
- },
|
|
|
- getPage(page, params) {
|
|
|
- this.loading = true
|
|
|
- getMessagePage(Object.assign({
|
|
|
- pageNo: page.pageNo,
|
|
|
- pageSize: page.pageSize,
|
|
|
- userId: this.userId,
|
|
|
- accountId: page.accountId,
|
|
|
- }, params)).then(response => {
|
|
|
- // 计算当前的滚动高度
|
|
|
- const msgDiv = document.getElementById('msg-div' + this.nowStr);
|
|
|
- let scrollHeight = 0
|
|
|
- if(msgDiv){
|
|
|
- scrollHeight = msgDiv.scrollHeight
|
|
|
- }
|
|
|
-
|
|
|
- // 处理数据
|
|
|
- const data = response.data.list.reverse();
|
|
|
- this.list = [...data, ...this.list]
|
|
|
- this.loading = false
|
|
|
- if (data.length < this.queryParams.pageSize || data.length === 0){
|
|
|
- this.loadMore = false
|
|
|
- }
|
|
|
- this.queryParams.pageNo = page.pageNo
|
|
|
- this.queryParams.pageSize = page.pageSize
|
|
|
-
|
|
|
- // 滚动到原来的位置
|
|
|
- if(this.queryParams.pageNo === 1) { // 定位到消息底部
|
|
|
- this.scrollToBottom()
|
|
|
- } else if (data.length !== 0) { // 定位滚动条
|
|
|
- this.$nextTick(() => {
|
|
|
- if (scrollHeight !== 0){
|
|
|
- msgDiv.scrollTop = document.getElementById('msg-div'+this.nowStr).scrollHeight - scrollHeight - 100
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
- },
|
|
|
- /**
|
|
|
- * 刷新回调
|
|
|
- */
|
|
|
- refreshChange() {
|
|
|
- this.getPage(this.queryParams)
|
|
|
- },
|
|
|
- /** 定位到消息底部 */
|
|
|
- scrollToBottom: function () {
|
|
|
- this.$nextTick(() => {
|
|
|
- let div = document.getElementById('msg-div' + this.nowStr)
|
|
|
- div.scrollTop = div.scrollHeight
|
|
|
- })
|
|
|
- },
|
|
|
- }
|
|
|
- };
|
|
|
+ } else if (data.length !== 0) { // 定位滚动条
|
|
|
+ this.$nextTick(() => {
|
|
|
+ if (scrollHeight !== 0){
|
|
|
+ msgDiv.scrollTop = document.getElementById('msg-div'+this.nowStr).scrollHeight - scrollHeight - 100
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 刷新回调
|
|
|
+ */
|
|
|
+ refreshChange() {
|
|
|
+ this.getPage(this.queryParams)
|
|
|
+ },
|
|
|
+ /** 定位到消息底部 */
|
|
|
+ scrollToBottom: function () {
|
|
|
+ this.$nextTick(() => {
|
|
|
+ let div = document.getElementById('msg-div' + this.nowStr)
|
|
|
+ div.scrollTop = div.scrollHeight
|
|
|
+ })
|
|
|
+ },
|
|
|
+ }
|
|
|
+};
|
|
|
</script>
|
|
|
<style lang="scss" scoped>
|
|
|
/* 因为 joolun 实现依赖 avue 组件,该页面使用了 comment.scss、card.scc */
|