<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;
}
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;
//⽤户是否授权过⼿机号
key: 'phone',
success: function (res) {
that.setData({
config: {
tipsshow1: false,
tipsshow2: false
},
})
}
})
//是否授权过⽤户信息
success: function(res) {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调⽤ getUserInfo 获取头像昵称
success: function(res) {
that.setData({
userInfo: res.userInfo,
config: {
tipsshow1: false,
},
})
}
})
}
}
})
},
getPhoneNumber: function(e) {
if (Msg == "getPhoneNumber:fail user deny") return; //⽤户允许授权
wx.showLoading()
var self = this
//1. 调⽤登录接⼝获取临时登录code
wx.login({
success: res => {
console.log(res, 555)
if (de) {
//2. 访问登录凭证校验接⼝获取session_key、openid
url: "xxxxxxx/index/author/login",
'js_code': de,
},
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. 解密
url: 'xxxxxx/index/author/number',
data: {
'appid': data.data.appid,
'sessionKey': data.data.session_key,
'encryptedData': ptedData,
'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 != '') {
url: 'xxxx/index/author/regist',
data: {
username: self.data.userInfo.nickName,
sex: self.der,
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 httpCurl($url, $params, $method = 'POST', $header = array(), $multi = false){  date_default_timezone_set('PRC');
$opts = array(
CURLOPT_TIMEOUT  => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER  => $header,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_COOKIE  =>session_name().'='.session_id(),
);
/* 根据请求类型设置特定参数 */
switch(strtoupper($method)){
case 'GET':
// $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
// 链接后拼接参数 & ⾮?
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
break;
case 'POST':
//判断是否传输⽂件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不⽀持的请求⽅式!');
}
/* 初始化并执⾏curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) throw new Exception('请求发⽣错误:' . $error);
}
/**
* 信息解密
* @param string $appid ⼩程序id
* @param string $sessionKey ⼩程序密钥
* @param string $encryptedData 在⼩程序中获取的encryptedData
* @param string $iv 在⼩程序中获取的iv
* @return array 解密后的数组
*/
function decryptData( $appid , $sessionKey, $encryptedData, $iv ){
$OK = 0;
$IllegalAesKey = -41001;
$IllegalIv = -41002;
$IllegalBuffer = -41003;
$DecodeBase64Error = -41004;
if (strlen($sessionKey) != 24) {
return $IllegalAesKey;
}
$aesKey=base64_decode($sessionKey);
if (strlen($iv) != 24) {
return $IllegalIv;
}
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json_decode( $result );
if( $dataObj == NULL )
{
return $IllegalBuffer;
}
if( $dataObj->watermark->appid != $appid )
{
return $DecodeBase64Error;
}
$data = json_decode($result,true);
return $result;
}
/**
* 请求过程中因为编码原因+号变成了空格
* 需要⽤下⾯的⽅法转换回来
*/
function define_str_replace($data)
{
return str_replace(' ','+',$data);
}
//获取⼿机号
public function number($appid , $sessionKey, $encryptedData, $iv)
{
include_once (ROOT_PATH."./public/author/wxBizDataCrypt.php"); //引⼊ wxBizDataCrypt.php ⽂件  $appid = $appid;
$sessionKey = $sessionKey;
$encryptedData= $encryptedData;
$iv = $iv;
$data = '';
$pc = new \WXBizDataCrypt($appid, $sessionKey); //注意使⽤\进⾏转义
$errCode = $pc->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
print($data . "\n");
} else {
print($errCode . "\n");
}
}
//登录
public function login(){
$get = input('get.');
$param['appid'] = 'xxxxxxxxxx'; //⼩程序id
$param['secret'] = 'xxxxxxxxxx'; //⼩程序密钥

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

图片转文字
« 上一篇
小程序-getUserProfile示例与后台(已纠正)
下一篇 »

发表评论

推荐文章

热门文章

最新文章

标签列表