|
@@ -13,7 +13,7 @@ use app\common\library\Wechat;
|
|
|
*/
|
|
|
class User extends Api
|
|
|
{
|
|
|
- protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
|
|
|
+ protected $noNeedLogin = ['login','mobilelogin','register','wechatlogin','bindmobile','resetpwd'];
|
|
|
protected $noNeedRight = '*';
|
|
|
|
|
|
public function _initialize()
|
|
@@ -131,6 +131,8 @@ class User extends Api
|
|
|
if(!$code){
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
+ $from = input('from','app');
|
|
|
+ $field = $from == 'app' ? 'app_openid' : 'mini_openid';
|
|
|
//微信
|
|
|
$wechat = new Wechat();
|
|
|
$wxuserinfo = $wechat->getAccessToken($code);
|
|
@@ -138,14 +140,15 @@ class User extends Api
|
|
|
if(!$wxuserinfo){
|
|
|
$this->error('openid获取失败');
|
|
|
}
|
|
|
- if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
|
|
|
+ if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid']) || !isset($wxuserinfo['unionid'])){
|
|
|
$this->error('openid获取失败');
|
|
|
}
|
|
|
|
|
|
$openid = $wxuserinfo['openid'];
|
|
|
+ $unionid = $wxuserinfo['unionid'];
|
|
|
|
|
|
//检查用户
|
|
|
- $user = Db::name('user')->where('wechat_openid',$openid)->find();
|
|
|
+ $user = Db::name('user')->where('unionid',$unionid)->find();
|
|
|
if ($user) {
|
|
|
if ($user['status'] == -1) {
|
|
|
$this->error('账户已注销');
|
|
@@ -153,6 +156,12 @@ class User extends Api
|
|
|
if ($user['status'] != 1) {
|
|
|
$this->error(__('Account is locked'));
|
|
|
}
|
|
|
+
|
|
|
+ /*$update = [
|
|
|
+ $field => $openid,//应该就只有mini支付时用
|
|
|
+ ];
|
|
|
+ Db::name('user')->where('id',$user['id'])->update($update);*/
|
|
|
+
|
|
|
//如果已经有账号则直接登录
|
|
|
$ret = $this->auth->direct($user['id']);
|
|
|
|
|
@@ -168,15 +177,17 @@ class User extends Api
|
|
|
} else {
|
|
|
//记录code和openid,绑定手机号的时候更新openid
|
|
|
$wechatCodeData = [
|
|
|
- 'code' => $code,
|
|
|
- 'openid' => $openid,
|
|
|
+ 'code' => $code,
|
|
|
+ 'openid' => $openid,
|
|
|
+ 'unionid' => $unionid,
|
|
|
+ 'from' => $from,
|
|
|
'createtime' => time(),
|
|
|
];
|
|
|
- $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
|
|
|
+ $wechatCode = Db::name('wechat_code')->where(['unionid'=>$unionid,'from'=>$from])->find();
|
|
|
if (empty($wechatCode)) {
|
|
|
Db::name('wechat_code')->insertGetId($wechatCodeData);
|
|
|
} else {
|
|
|
- Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
|
|
|
+ Db::name('wechat_code')->where(['unionid'=>$unionid,'from'=>$from])->update($wechatCodeData);
|
|
|
}
|
|
|
|
|
|
//直接返回
|
|
@@ -201,6 +212,9 @@ class User extends Api
|
|
|
$captcha = input('captcha');
|
|
|
$code = input('code');
|
|
|
|
|
|
+ $from = input('from','app');
|
|
|
+ $field = $from == 'app' ? 'app_openid' : 'mini_openid';
|
|
|
+
|
|
|
if (!$mobile || !$captcha || !$code) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
@@ -221,7 +235,7 @@ class User extends Api
|
|
|
}
|
|
|
|
|
|
//检查appid绑定的用户
|
|
|
- $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
|
|
|
+ $user = Db::name('user')->where('unionid',$wechatCode['unionid'])->find();
|
|
|
if ($user) {
|
|
|
if ($user['status'] == -1) {
|
|
|
$this->error('账户已注销');
|
|
@@ -239,17 +253,18 @@ class User extends Api
|
|
|
$where['mobile'] = $mobile;
|
|
|
$userData = Db::name('user')->where($where)->find();//老用户
|
|
|
if (!empty($userData)) {
|
|
|
- if (empty($userData['wechat_openid'])) {
|
|
|
- Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
|
|
|
+ if (empty($userData['unionid'])) {
|
|
|
+ Db::name('user')->where('id',$userData['id'])->update([$field => $wechatCode['openid'],'unionid' => $wechatCode['unionid']]);//老用户更新openid
|
|
|
} else {
|
|
|
- if ($userData['wechat_openid'] != $wechatCode['openid']) {
|
|
|
+ if ($userData['unionid'] != $wechatCode['unionid']) {
|
|
|
$this->error('该手机号已被其他用户绑定');
|
|
|
}
|
|
|
}
|
|
|
$ret = $this->auth->direct($userData['id']);
|
|
|
} else {
|
|
|
$extend = [
|
|
|
- 'wechat_openid' => $wechatCode['openid'],
|
|
|
+ $field => $wechatCode['openid'],
|
|
|
+ 'unionid' => $wechatCode['unionid'],
|
|
|
];
|
|
|
$ret = $this->auth->register('', '','', $mobile, $extend);
|
|
|
}
|
|
@@ -286,7 +301,8 @@ class User extends Api
|
|
|
$data = [
|
|
|
'status' => -1,
|
|
|
'mobile' => 'close_'.$this->auth->mobile,
|
|
|
- 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
|
|
|
+ 'app_openid' => 'close_'.$this->auth->app_openid,
|
|
|
+ 'mini_openid' => 'close_'.$this->auth->mini_openid,
|
|
|
// 'ios_user_id' => 'close_'.$this->auth->ios_user_id,
|
|
|
];
|
|
|
Db::name('user')->where('id',$this->auth->id)->update($data);
|