Sfoglia il codice sorgente

师傅端增加退出登录,更改密码,手机号接口

林海涛 1 anno fa
parent
commit
5db9008efd

+ 1 - 24
app/api/validate/PasswordValidate.php

@@ -25,7 +25,7 @@ class PasswordValidate extends BaseValidate
 
     protected $rule = [
         'mobile' => 'require|mobile',
-        'code' => 'require',
+        'code' => 'require|checkCode',
         'password' => 'require|length:6,20|alphaNum',
         'password_confirm' => 'require|confirm',
     ];
@@ -43,27 +43,4 @@ class PasswordValidate extends BaseValidate
     ];
 
 
-    /**
-     * @notes 重置登录密码
-     * @return PasswordValidate
-     * @author 段誉
-     * @date 2022/9/16 18:11
-     */
-    public function sceneResetPassword()
-    {
-        return $this->only(['mobile', 'code', 'password', 'password_confirm']);
-    }
-
-
-    /**
-     * @notes 修改密码场景
-     * @return PasswordValidate
-     * @author 段誉
-     * @date 2022/9/20 19:14
-     */
-    public function sceneChangePassword()
-    {
-        return $this->only(['password', 'password_confirm']);
-    }
-
 }

+ 19 - 0
app/workerapi/config/route.php

@@ -0,0 +1,19 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+return [
+    'middleware' => [
+        app\workerapi\http\middleware\InitMiddleware::class, // 初始化
+        app\workerapi\http\middleware\LoginMiddleware::class, // 登录验证
+    ],
+];

+ 0 - 14
app/workerapi/controller/LoginController.php

@@ -84,18 +84,4 @@ class LoginController extends BaseApiController
         return $this->success();
     }
 
-    /**
-     * @notes 更新用户头像昵称
-     * @return \think\response\Json
-     * @author 段誉
-     * @date 2023/2/22 11:15
-     */
-    public function updateUser()
-    {
-        $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
-        LoginLogic::updateUser($params, $this->userId);
-        return $this->success('操作成功', [], 1, 1);
-    }
-
-
 }

+ 43 - 0
app/workerapi/controller/MasterWokerController.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace app\workerapi\controller;
+
+use app\workerapi\logic\MasterWokerLogic;
+use app\workerapi\validate\MasterWokerValidate;
+
+class MasterWokerController extends BaseApiController
+{
+
+    /**
+     * @notes 修改密码
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/20 19:16
+     */
+    public function changePassword()
+    {
+        $params = (new MasterWokerValidate())->post()->goCheck('changePassword');
+        $result = MasterWokerLogic::changePassword($params, $this->userId);
+        if (true === $result) {
+            return $this->success('操作成功', [], 1, 1);
+        }
+        return $this->fail(MasterWokerLogic::getError());
+    }
+
+    /**
+     * 更改手机号
+     * @return void
+     * @author 林海涛
+     * @date 2024/7/10 下午2:23
+     */
+    public function changeMobile()
+    {
+        $params = (new MasterWokerValidate())->post()->goCheck('changeMobile');
+        $result = MasterWokerLogic::changeMobile($params, $this->userId);
+        if (true === $result) {
+            return $this->success('操作成功', [], 1, 1);
+        }
+        return $this->fail(MasterWokerLogic::getError());
+    }
+
+}

+ 56 - 0
app/workerapi/http/middleware/InitMiddleware.php

@@ -0,0 +1,56 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\workerapi\http\middleware;
+
+
+use app\common\exception\ControllerExtendException;
+use app\workerapi\controller\BaseApiController;
+use think\exception\ClassNotFoundException;
+use think\exception\HttpException;
+
+
+class InitMiddleware
+{
+
+    /**
+     * @notes 初始化
+     * @param $request
+     * @param \Closure $next
+     * @return mixed
+     * @throws ControllerExtendException
+     * @author 段誉
+     * @date 2022/9/6 18:17
+     */
+    public function handle($request, \Closure $next)
+    {
+        //获取控制器
+        try {
+            $controller = str_replace('.', '\\', $request->controller());
+            $controller = '\\app\\workerapi\\controller\\' . $controller . 'Controller';
+            $controllerClass = invoke($controller);
+            if (($controllerClass instanceof BaseApiController) === false) {
+                throw new ControllerExtendException($controller, '404');
+            }
+        } catch (ClassNotFoundException $e) {
+            throw new HttpException(404, 'controller not exists:' . $e->getClass());
+        }
+        //创建控制器对象
+        $request->controllerObject = invoke($controller);
+
+        return $next($request);
+    }
+
+}

+ 76 - 0
app/workerapi/http/middleware/LoginMiddleware.php

@@ -0,0 +1,76 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\workerapi\http\middleware;
+
+
+use app\common\cache\MasterWokerTokenCache;
+use app\common\cache\UserTokenCache;
+use app\common\service\JsonService;
+use app\api\service\UserTokenService;
+use app\workerapi\service\MasterWokerTokenService;
+use think\facade\Config;
+
+class LoginMiddleware
+{
+    /**
+     * @notes 登录验证
+     * @param $request
+     * @param \Closure $next
+     * @return mixed|\think\response\Json
+     * @author 令狐冲
+     * @date 2021/7/1 17:33
+     */
+    public function handle($request, \Closure $next)
+    {
+        $token = $request->header('token');
+        //判断接口是否免登录
+        $isNotNeedLogin = $request->controllerObject->isNotNeedLogin();
+
+        //不直接判断$isNotNeedLogin结果,使不需要登录的接口通过,为了兼容某些接口可以登录或不登录访问
+        if (empty($token) && !$isNotNeedLogin) {
+            //没有token并且该地址需要登录才能访问, 指定show为0,前端不弹出此报错
+            return JsonService::fail('请求参数缺token', [], 0, 0);
+        }
+
+        $userInfo = (new MasterWokerTokenCache())->getUserInfo($token);
+
+        if (empty($userInfo) && !$isNotNeedLogin) {
+            //token过期无效并且该地址需要登录才能访问
+            return JsonService::fail('登录超时,请重新登录', [], -1, 0);
+        }
+
+        //token临近过期,自动续期
+        if ($userInfo) {
+            //获取临近过期自动续期时长
+            $beExpireDuration = Config::get('project.user_token.be_expire_duration');
+            //token续期
+            if (time() > ($userInfo['expire_time'] - $beExpireDuration)) {
+                $result = MasterWokerTokenService::overtimeToken($token);
+                //续期失败(数据表被删除导致)
+                if (empty($result)) {
+                    return JsonService::fail('登录过期', [], -1);
+                }
+            }
+        }
+
+        //给request赋值,用于控制器
+        $request->userInfo = $userInfo;
+        $request->userId = $userInfo['user_id'] ?? 0;
+
+        return $next($request);
+    }
+
+}

+ 12 - 1
app/workerapi/logic/LoginLogic.php

@@ -27,7 +27,7 @@ class LoginLogic extends BaseLogic
     public static function confirmMobile(array $params)
     {
         try {
-            // 变更手机号场景
+            // 验证码请求
             $sceneId = NoticeEnum::OTHER_CAPTCHA;
             // 校验短信
 //            $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
@@ -99,4 +99,15 @@ class LoginLogic extends BaseLogic
             return false;
         }
     }
+
+    public static function logout($userInfo)
+    {
+        //token不存在,不注销
+        if (!isset($userInfo['token'])) {
+            return false;
+        }
+        //设置token过期
+        return MasterWokerTokenService::expireToken($userInfo['token']);
+    }
+
 }

+ 67 - 0
app/workerapi/logic/MasterWokerLogic.php

