keepSnapshots(true); $this->addBehavior( new SoftDelete([ 'field' => 'deleted', 'value' => 1, ]) ); } public function beforeCreate() { if (empty($this->avatar)) { $this->avatar = kg_default_avatar_path(); } elseif (Text::startsWith($this->avatar, 'http')) { $this->avatar = self::getAvatarPath($this->avatar); } $this->create_time = time(); } public function beforeUpdate() { if (time() - $this->update_time > 3 * 3600) { $syncer = new UserIndexSyncer(); $syncer->addItem($this->id); } if (Text::startsWith($this->avatar, 'http')) { $this->avatar = self::getAvatarPath($this->avatar); } $this->update_time = time(); } public function afterCreate() { $cache = new MaxUserIdCache(); $cache->rebuild(); } public function afterUpdate() { if ($this->hasUpdated('name') || $this->hasUpdated('avatar')) { $imUser = ImUser::findFirst($this->id); $imUser->update([ 'name' => $this->name, 'avatar' => $this->avatar, ]); } } public function afterFetch() { if (!Text::startsWith($this->avatar, 'http')) { $this->avatar = kg_ci_avatar_img_url($this->avatar); } } public static function getAvatarPath($url) { if (Text::startsWith($url, 'http')) { return parse_url($url, PHP_URL_PATH); } return $url; } public static function genderTypes() { return [ self::GENDER_MALE => '男', self::GENDER_FEMALE => '女', self::GENDER_NONE => '保密', ]; } public static function eduRoleTypes() { return [ self::EDU_ROLE_STUDENT => '学员', self::EDU_ROLE_TEACHER => '讲师', ]; } }