WechatJs.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use think\Cache;
  5. use think\Config;
  6. /**
  7. * 微信js接口
  8. * Class WeChat
  9. * @package app\expand\controller
  10. */
  11. class WechatJs extends BaseAuth
  12. {
  13. private $_Account = null;
  14. public function __construct(){
  15. parent::__construct();
  16. $this->_Account = $this->getKey($this->_apiCode);
  17. //验证是否具有访问这个接口的权限
  18. if(!isset($this->_Account['Wechat_js_appId'])
  19. || !isset($this->_Account['Wechat_js_appsecret'])){
  20. HelperService::returnJson(['code'=>400,'msg'=>'WECHAT js interface unauthorized access','data'=>[]]);
  21. }
  22. Config::set('WECHAT_APPID',$this->_Account['Wechat_js_appId']);
  23. Config::set('WECHAT_APPSECRET',$this->_Account['Wechat_js_appsecret']);
  24. }
  25. /**
  26. * 获取微信js-config
  27. */
  28. public function getConfigJs(){
  29. $cacheTicket = Cache::get('jsapi_'.$this->_apiCode);
  30. if(!empty($cacheTicket)){
  31. HelperService::returnJson(['code'=>200,'msg'=>'cache','data'=>"$cacheTicket"]);
  32. }
  33. $token = $this->getWxToken();
  34. $requestTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
  35. $ticketJson = HelperService::httpPost($requestTicketUrl,'',true);
  36. $ticketArr = @json_decode($ticketJson,true);
  37. if(!isset($ticketArr['ticket'])){
  38. HelperService::returnJson(['code'=>'400','msg'=>'ticket json is error','data'=>$ticketJson]);
  39. }
  40. $ticket = $ticketArr['ticket'];
  41. Cache::set('jsapi_'.$this->_apiCode,$ticket,7200);
  42. HelperService::returnJson(['code'=>200,'msg'=>'curl','data'=>"$ticket"]);
  43. }
  44. /**
  45. * 获取token
  46. */
  47. public function getToken(){
  48. $cacheToken = Cache::get('access_token_'.$this->_apiCode);
  49. if(!empty($cacheToken) && $this->_apiCode!="BAIXIONG"){//白熊
  50. HelperService::returnJson(['code'=>200,'msg'=>'cache','data'=>"$cacheToken"]);
  51. }
  52. $token = $this->getWxToken();
  53. Cache::set('access_token_'.$this->_apiCode,$token,7200);
  54. HelperService::returnJson(['code'=>200,'msg'=>'curl','data'=>"$token"]);
  55. }
  56. /**
  57. * 获取微信的token
  58. * @return string
  59. */
  60. public function getWxToken(){
  61. $requestTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".Config::get('WECHAT_APPID')."&secret=".Config::get('WECHAT_APPSECRET');
  62. $tokenJson = HelperService::httpPost($requestTokenUrl,'',true);
  63. $tokenArr = @json_decode($tokenJson,true);
  64. if(!isset($tokenArr['access_token'])){
  65. HelperService::returnJson(['code'=>'400','msg'=>'access_token error','data'=>$tokenJson]);
  66. }
  67. $token = $tokenArr['access_token'];
  68. return $token;
  69. }
  70. /**
  71. * 获取用户是否获取某卡券
  72. */
  73. public function getUserCardInfo(){
  74. $params = $this->_params;
  75. $rule = [
  76. 'openid|公众号id'=>'require|max:100',
  77. 'card_id|卡券id'=>'require|max:100',
  78. ];
  79. $validate = new Validate($rule);
  80. if(!$validate->check($params)){
  81. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
  82. }
  83. $token = $this->getWxToken();
  84. $url = "https://api.weixin.qq.com/card/user/getcardlist?access_token=$token";
  85. $data = [
  86. "openid"=>$params['openid'],
  87. "card_id"=>$params['card_id'],
  88. ];
  89. $cardListJson = HelperService::httpPost($url, $data, true);
  90. $cardListArr = @json_encode($cardListJson,true);
  91. if($cardListArr['errcode']==0 && $cardListArr['errmsg']=='ok'){
  92. HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$cardListArr['card_list']]);
  93. }
  94. HelperService::returnJson(['code'=>400,'msg'=>'error','data'=>$cardListJson]);
  95. }
  96. }