TimestampBehavior::class, 'value' => time() ] ]; } public function rules() { return [ [['is_delete', 'store_id', 'type_id', 'max_app_count', 'expire_time', 'saas_user_id', 'is_enable','advert_push_user_id'], 'integer'], [['max_price', 'rate'], 'number'], [['access_token'], 'string', 'max' => 60], [['email', 'name', 'username', 'password', 'type'], 'string', 'max' => 100], [['avatar'], 'string', 'max' => 255], [['email'], 'email'], [['access_token', 'create_at', 'update_at'], 'safe'], [['address', 'mobile'], 'string'] ]; } public function attributeLabels() { return [ 'id' => 'ID', 'province_id' => 'province_id', 'city_id' => 'city_id', 'district_id' => 'district_id', 'area_level' => 'area_level', 'access_token' => 'access token', 'create_at' => '创建时间', 'update_at' => '更新时间', 'username' => '用户名', 'password' => '密码', 'mobile' => '手机号', 'remark' => '备注', 'email' => '邮箱', 'name' => '昵称', 'avatar' => '头像', 'is_delete' => '是否已删除', 'store_id' => 'store_id', 'type' => '管理员类型', 'type_id' => 'type id', 'max_app_count' => '最大创建商城数量', 'expire_time' => '过期时间', 'max_price' => '允许设置的最大佣金', 'rate' => '代理商服务费率', 'is_enable' => '是否开启', 'address' => '代理地址' ]; } public function beforeSave($insert) { // 创建用户自动赋值access_token if (parent::beforeSave($insert)) { if ($this->isNewRecord) { $this->access_token = \Yii::$app->security->generateRandomString(); } if (empty($this->max_price)) { $this->max_price = 0.00; } if (empty($this->rate)) { $this->rate = 0.00; } $this->mobile = \str_replace(' ', '', $this->mobile); /*if($this->type == 'store' && $this->saas_user_id > 0){ if($this->isNewRecord){ $is = self::find()->where(['type' => 'store', 'saas_user_id' => $this->saas_user_id, 'is_delete' => 0])->one(); }else{ $is = self::find()->where(['type' => 'store', 'saas_user_id' => $this->saas_user_id, 'is_delete' => 0])->andWhere(['!=', 'id', $this->id])->one(); } if($is){ $this->addError('mobile', '操作失败,其他管理员['. $is->username .']已绑定此用户!请更换'); return false; } }*/ return true; } return false; } /** * @param int|string $id * @return Admin|IdentityInterface|null */ public static function findIdentity($id) { return static::findOne($id); } /** * @param mixed $token * @param null $type * @return Admin|IdentityInterface|null */ public static function findIdentityByAccessToken($token, $type = null) { return static::findOne(['access_token' => $token, 'is_delete' => 0]); } /** * @param $username */ public static function findByUsername($username) { return static::find()->where(['is_delete' => 0])->andWhere(['or',['username' => $username,],['name' => $username]])->one(); } /** * @param $email * @return Admin|null */ public static function findByEmail($email) { return static::findOne(['email' => $email, 'is_delete' => 0]); } /** * @param $mobile * @return Admin|null */ public static function findByMobile($mobile) { return static::findOne(['mobile' => $mobile, 'is_delete' => 0]); } /** * @return int|string */ public function getId() { return $this->id; } /** * 刷新token * @return bool */ public function refreshToken() { $this->access_token = \Yii::$app->security->generateRandomString(); return $this->save(); } /** * 验证密码 * @param $password * @return bool */ public function validatePassword($password) { return \Yii::$app->security->validatePassword($password, $this->password); } /** * 获取角色 * @return \yii\db\ActiveQuery * @throws \yii\base\InvalidConfigException */ public function getRoles() { return $this->hasMany(AuthRole::class, ['id' => 'role_id']) ->viaTable('{{%admin_role}}', ['admin_id' => 'id']); } /** * 获取平台角色 * @return \yii\db\ActiveQuery * @throws \yii\base\InvalidConfigException */ public function getSaasRoles() { return $this->hasMany(SaasAuthRole::class, ['id' => 'role_id']) ->viaTable('{{%saas_admin_role}}', ['admin_id' => 'id']); } }