yii identify

技术问题 · cqslxz · 于 5年前 发布 · 1433 次阅读
你好,@fecshop。我想请教个问题?我现在想加一个用户表,该表内用户可能不是fecshop商城的用户。只用在使用fercshop功能的时候,我在我们fecshop的customer表内,如果没有该用户就生成。流程是:我登录使用 新表【Yii::$app->gos->identity】,如果使用到fecshop就使用原来的【Yii::$app->user->identity】,让其fecshop登录代码在【fecshop\app\appserver\modules\AppserverTokenController】ini()里面代码如图

protected function actionLoginById($customer_id=0){
	  	   $data  =  null;
           if($customer_id){
              $model = $this->_customerLoginModel;
              $model->id = $customer_id;
              $data = $model->login();
              // $data =  $this->_customerModel->find()->where(['id'=>$customer_id])->one();
              // $data =  \Yii::$app->user->login($data, 0);
           }
           return $data;//'fecshop login fail';
	  }
现在【Yii::$app->gos->identity】上面在第一次运行后一直为空。数据对象没有存入。
我的配置文件【appserver/config/main.php】是
 'user' => [
            // 'class'            => 'fecshop\yii\web\User',
            // 【默认】不开启速度限制的 User Model
            'identityClass'     => 'fecshop\models\mysqldb\Customer',
            // 开启速度限制的 User Model
            // 'identityClass'     => 'fecshop\models\mysqldb\customer\CustomerAccessToken',
        ],
        'gos'=>[
             'class'            => 'fecshop\yii\web\User',
             'identityClass'     => 'common\local\local_models\mysqldb\User',
             // 'idParam'          => '__id',
        ],
我排了很久,不知道神魔原因?不知道您知不知道原因。十分感谢!!!感谢!
共收到 2 条回复 问题提问
Fecmall#15年前 0 个赞

用户不存在,则强行注册一个用户,然后登陆?

先说说你的思路,你到底要干什么?

Fecmall#25年前 0 个赞

appserver端的登陆是基于access-token的

参看代码:https://github.com/fecshop/yii2_fecshop/blob/master/services/Customer.php#L644

/** AppServer 部分使用的函数
     * @param $email | String
     * @param $password | String
     * 无状态登录,通过email 和password进行登录
     * 登录成功后,合并购物车,返回accessToken
     * ** 该函数是未登录用户,通过参数进行登录需要执行的函数。
     */
    protected function actionLoginAndGetAccessToken($email, $password)
    {
        $header = Yii::$app->request->getHeaders();
        if (isset($header['access-token']) && $header['access-token']) {
            $accessToken = $header['access-token'];
        }
        // 如果request header中有access-token,则查看这个 access-token 是否有效
        if ($accessToken) {
            $identity = Yii::$app->user->loginByAccessToken($accessToken);
            if ($identity !== null) {
                $access_token_created_at = $identity->access_token_created_at;
                $timeout = Yii::$service->session->timeout;
                if ($access_token_created_at + $timeout > time()) {
                    return $accessToken;
                }
            }
        }
        // 如果上面access-token不存在
        $data = [
            'email'     => $email,
            'password'  => $password,
        ];
        
        if (Yii::$service->customer->login($data)) {
            $identity = Yii::$app->user->identity;
            $identity->generateAccessToken();
            $identity->access_token_created_at = time();
            $identity->save();
            // 执行购物车合并等操作。
            Yii::$service->cart->mergeCartAfterUserLogin();
            $this->setHeaderAccessToken($identity->access_token);
            return $identity->access_token;
        }
    }
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics