12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace app\wechat\controller;
- use app\user\model\Account;use app\user\model\User;use think\App;use think\facade\Cache;use think\facade\Validate;
- class Wechat extends Base{
- protected $model=null;
- protected $appid='';
- protected $appsecret='';
- protected $wechat=null;
- protected $keepTime=3600;
- protected $isLogin=0;
- public function __construct(App $app) {
- parent::__construct($app);
- $this->appid = env('WECHAT.APPID');
- $this->appsecret = env('WECHAT.APPSECRET');
- $this->wechat = new \Wechat(['appid' => $this->appid, 'appsecret' => $this->appsecret]);
- }
- 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)return error($valid->getError());
- $uid =$this->wechat->getAccessTokenByCode($code['code']);
- if($uid==false)return error($this->wechat->errMsg);
- if(!isset($uid['openid'])|| $uid['openid']=='')return error('用户openid 未获取到');
- $userinfo = $this->checkOpendId($uid['openid']);
- $wxinfo = $this->wechat->getUserInfo($uid['openid']);
- return success('获取成功',['wxinfo'=>$wxinfo,'userinfo'=>$userinfo,'isLogin'=>1]);
- }
- protected function checkOpendId($openId){
- $accountId= User::where(['openId'=>$openId])->value('account_id',0);
- if($accountId==0)return [];
- $account =new Account();
- $isT = $account->with(['accountItem'=>['itemName'],'company_relaton','userInfo'])
- ->findOrEmpty($accountId);
- if($isT->isEmpty())return [];
- $isT->hidden(['userInfo','password','salt','accountItem','openId']);
- if ($isT->status==0) return error('账户已禁用');
- $token = makeToken($isT);
- \app\common\User::instance()->LoginUserInfo($isT->toArray(),$token,$this->keepTime);
- $isT['token'] = $token;
- $this->isLogin=1;
- return $isT;
- }
- public function getConfig(){
- $url =$this->request->only(['url'=>''],'post','trim');
- $valid =Validate::rule(['url|微信授权地址'=>'require|max:255|min:5']);
- if($valid->check($url)==false)return error($valid->getError());
- $baseconfig =$this->wechat->getJsTicket();
- if($baseconfig==false)return error('签名获取失败');
- $baseconfig =$this->wechat->getJsSign($url['url']);
- if($baseconfig==false)return error('签名获取失败');
- return success('获取成功',$baseconfig);
- }
-
- //获取公众号下定制模板
- public function getTemplate(){
- $template = Cache::get('Wx::Template');
- if($template==false){
- $data=$this->wechat->getAllPrivateTemplate();
- if($data==false) return error($this->wechat->errMsg);
- Cache::set('Wx::Template' ,$data['template_list'], new \DateTime(date('Y-m-d 23:59:59')) );
- $template = $data['template_list'];
- }
- return success('获取成功',$template);
- }
- }
|