微信小程序如何通过TP5.0后端获取用户信息及手机号?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1140个文字,预计阅读时间需要5分钟。
原文示例:本文实例为大家分享了微信小程序获取用户信息及手机号的整体代码,供大家参考,具体内容如下+wxml页面+view wx:if={{config.tipsShow1}} class='dialog-container' view class='dialog-mask'/view view
改写后:本文提供微信小程序获取用户信息和手机号的代码示例,供参考。具体内容包含+wxml页面+view wx:if={{config.tipsShow1}} class=dialog-container view class=dialog-mask/view view。
本文实例为大家分享了微信小程序获取用户信息及手机号的具体代码,供大家参考,具体内容如下
wxml页面
<view wx:if="{{config.tipsshow1}}" class='dialog-container'> <view class='dialog-mask'></view> <view class='dialog-info'> <view class='dialog-title'>login prompt</view> <view class='dialog-content'>To provide better service, click "allow" in the prompt box later!</view> <view class='dialog-footer'> <button class='dialog-btn' open-type="getUserInfo" bindgetuserinfo="getUserInfo">I see.</button> </view> </view> </view> <view wx:if="{{config.tipsshow2}}" class='dialog-container'> <view class='dialog-mask'></view> <view class='dialog-info'> <view class='dialog-title'>login prompt</view> <view class='dialog-content'>To provide better service, click "allow" in the prompt box later!</view> <view class='dialog-footer'> <button class='dialog-btn' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">To authorize.</button> </view> </view> </view>
wxss页面
.dialog-mask{ position: fixed; z-index: 1000; top: 0; right: 0; left: 0; bottom: 0; background: rgba(0, 0, 0, 0.3); } .dialog-info{ position: fixed; z-index: 5000; width: 80%; max-width: 600rpx; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); background-color: #FFFFFF; text-align: center; border-radius: 3px; overflow: hidden; } .dialog-title{ font-size: 36rpx; padding: 30rpx 30rpx 10rpx; } .dialog-content{ padding: 10rpx 30rpx 20rpx; min-height: 80rpx; font-size: 32rpx; line-height: 1.3; word-wrap: break-word; word-break: break-all; color: #999999; } .dialog-footer{ display: flex; align-items: center; position: relative; line-height: 90rpx; font-size: 34rpx; } .dialog-btn{ display: block; -webkit-flex: 1; flex: 1; position: relative; color: #3CC51F; }
js页面
data: { userName: '', pwd: '', getUserInfoFail: '', userInfo: [], hasUserInfo: '', phone: '', config: { tipsshow1: true, tipsshow2: false } }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { var that = this; //用户是否授权过手机号 wx.getStorage({ key: 'phone', success: function (res) { that.setData({ config: { tipsshow1: false, tipsshow2: false }, }) } }) //是否授权过用户信息 wx.getSetting({ success: function(res) { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: function(res) { that.setData({ userInfo: res.userInfo, config: { tipsshow1: false, }, }) } }) } } }) }, getPhoneNumber: function(e) { if (e.detail.errMsg == "getPhoneNumber:fail user deny") return; //用户允许授权 wx.showLoading() var self = this //1. 调用登录接口获取临时登录code wx.login({ success: res => { console.log(res, 555) if (res.code) { //2. 访问登录凭证校验接口获取session_key、openid wx.request({ url: "xxxxxxx/index/author/login", data: { 'js_code': res.code, }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'content-type': 'application/json' }, // 设置请求的 header success: function(data) { console.log(data, data) if (data.statusCode == 200) { //3. 解密 wx.request({ url: 'xxxxxx/index/author/number', data: { 'appid': data.data.appid, 'sessionKey': data.data.session_key, 'encryptedData': e.detail.encryptedData, 'iv': e.detail.iv, }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'content-type': 'application/json' }, // 设置请求的 header success: function(data2) { wx.hideLoading() console.log(data2.data.phoneNumber) if (data2.statusCode == 200 && data2.data.phoneNumber) { self.setData({ phone: data2.data.phoneNumber, config: { tipsshow1: false, tipsshow2: false, }, }) wx.setStorageSync('phone', data2.data.phoneNumber); if (self.data.userInfo != '') { wx.request({ url: 'xxxx/index/author/regist', data: { username: self.data.userInfo.nickName, sex: self.data.userInfo.gender, phone: self.data.phone, pwd: 123456, avatarimg: self.data.userInfo.avatarUrl }, success: function(data) { console.log(data.data,56565) if (data.data != null) { wx.showToast({ title: '登录中...', icon: 'loading', duration: 2000 }) wx.navigateTo({ url: '../managementList/managementList'//管理页面 }) } } }); } console.log(self.data, 526336) } }, fail: function(err) { console.log(err); } }) } }, fail: function(err) { console.log(err); } }) } } }) }, getUserInfo: function(e) { var that = this; console.log(e.detail.userInfo, "getuserinfo") if (e.detail.userInfo) { that.setData({ userInfo: e.detail.userInfo, config: { tipsshow1: false, tipsshow2: true, }, }) console.log(that.data.userInfo); } else { console.log("获取信息失败") } },
PHP后端
<?php namespace app\index\controller; use think\Controller; use app\admin\model\UserRecharge; use think\Db; class Author extends Controller { /** * 发送HTTP请求方法 * @param string $url 请求URL * @param array $params 请求参数 * @param string $method 请求方法GET/POST * @return array $data 响应数据 */ function api.weixin.qq.com/sns/jscode2session', $param, 'GET'); $session_key = json_decode($http_key,true);//获取openid和session_key //print_r(http_build_query($param)); if (!empty($session_key['session_key'])) { $data['appid'] = $param['appid']; $data['session_key'] = $session_key['session_key']; return json($data); }else{ echo '获取session_key失败!'; } } //用户注册 public function regist($username = "",$sex = "", $phone = "",$password = "",$avatarimg = "") { if ($phone){ //判断该用户是否已经注册 $userdata = Db::name('user')->where('phone',$phone)->find(); if ($userdata){ return json_encode(2); } //整合数组 $salt = '1122'; $password = Md5(Md5($password) . $salt); $data = [ 'name' => $username, 'sex' => $sex, 'phone' => $phone, 'password' => $password, 'avatarimg' => $avatarimg, 'logtime' => date("Y-m-d H:i:s"), 'addTime' => date("Y-m-d H:i:s") ]; //注册新用户 $userid = db('user')->insertGetId($data); if ($userid){ return json_decode(1); }else{ return json_encode(0); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计1140个文字,预计阅读时间需要5分钟。
原文示例:本文实例为大家分享了微信小程序获取用户信息及手机号的整体代码,供大家参考,具体内容如下+wxml页面+view wx:if={{config.tipsShow1}} class='dialog-container' view class='dialog-mask'/view view
改写后:本文提供微信小程序获取用户信息和手机号的代码示例,供参考。具体内容包含+wxml页面+view wx:if={{config.tipsShow1}} class=dialog-container view class=dialog-mask/view view。
本文实例为大家分享了微信小程序获取用户信息及手机号的具体代码,供大家参考,具体内容如下
wxml页面
<view wx:if="{{config.tipsshow1}}" class='dialog-container'> <view class='dialog-mask'></view> <view class='dialog-info'> <view class='dialog-title'>login prompt</view> <view class='dialog-content'>To provide better service, click "allow" in the prompt box later!</view> <view class='dialog-footer'> <button class='dialog-btn' open-type="getUserInfo" bindgetuserinfo="getUserInfo">I see.</button> </view> </view> </view> <view wx:if="{{config.tipsshow2}}" class='dialog-container'> <view class='dialog-mask'></view> <view class='dialog-info'> <view class='dialog-title'>login prompt</view> <view class='dialog-content'>To provide better service, click "allow" in the prompt box later!</view> <view class='dialog-footer'> <button class='dialog-btn' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">To authorize.</button> </view> </view> </view>
wxss页面
.dialog-mask{ position: fixed; z-index: 1000; top: 0; right: 0; left: 0; bottom: 0; background: rgba(0, 0, 0, 0.3); } .dialog-info{ position: fixed; z-index: 5000; width: 80%; max-width: 600rpx; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); background-color: #FFFFFF; text-align: center; border-radius: 3px; overflow: hidden; } .dialog-title{ font-size: 36rpx; padding: 30rpx 30rpx 10rpx; } .dialog-content{ padding: 10rpx 30rpx 20rpx; min-height: 80rpx; font-size: 32rpx; line-height: 1.3; word-wrap: break-word; word-break: break-all; color: #999999; } .dialog-footer{ display: flex; align-items: center; position: relative; line-height: 90rpx; font-size: 34rpx; } .dialog-btn{ display: block; -webkit-flex: 1; flex: 1; position: relative; color: #3CC51F; }
js页面
data: { userName: '', pwd: '', getUserInfoFail: '', userInfo: [], hasUserInfo: '', phone: '', config: { tipsshow1: true, tipsshow2: false } }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { var that = this; //用户是否授权过手机号 wx.getStorage({ key: 'phone', success: function (res) { that.setData({ config: { tipsshow1: false, tipsshow2: false }, }) } }) //是否授权过用户信息 wx.getSetting({ success: function(res) { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: function(res) { that.setData({ userInfo: res.userInfo, config: { tipsshow1: false, }, }) } }) } } }) }, getPhoneNumber: function(e) { if (e.detail.errMsg == "getPhoneNumber:fail user deny") return; //用户允许授权 wx.showLoading() var self = this //1. 调用登录接口获取临时登录code wx.login({ success: res => { console.log(res, 555) if (res.code) { //2. 访问登录凭证校验接口获取session_key、openid wx.request({ url: "xxxxxxx/index/author/login", data: { 'js_code': res.code, }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'content-type': 'application/json' }, // 设置请求的 header success: function(data) { console.log(data, data) if (data.statusCode == 200) { //3. 解密 wx.request({ url: 'xxxxxx/index/author/number', data: { 'appid': data.data.appid, 'sessionKey': data.data.session_key, 'encryptedData': e.detail.encryptedData, 'iv': e.detail.iv, }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'content-type': 'application/json' }, // 设置请求的 header success: function(data2) { wx.hideLoading() console.log(data2.data.phoneNumber) if (data2.statusCode == 200 && data2.data.phoneNumber) { self.setData({ phone: data2.data.phoneNumber, config: { tipsshow1: false, tipsshow2: false, }, }) wx.setStorageSync('phone', data2.data.phoneNumber); if (self.data.userInfo != '') { wx.request({ url: 'xxxx/index/author/regist', data: { username: self.data.userInfo.nickName, sex: self.data.userInfo.gender, phone: self.data.phone, pwd: 123456, avatarimg: self.data.userInfo.avatarUrl }, success: function(data) { console.log(data.data,56565) if (data.data != null) { wx.showToast({ title: '登录中...', icon: 'loading', duration: 2000 }) wx.navigateTo({ url: '../managementList/managementList'//管理页面 }) } } }); } console.log(self.data, 526336) } }, fail: function(err) { console.log(err); } }) } }, fail: function(err) { console.log(err); } }) } } }) }, getUserInfo: function(e) { var that = this; console.log(e.detail.userInfo, "getuserinfo") if (e.detail.userInfo) { that.setData({ userInfo: e.detail.userInfo, config: { tipsshow1: false, tipsshow2: true, }, }) console.log(that.data.userInfo); } else { console.log("获取信息失败") } },
PHP后端
<?php namespace app\index\controller; use think\Controller; use app\admin\model\UserRecharge; use think\Db; class Author extends Controller { /** * 发送HTTP请求方法 * @param string $url 请求URL * @param array $params 请求参数 * @param string $method 请求方法GET/POST * @return array $data 响应数据 */ function api.weixin.qq.com/sns/jscode2session', $param, 'GET'); $session_key = json_decode($http_key,true);//获取openid和session_key //print_r(http_build_query($param)); if (!empty($session_key['session_key'])) { $data['appid'] = $param['appid']; $data['session_key'] = $session_key['session_key']; return json($data); }else{ echo '获取session_key失败!'; } } //用户注册 public function regist($username = "",$sex = "", $phone = "",$password = "",$avatarimg = "") { if ($phone){ //判断该用户是否已经注册 $userdata = Db::name('user')->where('phone',$phone)->find(); if ($userdata){ return json_encode(2); } //整合数组 $salt = '1122'; $password = Md5(Md5($password) . $salt); $data = [ 'name' => $username, 'sex' => $sex, 'phone' => $phone, 'password' => $password, 'avatarimg' => $avatarimg, 'logtime' => date("Y-m-d H:i:s"), 'addTime' => date("Y-m-d H:i:s") ]; //注册新用户 $userid = db('user')->insertGetId($data); if ($userid){ return json_decode(1); }else{ return json_encode(0); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

