|
@@ -153,54 +153,63 @@ class User extends Api
|
|
|
}
|
|
|
}*/
|
|
|
|
|
|
- //微信登录+注册
|
|
|
+ //微信登录,预先假注册
|
|
|
public function wechatlogin(){
|
|
|
- $nickname = input('nickname','');
|
|
|
- $avatar = input('avatar','');
|
|
|
- $gender = input('gender',1);
|
|
|
- $wechat_openid = input('wechat_openid','');
|
|
|
|
|
|
- if (!$wechat_openid) {
|
|
|
+ $code = $this->request->param('code','');
|
|
|
+ if(!$code){
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
- if($gender != 1){
|
|
|
- $gender = 0;
|
|
|
+ //微信
|
|
|
+ $wechat = new Wechat();
|
|
|
+
|
|
|
+ $wxuserinfo = $wechat->getwxuserinfo($code);
|
|
|
+
|
|
|
+ if(!$wxuserinfo){
|
|
|
+ $this->error('openid获取失败');
|
|
|
}
|
|
|
+ $openid = $wxuserinfo['openid'];
|
|
|
|
|
|
- $user = \app\common\model\User::getByOpenid($wechat_openid);
|
|
|
+ $user = Db::name('user')->where('openid',$openid)->find();
|
|
|
if ($user) {
|
|
|
- if ($user->status == -1) {
|
|
|
+ if ($user['status'] == -1) {
|
|
|
$this->error('账户已注销');
|
|
|
}
|
|
|
- if ($user->status != 1) {
|
|
|
+ if ($user['status'] != 1) {
|
|
|
$this->error(__('Account is locked'));
|
|
|
}
|
|
|
//如果已经有账号则直接登录
|
|
|
- $ret = $this->auth->direct($user->id);
|
|
|
-
|
|
|
-
|
|
|
+ $ret = $this->auth->direct($user['id']);
|
|
|
+ $is_register = 0;
|
|
|
+ $userInfo = $this->auth->getUserinfo();
|
|
|
} else {
|
|
|
- if (!$nickname || !$avatar) {
|
|
|
- $this->error(__('Invalid parameters'));
|
|
|
- }
|
|
|
-
|
|
|
- $reg_data = [
|
|
|
- 'nickname'=>$nickname,
|
|
|
- 'avatar'=>$avatar,
|
|
|
- 'gender'=>$gender,
|
|
|
- 'register_from' => input('register_from',''),
|
|
|
+ //记录code和openid,绑定手机号的时候更新openid
|
|
|
+ $wechatCodeData = [
|
|
|
+ 'code' => $code,
|
|
|
+ 'openid' => $openid,
|
|
|
+ 'createtime' => time(),
|
|
|
];
|
|
|
- $ret = $this->auth->openid_register($wechat_openid,$reg_data);
|
|
|
-
|
|
|
+ $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
|
|
|
+ if (empty($wechatCode)) {
|
|
|
+ Db::name('wechat_code')->insertGetId($wechatCodeData);
|
|
|
+ } else {
|
|
|
+ Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
|
|
|
+ }
|
|
|
+ $ret = true;
|
|
|
+ $is_register = 1;
|
|
|
+ $userInfo = [];
|
|
|
+ $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
|
|
|
+ $this->success('获取信息成功', $data, 2);
|
|
|
}
|
|
|
if ($ret) {
|
|
|
- $data = $this->userInfo('return');
|
|
|
+ $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
|
|
|
$this->success(__('Logged in successful'), $data);
|
|
|
} else {
|
|
|
$this->error($this->auth->getError());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 运营商一键登录
|
|
|
*/
|
|
@@ -544,34 +553,57 @@ class User extends Api
|
|
|
*/
|
|
|
public function bindmobile()
|
|
|
{
|
|
|
- $user = $this->auth->getUser();
|
|
|
- $mobile = $this->request->request('mobile');
|
|
|
- $captcha = $this->request->request('captcha');
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $code = $this->request->param('code');
|
|
|
+ $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'));
|
|
|
- }*/
|
|
|
- if (\app\common\model\User::where('mobile', $mobile)->find()) {
|
|
|
- $this->error('该手机号已被其他用户绑定');
|
|
|
- }
|
|
|
- $result = Sms::check($mobile, $captcha, 'changemobile');
|
|
|
- if (!$result) {
|
|
|
- $this->error(__('Captcha is incorrect'));
|
|
|
- }
|
|
|
- $verification = $user->verification;
|
|
|
- $verification->mobile = 1;
|
|
|
- $user->verification = $verification;
|
|
|
- $user->mobile = $mobile;
|
|
|
- $user->save();
|
|
|
+ if (!$mobile || !$captcha || !$code) {
|
|
|
+ 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) {
|
|
|
+ throw new Exception(__('Captcha is incorrect'));
|
|
|
+ }
|
|
|
|
|
|
- Sms::flush($mobile, 'changemobile');
|
|
|
- $this->success('success',$this->userInfo('return'));
|
|
|
+ $wechatCodeWhere['code'] = $code;
|
|
|
+ $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
|
|
|
+ if (empty($wechatCode)) {
|
|
|
+ throw new Exception('请先微信登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $where['mobile'] = $mobile;
|
|
|
+ $userData = model('User')->where($where)->find();//老用户
|
|
|
+ if (!empty($userData)) {
|
|
|
+ if (empty($userData['openid'])) {
|
|
|
+ model('User')->update(['wechat_openid' => $wechatCode['openid']],$where);//老用户更新openid
|
|
|
+ } else {
|
|
|
+ if ($userData['wechat_openid'] != $wechatCode['openid']) {
|
|
|
+ throw new Exception('该手机号已被其他用户绑定');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $ret = $this->auth->direct($userData['id']);
|
|
|
+ } else {
|
|
|
+ $extend = [
|
|
|
+ 'wechat_openid' => $wechatCode['openid'],
|
|
|
+ ];
|
|
|
+ $ret = $this->auth->register('', '','', $mobile, $extend);
|
|
|
+ }
|
|
|
+ if (!$ret) {
|
|
|
+ throw new Exception($this->auth->getError());
|
|
|
+ }
|
|
|
+
|
|
|
+ Sms::flush($mobile, 'changemobile');
|
|
|
+ Db::commit();
|
|
|
+ $this->success('success',$this->userInfo('return'));
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|