Wxauth.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace weixin;
  3. /**
  4. * @title : 微信自动登录接口插件
  5. * @desc : 使用微信授权登录系统
  6. * @Author : Mr.Fu
  7. * @Date : 2023-04-06 19:06:59
  8. */
  9. class Wxauth
  10. {
  11. //微信认证URL
  12. private $Host="https://open.weixin.qq.com/connect/oauth2/authorize?";
  13. private $Response_type="code"; //private $Scope="snsapi_base";
  14. private $Scope="snsapi_base";//snsapi_userinfo/snsapi_base
  15. private $State="1";
  16. private $Appid="";
  17. private $Appsecret="";
  18. private $Redirect_uri="";//回调地址
  19. private $Auth_url="";
  20. public function __construct()
  21. {
  22. //读取微信登录配置
  23. // $this->Host =$this->Config['wxauth_url'];
  24. $this->Redirect_uri = urlencode('');
  25. $this->APPID =sysconfig('wchat.wxmini_user_appid');
  26. $this->APPSECRET =sysconfig('wchat.wxmini_user_secret');
  27. $this->Auth_url =$this->Host."appid=".$this->APPID."&redirect_uri=".$this->Redirect_uri."&response_type=".$this->Response_type."&scope=".$this->Scope."&state=".$this->State."#wechat_redirect";
  28. }
  29. /**
  30. * @title: 根据CODE获取AccessToken
  31. * @desc: 描述
  32. * @param {string} {code} {} {微信用户登录code}
  33. * @return {*}
  34. * @author: Mr.Fu
  35. * @method: POST
  36. * @Date: 2023-04-06 19:42:22
  37. */
  38. public function getAccesstoken(string $code=''){
  39. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
  40. $url .= "appid=$this->APPID&secret=$this->APPSECRET&CODE=$code&grant_type=authorization_code";
  41. $result = file_get_contents($url);
  42. $result = json_decode($result,true);
  43. WLog('wxlogin',serialize($result));
  44. if(empty($result['access_token'])){
  45. return Result(0,'微信登录失败');
  46. }else{
  47. return Result(1,'登录成功',$result);
  48. }
  49. }
  50. /**
  51. * @title: 返回微信登录请求URL
  52. * @desc: 描述
  53. * @return {*}
  54. * @author: Mr.Fu
  55. * @method: POST
  56. * @Date: 2023-04-06 19:43:31
  57. */
  58. public function GetLoginUrl()
  59. {
  60. return $this->Auth_url;
  61. }
  62. /**
  63. * @title: 立即跳转微信登录
  64. * @desc: 描述
  65. * @return {*}
  66. * @author: Mr.Fu
  67. * @method: POST
  68. * @Date: 2023-04-06 19:43:44
  69. */
  70. public function Login()
  71. {
  72. J($this->Auth_url);
  73. }
  74. /**
  75. * @title: 微信认证后的回调处理
  76. * @desc: 描述
  77. * @return {*}
  78. * @author: Mr.Fu
  79. * @method: POST
  80. * @Date: 2023-04-06 19:43:59
  81. */
  82. public function Auth()
  83. {
  84. $code=input('code');
  85. if($code==""){
  86. $this->error('微信登录回调失败',url('index/login'));
  87. }
  88. $result = $this->getAccesstoken($code);
  89. if($result['status']==1){
  90. $this->error("微信登录失败",url('index/login'));
  91. }else{
  92. $access_token = $result['data']['access_token'];
  93. $openid = $result['data']['openid'];
  94. }
  95. $url='https://api.weixin.qq.com/sns/userinfo?access_token=';
  96. $wxuserinfo=file_get_contents($url.$access_token."&openid=".$openid);
  97. $wxuserinfo=json_decode($wxuserinfo,true);
  98. // $userinfo=db('member')->where('wxopenid',$wxuserinfo['openid'])->find();
  99. //微信用户是否存在
  100. // if(is_array($userinfo) and $wxuserinfo['openid']!=""){
  101. // if($this->login_status($userinfo['id'])){
  102. // $this->Log_Write($userinfo['id'],$userinfo['username'],2,$userinfo['username'].'微信登录成功',0);
  103. // J('/');
  104. // }else{
  105. // $this->Log_Write($userinfo['id'],$userinfo['username'],2,$userinfo['username'].'微信登录失败',1);
  106. // $this->error('微信登录失败!','/');
  107. // }
  108. // }else{
  109. // //不存在直接注册微信会员
  110. // $wxusername="WX_".GetRandStr(18);
  111. // $wxuserpwd=GetRandStr(10);
  112. // $facepic_data=file_get_contents($wxuserinfo['headimgurl']);
  113. // $DIR1="/uploads/facepic/".MyDate('Ymd',time())."/";
  114. // $FILENAME=time().GetRandStr(10).".jpg";
  115. // $path=ROOT_PATH.$DIR1;
  116. // if(!file_exists($path)){
  117. // mkdir($path);
  118. // }
  119. // file_put_contents($path.$FILENAME,$facepic_data);
  120. // $ip=GetIP();
  121. // $time=time();
  122. // $safecode=GetRandStr(16);
  123. // $password=Encrypt($safecode,$wxuserpwd);
  124. // $data=array(
  125. // 'group'=>1,
  126. // 'username'=>$wxusername,
  127. // 'nickname'=>$wxuserinfo['nickname'],
  128. // 'password'=>$password,
  129. // 'safecode'=>$safecode,
  130. // 'wxopenid'=>$wxuserinfo['openid'],
  131. // 'facepic'=>$DIR1.$FILENAME,
  132. // 'balance'=>0,
  133. // 'integral'=>0,
  134. // 'type'=>'wx',
  135. // 'regtime'=>$time,
  136. // 'regip'=>$ip,
  137. // 'logintime'=>$time,
  138. // 'loginip'=>$ip,
  139. // 'status'=>0
  140. // );
  141. // $uid=db('member')->insertGetId($data);
  142. // if($this->LoginStatus($uid)){
  143. // $this->Log_Write($uid,$wxusername,2,$wxusername.'微信注册登录成功',0);
  144. // J('/');
  145. // }else{
  146. // $this->Log_Write($uid,$wxusername,2,$wxusername.'微信注册登录失败',1);
  147. // $this->error('微信注册登录失败!','/');
  148. // }
  149. // }
  150. }
  151. public function LoginStatus($id)
  152. {
  153. // $userinfo=db('member')->where('id',$id)->find();
  154. // if(is_array($userinfo) and $id>=1){
  155. // $ip=GetIP();
  156. // $time=time();
  157. // //开始登录
  158. // $login_data=array(
  159. // 'id'=>$userinfo['id'],
  160. // 'username'=>$userinfo['username'],
  161. // 'nickname'=>$userinfo['nickname'],
  162. // 'logintime'=>$time,
  163. // 'loginip'=>$ip
  164. // );
  165. // $update=array('logintime'=>$time,'loginip'=>$ip);
  166. // //记录登录时间和IP地址
  167. // db('member')->where('username',$userinfo['username'])->update($update);
  168. // //加密管理登录信息数据
  169. // $ciphertext=AuthCode(serialize($login_data),'ENCODE',$this->Config['cookie_secretkey']);
  170. // //把密文存储到会话中
  171. // session('userinfo',$ciphertext);
  172. // return true;
  173. // }else{
  174. // return false;
  175. // }
  176. }
  177. }
  178. ?>