|
@@ -404,83 +404,60 @@ class User extends Api
|
|
*/
|
|
*/
|
|
public function wxMiniProgramLogin() {
|
|
public function wxMiniProgramLogin() {
|
|
$openid = $this->request->request('openid');// openid值
|
|
$openid = $this->request->request('openid');// openid值
|
|
- $encryptedData = $this->request->request('encryptedData');// 加密数据
|
|
|
|
- $iv = $this->request->request('iv');// 加密算法
|
|
|
|
- $signature = $this->request->request('signature');// 签名验证
|
|
|
|
- $rawData = $this->request->request('rawData');// 签名验证
|
|
|
|
- $logintype = 2;// 登录方式:1=手机号,2=微信授权openid
|
|
|
|
|
|
+ $avatar = $this->request->request('avatar');
|
|
|
|
+ $nickname = $this->request->request('nickname');
|
|
|
|
+ $mobile = $this->request->request('mobile');
|
|
|
|
|
|
- if (!$openid || !$encryptedData || !$iv) {
|
|
|
|
|
|
+ //学生信息
|
|
|
|
+ $field = ['realname','gender','school_id','grade_id','classes_id','stu_mobile','idcard'];
|
|
|
|
+ $stu_data = request_post_hub($field);
|
|
|
|
+ //学生信息
|
|
|
|
+
|
|
|
|
+ if (!$openid) {
|
|
$this->error(__('Invalid parameters'));
|
|
$this->error(__('Invalid parameters'));
|
|
}
|
|
}
|
|
|
|
|
|
// 获取openid和sessionkey
|
|
// 获取openid和sessionkey
|
|
- $config = config('wxMiniProgram');
|
|
|
|
$openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find();
|
|
$openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find();
|
|
- $openid = $openidInfo['openid'];
|
|
|
|
$session_key = $openidInfo['sessionkey'];
|
|
$session_key = $openidInfo['sessionkey'];
|
|
|
|
|
|
-// // 数据签名校验
|
|
|
|
-// $signature2 = sha1($rawData . $session_key);
|
|
|
|
-// if ($signature != $signature2) {
|
|
|
|
-// $this->error(__('数据签名验证失败'));
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
- // 根据加密数据和加密算法获取用户信息
|
|
|
|
- $pc = new WXBizDataCrypt($config['appid'], $session_key);
|
|
|
|
- $data = '';
|
|
|
|
- $errCode = $pc->decryptData(urldecode($encryptedData), $iv, $data);
|
|
|
|
- if ($errCode != 0) {
|
|
|
|
- $this->error('解密失败',['code'=>$errCode]);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $data = json_decode($data,true);
|
|
|
|
- // 用户登录逻辑 === 开始
|
|
|
|
- if($logintype == 1) { // 手机号登录
|
|
|
|
- /*$userInfo = Db::name('user')->where(["mobile"=>$data["purePhoneNumber"]])->find();
|
|
|
|
- // 用户信息不存在时使用
|
|
|
|
- $extend = ["mobile"=>$data["purePhoneNumber"]];*/
|
|
|
|
- } else { // 微信授权openid登录
|
|
|
|
- $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
|
|
|
|
- // 用户信息不存在时使用
|
|
|
|
- $extend = [
|
|
|
|
- 'mini_openid' => $openid,
|
|
|
|
- 'nickname' => $data['nickName'],
|
|
|
|
- 'avatar' => $data['avatarUrl'],
|
|
|
|
- //'gender' => $data['gender']==1 ? 1 : 0,
|
|
|
|
- 'mini_sessionkey'=> $session_key,
|
|
|
|
- 'unionid' => $openidInfo['unionid'],
|
|
|
|
- //'mobile' => $data['purePhoneNumber'],
|
|
|
|
- ];
|
|
|
|
- }
|
|
|
|
|
|
+ // 微信授权openid登录
|
|
|
|
+ $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
|
|
|
|
+ // 用户信息不存在时使用
|
|
|
|
+ $extend = [
|
|
|
|
+ 'mini_openid' => $openid,
|
|
|
|
+ 'nickname' => $nickname,
|
|
|
|
+ 'avatar' => $avatar,
|
|
|
|
+ 'mini_sessionkey'=> $session_key,
|
|
|
|
+ 'unionid' => $openidInfo['unionid'],
|
|
|
|
+ 'mobile' => $mobile,
|
|
|
|
+ ];
|
|
|
|
+
|
|
// 判断用户是否已经存在
|
|
// 判断用户是否已经存在
|
|
if($userInfo) { // 登录
|
|
if($userInfo) { // 登录
|
|
Db::name('user')->where('id',$userInfo['id'])->update(['logintime'=>time()]);
|
|
Db::name('user')->where('id',$userInfo['id'])->update(['logintime'=>time()]);
|
|
$res = $this->auth->direct($userInfo['id']);
|
|
$res = $this->auth->direct($userInfo['id']);
|
|
- } else { // 注册
|
|
|
|
- // 先随机一个用户名,随后再变更为u+数字id
|
|
|
|
|
|
+ } else {
|
|
|
|
+ // 注册
|
|
$username = '';
|
|
$username = '';
|
|
$password = '';
|
|
$password = '';
|
|
|
|
|
|
- /*Db::startTrans();
|
|
|
|
- try {*/
|
|
|
|
// 默认注册一个会员
|
|
// 默认注册一个会员
|
|
$result = $this->auth->register($username, $password, '','', $extend);
|
|
$result = $this->auth->register($username, $password, '','', $extend);
|
|
if (!$result) {
|
|
if (!$result) {
|
|
$this->error("注册失败!");
|
|
$this->error("注册失败!");
|
|
}
|
|
}
|
|
|
|
|
|
- /* Db::commit();
|
|
|
|
- } catch (PDOException $e) {
|
|
|
|
- Db::rollback();
|
|
|
|
- $this->auth->logout();
|
|
|
|
- return false;
|
|
|
|
- }*/
|
|
|
|
|
|
+ //学生入库
|
|
|
|
+ $stu_data['user_id'] = $this->auth->id;
|
|
|
|
+ Db::name('user_student')->insertGetId($stu_data);
|
|
|
|
+
|
|
|
|
+ //从学生库找人,放到注册人名下
|
|
|
|
+ Db::name('user_student')->where('user_id',0)->where('mobile',$mobile)->update(['user_id'=>$this->auth->id]);
|
|
|
|
|
|
- // 写入登录Cookies和Token
|
|
|
|
$res = $this->auth->direct($this->auth->id);
|
|
$res = $this->auth->direct($this->auth->id);
|
|
}
|
|
}
|
|
- $userInfo = $this->userInfo('return');
|
|
|
|
|
|
+ $userInfo = $this->getUserinfo('return');
|
|
if($res) {
|
|
if($res) {
|
|
$this->success("登录成功!",$userInfo);
|
|
$this->success("登录成功!",$userInfo);
|
|
} else {
|
|
} else {
|