|
@@ -740,6 +740,9 @@ class User extends Api
|
|
|
} else {
|
|
|
$extend = [
|
|
|
'openid' => $openid,
|
|
|
+ 'nickname' => $wxuserinfo['nickname'],
|
|
|
+ 'sex' => $wxuserinfo['sex'],
|
|
|
+ 'status' => 'new',
|
|
|
];
|
|
|
$mobile = '';
|
|
|
$ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
|
|
@@ -1121,38 +1124,70 @@ class User extends Api
|
|
|
*/
|
|
|
public function bindmobile()
|
|
|
{
|
|
|
- //$user = $this->auth->getUser();
|
|
|
- $user = model('User')->find($this->auth->id);
|
|
|
- $mobile = $this->request->param('mobile');
|
|
|
- $captcha = $this->request->param('captcha');
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $user = model('User')->find($this->auth->id);
|
|
|
+ $mobile = $this->request->param('mobile');
|
|
|
+ $captcha = $this->request->param('captcha');
|
|
|
|
|
|
- if(!empty($this->auth->mobile)){
|
|
|
- $this->error('已经绑定了手机号');
|
|
|
- }
|
|
|
- if (!$mobile || !$captcha) {
|
|
|
- $this->error(__('Invalid parameters'));
|
|
|
- }
|
|
|
- if (!Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
- $this->error(__('Mobile is incorrect'));
|
|
|
- }
|
|
|
- $userData = model('User')->where('mobile', $mobile)->find();
|
|
|
- if (!empty($userData)) {
|
|
|
- if ($userData['id'] != $this->auth->id) {
|
|
|
- $this->error('该手机号已被其他用户绑定');
|
|
|
+ if(!empty($this->auth->mobile) && $mobile != $this->auth->mobile){
|
|
|
+ throw new Exception('已经绑定了手机号');
|
|
|
}
|
|
|
+ if (!$mobile || !$captcha) {
|
|
|
+ throw new Exception(__('Invalid parameters'));
|
|
|
+ }
|
|
|
+ if (!Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
+ throw new Exception(__('Mobile is incorrect'));
|
|
|
+ }
|
|
|
+ $result = Sms::check($mobile, $captcha, 'changemobile');
|
|
|
+ if (!$result && $captcha != 1212 ) {
|
|
|
+ throw new Exception(__('Captcha is incorrect'));
|
|
|
+ }
|
|
|
+ $where['mobile'] = $mobile;
|
|
|
+ $where['id'] = ['neq',$this->auth->id];
|
|
|
+ $where['status'] = ['neq','new'];
|
|
|
+ $userData = model('User')->where($where)->find();//老用户
|
|
|
+ if (!empty($userData)) {
|
|
|
+ if (empty($userData['openid'])) {
|
|
|
+ model('User')->update(['openid'=>$user['openid']],$where);//老用户更新openid
|
|
|
+ } else {
|
|
|
+ throw new Exception('该手机号已被其他用户绑定');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //清除微信登录没绑定手机号的数据
|
|
|
+ $whereDel['mobile'] = '';
|
|
|
+ $whereDel['status'] = 'new';
|
|
|
+ $userDel = model('User')->where($whereDel)->find();
|
|
|
+ $userPowerWhere['user_id'] = $userDel['id'];
|
|
|
+ model('UserPower')->where($userPowerWhere)->delete();
|
|
|
+ model('User')->where($whereDel)->delete();
|
|
|
+
|
|
|
+ Sms::flush($mobile, 'changemobile');
|
|
|
+ $this->auth->direct($userData['id']);
|
|
|
+ //更新token
|
|
|
+ $userTokenWhere['user_id'] = $userDel['id'];
|
|
|
+ $userToken = Db::name('user_token')->where($userTokenWhere)->find();
|
|
|
+ if (!empty($userToken)) {
|
|
|
+ $userTokenStr = $userToken['token'];
|
|
|
+ $userTokenDelRes = Db::name('user_token')->where($userTokenWhere)->delete();
|
|
|
+ if (!$userTokenDelRes) {
|
|
|
+ throw new Exception('绑定失败');
|
|
|
+ }
|
|
|
+ $userOldTokenWhere['user_id'] = $userData['id'];
|
|
|
+ $userOldTOken = Db::name('user_token')->where($userOldTokenWhere)->find();
|
|
|
+ if (!empty($userOldTOken)) {
|
|
|
+ $userOldTOkenRes = Db::name('user_token')->where($userOldTokenWhere)->update(['token'=>$userTokenStr]);
|
|
|
+ if (!$userOldTOkenRes) {
|
|
|
+ throw new Exception('绑定登录失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ $this->success('success',$this->userInfo('return'));
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
- $result = Sms::check($mobile, $captcha, 'changemobile');
|
|
|
- if (!$result && $captcha != 1212 ) {
|
|
|
- $this->error(__('Captcha is incorrect'));
|
|
|
- }
|
|
|
- $verification = $user->verification;
|
|
|
- $verification->mobile = 1;
|
|
|
- $user->verification = $verification;
|
|
|
- $user->mobile = $mobile;
|
|
|
- $user->save();
|
|
|
-
|
|
|
- Sms::flush($mobile, 'changemobile');
|
|
|
- $this->success('success',$this->userInfo('return'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1163,31 +1198,49 @@ class User extends Api
|
|
|
*/
|
|
|
public function bindopenid()
|
|
|
{
|
|
|
- $code = $this->request->param('code','');
|
|
|
- if(!$code){
|
|
|
- $this->error(__('Invalid parameters'));
|
|
|
- }
|
|
|
- //微信
|
|
|
- $wechat = new Wechat();
|
|
|
- $openid = $wechat->getOpenid($code);
|
|
|
-
|
|
|
- if(!$openid){
|
|
|
- $this->error('openid获取失败');
|
|
|
- }
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $code = $this->request->param('code','');
|
|
|
+ if(!$code){
|
|
|
+ throw new Exception(__('Invalid parameters'));
|
|
|
+ }
|
|
|
+ //微信
|
|
|
+ $wechat = new Wechat();
|
|
|
+ $openid = $wechat->getOpenid($code);
|
|
|
|
|
|
- $user = model('User')->find($this->auth->id);
|
|
|
- if(!empty($this->auth->openid)){
|
|
|
- $this->error('已经绑定了微信号');
|
|
|
- }
|
|
|
+ if(!$openid){
|
|
|
+ throw new Exception('openid获取失败');
|
|
|
+ }
|
|
|
|
|
|
- if (\app\common\model\User::where('openid', $openid)->find()) {
|
|
|
- $this->error('该微信号已被其他用户绑定');
|
|
|
- }
|
|
|
+ $user = model('User')->find($this->auth->id);
|
|
|
+ if(!empty($this->auth->openid) && $openid != $this->auth->openid){
|
|
|
+ throw new Exception('已经绑定了微信号');
|
|
|
+ }
|
|
|
+ $where['openid'] = $openid;
|
|
|
+ $where['id'] = ['neq',$this->auth->id];
|
|
|
+ $where['status'] = ['neq','new'];
|
|
|
+ $userData = model('User')->where($where)->find();
|
|
|
+ if (!empty($userData)) {
|
|
|
+ throw new Exception('该微信号已被其他用户绑定');
|
|
|
+ }
|
|
|
|
|
|
- $user->openid = $openid;
|
|
|
- $user->save();
|
|
|
+ $user->openid = $openid;
|
|
|
+ $userRes = $user->save();
|
|
|
+ if ($userRes) {
|
|
|
+ $whereDel['openid'] = '';
|
|
|
+ $whereDel['status'] = 'new';
|
|
|
+ $userDel = model('User')->where($whereDel)->find();
|
|
|
+ $userPowerWhere['user_id'] = $userDel['id'];
|
|
|
+ model('UserPower')->where($userPowerWhere)->delete();
|
|
|
+ model('User')->where($whereDel)->delete();
|
|
|
+ }
|
|
|
|
|
|
- $this->success('success',$this->userInfo('return'));
|
|
|
+ Db::commit();
|
|
|
+ $this->success('success',$this->userInfo('return'));
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//用户详细资料
|