1. 客户申请会员卡 . 用户填写 姓名 性别 生日 电话 等信息 ,提交到后台 ,后台管理员根据信息分配code(卡号)给用户。
2. 客户根据code,执行领取激活操作
代码如下:
前端:
<a class="layui-btn" onclick="check()" style="background: #5999FC">领取会员卡</a>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
function check() {
wx.config({
debug: false,
appId: "{$signature['appId']}",
timestamp: "{$signature['timestamp']}",
nonceStr: "{$signature['nonceStr']}",
signature: "{$signature['signature']}",
jsApiList: [
'addCard'
]
});
wx.ready(function(){
//添加卡券
//document.querySelector('#addCard').onclick = function () {
wx.addCard({
cardList: [
{
cardId: "{$signature['card_id']}",
cardExt: '{"code":"{$signature['code']}","openid":"{$signature['openid']}","timestamp":"{$signature['timestamp']}","nonce_str":"{$signature['nonceStr']}","signature":"{$signature['signature']}"}'
}
],
success: function (res) {
$.ajax({
url: "{:cmf_url('portal/card/edit')}",
type: 'GET',
success: function (res) {
if(res.code == 1){
layer.alert(res.msg,{closeBtn: 0,title: ''}, function () {
window.parent.location = res.url;
})
}else{
layer.alert(res.msg,{closeBtn: 0,title: ''})
}
}
});
},
cancel: function (res) {
alert(JSON.stringify(res))
}
});
//};
});
}
后台:
public function myCards($id='')
{
$user = session('user');
$card = Db::name('card')->where(['user_id'=>$user['id']])->find();
$u = Db::name('user')->where(['id'=>$user['id']])->field('score')->find();
$level = Db::name('card_level')->order('score ASC')->select();
foreach ($level as $k => $v) {
if ($u['score']<$v['score']) {
$lv = $level[$k-1]['name'];
break;
}
}
$api_ticket = $this->getJsapiTicket();
$noncestr = $this->getRandomStrings();
$timestamp = time();
$code = $card['number'];
$openid = $card['openid'];
$card_id = $card['card_id'];
$arr = array($api_ticket,$timestamp,$noncestr,$card_id,$code,$openid);
sort($arr,SORT_STRING);
$signature = sha1(implode($arr));
$signPackage = array(
'appId' => $this->appid,
'nonceStr' => $noncestr,
'timestamp' => $timestamp,
'signature' => $signature,
'api_ticket'=> $api_ticket,
'openid' => $openid,
'card_id' => $card_id,
'code' => $code
);
$this->assign('signature',$signPackage);
$this->assign('lv',$lv);
$this->assign('art',empty($card)?'':$card);
return $this->fetch(':my_card');
}
#1、获取access_token access_token 的有效时间是7200s,故可以采用文件存储的方法进行保存,避免多次请求;
public function getAccessToken(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
$res = cmf_curl_get($url);
$res = json_decode($res,1);
return $res;
}
#2、获取jsapi_ticket
public function getJsapiTicket() {
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token['access_token']}&type=wx_card";
$re = cmf_curl_get($url);
$arr = json_decode($re,true);
$jsapi_ticket = $arr['ticket'];
return $jsapi_ticket;
}
# 3.生成签名
public function getSignpackage(){
$user = session('user');
$jsapi_ticket = $this->getJsapiTicket(); // 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$noncestr = $this->getRandomStrings();
$timestamp = time();
$string1 = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url;
$signature = sha1($string1);
$signPackage = array(
'appId' => $this->appid,
'nonceStr' => $noncestr,
'timestamp' => $timestamp,
'signature' => $signature,
'status' => 1 //1代表没有存在卡,0代表存在卡
);
return json_encode($signPackage,true);
}
// 生成n位随机码
function getRandomStrings()
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand(10000000*(double)microtime());
for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < 16; $i++) {
$str .= $chars[mt_rand(0, $lc)];
}
return $str;
}
card表: