Commit 541ee81300b745dbe3d345519017955e75d7eadd
1 parent
6325dd28
mp:前端接入发送消息
Showing
6 changed files
with
59 additions
and
59 deletions
yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/message/MpMessageController.java
| ... | ... | @@ -37,10 +37,11 @@ public class MpMessageController { |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | @PostMapping("/send") |
| 40 | -// @ApiOperation("获得粉丝消息表分页") | |
| 41 | -// @PreAuthorize("@ss.hasPermission('mp:message:query')") | |
| 42 | - public CommonResult<Long> sendMessage(@Valid @RequestBody MpMessageSendReqVO reqVO) { | |
| 43 | - return success(mpMessageService.sendKefuMessage(reqVO)); | |
| 40 | + @ApiOperation("给粉丝发送消息") | |
| 41 | + @PreAuthorize("@ss.hasPermission('mp:message:send')") | |
| 42 | + public CommonResult<MpMessageRespVO> sendMessage(@Valid @RequestBody MpMessageSendReqVO reqVO) { | |
| 43 | + MpMessageDO message = mpMessageService.sendKefuMessage(reqVO); | |
| 44 | + return success(MpMessageConvert.INSTANCE.convert(message)); | |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | } | ... | ... |
yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/message/MpMessageService.java
| ... | ... | @@ -52,8 +52,8 @@ public interface MpMessageService { |
| 52 | 52 | * 注意,该方法会真实发送消息 |
| 53 | 53 | * |
| 54 | 54 | * @param sendReqVO 消息内容 |
| 55 | - * @return 消息编号 | |
| 55 | + * @return 消息 | |
| 56 | 56 | */ |
| 57 | - Long sendKefuMessage(MpMessageSendReqVO sendReqVO); | |
| 57 | + MpMessageDO sendKefuMessage(MpMessageSendReqVO sendReqVO); | |
| 58 | 58 | |
| 59 | 59 | } | ... | ... |
yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/message/MpMessageServiceImpl.java
| ... | ... | @@ -104,7 +104,7 @@ public class MpMessageServiceImpl implements MpMessageService { |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | @Override |
| 107 | - public Long sendKefuMessage(MpMessageSendReqVO sendReqVO) { | |
| 107 | + public MpMessageDO sendKefuMessage(MpMessageSendReqVO sendReqVO) { | |
| 108 | 108 | // 校验消息格式 |
| 109 | 109 | MpUtils.validateMessage(validator, sendReqVO.getType(), sendReqVO); |
| 110 | 110 | |
| ... | ... | @@ -116,18 +116,17 @@ public class MpMessageServiceImpl implements MpMessageService { |
| 116 | 116 | WxMpKefuMessage wxMessage = MpMessageConvert.INSTANCE.convert(sendReqVO, user); |
| 117 | 117 | WxMpService mpService = mpServiceFactory.getRequiredMpService(user.getAppId()); |
| 118 | 118 | try { |
| 119 | - boolean result = mpService.getKefuService().sendKefuMessage(wxMessage); | |
| 120 | - System.out.println(result); | |
| 119 | + mpService.getKefuService().sendKefuMessageWithResponse(wxMessage); | |
| 121 | 120 | } catch (WxErrorException e) { |
| 122 | 121 | throw new RuntimeException(e); |
| 123 | 122 | } |
| 124 | 123 | |
| 125 | 124 | // 记录消息 |
| 126 | 125 | MpMessageDO message = MpMessageConvert.INSTANCE.convert(wxMessage, account, user); |
| 127 | - message.setSendFrom(MpMessageSendFromEnum.USER_TO_MP.getFrom()); | |
| 126 | + message.setSendFrom(MpMessageSendFromEnum.MP_TO_USER.getFrom()); | |
| 128 | 127 | downloadMessageMedia(mpService, message); |
| 129 | 128 | mpMessageMapper.insert(message); |
| 130 | - return message.getId(); | |
| 129 | + return message; | |
| 131 | 130 | } |
| 132 | 131 | |
| 133 | 132 | /** | ... | ... |
yudao-ui-admin/src/api/mp/message.js
| 1 | 1 | import request from '@/utils/request' |
| 2 | 2 | |
| 3 | -// 创建粉丝消息表 | |
| 4 | -export function createMessage(data) { | |
| 5 | - return request({ | |
| 6 | - url: '/mp/message/create', | |
| 7 | - method: 'post', | |
| 8 | - data: data | |
| 9 | - }) | |
| 10 | -} | |
| 11 | - | |
| 12 | -// 获得粉丝消息表 | |
| 13 | -export function getMessage(id) { | |
| 14 | - return request({ | |
| 15 | - url: '/mp/message/get?id=' + id, | |
| 16 | - method: 'get' | |
| 17 | - }) | |
| 18 | -} | |
| 19 | - | |
| 20 | -// 获得粉丝消息表 分页 | |
| 3 | +// 获得粉丝消息表分页 | |
| 21 | 4 | export function getMessagePage(query) { |
| 22 | 5 | return request({ |
| 23 | 6 | url: '/mp/message/page', |
| ... | ... | @@ -25,3 +8,12 @@ export function getMessagePage(query) { |
| 25 | 8 | params: query |
| 26 | 9 | }) |
| 27 | 10 | } |
| 11 | + | |
| 12 | +// 给粉丝发送消息 | |
| 13 | +export function sendMessage(data) { | |
| 14 | + return request({ | |
| 15 | + url: '/mp/message/send', | |
| 16 | + method: 'post', | |
| 17 | + data: data | |
| 18 | + }) | |
| 19 | +} | ... | ... |
yudao-ui-admin/src/views/mp/components/wx-msg/main.vue
| ... | ... | @@ -88,7 +88,7 @@ |
| 88 | 88 | </template> |
| 89 | 89 | |
| 90 | 90 | <script> |
| 91 | - import { getMessagePage } from '@/api/mp/message' | |
| 91 | +import {getMessagePage, sendMessage} from '@/api/mp/message' | |
| 92 | 92 | import WxReplySelect from '@/views/mp/components/wx-reply/main.vue' |
| 93 | 93 | import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue'; |
| 94 | 94 | import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue'; |
| ... | ... | @@ -107,16 +107,14 @@ |
| 107 | 107 | WxMusic |
| 108 | 108 | }, |
| 109 | 109 | props: { |
| 110 | - wxUserId: { | |
| 111 | - type: String | |
| 110 | + userId: { | |
| 111 | + type: String, | |
| 112 | + required: true | |
| 112 | 113 | } |
| 113 | 114 | }, |
| 114 | 115 | data() { |
| 115 | 116 | return { |
| 116 | 117 | nowStr: new Date().getTime(), // 当前的时间戳,用于每次消息加载后,回到原位置;具体见 :id="'msg-div' + nowStr" 处 |
| 117 | - objData:{ | |
| 118 | - repType: 'text' | |
| 119 | - }, | |
| 120 | 118 | sendLoading: false, // 发送消息是否加载中 |
| 121 | 119 | tableLoading: false, // 消息列表是否正在加载中 |
| 122 | 120 | loadMore: true, // 是否可以加载更多 |
| ... | ... | @@ -133,6 +131,9 @@ |
| 133 | 131 | nickname: '公众号', |
| 134 | 132 | avatar: require("@/assets/images/wechat.png"), |
| 135 | 133 | }, |
| 134 | + objData:{ // 微信发送消息 | |
| 135 | + repType: 'text' | |
| 136 | + }, | |
| 136 | 137 | } |
| 137 | 138 | }, |
| 138 | 139 | created() { |
| ... | ... | @@ -140,30 +141,37 @@ |
| 140 | 141 | }, |
| 141 | 142 | methods:{ |
| 142 | 143 | sendMsg(){ |
| 143 | - if (this.objData) { | |
| 144 | - if(this.objData.repType === 'news'){ | |
| 145 | - this.objData.content.articles = [this.objData.content.articles[0]] | |
| 146 | - this.$message({ | |
| 147 | - showClose: true, | |
| 148 | - message: '图文消息条数限制在1条以内,已默认发送第一条', | |
| 149 | - type: 'success' | |
| 150 | - }) | |
| 151 | - } | |
| 152 | - this.sendLoading = true | |
| 153 | - addObj(Object.assign({ | |
| 154 | - wxUserId: this.wxUserId | |
| 155 | - },this.objData)).then(data => { | |
| 156 | - this.sendLoading = false | |
| 157 | - data = data.data | |
| 158 | - this.tableData = [...this.tableData , ...[data] ] | |
| 159 | - this.scrollToBottom() | |
| 160 | - this.objData = { | |
| 161 | - repType: 'text' | |
| 162 | - } | |
| 163 | - }).catch(() => { | |
| 164 | - this.sendLoading = false | |
| 144 | + if (!this.objData) { | |
| 145 | + return; | |
| 146 | + } | |
| 147 | + if (this.objData.repType === 'news') { | |
| 148 | + this.objData.content.articles = [this.objData.content.articles[0]] | |
| 149 | + this.$message({ | |
| 150 | + showClose: true, | |
| 151 | + message: '图文消息条数限制在1条以内,已默认发送第一条', | |
| 152 | + type: 'success' | |
| 165 | 153 | }) |
| 166 | 154 | } |
| 155 | + this.sendLoading = true | |
| 156 | + sendMessage(Object.assign({ | |
| 157 | + userId: this.userId | |
| 158 | + }, { | |
| 159 | + ...this.objData, | |
| 160 | + type: this.objData.repType, | |
| 161 | + content: this.objData.repContent, | |
| 162 | + // TODO 芋艿:临时适配,保证可用 | |
| 163 | + })).then(response => { | |
| 164 | + this.sendLoading = false | |
| 165 | + // 添加到消息列表,并滚动 | |
| 166 | + this.tableData = [...this.tableData , ...[response.data] ] | |
| 167 | + this.scrollToBottom() | |
| 168 | + // 重置 objData 状态 | |
| 169 | + this.objData = { | |
| 170 | + repType: 'text' | |
| 171 | + } | |
| 172 | + }).catch(() => { | |
| 173 | + this.sendLoading = false | |
| 174 | + }) | |
| 167 | 175 | }, |
| 168 | 176 | loadingMore() { |
| 169 | 177 | this.page.pageNo++ |
| ... | ... | @@ -174,7 +182,7 @@ |
| 174 | 182 | getMessagePage(Object.assign({ |
| 175 | 183 | pageNo: page.pageNo, |
| 176 | 184 | pageSize: page.pageSize, |
| 177 | - wxUserId: this.wxUserId | |
| 185 | + userId: this.userId | |
| 178 | 186 | }, params)).then(response => { |
| 179 | 187 | // 计算当前的滚动高度 |
| 180 | 188 | const msgDiv = document.getElementById('msg-div' + this.nowStr); | ... | ... |
yudao-ui-admin/src/views/mp/message/index.vue