参考文章:php 使用redis锁限制并发访问类
var util = require("../../utils/util.js");
var app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
},
//初始化点击次数变量
shezhi: 0,
//去支付
onShoppinglist: function (e) {
var that = this;
if (that.shezhi == 1) {
return;
}
that.shezhi = 1;
wx.showLoading({
title: '请稍等',
mask: true,
})
//发送请求执行demo.php,请求的数据返回orderPayCallback函数
},
//初始化访问链接次数
Loadtime: 0,
//下单回调
orderPayCallback: function (that, data) {
//如果当前的锁没有释放
if (data.msg == 'is_lock') {
//累加回调onShoppinglist次数
that.Loadtime++;
//如果当前回调的次数大于等于5次,返回上一页,否则初始化点击次数变量,重新发起去支付onShoppinglist
if (that.Loadtime >= 5) {
wx.showModal({
title: '提示',
content: '当前商品下单人数过多,请稍后再试!',
showCancel: false,
success: function (res) {
if (res.confirm) { //这里是点击了确定以后
wx.navigateBack({
delta: 1
})
}
}
})
return;
} else {
//初始化点击次数变量,重新发起去支付onShoppinglist
that.shezhi = 0;
that.onShoppinglist();
return;
}
}
var shopDanhao = data.shopDanhao;
wx.showLoading({
title: '请稍等',
mask: true
})
//请求执行pay.php,请求微信支付接口,返回接口回调paySuccessCallback
},
//支付回调
paySuccessCallback: function (that, data) {
//console.log(data);
if (data.msg == "yuePaySuccess") {
wx.hideLoading();
wx.setStorageSync('tempSuccessGoUrl', '/pages/member/member-order/member-order?admin=20');
wx.showModal({
title: '提示',
content: "支付成功",
showCancel: false,
complete: function () {
wx.switchTab({
url: '/pages/user/user'
})
}
})
return;
}
// 发起微信支付
wx.requestPayment({
'appId': data.subjects.appId,
'timeStamp': data.subjects.timeStamp,
'nonceStr': data.subjects.nonceStr,
'package': data.subjects.package,
'signType': data.subjects.signType,
'paySign': data.subjects.paySign,
'success': function (res) {
//console.log(res)
wx.hideLoading();
wx.setStorageSync('tempSuccessGoUrl', '/pages/member/member-order/member-order?admin=20');
wx.showModal({
title: '提示',
content: '支付成功',
showCancel: false,
complete: function () {
wx.switchTab({
url: '/pages/user/user'
})
}
})
},
'fail': function (res) {
//console.log(res)
wx.hideLoading();
wx.setStorageSync('tempSuccessGoUrl', '/pages/member/member-order/member-order?admin=10');
wx.showModal({
title: '提示',
content: '支付失败\r\n您可以选择重新付款',
showCancel: false,
complete: function () {
wx.switchTab({
url: '/pages/user/user'
})
}
})
}
});
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
})
demo.php文件
<?php
// 创建redislock对象
$RedisLock = new RedisLock(array(
'host' => '127.0.0.1',
'port' => 6379,
'index' => 0,
'auth' => '',
'timeout' => 1,
'reserved' => NULL,
'retry_interval' => 100,
));
// 定义锁标识
$RedisLock_key = 'lock_order_' . $detailId . '';
// 获取锁
$is_lock = $RedisLock->lock($RedisLock_key, 20);
if (!$is_lock) {
$infoArray = array();
$infoArray["code"] = 200;
$infoArray["msg"] = "is_lock";
$infoArray["rm_id"] = $rm["id"];
$json = json_encode($infoArray, true);
echo $json;
exit;
}
//生成订单号(自己的生成订单代码逻辑)
//释放锁
$RedisLock->unlock($RedisLock_key);
pay.php
微信小程序接口基本类文档下载链接? 提取码为:wgtp
注意:微信支付需要生成对应的验证文件后才可以正常使用,人多的话,在重更上一篇微信支付配置的文章
<?
//微信支付
$qwb_danhao = "wxapp" . $rsDG["danhao"];//wxpc wxmp wxapp
$qwb_jine = $ShopJine;
$qwb_jine = $qwb_jine * 100;
include_once("../wxpay/WxPayPubHelper/WxPayPubHelper.php");
$jsApi = new JsApi_pub();//实例化微信接口
$openid = $rm["WXopenid"];//用户openid
$unifiedOrder = new UnifiedOrder_pub();
$unifiedOrder->setParameter("openid", $openid);
$unifiedOrder->setParameter("body", ""); //商品描述
$unifiedOrder->setParameter("out_trade_no", $qwb_danhao);//支付单号
$unifiedOrder->setParameter("total_fee", $qwb_jine);//支付金额
$unifiedOrder->setParameter("notify_url", WxPayConf_pub::NOTIFY_URL);
$unifiedOrder->setParameter("trade_type", "JSAPI");
$prepay_id = $unifiedOrder->getPrepayId();
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();
//合并数组反馈到小程序
$payArray = json_decode($jsApiParameters, true);
$infoArray["subjects"] = array_merge($infoArray["subjects"], $payArray);
|