- 46浏览
- 2023-03-30
效果
wxml页面
给button绑定冒泡事件,也就是我们说的点击事件
<button bindtap="getUserProfile" >授权登录</button>
js页面
这里wx.getUserProfile方法可以从底部弹框提示用户授权,success是用户确认授权回调方法,
可以console.log(result)查看以下数据,可以在userInfo中获取用户的一些基本信息,例如头像,昵称。通过wx.login()获取微信平台提供给我们的code(5分钟内有效,每次请求code都不一样),有了code我们就可以通过wx.request()向后端发起请求了
getUserProfile(e){
let that = this;
wx.getUserProfile({
desc: '展示用户信息',
success: (result) => {
// 接收名称和头像
let nickName = result.userInfo.nickName;
let avatarUrl = result.userInfo.avatarUrl;
wx.login({
timeout:10000,
success(res){
let code = res.code//获取code
wx.request({
url: 'http://www.day12.com/login',
method:'post',
data:{
code:code,
nickName:nickName,
avatarUrl:avatarUrl
},success: ret=>{
console.log(ret)
}
})
}
})
},
fail: (res) => {
console.log(res)
},
complete: (res) => {},
})
},
php后端
通过code和自己开发平台的appid和appSecret调用微信平台提供给我们的url获取openid和session_key,这里我们可以用file_get_contents()或者curl都可以
function login()
{
$code = request()->param('code');
$nickName = request()->param('nickName');
$avatarUrl = request()->param('avatarUrl');
$appId = "";//appid
$appSecret = "";//填写你微信公众开发平台的appSecret
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
//获取openid和session_key
$userInfo = json_decode(file_get_contents($url),true);
$userData = [
'nick_name'=>$nickName,
'avatar_url'=>$avatarUrl,
'open_id'=>$userInfo['openid'],
'session_key'=>$userInfo['session_key']
];
$user = new User();
//根据openid查询用户数据判断用户数是否首次登陆
$res = User::get(['open_id'=>$userInfo['openid']]);
if($res)
{
//返回查询数据的id //更新用户昵称头像
$id = $res->id;
$user->save(['nick_name'=>$nickName,'avatar_url'=>$avatarUrl],['id'=>$id]);
}else{
$res = $user->save($userData);//新增
$id = $user->id;
}
return json(['code'=>200,'msg'=>'success','data'=>$id]);
}
sql user表
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '姓名',
`phone` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '手机号',
`nick_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '昵称',
`avatar_url` varchar(150) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '头像',
`open_id` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`session_key` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`create_time` datetime(0) NULL DEFAULT NULL,
`last_time` datetime(0) NULL DEFAULT NULL,
`update_time` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
模拟数据
INSERT INTO `user` VALUES (1, '张三丰', NULL, '遇见', 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLu9S5py5YgSdCUiaHW38VMWxibgubhUHBcx5fb3tm84Z3c0kmONh', '', NULL, NULL, NULL, '2022-01-18 16:54:52');
INSERT INTO `user` VALUES (2, '梨花带雨', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (3, '大马路法拉', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (4, '编程浪子', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (5, '被罚款九分裤', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (6, '翻看积分', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (7, '法方面来看', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (8, '大鱼', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `user` VALUES (9, NULL, NULL, '遇见', 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLu9S5py5YgSdCUiaHW38VMWxibgubhUHBcx5fb3tm84Z3c0kmONh', NULL, NULL, '2022-01-18 16:47:50', NULL, '2022-01-18 16:47:50');
INSERT INTO `user` VALUES (10, NULL, NULL, '遇见', 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLu9S5py5YgSdCUiaHW38VMWxibgubhUHBcx5fb3tm84Z3c0kmONh', NULL, NULL, '2022-01-18 16:48:15', NULL, '2022-01-18 16:48:15');
SET FOREIGN_KEY_CHECKS = 1;
版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、本文由会员转载自互联网,如果您是文章原创作者,请联系本站注明您的版权信息。