微信小程序新增手机号码一键登录
parent
80f015d899
commit
64f0ed860a
|
|
@ -224,7 +224,7 @@ class User extends Common
|
|||
{
|
||||
$result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);
|
||||
|
||||
if(is_array($result))
|
||||
if(!empty($result) && is_array($result))
|
||||
{
|
||||
$result['nick_name'] = isset($result['nickName']) ? $result['nickName'] : '';
|
||||
$result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : '';
|
||||
|
|
@ -591,37 +591,85 @@ class User extends Common
|
|||
return DataReturn($ret, -1);
|
||||
}
|
||||
|
||||
// 先从数据库获取用户信息
|
||||
$user = UserService::AppUserInfoHandle(null, 'baidu_openid', $this->data_post['openid']);
|
||||
if(empty($user))
|
||||
// 解密数据
|
||||
$config = [
|
||||
'appid' => MyC('common_app_mini_baidu_appid'),
|
||||
'key' => MyC('common_app_mini_baidu_appkey'),
|
||||
'secret' => MyC('common_app_mini_baidu_appsecret'),
|
||||
];
|
||||
$result = (new \base\Baidu($config))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid'], 'mobile_bind');
|
||||
if($result['status'] == 0 && !empty($result['data']))
|
||||
{
|
||||
$config = [
|
||||
'appid' => MyC('common_app_mini_baidu_appid'),
|
||||
'key' => MyC('common_app_mini_baidu_appkey'),
|
||||
'secret' => MyC('common_app_mini_baidu_appsecret'),
|
||||
$data = [
|
||||
'openid' => $this->data_post['openid'],
|
||||
'mobile' => $result['data']['mobile'],
|
||||
'nickname' => isset($this->data_post['nickname']) ? $this->data_post['nickname'] : '',
|
||||
'avatar' => isset($this->data_post['avatar']) ? $this->data_post['avatar'] : '',
|
||||
'province' => isset($this->data_post['province']) ? $this->data_post['province'] : '',
|
||||
'city' => isset($this->data_post['city']) ? $this->data_post['city'] : '',
|
||||
'gender' => isset($this->data_post['gender']) ? intval($this->data_post['gender']) : '',
|
||||
'referrer' => isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0,
|
||||
'is_onekey_mobile_bind' => 1,
|
||||
];
|
||||
$result = (new \base\Baidu($config))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid'], 'mobile_bind');
|
||||
if($result['status'] == 0 && !empty($result['data']))
|
||||
{
|
||||
$data = [
|
||||
'openid' => $this->data_post['openid'],
|
||||
'mobile' => $result['data']['mobile'],
|
||||
'nickname' => isset($this->data_post['nickname']) ? $this->data_post['nickname'] : '',
|
||||
'avatar' => isset($this->data_post['avatar']) ? $this->data_post['avatar'] : '',
|
||||
'province' => isset($this->data_post['province']) ? $this->data_post['province'] : '',
|
||||
'city' => isset($this->data_post['city']) ? $this->data_post['city'] : '',
|
||||
'gender' => isset($this->data_post['gender']) ? intval($this->data_post['gender']) : '',
|
||||
'referrer' => isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0,
|
||||
'is_onekey_mobile_bind' => 1,
|
||||
];
|
||||
return UserService::AuthUserProgram($data, 'baidu_openid');
|
||||
} else {
|
||||
return DataReturn($result['msg'], -1);
|
||||
}
|
||||
return UserService::AuthUserProgram($data, 'baidu_openid');
|
||||
} else {
|
||||
return DataReturn('授权成功', 0, $user);
|
||||
return DataReturn($result['msg'], -1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小程序用户手机绑定
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 1.0.0
|
||||
* @date 2020-09-20
|
||||
* @desc description
|
||||
*/
|
||||
public function WeixinUserMobileBind()
|
||||
{
|
||||
// 参数校验
|
||||
$p = [
|
||||
[
|
||||
'checked_type' => 'empty',
|
||||
'key_name' => 'openid',
|
||||
'error_msg' => 'openid为空',
|
||||
],
|
||||
[
|
||||
'checked_type' => 'empty',
|
||||
'key_name' => 'encrypted_data',
|
||||
'error_msg' => '解密数据为空',
|
||||
],
|
||||
[
|
||||
'checked_type' => 'empty',
|
||||
'key_name' => 'iv',
|
||||
'error_msg' => 'iv为空,请重试',
|
||||
]
|
||||
];
|
||||
$ret = ParamsChecked($this->data_post, $p);
|
||||
if($ret !== true)
|
||||
{
|
||||
return DataReturn($ret, -1);
|
||||
}
|
||||
|
||||
// 解密数据
|
||||
$result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);
|
||||
if(!empty($result) && is_array($result) && !empty($result['purePhoneNumber']))
|
||||
{
|
||||
$data = [
|
||||
'openid' => $this->data_post['openid'],
|
||||
'mobile' => $result['purePhoneNumber'],
|
||||
'nickname' => isset($this->data_post['nickname']) ? $this->data_post['nickname'] : '',
|
||||
'avatar' => isset($this->data_post['avatar']) ? $this->data_post['avatar'] : '',
|
||||
'province' => isset($this->data_post['province']) ? $this->data_post['province'] : '',
|
||||
'city' => isset($this->data_post['city']) ? $this->data_post['city'] : '',
|
||||
'gender' => isset($this->data_post['gender']) ? intval($this->data_post['gender']) : '',
|
||||
'referrer' => isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0,
|
||||
'is_onekey_mobile_bind' => 1,
|
||||
];
|
||||
return UserService::AuthUserProgram($data, 'weixin_openid');
|
||||
} else {
|
||||
return DataReturn($result, -1);
|
||||
}
|
||||
return DataReturn(empty($result) ? '获取用户手机号码失败' : $result, -100);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -1708,10 +1708,31 @@ class UserService
|
|||
'referrer' => isset($params['referrer']) ? $params['referrer'] : 0,
|
||||
];
|
||||
|
||||
// 是否一键登录
|
||||
$is_onekey_mobile_bind = isset($params['is_onekey_mobile_bind']) && $params['is_onekey_mobile_bind'] == 1 ? 1 : 0;
|
||||
|
||||
// 用户信息处理
|
||||
$user = self::AppUserInfoHandle(null, $field, $params['openid']);
|
||||
if(!empty($user))
|
||||
{
|
||||
// 如果是一键登录、如当前用户不存在手机号码则绑定
|
||||
if(empty($user['mobile']) && !empty($data['mobile']) && $is_onekey_mobile_bind == 1)
|
||||
{
|
||||
// 手机号码不存在则绑定到当前账号下
|
||||
$temp = self::AppUserInfoHandle(null, 'mobile', $data['mobile']);
|
||||
if(empty($temp))
|
||||
{
|
||||
$upd_data = [
|
||||
'mobile' => $data['mobile'],
|
||||
'upd_time' => time(),
|
||||
];
|
||||
if(Db::name('User')->where(['id'=>$user['id']])->update($upd_data))
|
||||
{
|
||||
return DataReturn('绑定成功', 0, self::AppUserInfoHandle($user['id']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return DataReturn('授权成功', 0, $user);
|
||||
} else {
|
||||
// 用户unionid
|
||||
|
|
@ -1723,11 +1744,24 @@ class UserService
|
|||
if(!empty($user_unionid))
|
||||
{
|
||||
// openid绑定
|
||||
if(Db::name('User')->where(['id'=>$user_unionid['id']])->update([$field=>$params['openid'], 'upd_time'=>time()]))
|
||||
$upd_data = [
|
||||
$field => $params['openid'],
|
||||
'upd_time' => time(),
|
||||
];
|
||||
|
||||
// 如果是一键登录、如当前用户不存在手机号码则绑定
|
||||
if(empty($user['mobile']) && !empty($data['mobile']) && $is_onekey_mobile_bind == 1)
|
||||
{
|
||||
// 直接返回用户信息
|
||||
$user_unionid[$field] = $params['openid'];
|
||||
return DataReturn('授权成功', 0, $user_unionid);
|
||||
// 手机号码不存在则绑定到当前账号下
|
||||
$temp = self::AppUserInfoHandle(null, 'mobile', $data['mobile']);
|
||||
if(empty($temp))
|
||||
{
|
||||
$upd_data['mobile'] = $data['mobile'];
|
||||
}
|
||||
}
|
||||
if(Db::name('User')->where(['id'=>$user_unionid['id']])->update($upd_data))
|
||||
{
|
||||
return DataReturn('绑定成功', 0, self::AppUserInfoHandle($user_unionid['id']));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1747,7 +1781,7 @@ class UserService
|
|||
}
|
||||
} else {
|
||||
// 强制绑定手机号码、是否一键获取操作绑定
|
||||
if(isset($params['is_onekey_mobile_bind']) && $params['is_onekey_mobile_bind'] == 1 && !empty($data['mobile']))
|
||||
if($is_onekey_mobile_bind == 1 && !empty($data['mobile']))
|
||||
{
|
||||
// 如果手机号码存在则直接绑定openid
|
||||
// 不存在添加,存在更新openid
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
{
|
||||
"pages": [
|
||||
"pages/login/login",
|
||||
"pages/index/index",
|
||||
"pages/goods-category/goods-category",
|
||||
"pages/cart/cart",
|
||||
"pages/user/user",
|
||||
"pages/web-view/web-view",
|
||||
|
||||
"pages/login/login",
|
||||
"pages/paytips/paytips",
|
||||
"pages/goods-search/goods-search",
|
||||
"pages/goods-detail/goods-detail",
|
||||
|
|
|
|||
|
|
@ -203,12 +203,12 @@ Page({
|
|||
"encrypted_data": encrypted_data,
|
||||
"iv": iv,
|
||||
"openid": this.data.user.baidu_openid,
|
||||
"nickname": this.data.user.nickname,
|
||||
"avatar": this.data.user.avatar,
|
||||
"province": this.data.user.province,
|
||||
"city": this.data.user.city,
|
||||
"gender": this.data.user.gender,
|
||||
"referrer": referrer
|
||||
"nickname": this.data.user.nickname || '',
|
||||
"avatar": this.data.user.avatar || '',
|
||||
"province": this.data.user.province || '',
|
||||
"city": this.data.user.city || '',
|
||||
"gender": this.data.user.gender || 0,
|
||||
"referrer": referrer || 0
|
||||
};
|
||||
swan.showLoading({ title: "处理中..." });
|
||||
var self = this;
|
||||
|
|
|
|||
|
|
@ -9,19 +9,19 @@
|
|||
<button type="default" formType="submit" hover-class="none" plain loading="{{form_submit_loading}}" disabled="{{form_submit_loading}}" class="submit {{form_submit_loading ? 'my-btn-gray' : 'my-btn-default'}}">确认绑定</button>
|
||||
</form>
|
||||
|
||||
<!-- 确认授权方式 -->
|
||||
<!-- 确认绑定方式 -->
|
||||
<view s-if="login_type_status == 0" class="tc confirm-container">
|
||||
<image class="login-logo" src="{{(user.avatar || null) == null ? '/images/default-user.png' : user.avatar}}" mode="widthFix" />
|
||||
<view s-if="{{(user.nickname || null) != null}}" class="cr-666 nickname">{{user.nickname}}</view>
|
||||
<view s-if="(user.nickname || null) != null" class="cr-666 nickname">{{user.nickname}}</view>
|
||||
<view class="submit-list">
|
||||
<button s-if="{{common_user_is_onekey_bind_mobile == 1}}" type="warn" class="mobile-submit" open-type="getPhoneNumber" bindgetphonenumber="confirm_phone_number_event">获取手机号码一键登录</button>
|
||||
<button s-if="common_user_is_onekey_bind_mobile == 1" type="warn" class="mobile-submit" open-type="getPhoneNumber" bindgetphonenumber="confirm_phone_number_event">获取手机号码一键登录</button>
|
||||
<button type="warn" class="mobile-submit" plain="{{true}}" bindtap="confirm_verify_event">验证码登录</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 获取用户信息授权 -->
|
||||
<view s-if="user == null" class="user-login tc">
|
||||
<view s-else class="user-login tc">
|
||||
<view class="cr-888 fs-12">确认登录授权,为您提供更优质的服务</view>
|
||||
<button type="primary" size="mini" open-type="getUserInfo" bindgetuserinfo="get_user_info_event">授权登录</button>
|
||||
</view>
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ App({
|
|||
// 请求地址
|
||||
request_url: "{{request_url}}",
|
||||
request_url: 'http://shopxo.com/',
|
||||
request_url: 'https://dev.shopxo.net/',
|
||||
// request_url: 'https://dev.shopxo.net/',
|
||||
|
||||
// 基础信息
|
||||
application_title: "{{application_title}}",
|
||||
|
|
|
|||
|
|
@ -10,20 +10,38 @@ Page({
|
|||
form_submit_loading: false,
|
||||
verify_time_total: 60,
|
||||
temp_clear_time: null,
|
||||
|
||||
// 基础配置
|
||||
// 0 确认绑定方式, 1 验证码绑定
|
||||
login_type_status: 0,
|
||||
common_user_is_onekey_bind_mobile: 0,
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面加载初始化
|
||||
*/
|
||||
// 页面加载初始化
|
||||
onLoad(option) {
|
||||
// 设置用户信息
|
||||
this.setData({
|
||||
params: option,
|
||||
user: app.get_user_cache_info() || null
|
||||
});
|
||||
},
|
||||
|
||||
// 标题设置
|
||||
// 页面显示
|
||||
onShow() {
|
||||
wx.setNavigationBarTitle({ title: (this.data.user == null) ? '授权用户信息' : '手机绑定' });
|
||||
|
||||
// 初始化配置
|
||||
this.init_config();
|
||||
},
|
||||
|
||||
// 初始化配置
|
||||
init_config(status) {
|
||||
if((status || false) == true) {
|
||||
this.setData({
|
||||
common_user_is_onekey_bind_mobile: app.get_config('config.common_user_is_onekey_bind_mobile'),
|
||||
});
|
||||
} else {
|
||||
app.is_config(this, 'init_config');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -104,14 +122,12 @@ Page({
|
|||
}, 1000);
|
||||
} else {
|
||||
this.setData({verify_submit_text: '获取验证码', verify_loading: false, verify_disabled: false});
|
||||
|
||||
app.showToast(res.data.msg);
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
this.setData({verify_submit_text: '获取验证码', verify_loading: false, verify_disabled: false});
|
||||
|
||||
app.showToast("服务器请求出错");
|
||||
}
|
||||
});
|
||||
|
|
@ -178,18 +194,82 @@ Page({
|
|||
}, 1000);
|
||||
} else {
|
||||
this.setData({form_submit_loading: false});
|
||||
|
||||
app.showToast(res.data.msg);
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
this.setData({form_submit_loading: false});
|
||||
|
||||
app.showToast("服务器请求出错");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 获取手机号码一键登录
|
||||
confirm_phone_number_event(e) {
|
||||
var encrypted_data = e.detail.encryptedData || null;
|
||||
var iv = e.detail.iv || null;
|
||||
if(encrypted_data != null && iv != null) {
|
||||
// 邀请人参数
|
||||
var params = wx.getStorageSync(this.data.cache_launch_info_key) || null;
|
||||
var referrer = (params == null) ? 0 : (params.referrer || 0);
|
||||
|
||||
// 解密数据并绑定手机
|
||||
var data = {
|
||||
"encrypted_data": encrypted_data,
|
||||
"iv": iv,
|
||||
"openid": this.data.user.weixin_openid,
|
||||
"nickname": this.data.user.nickname || '',
|
||||
"avatar": this.data.user.avatar || '',
|
||||
"province": this.data.user.province || '',
|
||||
"city": this.data.user.city || '',
|
||||
"gender": this.data.user.gender || 0,
|
||||
"referrer": referrer || 0
|
||||
};
|
||||
wx.showLoading({ title: "处理中..." });
|
||||
var self = this;
|
||||
wx.request({
|
||||
url: app.get_request_url('weixinusermobilebind', 'user'),
|
||||
method: 'POST',
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
header: { 'content-type': 'application/x-www-form-urlencoded' },
|
||||
success: (res) => {
|
||||
wx.hideLoading();
|
||||
if (res.data.code == 0 && (res.data.data || null) != null) {
|
||||
app.showToast(res.data.msg, 'success');
|
||||
|
||||
wx.setStorage({
|
||||
key: app.data.cache_user_info_key,
|
||||
data: res.data.data
|
||||
});
|
||||
|
||||
var event_callback = this.data.params.event_callback || null;
|
||||
setTimeout(function()
|
||||
{
|
||||
// 触发回调函数
|
||||
if(event_callback != null)
|
||||
{
|
||||
getCurrentPages()[getCurrentPages().length-2][event_callback]();
|
||||
}
|
||||
wx.navigateBack();
|
||||
}, 1000);
|
||||
} else {
|
||||
app.showToast(res.data.msg);
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
self.showToast('服务器请求出错');
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 确认使用验证码
|
||||
confirm_verify_event(e) {
|
||||
this.setData({login_type_status: 1});
|
||||
},
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
<view wx:if="{{user != null}}" class="content">
|
||||
<form bindsubmit="formSubmit">
|
||||
<!-- 表单验证码 -->
|
||||
<form wx:if="{{login_type_status == 1}}" bindsubmit="formSubmit">
|
||||
<input type="number" placeholder="输入手机号码" maxlength="11" name="mobile" bindinput="bind_key_input" class="mobile" />
|
||||
<view class="code clearfix">
|
||||
<input type="number" placeholder="验证码" maxlength="4" name="verify" class="verify" />
|
||||
|
|
@ -7,9 +8,20 @@
|
|||
</view>
|
||||
<button type="default" formType="submit" hover-class="none" plain loading="{{form_submit_loading}}" disabled="{{form_submit_loading}}" class="submit {{form_submit_loading ? 'my-btn-gray' : 'my-btn-default'}}">确认绑定</button>
|
||||
</form>
|
||||
|
||||
<!-- 确认绑定方式 -->
|
||||
<view wx:if="{{login_type_status == 0}}" class="tc confirm-container">
|
||||
<image class="login-logo" src="{{(user.avatar || null) == null ? '/images/default-user.png' : user.avatar}}" mode="widthFix" />
|
||||
<view wx:if="{{(user.nickname || null) != null}}" class="cr-666 nickname">{{user.nickname}}</view>
|
||||
<view class="submit-list">
|
||||
<button wx:if="{{common_user_is_onekey_bind_mobile == 1}}" type="warn" class="mobile-submit" open-type="getPhoneNumber" bindgetphonenumber="confirm_phone_number_event">获取手机号码一键登录</button>
|
||||
<button type="warn" class="mobile-submit" plain="{{true}}" bindtap="confirm_verify_event">验证码登录</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{user == null}}" class="user-login tc">
|
||||
<!-- 获取用户信息授权 -->
|
||||
<view wx:else class="user-login tc">
|
||||
<view class="cr-888 fs-12">确认登录授权,为您提供更优质的服务</view>
|
||||
<button type="primary" size="mini" open-type="getUserInfo" bindgetuserinfo="get_user_info_event">授权登录</button>
|
||||
</view>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ page{
|
|||
height: 100vh;
|
||||
}
|
||||
.content {
|
||||
padding: 30% 40rpx 0 40rpx;
|
||||
padding: 25% 40rpx 0 40rpx;
|
||||
}
|
||||
.content .mobile{
|
||||
width: 100%;
|
||||
|
|
@ -60,4 +60,23 @@ page{
|
|||
}
|
||||
.user-login button {
|
||||
margin-top: 30rpx;
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录确认
|
||||
*/
|
||||
.confirm-container .login-logo {
|
||||
width: 200rpx;
|
||||
height: 200rpx;
|
||||
margin: 0 auto;
|
||||
border-radius: 50%;
|
||||
}
|
||||
.confirm-container .nickname {
|
||||
margin-top: 10rpx;
|
||||
}
|
||||
.confirm-container .submit-list {
|
||||
margin-top: 100rpx;
|
||||
}
|
||||
.confirm-container .submit-list button:not(:last-child) {
|
||||
margin-bottom: 40rpx;
|
||||
}
|
||||
Loading…
Reference in New Issue