Wechat.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\wechat\controller;
  3. use app\user\model\Account;use app\user\model\User;use think\App;use think\facade\Cache;use think\facade\Validate;
  4. class Wechat extends Base{
  5. protected $model=null;
  6. protected $appid='';
  7. protected $appsecret='';
  8. protected $wechat=null;
  9. protected $keepTime=3600;
  10. protected $isLogin=0;
  11. public function __construct(App $app) {
  12. parent::__construct($app);
  13. $this->appid = env('WECHAT.APPID');
  14. $this->appsecret = env('WECHAT.APPSECRET');
  15. $this->wechat = new \Wechat(['appid' => $this->appid, 'appsecret' => $this->appsecret]);
  16. }
  17. public function WebAuth(){
  18. $code =$this->request->only(['code'=>''],'post','trim');
  19. $valid =Validate::rule([
  20. 'code|微信授权Code'=>'require|max:255|min:5',
  21. ]);
  22. if($valid->check($code)==false)return error($valid->getError());
  23. $uid =$this->wechat->getAccessTokenByCode($code['code']);
  24. if($uid==false)return error($this->wechat->errMsg);
  25. if(!isset($uid['openid'])|| $uid['openid']=='')return error('用户openid 未获取到');
  26. $userinfo = $this->checkOpendId($uid['openid']);
  27. $wxinfo = $this->wechat->getUserInfo($uid['openid']);
  28. return success('获取成功',['wxinfo'=>$wxinfo,'userinfo'=>$userinfo,'isLogin'=>1]);
  29. }
  30. protected function checkOpendId($openId){
  31. $accountId= User::where(['openId'=>$openId])->value('account_id',0);
  32. if($accountId==0)return [];
  33. $account =new Account();
  34. $isT = $account->with(['accountItem'=>['itemName'],'company_relaton','userInfo'])
  35. ->findOrEmpty($accountId);
  36. if($isT->isEmpty())return [];
  37. $isT->hidden(['userInfo','password','salt','accountItem','openId']);
  38. if ($isT->status==0) return error('账户已禁用');
  39. $token = makeToken($isT);
  40. \app\common\User::instance()->LoginUserInfo($isT->toArray(),$token,$this->keepTime);
  41. $isT['token'] = $token;
  42. $this->isLogin=1;
  43. return $isT;
  44. }
  45. public function getConfig(){
  46. $url =$this->request->only(['url'=>''],'post','trim');
  47. $valid =Validate::rule(['url|微信授权地址'=>'require|max:255|min:5']);
  48. if($valid->check($url)==false)return error($valid->getError());
  49. $baseconfig =$this->wechat->getJsTicket();
  50. if($baseconfig==false)return error('签名获取失败');
  51. $baseconfig =$this->wechat->getJsSign($url['url']);
  52. if($baseconfig==false)return error('签名获取失败');
  53. return success('获取成功',$baseconfig);
  54. }
  55. //获取公众号下定制模板
  56. public function getTemplate(){
  57. $template = Cache::get('Wx::Template');
  58. if($template==false){
  59. $data=$this->wechat->getAllPrivateTemplate();
  60. if($data==false) return error($this->wechat->errMsg);
  61. Cache::set('Wx::Template' ,$data['template_list'], new \DateTime(date('Y-m-d 23:59:59')) );
  62. $template = $data['template_list'];
  63. }
  64. return success('获取成功',$template);
  65. }
  66. }