WechatJs.php 4.0 KB

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