修复注册用户录入错误

This commit is contained in:
mkm 2023-02-02 11:56:19 +08:00
parent c580709bb2
commit dda7d87b29
8 changed files with 59 additions and 41 deletions

View File

@ -108,7 +108,6 @@ abstract class BaseController
return false;
}
$user = Db::table('fa_user')->where('id',$user['n_user_id'])->find();
//$request->uid = $jwt_data['userid'];
define('JWT_UID', $user['id']);
// $response = $next($request);

View File

@ -93,14 +93,13 @@ class Userinfo extends BaseController
*/
public function Binding()
{
$post = get_params();
if(!$post['idcard'] || !$post['area_id'] || !$post['street_id'] || !$post['village_id'] || !$post['name']){
$this->apiError('缺少参数');
}
$where['id'] = JWT_UID;
$post['user_id'] = JWT_UID;
$where['id'] = $this->request->uid;
$post['user_id'] = $this->request->uid;
$user = Db::table('fa_user')->where($where)->field('id,nickname,group_id,mobile,avatar,username,createtime,score')->find();
// 已绑定
if ($user) {

View File

@ -16,32 +16,9 @@ use think\Response;
class Auth
{
protected $cookieDomain;
protected $header = [
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => 1800,
'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',
];
public function __construct(Config $config)
{
$this->cookieDomain = $config->get('cookie.domain', '');
}
public function handle($request, \Closure $next, ? array $header = [])
{
$header = !empty($header) ? array_merge($this->header, $header) : $this->header;
if (!isset($header['Access-Control-Allow-Origin'])) {
$origin = $request->header('origin');
if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
$header['Access-Control-Allow-Origin'] = $origin;
} else {
$header['Access-Control-Allow-Origin'] = '*';
}
}
$token = Request::header('x-Token');
if ($token) {
if (strpos($token, 'Bearer') === 0){
@ -50,22 +27,16 @@ class Auth
if (count(explode('.', $token)) != 3) {
return json(['code'=>404,'msg'=>'非法请求']);
}
$config = get_system_config('token');
//var_dump($config);exit;
try {
JWT::$leeway = 60;//当前时间减去60把时间留点余地
$decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式这里要和签发的时候对应
//return (array)$decoded;
// $decoded_array = json_decode(json_encode($decoded),TRUE);
// $jwt_data = $decoded_array['data'];
//$request->uid = $jwt_data['userid'];
// define('JWT_UID', $jwt_data['userid']);
$response = $next($request);
return $response;
//return $next($request);
$user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find();
if ($user &&$user['n_user_id']!=0){
$request->uid=$user['n_user_id'];
}else{
$request->uid=$this->addUser($decoded->jti[0],$user);
}
return $next($request);
} catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确
return json(['code'=>403,'msg'=>'签名错误']);
}catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用
@ -82,6 +53,55 @@ class Auth
} else {
return json(['code'=>404,'msg'=>'token不能为空']);
}
return $next($request)->header($header);;
}
public function addUser($uid,$nk_user){
$user=Db::connect('shop')->name('user')->where('uid',$uid)->find();
$msg=Db::table('fa_szxc_information_usermsg')->where('phone',$user['account'])->find();
Db::startTrans();
try {
if ($msg){
$users=Db::table('fa_user')->where('id',$msg['user_id'])->find();
if (!$nk_user){
$datas=[
'user_id'=>$user['uid'],
'n_user_id'=>$msg['user_id'],
'group_id'=>$users['group_id']
];
Db::connect('shop')->name('nk_user')->insert($datas);
}else{
Db::connect('shop')->name('nk_user')->where('id',$nk_user['id'])->update(['user_id'=>$user['uid'],'group_id'=>$users['group_id']]);
}
}else{
$time=time();
$user_data=[
'openid'=>'wx'.$time,
'group_id'=>1,
'username'=>'wx'.$time,
'nickname'=>'微信用户'.$time,
'avatar'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230104/32a639be4ee32349705e947fbbd1e114.png',
'level'=>1,
'prevtime'=>$time,
'jointime'=>$time,
'createtime'=>$time,
'updatetime'=>$time,
'status'=>'normal'
];
$n_user_id= Db::table('fa_user')->insertGetId($user_data);
$datas=[
'user_id'=>$user['uid'],
'n_user_id'=>$n_user_id,
'group_id'=>1
];
Db::connect('shop')->name('nk_user')->insert($datas);
return $n_user_id;
}
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return json(['code'=>400,'msg'=>$e->getMessage()]);
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 27 KiB