1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace app\controller;
- use app\model\Account;use think\App;
- use think\facade\Cache;use think\facade\Validate;
- class WeChat extends Base {
- private $wechat=null;
- private $token_time=3600;
- protected $isLogin = 0;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $wechat_conf=['appid'=>env('WECHAT.APPID'),'appsecret'=>env('WECHAT.APPSECRET')];
- $this->wechat=new \Wechat($wechat_conf);
- $this->token_time= env('token.expire');
- }
-
- public function WebAuth(){
- $code =$this->request->only(['code'=>''],'post','trim');
- $valid =Validate::rule([
- 'code|微信授权Code'=>'require|max:255|min:5',
- ]);
- if($valid->check($code)==false) $this->error($valid->getError());
-
- $uid =$this->wechat->getAccessTokenByCode($code['code']);
- if($uid==false){
- $this->error('未获取到用户数据');
- }
- if(!isset($uid['openid'])|| $uid['openid']=='') $this->error('用户openid 未获取到');
- $userinfo = $this->checkOpendId($uid['openid']);
- $wxinfo = $this->wechat->getUserInfo($uid['openid']);
- $this->success("获取成功",["wxinfo"=>$wxinfo,"userinfo"=>$userinfo,"isLogin"=>$this->isLogin]);
-
- }
- //网页端调用jssdk配置config
- public function getConfig(){
- $url =$this->request->only(['url'=>''],'post','trim');
- $valid =Validate::rule(['url|微信授权地址'=>'require|max:255|min:5']);
- if($valid->check($url)==false)$this->error($valid->getError());
- $baseconfig =$this->wechat->getJsTicket();
- if($baseconfig==false){
- $this->error('签名获取失败');
- }
- $baseconfig =$this->wechat->getJsSign($url['url']);
- if($baseconfig==false){
- $this->error('签名获取失败');
- }
- $this->success('获取成功',$baseconfig);
- }
-
- protected function checkOpendId($openId){
- $account =new Account();
- $isT = $account->withJoin(["userinfo","accountitem"],"left")
- ->where(["openId"=>$openId])
- ->findOrEmpty();
- if($isT->isEmpty())return [];
- $token = makeToken($isT);
- $cache = Cache::store('redis')->set("user:info:{$token}", $isT->toArray(), $this->token_time);
- if ($cache == false) return [];
- $isT['token'] = $token;
- $this->isLogin=1;
- return $isT;
- }
-
- }
|