绑定手机号,绑定头像昵称
绑定手机号,绑定头像昵称
<image class="person_bc" src="/img/person/bc.png">
<view class='in'>
<image class='avatar' src="{{userInfo.avatar}}"></image>
<view class='right'>
<view class='top'>
<text>{{userInfo.nickname}}</text>
<block wx:if="{{userInfo.need_init}}">
<button class="get_button" bindgetuserinfo="getUserInfo" open-type="getUserInfo">
使用微信信息
</button>
</block>
</view>
<view class='bottom'>
<block wx:if="{{userInfo.telephone}}">
{{userInfo.telephone}}
</block>
<block wx:else>
<button class="get_button" bindgetphonenumber="getPhoneNumber" open-type="getPhoneNumber">
绑定手机号
</button>
</block>
</view>
</view>
</view>
</image>
js
init: function () {
let that = this;
util.getData('userInfo', {
openid: app.globalData.openid,
method: 'POST'
}, function (data) {
that.setData({
userInfo: data.data
})
})
},
getPhoneNumber: function (e) {
let that = this;
if (e.detail.errMsg == 'getPhoneNumber:ok') {
wx.login({
success: function (res) {
util.getData('decryptData', {
iv: e.detail.iv,
encryptedData: e.detail.encryptedData,
session_key: app.globalData.session_key,
method: "POST"
}, function (dataDed) {
let telephone = dataDed.data.phoneNumber;
util.getData('bindTel', {
openid: app.globalData.openid,
telephone: telephone,
method: 'POST'
}, function (dataTel) {
that.init()
})
})
}
})
}
},
getUserInfo: function (e) {
let that = this;
if (e.detail.errMsg == "getUserInfo:ok") {
wx.login({
success: function (res) {
util.getData("userInit", {
openid: app.globalData.openid,
nickname: e.detail.userInfo.nickName,
avatar: e.detail.userInfo.avatarUrl,
method: "POST"
}, function (data) {
that.init()
})
}
})
}
}
php
// 设置用户头像和昵称 init
public function init() {
$openid = trim($_POST['openid']);
$nickname = trim($_POST['nickname']);
$avatar = trim($_POST['avatar']);
$user = M('user');
if(!$openid){
$this->json->setErr(10001,'缺少参数openid');
$this->json->Send();
}
$user_info = $user->where(['openid'=>$openid])->find();
if(!$user_info){
$this->json->setErr(10002,'暂无此用户信息');
$this->json->Send();
}else{
$data = [
'nickname' => $this->filterEmoji($nickname),
'avatar' => $avatar,
'update_time'=> time()
];
$user->where(['openid'=>$openid])->save($data);
}
$this->json->setErr('0','操作成功');
$this->json->Send();
}
//绑定用户手机号
public function bindTel(){
$openid = trim($_POST['openid']);
$telephone = trim($_POST['telephone']);
if(!$openid){
$this->json->setErr(10001,'缺少参数openid');
$this->json->Send();
}
if(!$telephone){
$this->json->setErr(10002,'缺少参数手机号码');
$this->json->Send();
}
$user = M('User');
$result = $user->where(array('telephone'=>$telephone))->find();
if($result){
$this->json->setErr(10004,'此手机号已经被绑定了,如有疑问请与管理员联系');
$this->json->Send();
}
$data = [
'telephone' => $telephone,
'bind_time' => time()
];
$edit_flag = $user->where(['openid'=>$openid])->save($data);
if (!$edit_flag){
$this->json->setErr(10099,'系统错误,请联系管理员');
$this->json->Send();
}else{
$this->json->setErr(0,'绑定成功');
$this->json->Send();
}
}
//获取用户信息
public function userInfo(){
$openid = trim($_POST['openid']);
if (!$openid){
$this->json->setAttr(10001,'缺少参数');
$this->json->Send();
}
$user = M('User');
$user_flag = $user->where(array('openid'=>$openid))->find();
if (!$user_flag) {
$this->json->setAttr(10002,'用户不存在');
$this->json->Send();
}
// 判断是否需要初始化
if (strpos($user_flag['avatar'],'avatar') !== false || $user_flag['update_time'] < strtotime('-30 days')) {
$user_flag['need_init'] = 1;
} else {
$user_flag['need_init'] = 0;
}
if (!$user_flag['telephone']) {
$user_flag['need_bind_tel'] = 1;
} else {
$user_flag['need_bind_tel'] = 0;
$user_flag['telephone'] = Func::encryptTel($user_flag['telephone']);
}
$this->json->setAttr('data',$user_flag);
$this->json->Send();
}
public function decryptData(){
$encryptedData = trim($_POST['encryptedData']);
$iv = trim($_POST['iv']);
$sessionkey = trim($_POST['session_key']);
$appid = C('APPID');
if (strlen($sessionkey) != 24) {
$this->json->setErr(-41001,'aes非法');
$this->json->Send();
}
$aesKey = base64_decode($sessionkey);
if (strlen($iv) != 24) {
$this->json->setErr(-41002,'iv非法');
$this->json->Send();
}
$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){
$this->json->setErr(-41003,'aes解密失败');
$this->json->Send();
}
if($dataObj->watermark->appid !=$appid){
$this->json->setErr(-41007,'aes解密失败');
$this->json->Send();
}
$this->json->setAttr('data',$dataObj);
$this->json->Send();
}