收藏
回答

你是否也遇到小程序卡卷签名错误,求解决~

微信小程序卡卷签名错误无法找到原因,希望这方面的大佬能正解!万分感谢~

最近开发微信小程序的时候遇到卡卷签名错误的问题,因为我是前端人员,不太懂后台,在网上各种找结果还是签名错误,让我很头痛,希望小程序这方面的大佬能正解一下,在下感激不尽!

后台已经成功获得签名并校验(这里有个小问题就是签名校验排序问题,我在我电脑测试的时候api_ticket与timestamp没有进行交换位置所以签名校验(地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=cardsign)不一样,但是他们电脑出现位置交换(timestamp在第一个)),我们没有自定义code没有指定openid,所以这两个参数都没有用到!

后台代码:

<?php
define("appID", $_POST["appid"]);
define("appsecret", $_POST["secret"]);
 
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
  $this->appId = $appId;
  $this->appSecret = $appSecret;
}
public function getSignPackage() {
    //获取access_token
        $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
        $json=file_get_contents($TOKEN_URL);
        $result=json_decode($json,true);
        $ACCESS_TOKEN=$result['access_token'];
        //echo $ACCESS_TOKEN.'<br /><br />';
    //jsapiTicket
        $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$ACCESS_TOKEN&type=wx_card";
        $json=file_get_contents($TOKEN_URL);
        $result=json_decode($json,true);
        $api_ticket=$result['ticket'];
        //echo $jsapi_ticket.'<br /><br />';
    //$jsapi_ticket = $this->getJsApiTicket();
  //$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ;
    $card_id = $_POST["card_id"] ;
  $timestamp = time();
  $nonceStr = $this->createNonceStr();
  //$string = "api_ticket=$api_ticket×tamp=$timestamp&nonce_str=$nonceStr&card_id=$card_id" ;
  //$string = $timestamp.$nonceStr.$api_ticket.$card_id ;
  //echo '<br /><br />';
 $arr = array( $card_id,$api_ticket,$nonceStr,$timestamp);
 asort($arr, SORT_STRING);
 $string = "";
 foreach($arr as $temp){
  $string = $string.$temp;
 }
$signature = sha1($string);
//$signature = sha1($sortString );
 //$signature = sha1($string );
  $signPackage = array(
    "appId"     => $this->appId,
    "nonceStr"  => $nonceStr,
    "timestamp" => $timestamp,
    "card_id" => $card_id,
    "signature" => $signature,
    "rawString" => $string,
    "api_ticket" => $api_ticket
  );
  return $signPackage;
}
private function createNonceStr($length = 16) {
  $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  $str = "";
  for ($i = 0; $i < $length; $i++) {
    $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  }
  return $str;
}
/*public function httpGet($url) {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  curl_setopt($curl, CURLOPT_URL, $url);
  $res = curl_exec($curl);
  curl_close($curl);
  return $res;
}*/
 }
$jssdk=new JSSDK(appID,appsecret);
$signPackage = $jssdk->GetSignPackage();
  //echo $signPackage["signature"];
  $res = "<signPackage><card_id>".$signPackage["card_id"]."</card_id><timestamp>".$signPackage["timestamp"]."</timestamp><nonceStr>".$signPackage["nonceStr"]."</nonceStr><signature>".$signPackage["signature"]."</signature><api_ticket>".$signPackage["api_ticket"]."</api_ticket></signPackage>";
  /*function arrayToXml( $data, $wrap= 'xml' ){
    $str = "<{$wrap}>";
    if( is_array( $data ) ){
         if( hasIndex( $data ) ){       
            foreach( $data as $k=>$v ){
                 $str .= arrayToXml( $v, $k );
            }
         }else{
            foreach( $data as $v ){
                foreach( $v as $k1=>$v1 ) 
                $str .= arrayToXml( $v1, $k1 );
            }
         }
    }else
        $str .= $data;
    $str .= "</{$wrap}>";
    return $str;
  }
  $res = $this->arrayToXml($signPackage);
  header("Content-type: text/xml");*/
  echo $res;
