php-微信公众号登录

微信公众号登录

getCode();
}

$LoginWxGzh->setCode($code);
return $LoginWxGzh->initialization();
/*end  登录  end*/

/**
 * 微信公众号登录
 * Created by Sublime.
 * User: dongfh
 * Date: 2020/05/29
 */
class LoginWxGzh
{
/**
 * error code 业务错误说明.
 * 
    *
  • 1000: 成功
  • *
  • 1004: 失败
  • *
  • 1005: 业务失败
  • *
*/ private static $success = 1000; private static $fail = 1004; private static $business = 1005; private $code; public function setCode($code){ $this->code = $code; } private static $appid = 'xxxx'; private static $appsecret = 'xxxx'; private $openid; private $access_token; private $member_id; private $UserToken; private static $CodeRedirectUri = 'http://www.abc.com/user-login?param1=xxx¶m2=xxx'; private static $CodeAuthorizeUri = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect'; private static $AccessTokenUri = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code'; private static $UserInfoUri = 'https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN'; /** * [initialization 初始化登录] * @return [type] [description] */ public function initialization(){ //获取openID $this->getOpenId(); //获取用户ID $this->getMemberId(); //获取User-Token $this->getUserToken(); $info = [ "user_token" => $this->UserToken ]; return $this->JsonSuccess($info,"登录成功",self::$success); } /** * [getCode 获取登录code] * @return [type] [description] */ public function getCode(){ //格式化请求地址 请求接口 $getCodeAuthorizeUri = sprintf(self::$CodeAuthorizeUri,self::$appid,urlencode(self::$CodeRedirectUri),md5(uniqid(rand(), TRUE))); //跳转 header("Location: ".$getCodeAuthorizeUri); exit; } /** * [getOpenId 获取OpenId] * @return [type] [description] */ private function getOpenId(){ //格式化请求地址 请求接口 $getAccessTokenUri = sprintf(self::$AccessTokenUri,self::$appid,self::$appsecret,$this->code); //获取access_token $responses = $this->https_request($getAccessTokenUri); $result = json_decode($responses, true); if(!isset($result['access_token']) && !isset($result['openid'])) { return $this->JsonError("获取token失败!",self::$business); } $this->openid = $result["openid"]; $this->access_token = $result["access_token"]; } /** * [getMemberId 获取用户ID] * @return [type] [description] */ private function getMemberId(){ //查询数据 $userInfo_db = Db::name("member")->where("openid",$this->openid)->find(); if(empty($userInfo_db->id)){ //格式化请求地址 请求接口 $getUserInfoUri = sprintf(self::$UserInfoUri,$this->access_token,$this->openid); $getUserInfo = $this->https_request($getUserInfoUri); $userInfo = json_decode($getUserInfo, true); if(!empty($userInfo['openid'])){ $Member_params = [ "nickname" => $userInfo["nickname"], "head_portrait" => $userInfo["head_portrait"], "openid" => $userInfo["openid"], "create_time" => time() ]; $user_id = Db::name("member")->insert($Member_params); if(!$user_id){ return $this->JsonError("注册用户信息失败!",self::$fail,$Member_params); } $userInfo_db = Db::name("member")->where("member_id",$user_id)->find(); }else{ return $this->JsonError("获取用户信息失败!",self::$business,["openid" => $this->openid]); } } if(empty($userInfo_db["id"])){ return $this->JsonError("获取用户信息失败!",self::$business,["openid" => $this->openid]); } $this->member_id = $userInfo_db["id"]; } /** * [getUserToken 获取User-Token] * @return [type] [description] */ private function getUserToken(){ //生成用户User-Token $UserToken = $this->generateUserToken(); $timestamp = time() + 2592000; //查询token $MemberToken = Db::name("MemberToken")->where("member_id",$this->member_id)->find(); //不存在 if(!$MemberToken){ $Params = [ "member_id" => $this->member_id , "token" => $UserToken , "create_time" => $timestamp ]; $result = Db::name("MemberToken")->insert($Params); if(!$result){ return $this->JsonError("添加token失败",self::$fail,$Params,$Params); } }else{ $timeParams = [ "token" => $UserToken , "update_time" => $timestamp ]; $result = Db::name("MemberToken")->where("member_id",$this->member_id)->update($timeParams); if(!$result){ return $this->JsonError("更改过期时间失败",self::$fail,$timeParams); } } $this->UserToken = $UserToken."#".base64_encode($this->member_id); } /** * [generateUserToken 生成token] * @return [type] [description] */ private function generateUserToken(){ $token = strtoupper(md5($this->member_id.'dongfh_'.mt_rand(100,999))); return $token; } /** * [https_request 请求] * @param [type] $url [路径] * @param [type] $data [参数] * @return [type] [description] */ private function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); if(curl_errno($curl)){ $output = curl_error($curl); } curl_close($curl); return $output; } /** * [JsonSuccess 返回数据] * @param [type] $data [数据] * @param string $msg [信息] * @param integer $code [状态码] */ private function JsonSuccess($data=[],$msg='成功',$code=1000){ $data = [ "code" => $code, "msg" => $msg, "data" => $data ]; return json_encode($data); } /** * [JsonError 返回数据] * @param integer $code [状态码] * @param string $msg [信息] * @param [type] $data [数据] */ private function JsonError($msg='失败',$code=1004,$data=[]){ return $this->JsonSuccess($data,$msg,$code); } } ?>

你可能感兴趣的:(php-微信公众号登录)