123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use think\Cache;
- use think\Config;
- /**
- * 微信js接口
- * Class WeChat
- * @package app\expand\controller
- */
- class WechatJs extends BaseAuth
- {
- private $_Account = null;
- public function __construct(){
- parent::__construct();
- $this->_Account = $this->getKey($this->_apiCode);
- //验证是否具有访问这个接口的权限
- if(!isset($this->_Account['Wechat_js_appId'])
- || !isset($this->_Account['Wechat_js_appsecret'])){
- HelperService::returnJson(['code'=>400,'msg'=>'WECHAT js interface unauthorized access','data'=>[]]);
- }
- Config::set('WECHAT_APPID',$this->_Account['Wechat_js_appId']);
- Config::set('WECHAT_APPSECRET',$this->_Account['Wechat_js_appsecret']);
- }
- /**
- * 获取微信js-config
- */
- public function getConfigJs(){
- $cacheTicket = Cache::get('jsapi_'.$this->_apiCode);
- if(!empty($cacheTicket)){
- HelperService::returnJson(['code'=>200,'msg'=>'cache','data'=>"$cacheTicket"]);
- }
-
- $token = $this->_getWxToken();
-
- $requestTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
- $ticketJson = HelperService::httpPost($requestTicketUrl,'',true);
- $ticketArr = @json_decode($ticketJson,true);
-
- if(!isset($ticketArr['ticket'])){
- HelperService::returnJson(['code'=>'400','msg'=>'ticket json is error','data'=>$ticketJson]);
- }
-
- $ticket = $ticketArr['ticket'];
- Cache::set('jsapi_'.$this->_apiCode,$ticket,7200);
- HelperService::returnJson(['code'=>200,'msg'=>'curl','data'=>"$ticket"]);
- }
- /**
- * 获取token
- */
- public function getToken(){
- $cacheToken = Cache::get('access_token_'.$this->_apiCode);
- if(!empty($cacheToken)){
- HelperService::returnJson(['code'=>200,'msg'=>'cache','data'=>"$cacheToken"]);
- }
-
- $token = $this->_getWxToken();
- Cache::set('access_token_'.$this->_apiCode,$token,7200);
- HelperService::returnJson(['code'=>200,'msg'=>'curl','data'=>"$token"]);
- }
-
- /**
- * 获取微信的token
- * @return type
- */
- private function _getWxToken(){
-
- $requestTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".Config::get('WECHAT_APPID')."&secret=".Config::get('WECHAT_APPSECRET');
- $tokenJson = HelperService::httpPost($requestTokenUrl,'',true);
- $tokenArr = @json_decode($tokenJson,true);
-
- if(!isset($tokenArr['access_token'])){
- HelperService::returnJson(['code'=>'400','msg'=>'access_token error','data'=>$tokenJson]);
- }
-
- $token = $tokenArr['access_token'];
- return $token;
- }
-
- /**
- * 获取用户是否获取某卡券
- */
- public function getUserCardInfo(){
-
- $params = $this->_params;
- $rule = [
- 'openid|公众号id'=>'require|max:100',
- 'card_id|卡券id'=>'require|max:100',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
- }
-
- $token = $this->_getWxToken();
- $url = "https://api.weixin.qq.com/card/user/getcardlist?access_token=$token";
- $data = [
- "openid"=>$params['openid'],
- "card_id"=>$params['card_id'],
- ];
- $cardListJson = HelperService::httpPost($url, $data, true);
- $cardListArr = @json_encode($cardListJson,true);
- if($cardListArr['errcode']==0 && $cardListArr['errmsg']=='ok'){
- HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$cardListArr['card_list']]);
- }
-
- HelperService::returnJson(['code'=>400,'msg'=>'error','data'=>$cardListJson]);
- }
- }
|