...
|
...
|
@@ -211,100 +211,7 @@ class SalesmangoodsController extends WeChatBaseController{ |
|
|
|
|
|
}
|
|
|
|
|
|
// 获取签名
|
|
|
public function getSignPackage() {
|
|
|
// 获取token
|
|
|
$token = $this->getAccessToken();
|
|
|
// 获取ticket
|
|
|
$ticketList = $this->getJsApiTicket($token['accessToken']);
|
|
|
$ticket = $ticketList['ticket'];
|
|
|
// 该URL为使用JSSDK接口的URL
|
|
|
$url = 'http://xkeasy.w.bronet.cn/Salesmangoods/salesman_goods_add';
|
|
|
// 时间戳
|
|
|
$timestamp = time();
|
|
|
// 随机字符串
|
|
|
$nonceStr = $this->createNoncestr();
|
|
|
// 这里参数的顺序要按照 key 值 ASCII 码升序排序 j -> n -> t -> u
|
|
|
$string = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
|
|
|
$signature = sha1($string);
|
|
|
$signPackage = array (
|
|
|
"appId" => "wxdf34ec2179e19b9a",
|
|
|
"nonceStr" => $nonceStr,
|
|
|
"timestamp" => $timestamp,
|
|
|
"url" => $url,
|
|
|
"signature" => $signature,
|
|
|
"rawString" => $string,
|
|
|
"ticket" => $ticket,
|
|
|
"token" => $token['accessToken']
|
|
|
);
|
|
|
// 提供数据给前端
|
|
|
return json_encode($signPackage);
|
|
|
}
|
|
|
|
|
|
public function getAccessToken() {
|
|
|
// access_token 应该全局存储与更新
|
|
|
// 获取数据库中的access_token
|
|
|
$token = Db::name('token') -> where('id',1) -> find();
|
|
|
// accessToken过期或不存在时
|
|
|
if($token['time'] + $token['expiresIn'] < time() || $token['accessToken'] == NULL){
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="."wxdf34ec2179e19b9a"."&secret="."133ced06db2a45843ab52abc691ee82d";
|
|
|
// 微信返回的信息
|
|
|
$returnData = json_decode($this->httpGet($url));
|
|
|
// 组装数据
|
|
|
$resData['accessToken'] = $returnData->access_token;
|
|
|
$resData['expiresIn'] = $returnData->expires_in;
|
|
|
$resData['time'] = time();
|
|
|
// 把数据存进数据库
|
|
|
Db::name('token') -> where('id',1) -> update($resData);
|
|
|
$res = $resData;
|
|
|
}else{
|
|
|
$res = $token;
|
|
|
}
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
public function getJsApiTicket($accessToken) {
|
|
|
// jsapi_ticket 应该全局存储与更新
|
|
|
// 获取数据库中的jsapi_ticket
|
|
|
$ticket = Db::name('token') -> where('id',2) -> find();
|
|
|
// 如果ticket失效
|
|
|
if($ticket['time'] + $ticket['expiresIn'] < time() || $ticket['ticket'] == NULL){
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$accessToken&&type=jsapi";
|
|
|
// 微信返回的信息
|
|
|
$returnData = json_decode($this->httpGet($url));
|
|
|
// 组装数据
|
|
|
$resData['ticket'] = $returnData->ticket;
|
|
|
$resData['expiresIn'] = $returnData ->expires_in;
|
|
|
$resData['time'] = time();
|
|
|
// 把数据存进数据库
|
|
|
Db::name('token') -> where('id',2) -> update($resData);
|
|
|
$res = $resData;
|
|
|
}else{
|
|
|
$res = $ticket;
|
|
|
}
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
// 创建随机字符串
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
private 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;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
...
|
...
|
|