大家好,欢迎来到IT知识分享网。
goEasy的简单使用
案列介绍:
- 前端配置goEasy(这里是配置vue项目)
在main.js中配置一个全局参数
因为前端只是为了接收消息,不需要推送消失所以我配置appkey是BS开头的那个
// //初始化
const goEasy = GoEasy.getInstance({
host: "hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey: "BS-********",
modules: ['pubsub'] //根据需要,传入‘pubsub’或'im’,或数组方式同时传入
});
Vue.prototype.goEasy = goEasy;
Vue.prototype.pubsub = goEasy.pubsub;
使用示例
export default {
beforeMount() {
var self = this;
//连接GoEasy
this.goEasy.connect({
data: {
},
onProgress: function(attempts) {
console.log("GoEasy is connecting" + attempts);
// self.unshiftMessage("GoEasy is connecting" + attempts);
},
onSuccess: function() {
// self.unshiftMessage("GoEasy connect successfully.");
console.log("GoEasy connect successfully.");
},
onFailed: function(error) {
console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error.content);
// self.unshiftMessage("Failed to connect GoEasy, code:" + error.code + ",error:" + error.content);
}
});
//接收消息
this.pubsub.subscribe({
channel: "WXPay",
onMessage: function(message) {
// self.unshiftMessage(message.content);
console.log("微信回调成功:"+message.content);
if(message.content=="支付成功"){
self.QRcodeUrl=imgUrl
}
},
onSuccess: function() {
// self.unshiftMessage('订阅成功.');
console.log("订阅成功");
},
onFailed: function(error) {
// self.unshiftMessage("订阅失败,错误编码:" + error.code + " 错误信息:" + error.content);
console.log("订阅失败,错误编码:"+error.code + " 错误信息:" + error.content);
}
});
},
components: {
Pagination
},
data() {
return {
imgUrl,
QRcodeUrl: '',
dialogVisible: false,
list: null,
listLoading: true,
total: 0,
goodsId: '',
listQuery: {
page: 1,
limit: 10
}
}
},
created() {
this.fetchData()
},
methods: {
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
})
.catch(_ => {
});
},
fetchData() {
var vm = this;
this.axios({
method: 'post',
url: 'http://localhost:7002/llcItems/page',
data: {
"goodsId": vm.goodsId.trim(),
"page": vm.listQuery.page,
"size": vm.listQuery.limit
}
}).then(function(response) {
console.log(response.data.data.list),
vm.list = response.data.data.list,
vm.total = response.data.data.total,
vm.listLoading = false
})
},
serchMsg() {
this.listQuery.page = 1
this.fetchData()
},
updateMsg(id) {
this.$router.push("/editproduct/" + id)
},
addMsg(id) {
this.$router.push("/addproduct")
},
// 获取二维码
getQRcode(id) {
var vm = this;
this.axios({
method: 'get',
url: 'http://localhost:7002/wx/wxpayOrder?goodsId=' + id,
responseType: 'blob', // 重点
}).then(res => {
// res.data是返回的文档流
const blob = new Blob([res.data]);
const url = window.URL.createObjectURL(blob);
vm.QRcodeUrl = url;
vm.dialogVisible = true
})
},
deleteMsg(id) {
var vm = this;
this.axios({
method: 'delete',
url: 'http://localhost:7002/llcItems/delete?goodsId=' + id,
}).then(function(res) {
if (res.data.data == true) {
vm.$message({
message: '删除成功',
type: 'success'
});
vm.fetchData();
}
}).catch(function(error) {
vm.$message.error('删除失败');
})
}
}
}
- 后端推给前端消息(就是支付成功微信回调的时候将消息推送给前端)
注意这里的channl是唯一通道所以请自行考虑,还是我配置的appkey是BC开头那个因为需要接受和推送消息。
<!--goeasy-->
<dependency>
<groupId>io.goeasy</groupId>
<artifactId>goeasy-sdk</artifactId>
<version>0.3.14</version>
</dependency>
@PostMapping("/wxpay")
@ApiOperation(value = "微信回调地址")
public Result<String> getPage(HttpServletRequest httpServletRequest) throws IOException {
Map<String, String> map = WXutil.parseParam(httpServletRequest);
System.out.println("参数解析:" + map);
PayCallBackInfo payCallBackInfo = this.checkSign(map, MCH_KEY);
if (null != payCallBackInfo && PayCallBackInfo.PAY_SUCCESS == payCallBackInfo.getStatus()) {
applicationContext.publishEvent(new PaySuccessEvent(this, payCallBackInfo));
} else {
applicationContext.publishEvent(new PayFailEvent(this, payCallBackInfo));
}
Document responseDocument = DocumentHelper.createDocument();
Element responseRootElement = responseDocument.addElement("xml");
responseRootElement.addElement("return_code").addCDATA("SUCCESS");
responseRootElement.addElement("return_msg").addCDATA("OK");
System.out.println("微信响应参数:" + responseDocument.asXML());
//更新订单状态
ThreadUtil.execAsync(()->llcOrderService.update(map.get("out_trade_no")));
//使用goEasy将支付成功的消息推送给前端
GoEasy goEasy=new GoEasy("http://rest-hangzhou.goeasy.io","BC-********");
goEasy.publish("WXPay","支付成功");
return new Result<>(responseDocument.asXML());
}
演示过程
vue项目参考demo
https://gitee.com/goeasy-io/GoEasyDemo-Vue-Helloworld
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/12255.html