本文主要是介绍PHP TP模板下的微信登录(PC),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.微信开发者账号的申请就不说,企业级;官网域名回跳地址。(微信开放平台APPID)
还有注意点:很多应用是放在微信公众号中的,此地微信登录是:微信公众平台APPID
2.请求代码构
<?phpclass ui_login_weixin_login extends ui
{public function __construct(){parent::__construct();//当前由用户if (empty($this->uid)) {$biz_weixin_loginapi = new biz_weixin_loginapi();$weixin_login_url = $biz_weixin_loginapi->get_url_code();//请求到地址栏header('location:' . $weixin_login_url);} else {header('location:' . WWW_DOMAIN);}}
}
public function get_url_code(){$url = self::WEIXIN_URL;//回调地址$code_redirect_uri = urlencode(self::CODE_REDIRECT_URI); // self::CODE_REDIRECT_URI 回调地址需要urlencode ;$state = md5(uniqid(mt_rand(), true));$_SESSION['weixin_login_state'] = $state;//请求地址$url .= 'connect/qrconnect?appid='.self::APPID.'&redirect_uri='.$code_redirect_uri.'&response_type=code&scope=snsapi_login&state='.$state.'#wechat_redirect';return $url;}
这两部代码即可完成对微信的请求
2.编写回调代码:
步骤:
2.1 判断是不是有返回值;
2.2 获取access_token open_id;
2.3 获取微信用户信息;
2.4 检验微信token是否有效;
2.5 根据unionid查询查询本地 是否有数据 有得话,执行本地登录方法,成功或者失败 ;
2.6 没有得话 写入weixin_login user 表各一份数据 微信登陆表 用户表,执行本地登录方法,成功或者失败;
<?phpclass ui_login_weixin_logincallback extends ui
{public function __construct($S, $param){parent::__construct();// 如果已登陆 跳转到首页if (! empty($this->uid)) {header('location:' . WWW_DOMAIN);return null;}$header = loadMod('glob/header');$footer = loadMod('glob/footer');$S->assign('header', $header);$S->assign('footer', $footer);$info = $this->login_callback($param['code'], $param['state']);$S->assign('info', $info);}/*** 微信登陆扫码后 访问得方法*/protected function login_callback($code, $state){// 如果返回值无code 或者state不对 跳转到首页if (empty($code) || $state !== $_SESSION['weixin_login_state']) {return array('title' => '登陆失败,请重新尝试!','url' => '/','sec' => 5,'msg' => 'alert');}// 获取access_token open_id$biz_weixin_loginapi = new biz_weixin_loginapi();$result_access_token = $biz_weixin_loginapi->get_access_token($code);if ($result_access_token['msg'] !== 'success') {return array('title' => '登陆失败,请重新尝试!','url' => '/','sec' => 5,'msg' => 'alert');}$access_token = $result_access_token['data']['access_token'];$open_id = $result_access_token['data']['openid'];// 获取用户信息$result_user_info = $biz_weixin_loginapi->get_user_info($access_token, $open_id);if ($result_user_info['msg'] !== 'success' || empty($result_user_info['data']['unionid'])) {return array('title' => '登陆失败,请重新尝试!','url' => '/','sec' => 5,'msg' => 'alert');}// 检验token是否有效$check_token_res = $biz_weixin_loginapi->check_token($access_token, $open_id);if ($check_token_res['msg'] !== 'success') {return array('title' => '登陆失败,请重新尝试!','url' => '/','sec' => 5,'msg' => 'alert');}// 登陆成功 根据unionid查询weixin_login user 是否有数据 有得话 直接读取 没有得话$biz_login_weixin = new biz_login_weixin();$user_id = $biz_login_weixin->get_userid_by_weixin_unionid($result_user_info['data']['unionid']);if (is_id($user_id['data'])) {$biz_login = new biz_login();$biz_login->login($user_id['data'], 'is_login');return array('title' => '登陆成功!','url' => '/home/welcome','sec' => 3,'msg' => 'success');}// 没有得话 写入weixin_login user 表各一份数据 微信登陆表 用户表$openid = $result_user_info['data']['openid'];$nickname = $result_user_info['data']['nickname'];$sex = $result_user_info['data']['sex'];$language = $result_user_info['data']['language'];$city = $result_user_info['data']['city'];$province = $result_user_info['data']['province'];$country = $result_user_info['data']['country'];$headimgurl = $result_user_info['data']['headimgurl'];$unionid = $result_user_info['data']['unionid'];$insert_res = $biz_login_weixin->add_weixin($openid, $nickname, $sex, $language, $city, $province, $country, $headimgurl, $unionid);if ($insert_res['msg'] !== 'success' || ! is_id($insert_res['data']['user_id'])) {return array('title' => '登陆失败,请重新尝试!','url' => '/','sec' => 5,'msg' => 'alert');}$biz_login = new biz_login();$biz_login->login($insert_res['data']['user_id'], 'is_login');return array('title' => '登陆成功!','url' => '/home/welcome','sec' => 3,'msg' => 'success');}
}
public function get_access_token($code, $type = 1){if(empty($code)){return array('msg' => 'code_error');}$url = self::API_URL;if($type === 1){$appid = self::APPID;$secret = self::APP_SECRET;}else{$appid = self::APPID_GZ;$secret = self::APP_SECRET_GZ;}$url .= 'sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';//获取access_token$res_json = runCurl($url);if(!$res_json){return array('msg' => 'runCurl_error'); }$res_arr = json_decode($res_json, true);if(isset($res_arr['errcode'])){return array('msg' => $res_arr['errmsg']);}return array('msg' => 'success', 'data' => $res_arr);}
public function get_user_info($access_token, $open_id){if(empty($access_token) || empty($open_id)){return array('msg' => 'empty_token_or_openid');}$url = self::API_URL;$url .= 'sns/userinfo?access_token='.$access_token.'&openid='.$open_id;$res_json = runCurl($url);if(!$res_json){return array('msg' => 'runCurl_error');}$res_arr = json_decode($res_json, true);if(isset($res_arr['errcode'])){return array('msg' => $res_arr['errmsg']);}return array('msg' => 'success', 'data' => $res_arr);}
public function check_token($access_token, $open_id){if(empty($access_token) || empty($open_id)){return array('msg' => 'empty_token_or_openid');}$url = self::API_URL;$url .= 'sns/auth?access_token='.$access_token.'&openid='.$open_id;$res_json = runCurl($url);if(!$res_json){return array('msg' => 'runCurl_error');}$res_arr = json_decode($res_json, true);if( $res_arr['errcode'] !== 0 ){return array('msg' => $res_arr['errmsg']);}return array('msg' => 'success');}
这篇关于PHP TP模板下的微信登录(PC)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!