完成微信小程序卡券投放功能(签名错误)

先说明一下我是用mpvue来写的小程序,但是其实和小程序直接写也差不多,然后后端是使用php(直接面向过程原生写的)。

前端时间接到了一个微信小程序的项目,基本就是卡券投放的功能。之前虽然对小程序接触的不是特别特别深入,但是微信的各种文档好歹还是看过的。之前一直很推崇微信的开发者文档,毕竟是国人的做出来的东西,很多东西很符合国人看文档的习惯,而且对初级开发者很友好了,很多地方又很像vue react之类的,看起来还是非常轻松的。

但是!!这一次卡券投放功能差点就快写自闭了,文档乱七八糟,官方给的很多链接打开却是另一篇文档,看了几十篇文档和别人的文章之后才搞懂一二。

拿到需求,首先先去看微信官方文档https://developers.weixin.qq.com/miniprogram/dev/api/wx.addCard.html

嗯前端就这一个东西,按照官方给的格式书写就好,重点是这里的签名signature,先完成后端然后再完善前端代码

简述一下签名产生流程:

1.使用创建卡券公众号的appid和secret获取access_token(注意是公众号的appid和secret,而不是小程序的),因为access_token每天调用次数有限每次时效2小时,请务必使用某种方法去全局保存access_token

2.使用获取到的access_token去换取ticket(这个ticket是用来参与生成签名的也是有调用次数限制和时效2小时,务必全局保存)

3.按照字典序排序以下参数   必填参数:ticket(步骤2中换取的) timestamp(当前时间戳)nonce_str(不超过32位的随机字符串) card_id(要投放的卡券的卡券id)  选填参数:code(自定义code模式下填写) openid(指定领取者的情况下填写)

4.将3.中字典序排序完成之后的参数进行字符串拼接然后使用sha1加密,即为前端需要用到的signature

 

第一步后端代码如下

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . "";

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);

$result = json_decode($result);

这样便获取到了access_token,保存在$result中

第二步后端代码如下

//$access即为access_token

$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='. $access .'&type=wx_card';

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);

$result = json_decode($result);

此时的$result即为使用access_token换取到的ticket

第三步与第四步后端代码如下

$strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";

$nonce=substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),32);

$ticket = $_GET['ticket'];

$cardid = $_GET['cardid'];

$time = time();

$arr['timestamp'] = $time;

$arr['ticket'] = $ticket;

$arr['nonce_str'] = $nonce;

$arr['card_id'] = $cardid;

sort($arr,SORT_STRING);

$str = '';

foreach ($arr as $v) {

    $str .= $v;

}

$sign = sha1($str);

此时的$sign即为前端投放卡券所需要的signature

切记!这里参与签名所使用到的timestamp nonce_str card_id 也要和signature一起返回给前端

前端代码如下:

wx.addCard({

cardList: [

{

cardId:''+this.wxcard_id +'',

            cardExt:'{"timestamp": "' + that.time1 +'","nonce_str":"'+ that.nonce +'","signature":"'+ that.signature +'"}'

        }

],

})

这里的timestamp nonce_str cardId即为后端生成签名的时候所使用到的参数

然后就可以正常投放卡券了,我被 签名错误 这个问题卡了好久,下面讲一下可能出现签名错误的几种情况和解决方法

微信提供的卡券接口

这里就有一个小坑了,点进去微信卡券接口文档有一句话是这样的

2.3. 以小程序AppId调用添加/查看卡券JS-API,签名参数与公众号Addcard/Opencard JS-SDK一致;

大家看见JS-API,可能就会先获取access-token然后换取jsticket就会使用下面这个url

 https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='. $access .'&type=jsapi

但是注意!!微信卡券的ticket与正常js_api不同,这里一定要把url后的type改成wx_card

 

然后给大家一个测试卡券签名是否正确的链接吧,大家可以测试一下签名错误的时候是否是由于生成签名错误导致

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=cardsign

如果这个url下生成的签名和你后端生成的签名一模一样,且参与签名的ticket没有过期,那就是前端的问题了

如果后端参与签名的有 timestamp nonce_str openid 那前端caedExt中也需要有这几个参数,换句话说就是有几个参数在后端参与了生成签名,那就得在前端也得写入。基本就是这样了!good luck!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于微信卡券的开发,需要先进行微信公众号或小程序的授权认证,获取到相关的 API 信息。然后根据不同的卡券类型,进行相应的开发。 下面以微信小程序为例,介绍一下卡券的开发流程: 1. 配置小程序的接口权限,包括卡券相关的接口权限。 2. 创建卡券,可以在微信公众平台或小程序后台进行创建。创建时需要填写卡券的基本信息,包括卡券的类型、商户信息等。 3. 使用 API 接口进行卡券的管理,包括卡券的发放、核销等。需要对 API 进行相应的封装,方便开发人员进行调用。 4. 在小程序中展示卡券,可以通过卡券列表、卡券详情页等方式进行展示。 这里提供一个简单的 PHP 微信卡券 demo,供参考: ```php <?php // 设置公众号或小程序的 appid 和 appsecret $appid = 'your_appid'; $appsecret = 'your_appsecret'; // 获取 access_token $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; $response = file_get_contents($url); $result = json_decode($response, true); $access_token = $result['access_token']; // 创建卡券 $url = 'https://api.weixin.qq.com/card/create?access_token='.$access_token; $data = array( 'card' => array( 'card_type' => 'GROUPON', 'groupon' => array( 'base_info' => array( 'logo_url' => 'http://mmbiz.qpic.cn/mmbiz_jpg/xxxxxxxx/0', 'brand_name' => '测试商户', 'code_type' => 'CODE_TYPE_QRCODE', 'title' => '测试优惠券', 'sub_title' => '测试商户', 'color' => 'Color010', 'notice' => '测试优惠券', 'service_phone' => '18888888888', 'description' => '测试优惠券', 'date_info' => array( 'type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => strtotime('2021-01-01'), 'end_timestamp' => strtotime('2021-12-31') ), 'sku' => array( 'quantity' => 1000000 ), 'get_limit' => 1, 'use_custom_code' => false, 'bind_openid' => false, 'can_share' => true, 'can_give_friend' => true, 'location_id_list' => array(), 'url_name_type' => 'URL_NAME_TYPE_RESERVATION', 'custom_url' => 'http://www.xxx.com', 'source' => '测试商户' ), 'deal_detail' => '测试详情' ) ) ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); $card_id = $result['card_id']; // 发放卡券 $url = 'https://api.weixin.qq.com/card/groupon/add?access_token='.$access_token; $data = array( 'card_id' => $card_id, 'quantity' => 10 ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); echo $result['errcode'] == 0 ? '发放成功' : '发放失败'; // 核销卡券 $url = 'https://api.weixin.qq.com/card/code/consume?access_token='.$access_token; $data = array( 'code' => 'xxxxxxxx', 'card_id' => $card_id ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); echo $result['errcode'] == 0 ? '核销成功' : '核销失败'; // 查询卡券详情 $url = 'https://api.weixin.qq.com/card/get?access_token='.$access_token; $data = array( 'card_id' => $card_id ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); print_r($result); function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) )); $response = curl_exec($ch); curl_close($ch); return $response; } ?> ``` 需要注意的是,此 demo 中的 access_token 获取方式不够安全,建议使用官方 SDK 或者其他安全的方式进行获取。另外,卡券的创建、发放、核销等操作需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值