?>

前端代码:

onLoad:function(options){
 wx.login({
  success: res =>{
    var code = res.code;
    console.log(code);
    if (res.code) {
      //发起网络请求
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=小程序appid&secret=小程序密钥&js_code=' + code + '&grant_type=authorization_code',
          method: 'GET',
          data:{
            'appid': "小程序appid",
            'secret': "小程序密钥",
            "code": res.code
          },
          header:{
            'content-type': 'application/json'
          },//以上获取的code与openid只是为了后面的,其实现在并不用到
          success:function(res){
            console.log(res);
            var openid = res.data.openid //返回openid
            console.log(openid);
            
            wx.request({
              url: 'https://www.dgsy888888.com/DGKJNEW/sc/sign3.php?appid="公众号appid"&secret="公众号密钥"',
              method:'POST',
              data:{                   
                "appid":"公众号appid", //获取access_token时,使用的appid是公众号的而非小程序的
                "secret":"公众号密钥"
              },
              header:{
               //'Content-type': 'application/json'
               'Content-Type': 'application/x-www-form-urlencoded'
              },
              success:function(res){
                var strmobile = res.data;
                console.log(strmobile);
                var XMLParser = new Parser.DOMParser() //后台传来的是XML格式,这里就是解析
                var doc = XMLParser.parseFromString(strmobile)
                var timestamp = doc.getElementsByTagName('timestamp')['0'];
    
                var nonceStr = doc.getElementsByTagName('nonceStr')['0'];
             
                var signature = doc.getElementsByTagName('signature')['0'];
               
                var api_ticket = doc.getElementsByTagName('api_ticket')['0'];
                //strmobile = [timestamp.firstChild.data, nonceStr.firstChild.data, signature.firstChild.data,api_ticket.firstChild.data]
                //console.log(strmobile)
                
                //添加卡卷
                wx.addCard({
                  cardList:[{
                    cardId: '我们现在用的是固定cardid,这里我写死了',
                    cardExt:'{"timestamp":"'+timestamp.firstChild.data+'","api_ticket":"'+api_ticket.firstChild.data+'","nonceStr":"'+nonceStr.firstChild.data+'","signature":"'+signature.firstChild.data+'"}'
                  }],
                  success:function(res){
                    console.log(cardList)//卡券添加结果
                    console.log("你成功领取会员卡")
                  },
                  fail: function (res){
                    console.log("失败");
                  },
                  complete: function (res){
                    console.log("成功失败我都会执行");
                  }
                })
              }
            })
          }
        })
    }else{
      console.log('获取用户登录态失败!'+res.errMsg);
    }
  }
})
}

希望大佬们出出主意帮帮我这个还没有入道的小辈,在下一鞠躬!
我的联系方式
WX:wj2578025916
邮箱:2578025916@qq.com

回答关注问题邀请回答
收藏

2 个回答

  • 稻草人
    稻草人
    2019-01-13

    微信支付栏目无法发帖。。。。

    2019-01-13
    有用
    回复
  • 灵芝
    灵芝
    2019-01-03

    支付相关问题请移步微信支付专区提问,感谢配合

    2019-01-03
    有用
    回复 4
    • 稻草人
      稻草人
      2019-01-13

      别人问的是卡券,跟支付有关系吗?

      2019-01-13
      回复
    • 灵芝
      灵芝
      2019-01-14回复稻草人

      卡券也是在微信支付专区提问,感谢配合

      2019-01-14
      回复
    • 稻草人
      稻草人
      2019-01-14回复灵芝

      我也知道配合,可是微信支付哪里发不了贴。。。。。

      2019-01-14
      回复
    • 灵芝
      灵芝
      2019-01-14回复稻草人

      发不了帖具体表现是什么?

      2019-01-14
      回复
登录 后发表内容