50], [['password'], 'string', 'max' => 255], [['auth_key', 'access_token'], 'string', 'max' => 128], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'username' => '登录账号', 'account' => '账号名称', 'password' => '密码', 'mall_id' => '商城id', 'store_id' => '门店id', 'user_id' => '会员id', 'auth_key' => 'Auth Key', 'access_token' => 'Access Token', 'admin_type' => '管理员类型1超级管理员2管理员3操作员', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'status' => '状态[-1:删除;0:禁用;1启用]', ]; } /**YII登录机制实现接口 */ public static function findIdentity($id) { return static::findOne($id); } public static function findIdentityByAccessToken($token, $type = null) { return static::findOne(['access_token' => $token]); } public function getId() { return $this->id; } public function getAuthKey() { return $this->auth_key; } public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password); } public static function login($params) { try { $username = $params['username'] ?? ''; $password = $params['password'] ?? ''; $where = ['status' => StatusEnum::ENABLED, 'username' => $username, 'mall_id' => $params['mall_id']]; $admin = HappinessStoreAdmin::findOne($where); if (empty($admin)) throw new Exception('登录账号不存在'); $store = HappinessStore::findOne(['id' => $admin['store_id'], 'mall_id' => $params['mall_id']]); if (empty($store) || $store->status == StatusEnum::DELETE) throw new Exception('门店不存在'); if ($store->status == StatusEnum::DISABLED) throw new Exception('该门店已被禁用'); // if (!empty($store['expire_time']) && time() > $store['expire_time']) { // throw new Exception('您的账号已过期'); // } // 密码校验 if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误'); // 管理员类型处理 $redirect_url = 'happiness/client/index/index'; Yii::$app->session->set('happiness_store', $store); $mall = Mall::findOne($params['mall_id']); Yii::$app->session->set('mall', $mall); // 执行登录 $config_params = \Yii::$app->params; $happiness_login_remember_time = $config_params['happiness_login_remember_time'] ?? 86400 * 30; $happiness_login_no_remember_time = $config_params['happiness_login_no_remember_time'] ?? 3600 * 8; if (Yii::$app->happiness_store_user->login($admin, $params['is_remember'] ? $happiness_login_remember_time : $happiness_login_no_remember_time)) { return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']]; } else { throw new \Exception('登录失败'); } } catch (Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 检测参数数据 * @Author: 广东七件事 zal * @Date: 2020-04-08 * @Time: 19:49 * @param $arr * @param $data * @throws \Exception */ private static function checkData($arr, $data) { foreach ($arr as $key => $item) { if (!isset($data[$key])) { throw new \Exception('请传参数' . $key . '->' . $item); } } } public static function del($params) { $tr = \Yii::$app->db->beginTransaction(); try { $model = self::findOne($params); $model->status = StatusEnum::DELETE; if ($model->admin_type == '1' && $model->username == 'admin') throw new Exception('超级管理员不允许删除'); $res = $model->save(); if ($res === false) throw new Exception(self::getStaticError()); $tr->commit(); return $model; } catch (Exception $e) { $tr->rollBack(); self::$static_error = $e->getMessage(); return false; } } public static function loginByH5($params) { try { $username = $params['username'] ?? ''; $password = $params['password'] ?? ''; $where = ['status' => StatusEnum::ENABLED, 'username' => $username, 'mall_id' => $params['mall_id']]; $admin = HappinessStoreAdmin::findOne($where); if (empty($admin)) throw new Exception('登录账号不存在'); // 密码校验 if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误'); $store = HappinessStore::findOne(['id' => $admin['store_id']]); if (empty($store['status'])) { throw new Exception('门店已禁用'); } // if ($store['expire_time'] > 0 && $store['expire_time'] < time()) { // throw new Exception('门店已过期'); // } $where = []; $where[] = ['mall_id' => $params['mall_id']]; $where[] = ['id' => $admin['user_id']]; $where[] = ['status' => StatusEnum::ENABLED]; $user = \common\models\user\User::getUser($where); if (empty($user)) throw new Exception('门店会员不存在'); $token = Yii::$app->services->apiAccessToken->getAccessToken($user, 'happiness'); if (empty($token['access_token'])) throw new Exception('登录错误'); $token['access_happiness_token'] = $token['access_token']; $api_access_token = Yii::$app->services->apiAccessToken->getUserToken($user, 'api'); $token['access_token'] = $api_access_token['access_token']; AccessToken::updateAll(['status' => StatusEnum::ENABLED], ['access_token' => $api_access_token['access_token']]); return $token; } catch (Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 获取门店ID获取管理员 * * @param integer $mall_id * @param integer $store_id * @return static|null */ public static function getAdminByStoreId(int $mall_id, int $store_id) { return static::find() ->where(compact('mall_id', 'store_id')) ->one(); } /** * 修改密码 * * @param string $pwd * @return boolean */ public function changePwd(string $pwd) { $this->password = Yii::$app->security->generatePasswordHash($pwd); return $this->save(); } }