工作中的登录流程以及代码备份

来源:互联网 发布:linux 查看最后几行 编辑:程序博客网 时间:2024/05/22 02:08

最近我在给APP提供用户相关接口,其中用到登录接口,注册接口.....下面我将一一总结我的接口流程和代码,以免以后我会忘,嘿嘿,当然了因为我是一个忘事大王,写过的东西都会忘...........

 【登录】

                   流程:

                - 判断账号和密码不能为空; - 判断密码格式 - 判断密码错误 - 判断用户信息是否存在 - 判断用户状态 - 当登录成功后,将用户的token信息,sig,user_id返回 - 当登录成功后去更新登录表中的最后登录IP和修改时间字段
                  代码:
     /**      * Action Login      * 用户登录[手机号+密码]      */     public function actionLogin()      {        $params = Yii::$app->request->post();        $mAccount = new Account();        $util = Yii::$app->util;        if (!$params) {            return yii::$app->util->formatResData(1100, '无参数提供', (object)[], false);        }        if (!isset($params['mobile']) || !$params['mobile']) {            return yii::$app->util->formatResData(1101, '手机号不能为空', (object)[], false);        }        if (!$util->regularMobile($params['mobile'])) {            return $util->formatResData(1102, '手机号码格式不正确', (object)[], false);        }        if (!isset($params['password']) || !$params['password']) {            return yii::$app->util->formatResData(1103, '密码不能为空', (object)[], false);        }        $regular = "/((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}$/";        if (!preg_match($regular, $params['password'])) {            return $util->formatResData(1104, '密码格式不正确', (object)[], false);        }        $user = $mAccount->getByMobilePwd($params['mobile'], $params['password']);        $inputPwd = md5(md5($params['password']) . $user['salt']);        //判断该用户是否存在        if ($user) {            if ($user['status'] == Account::STATUS_DISABLE) {                return yii::$app->util->formatResData(1106, '该用户已被禁用,请联系管理员', (object)[], false);            } else if ($user['status'] == Account::STATUS_UNINIT){                return $util->formatResData(3101, '用户信息不存在,请您注册', (object)[], false);            }        } else {            return $util->formatResData(3101, '用户信息不存在,请您注册', (object)[], false);        }        if ($inputPwd != $user['password']) {            return yii::$app->util->formatResData(1105, '密码错误', (object)[], false);        }        $lastLoginIp = Yii::$app->util->getClientIP();        //待修改字段        $fields = ['last_login_ip' => $lastLoginIp];        //创建token,并将token存储在redis        $handleData = $this->handleSession($user["user_id"], $params["mobile"]);        if (!isset($handleData['variable']) || !$handleData['variable']) {            return $handleData['error'];        }        $session = $handleData['variable'];        //修改用户登录表中的last_login_ip,updated_at        $result = $mAccount->renew($params['mobile'], $fields, $inputPwd);        return yii::$app->util->formatResData(0, '登录成功', $session, false);    }
      里面涉及的方法:
    /**     * 创建token,并将token存储在redis上     *     * @param string $userId 用户ID     * @param string $mobile 手机号     * @param int  $atTime  access_token保存时间     * @param int  $rtTime  refresh_token保存时间     *     * @return array     * 说明: 返回的值: access_token,refresh_token,user_id,sig     */    // protected function handleSession($userId, $mobile, $atTime = 300, $rtTime = 2592000)    // protected function handleSession($userId, $mobile, $atTime = 120, $rtTime = 2592000)    protected function handleSession($userId, $mobile, $atTime = 86400, $rtTime = 2592000)    {        $now = time();        $accessToken =  md5('at' . $mobile . $userId . $now);        $refreshToken = md5('rt' . $mobile . $userId . $now);        $token = ['access_token' => $accessToken, 'refresh_token' => $refreshToken];        try {            // FIXED: 设置过期时间设置的为经过多少秒过去而不是时间点,如果是时间点应该单独使用expireat            $cache = Yii::$app->cache->instance('base');            $cache->setex('user:at:' . $accessToken, $atTime, $userId);            $cache->setex('user:rt:' . $refreshToken, $rtTime, $userId);            $sig = Yii::$app->sig->getSig($userId);            $result['variable'] = [                'access_token' => $accessToken,                'refresh_token' => $refreshToken,                'user_id' => $userId,                'sig' => $sig            ];        } catch (\Exception $e) {            $result['error'] = yii::$app->util->formatResData(-100, '连接redis服务器失败,请稍后重试 ', (object)[], false);        }        return $result;    }

感想: 

         其实看似登录注册很简单,其实不然,里面需要考虑的事情有好多,因为我们项目中我们组长由得封装好了,而我只是拿来用,作为一个新手来说,我有好多不会的啊,所以我就整理了一下自己的工作项目代码,一来是以后有个参照,二来是自己记性差,以后要是忘了,可以没事看看!

0 0
原创粉丝点击