@@ -0,0 +1,67 @@
+<?php
+namespace app\workerapi\logic;
+use app\common\logic\BaseLogic;
+use app\common\model\master_worker\MasterWorker;
+use think\facade\Config;
+
+/**
+ * @author 林海涛
+ * @date 2024/7/10 下午1:45
+ */
+class MasterWokerLogic extends  BaseLogic
+{
+    public static function changePassword(array $params, int $userId)
+    {
+        try {
+
+            $user = MasterWorker::findOrEmpty($userId);
+            if ($user->isEmpty()) {
+                throw new \Exception('用户不存在');
+            }
+            // 密码盐
+            $passwordSalt = Config::get('project.unique_identification');
+            if (!empty($user['password'])) {
+                if (empty($params['old_password'])) {
+                    throw new \Exception('请填写旧密码');
+                }
+                $oldPassword = create_password($params['old_password'], $passwordSalt);
+                if ($oldPassword != $user['password']) {
+                    throw new \Exception('原密码不正确');
+                }
+            }
+            // 保存密码
+            $password = create_password($params['password'], $passwordSalt);
+            $user->password = $password;
+            $user->save();
+            return true;
+        } catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+    public static function changeMobile(array $params, int $userId)
+    {
+        try {
+
+            $user = MasterWorker::findOrEmpty($userId);
+            if ($user->isEmpty()) {
+                throw new \Exception('用户不存在');
+            }
+            if($user->mobile == $params['mobile']){
+                throw new \Exception('输入的手机号相同');
+            }
+            $where = [['mobile', '=', $params['mobile']]];
+            $exitUser = MasterWorker::where($where)->findOrEmpty();
+            if (!$exitUser->isEmpty()) {
+                throw new \Exception('该手机号已被使用');
+            }
+            $user->password = $params['mobile'];
+            $user->save();
+            return true;
+        } catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+}

+ 52 - 0
app/workerapi/validate/MasterWokerValidate.php

@@ -0,0 +1,52 @@
+<?php
+namespace app\workerapi\validate;
+use app\common\enum\notice\NoticeEnum;
+use app\common\service\sms\SmsDriver;
+use app\common\validate\BaseValidate;
+
+/**
+ * @author 林海涛
+ * @date 2024/7/10 下午1:42
+ */
+
+class MasterWokerValidate  extends BaseValidate
+{
+    protected $rule = [
+        'mobile' => 'require|mobile',
+        'code' => 'require|checkCode',
+        'password' => 'require|length:6,20|alphaNum',
+        'password_confirm' => 'require|confirm',
+    ];
+
+
+    protected $message = [
+        'mobile.require' => '请输入手机号',
+        'mobile.mobile' => '请输入正确手机号',
+        'code.require' => '请填写验证码',
+        'password.require' => '请输入密码',
+        'password.length' => '密码须在6-25位之间',
+        'password.alphaNum' => '密码须为字母数字组合',
+        'password_confirm.require' => '请确认密码',
+        'password_confirm.confirm' => '两次输入的密码不一致'
+    ];
+
+    public function checkCode($code, $rule, $data)
+    {
+        $smsDriver = new SmsDriver();
+        $result = $smsDriver->verify($data['mobile'], $code, NoticeEnum::CHANGE_MOBILE_CAPTCHA);
+        if ($result) {
+            return true;
+        }
+        return true;
+        //return '验证码错误';
+    }
+
+    public function sceneChangeMobile(){
+        return $this->only(['mobile', 'code']);
+    }
+
+    public function sceneChangePassword()
+    {
+        return $this->only(['password', 'password_confirm']);
+    }
+}

+ 71 - 0
app/workerapi/validate/SetMasterWorkerInfoValidate.php

@@ -0,0 +1,71 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\workerapi\validate;
+
+
+use app\common\model\user\User;
+use app\common\validate\BaseValidate;
+
+
+/**
+ * 设置师傅信息
+ */
+class SetMasterWorkerInfoValidate extends BaseValidate
+{
+    protected $rule = [
+        'field' => 'require|checkField',
+        'value' => 'require',
+    ];
+
+    protected $message = [
+        'field.require' => '参数缺失',
+        'value.require' => '值不存在',
+    ];
+
+
+    /**
+     * @notes 校验字段内容
+     * @param $value
+     * @param $rule
+     * @param $data
+     * @return bool|string
+     * @author 段誉
+     * @date 2022/9/21 17:01
+     */
+    protected function checkField($value, $rule, $data)
+    {
+        $allowField = [
+            'nickname', 'account', 'sex', 'avatar', 'real_name',
+        ];
+
+        if (!in_array($value, $allowField)) {
+            return '参数错误';
+        }
+
+        if ($value == 'account') {
+            $user = User::where([
+                ['account', '=', $data['value']],
+                ['id', '<>', $data['id']]
+            ])->findOrEmpty();
+            if (!$user->isEmpty()) {
+                return '账号已被使用!';
+            }
+        }
+
+        return true;
+    }
+
+
+}