goEasy的简单使用

goEasy的简单使用

案列介绍:
在这里插入图片描述

  1. 前端配置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('删除失败');
        })
      }

    }
  }
  1. 后端推给前端消息(就是支付成功微信回调的时候将消息推送给前端)
    注意这里的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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱上编程2705

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值