之前上个公司做过一个二维码付款功能,涉及到websocket功能,直接上代码

小程序onShow方法下加载:

/**  页面的初始数据 **/
data: {
code: "",
onshowCode: "",
hiden: true,
show: false,
array: {},
has_password: "",
openid: "",
showPop: false,
pwd: "",
num: "",
cashierid: "",
socketOpen: false
},
onShow: function() {
var _this = this;
a.get("ipass.get_user_qr", {}, function(e) {
//获取到openID(传的参数)
console.log(e);
var openid = e.openid;
console.log(openid);
_this.setData({
array: e,
openid: openid
});
var token = _this.data.openid;
var socketOpen = false;
var data = { type: "ready", uid: token }; // 给websocket传值
var socketMsgQueue = JSON.stringify(data); // 将值转化为字符串
// 连接websocket
wx.connectSocket({
url: "wss://paytest.kuaiyunma.com", //websocket 地址
success(e) {
console.log(e);
}
});
// 监听WebSocket 连接打开事件
wx.onSocketOpen(function(res) {
console.log("123");
_this.data.socketOpen = true;
console.log("数据发送中" + socketMsgQueue);
_this.sendSocketMessage(socketMsgQueue); // 自定义方法
});
// 监听WebSocket 错误事件
wx.onSocketError(function(res) {
console.log("WebSocket连接打开失败,请检查!");
});
// 监听WebSocket 接受到服务器的消息事件
wx.onSocketMessage(function(res) {
console.log(JSON.parse(res.data));
//获取到websocket返回的值
var res = JSON.parse(res.data);
//将值转化为对象
var num = res.fee;
//定义传过来的数值
var cashierid = res.cashierid;
// 根据传的值进行判断
if (res.type == "password") {
_this.setData({
showPop: true,
num: num,
res: res,
cashierid: cashierid
});
} else if (res.type == "pay_result") {
if (res.result == "ok") {
a.post(
"ipass.pay",
{
cashierid: _this.data.cashierid,
money: _this.data.num
},
function(e) {
console.log(e);
if (e.result.success === true) {
wx.showToast({
title: "支付成功!",
icon: "success"
});
} else {
wx.showToast({
title: "支付失败",
icon: "none"
});
}
_this.clearNum();
}
);
} else {
wx.showToast({
title: "支付失败",
icon: "none"
});
}
}
});
});
},
//设置公共传值的方法~~
sendSocketMessage: function(msg) {
var socketOpen = this.data.socketOpen;
if (socketOpen) {
wx.sendSocketMessage({
data: msg,
success(e) {
console.log(e);
}
});
}
},
inputPwd(e) {
console.log(e);
var pwd = e.target.dataset.value;
let rule = /[0-9]/;
if (!rule.test(pwd)) {
return;
}
pwd = this.data.pwd + pwd;
console.log(pwd);
this.setData({
pwd: pwd
});
var _this = this;
if (pwd.toString().length == 6) {
wx.showLoading({
title: "支付中...",
mask: true
});
console.log("成功");
a.post(
"ipass.password.auth",
{
password: _this.data.pwd
},
function(e) {
console.log(e);
wx.hideLoading();
// var socketOpen = true;
// 定义传的data值
var data1 = {
type: "validate_password",
token: _this.data.res.token,
cashierid: _this.data.res.cashierid,
password: _this.data.pwd
};
// 将转换的对象转换为字符串
var socketMsgQueue1 = JSON.stringify(data1);
if (!e.auth) {
_this.setData({
showPop: false,
pwd: ""
});
wx.showToast({
title: "支付密码错误",
icon: "none"
});
} else {
// 发送socket信息
_this.sendSocketMessage(socketMsgQueue1); _this.setData({
showPop: false,
pwd: ""
});
}
}
);
setTimeout(() => {
wx.hideLoading({
success: function() {
_this.setData({
showPop: false,
hasPaid: true
});
}
});
}, 2000);
}
},

最新文章

  1. SQL初级
  2. 浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真
  3. 剑指架构师系列-InnoDB存储引擎、Spring事务与缓存
  4. Unity 中 使用c#线程
  5. winform中利用反射实现泛型数据访问对象基类(3)
  6. log4net--帮助程序员将日志信息输出到各种目标(控制台、文件、数据库等)的工具
  7. Android:布局实例之模仿微信Tab
  8. AndroidStudio KeyMap
  9. 09_Java8操作集合的一些新特性
  10. JS实现各种页面的刷新
  11. iOS定位功能
  12. 关于Visual Studio调试 无效指针提示
  13. 1-SDK开发初探-8266
  14. python之MySQL MySQLdb 推荐使用姿势,解决中文乱码
  15. JDK1.8 StampedLock: 解决ReentrantReadWriteLock在读多写少情况下,写线程饥饿问题
  16. mac系统 pip3 install scrapy 失败 No local packages or working download links found for incremental>=16.10.1
  17. 「TJOI2015」线性代数 解题报告
  18. javap的使用
  19. flask 模板block super()的讲解
  20. 汇合confluence

热门文章

  1. 动态代理模拟实现aop
  2. MyBatis if标签的用法
  3. 【DFS练习】Pku1950 Dessert-C++
  4. Mysql常用语法及入门开篇(一)
  5. SpringBoot热部署报错(BeanCreationException: Error creating bean with name 'classPathFileSystemWatcher' d)
  6. c语言进阶14-线性表之链表
  7. C#3.0新增功能09 LINQ 基础05 使用 LINQ 进行数据转换
  8. Java提供的几种线程池
  9. mysql8.0忘记密码如何操作?
  10. JavaScript ES6 Promiss对象