作者 liyuhang

gx

@@ -3,10 +3,10 @@ @@ -3,10 +3,10 @@
3 namespace App\Http\Controllers\Bside; 3 namespace App\Http\Controllers\Bside;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 -use App\Http\Logic\Bside\ComLogic;  
7 use App\Models\Project as ProjectModel; 6 use App\Models\Project as ProjectModel;
8 use App\Models\ProjectMenu as ProjectMenuModel; 7 use App\Models\ProjectMenu as ProjectMenuModel;
9 use App\Models\ProjectRole as ProjectRoleModel; 8 use App\Models\ProjectRole as ProjectRoleModel;
  9 +use App\Models\User as UserModel;
10 use Illuminate\Support\Facades\DB; 10 use Illuminate\Support\Facades\DB;
11 use Illuminate\Support\Facades\Validator; 11 use Illuminate\Support\Facades\Validator;
12 12
@@ -38,8 +38,8 @@ class ComController extends BaseController @@ -38,8 +38,8 @@ class ComController extends BaseController
38 if($validate->errors()->first()){ 38 if($validate->errors()->first()){
39 return $this->response($validate->errors()->first(),Code::USER_ERROR,$this->param); 39 return $this->response($validate->errors()->first(),Code::USER_ERROR,$this->param);
40 } 40 }
41 - $comLogic = new ComLogic();  
42 - $res = $comLogic->login($this->param); 41 + $userModel = new UserModel();
  42 + $res = $userModel->login($this->param);
43 if($res === false){ 43 if($res === false){
44 $this->response('当前用户不存在或者被禁用,登录失败',Code::USER_ERROR,[]); 44 $this->response('当前用户不存在或者被禁用,登录失败',Code::USER_ERROR,[]);
45 } 45 }
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Bside;
  4 +
  5 +use App\Models\ProjectRole as ProjectRoleModel;
  6 +use App\Models\User as UserModel;
  7 +use Illuminate\Support\Facades\Cache;
  8 +
  9 +class ComLogic extends BaseLogic
  10 +{
  11 + //统一设置
  12 + protected $casts = [
  13 + 'created_at' => 'datetime:Y-m-d H:i:s',
  14 + 'updated_at' => 'datetime:Y-m-d H:i:s',
  15 + ];
  16 + /***
  17 + * @name :登录
  18 + * @return void
  19 + * @author :liyuhang
  20 + * @method
  21 + */
  22 + public function login($param){
  23 + $userModel = new UserModel();
  24 + if(!isset($param['login_method'])){
  25 + //密码加密
  26 + $param['password'] = base64_encode(md5($param['password']));
  27 + $info = $userModel->read(['mobile'=>$param['mobile'],'password'=>$param['password'],'status'=>0], ['*']);
  28 + }else{
  29 + //TODO::验证验证码是否正确
  30 + $info = $userModel->read(['mobile'=>$param['mobile']],['*']);
  31 + }
  32 + if(empty($info)){
  33 + return false;
  34 + }
  35 + //当前用户角色是否被禁用
  36 + $projectRoleModel = new ProjectRoleModel();
  37 + $role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>0]);
  38 + if(empty($role_info)){
  39 + return false;
  40 + }
  41 + //验证码登录
  42 + if(isset($info['token']) && !empty($info['token'])){
  43 + //清除上一次用户缓存
  44 + Cache::pull($info['token']);
  45 + }
  46 + //生成新token
  47 + $token = md5(uniqid().$info['id']);
  48 + //存储缓存
  49 + Cache::add($token,$info);
  50 + $rs = $userModel->edit(['token'=>$token],['id'=>$info['id']]);
  51 + if($rs === false){
  52 + return false;
  53 + }
  54 + return $info;
  55 + }
  56 +}
@@ -3,10 +3,12 @@ @@ -3,10 +3,12 @@
3 namespace App\Models; 3 namespace App\Models;
4 4
5 //use Illuminate\Contracts\Auth\MustVerifyEmail; 5 //use Illuminate\Contracts\Auth\MustVerifyEmail;
  6 +use App\Models\ProjectRole as ProjectRoleModel;
6 use Illuminate\Database\Eloquent\Factories\HasFactory; 7 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model; 8 use Illuminate\Database\Eloquent\Model;
8 //use Illuminate\Foundation\Auth\User as Authenticatable; 9 //use Illuminate\Foundation\Auth\User as Authenticatable;
9 use Illuminate\Notifications\Notifiable; 10 use Illuminate\Notifications\Notifiable;
  11 +use Illuminate\Support\Facades\Cache;
10 use Laravel\Sanctum\HasApiTokens; 12 use Laravel\Sanctum\HasApiTokens;
11 13
12 class User extends Base 14 class User extends Base
@@ -43,5 +45,47 @@ class User extends Base @@ -43,5 +45,47 @@ class User extends Base
43 */ 45 */
44 protected $casts = [ 46 protected $casts = [
45 'email_verified_at' => 'datetime', 47 'email_verified_at' => 'datetime',
  48 + 'created_at' => 'datetime:Y-m-d H:i:s',
  49 + 'updated_at' => 'datetime:Y-m-d H:i:s',
46 ]; 50 ];
  51 +
  52 + /***
  53 + * @name :登录
  54 + * @return void
  55 + * @author :liyuhang
  56 + * @method
  57 + */
  58 + public function login($param){
  59 + if(!isset($param['login_method'])){
  60 + //密码加密
  61 + $param['password'] = base64_encode(md5($param['password']));
  62 + $info = $this->read(['mobile'=>$param['mobile'],'password'=>$param['password'],'status'=>0], ['*']);
  63 + }else{
  64 + //TODO::验证验证码是否正确
  65 + $info = $this->read(['mobile'=>$param['mobile']],['*']);
  66 + }
  67 + if(empty($info)){
  68 + return false;
  69 + }
  70 + //当前用户角色是否被禁用
  71 + $projectRoleModel = new ProjectRoleModel();
  72 + $role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>0]);
  73 + if(empty($role_info)){
  74 + return false;
  75 + }
  76 + //验证码登录
  77 + if(isset($info['token']) && !empty($info['token'])){
  78 + //清除上一次用户缓存
  79 + Cache::pull($info['token']);
  80 + }
  81 + //生成新token
  82 + $token = md5(uniqid().$info['id']);
  83 + //存储缓存
  84 + Cache::add($token,$info);
  85 + $rs = $this->edit(['token'=>$token],['id'=>$info['id']]);
  86 + if($rs === false){
  87 + return false;
  88 + }
  89 + return $info;
  90 + }
47 } 91 }