model = new WechatOfficialAccount(); } public function getConfig(): array { $model = $this->model->findOrEmpty(self::CONFIG_ID); if (!$model->isEmpty()) { return $model->toArray(); } return [ 'id' => self::CONFIG_ID, 'logo' => '', 'account_name' => '', 'description' => '', 'follow_message' => '', 'follow_url' => '', 'follow_enabled' => 0, 'appid' => '', 'original_id' => '', 'appsecret' => '', 'server_url' => '', 'token' => '', 'encoding_aes_key' => '', 'encryption_mode' => 1, 'free_recharge_times' => 0, 'follow_free_recharge' => 0, 'all_merchant_follow' => 0, ]; } public function saveConfig(array $data): bool { foreach (self::STRING_FIELDS as $field) { if (array_key_exists($field, $data) && is_string($data[$field])) { $data[$field] = trim($data[$field]); } } foreach (['follow_enabled', 'encryption_mode', 'free_recharge_times', 'follow_free_recharge', 'all_merchant_follow'] as $field) { if (array_key_exists($field, $data)) { $data[$field] = (int) $data[$field]; } } unset($data['id'], $data['created_by'], $data['create_time'], $data['updated_by'], $data['update_time']); $model = $this->model->findOrEmpty(self::CONFIG_ID); if ($model->isEmpty()) { $data['id'] = self::CONFIG_ID; return (bool) $this->model->create($data)->getKey(); } return $model->save($data) !== false; } }