|
@@ -25,6 +25,57 @@ class User extends Api
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信小程序登录+注册
|
|
|
+ * code得到注册手机号,此手机号登录+注册
|
|
|
+ */
|
|
|
+ public function wxmini_regmobile_login(){
|
|
|
+ $code = input('code');
|
|
|
+ $opencode = input('opencode');
|
|
|
+ if (!$code || !$opencode) {
|
|
|
+ $this->error(__('Invalid parameters'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $config = config('wxMiniProgram');
|
|
|
+ $wechat = new Wechat($config['appid'],$config['secret']);
|
|
|
+
|
|
|
+ $getuserphonenumber = $wechat->getuserphonenumber($code);
|
|
|
+ if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
|
|
|
+ $this->error('授权获取手机号失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取openid
|
|
|
+ $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$opencode.'&grant_type=authorization_code';
|
|
|
+ $openidInfo = $this->getJson($getopenid);
|
|
|
+ if(!isset($openidInfo['openid'])) {
|
|
|
+ $this->error('用户openid获取失败',$openidInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ $openid = $openidInfo['openid'];
|
|
|
+ if (!$openid) {
|
|
|
+ $this->error('用户openid获取失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
|
|
|
+
|
|
|
+ $userInfo = Db::name('user')->where('mobile',$mobile)->find();
|
|
|
+ // 判断用户是否已经存在
|
|
|
+ if($userInfo) { // 登录
|
|
|
+ if ($userInfo['status'] != 1) {
|
|
|
+ $this->error(__('Account is locked'));
|
|
|
+ }
|
|
|
+ //如果已经有账号则直接登录
|
|
|
+ $res = $this->auth->direct($userInfo['id']);
|
|
|
+ } else {
|
|
|
+ $extend = ['wxmini_openid'=>$openid];
|
|
|
+ $res = $this->auth->register('', '', '',$mobile, $extend);
|
|
|
+ }
|
|
|
+ if($res) {
|
|
|
+ $this->success("登录成功!",$this->auth->getUserinfo());
|
|
|
+ } else {
|
|
|
+ $this->error($this->auth->getError());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//微信登录,预先假注册
|
|
|
public function wechatlogin(){
|