PHP TP模板下的微信登录(PC)

2024-05-09 17:48
文章标签 php tp 微信 pc 模板 登录

本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/974071

相关文章

微信OAuth授权获取用户OpenId-JAVA(个人经验)

个人微信小程序 可扫码体验 本文更新有可能先在开源中国。地址为:https://my.oschina.net/xshuai/blog/293458 https://open.weixin.qq.com/ 这个是授权登陆自己网站的和我的这个是有区别的。 带评论昵称  才同意加QQ ‍鉴于老是有人问我。就更新一下了。 更新时间 2016年10月18日 修改了测试号权限不足导致授权获取信息抛

NFS服务器搭建-共享PC与ARM主板文件

NFS服务器搭建-共享PC与ARM主板文件 在搭建好交叉编译环境之后需要实现目标板与宿主机的文件共享,在这里选择NFS,由于资料较多。需要注意的以下几点: 目标板与宿主机需要连接在同一个网段内。宿主机需要搭建nfs服务器,同样目标板在编译内核的时候需要选择nfs。 步骤一 宿主机环境设置 安装nfs-kernel-server; sudo apt-get install nfs-kern

selenium自动追踪微信小程序审核方案

小程序随着腾讯的不断推广,变的越来越普及,同时更新迭代的速度也越来越快,种类越来越多,那么在如何保证时效性就显得尤为重要,其中很重要一个环节就在于小程序审核通过之后,能否立刻通知到相关技术人员进行发布成为一件麻烦事,因为有些公司的发布权限在运营而非主要负责的开发人员手中,这时候就需要自动化测试工具出手了!     场景:A公司,有多个技术团队,每个团队已研发出多个小程序or小游戏,并不断

PHP中isset、empty、is_null实验测试

#$abc = "abc";     $def;     #var_dump(isset($abc));     //var_dump(empty($def));     //var_dump(false);     //var_dump(empty(''));     var_dump(isset($def));     var_dump(is_null($def));     #

XMPP系列:三、用户登录XMPP

一、XMPP中常见类的作用 XMPPStream:xmpp基础服务类 XMPPRoster:好友列表类 XMPPRosterCoreDataStorage:好友列表(用户账号)在core data中的操作类 XMPPvCardCoreDataStorage:好友名片(昵称,签名,性别,年龄等信息)在core data中的操作类 XMPPvCardTemp:好友名片实体类,

微信摇一摇之获取设备与用户信息

一.前言 利用微信摇一摇的功能接口可实现我们日常的某些需求,比如微信摇一摇签到等。首先需要购买设备,然后在开发者后台创建应用以及绑定回调界面,回接下来就开始开发了。 二.开始 1.请求说明 Https请求方式: POST https://qyapi.weixin.qq.com/cgi-bin/shakearound/getshakeinfo?access_token=ACCESS_TOKE

微信企业号之userid与openid互转

一.前言 该接口使用场景为微信支付、微信红包和企业转账,企业号用户在使用微信支付的功能时,需要自行将企业号的userid转成openid。在使用微信红包功能时,需要将应用id和userid转成appid和openid才能使用。 二.userid转换为openid 请求说明 Https请求方式: POST https://qyapi.weixin.qq.com/cgi-bin/user/co

微信企业号之获取access_token

一.前言 获取access_token是调用企业微信API接口的第一步,相当于创建了一个登录凭证,其它的业务API接口,都需要依赖于access_token来鉴权调用者身份。因此开发者,在使用业务接口前,要明确access_token的颁发来源,使用正确的access_token。 二.使用流程 请求方式: GET(HTTPS) 请求地址: https://qyapi.weixin.qq.c

bootstrap之登录窗口居中布局

效果图 代码示例 <!DOCTYPE html><html lang="en"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=9" /><head><meta charset="utf-8"><t

bootstrap登录界面切换密码登录和二维码登录

代码如下: <html xmlns:th="http://www.w3.org/1999/xhtml"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><head><title>用户登录</title><link href="http://cdn.static.runoob.com/libs/boots