| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Http\Controllers\Points;
- use App\Libs\ApiResponse;
- use App\Libs\Utils;
- use App\Services\PointsService;
- use App\Transformer\Points\PointsTransformer;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller as BaseController;
- use Illuminate\Support\Facades\Validator;
- class PointsController extends BaseController
- {
- use ApiResponse;
- protected $pointsService;
- public function __construct(PointsService $pointsService)
- {
- $this->pointsService = $pointsService;
- }
- /**
- * 积分流水列表(前端查看积分明细)
- *
- * @param Request $request
- * @return mixed
- */
- public function records(Request $request)
- {
- $result = $this->pointsService->getUserPointsRecords($request->all());
- $transformer = new PointsTransformer();
- return $this->success([
- 'summary' => $result['summary'],
- 'meta' => getMeta($result['records']),
- 'list' => $transformer->newEachPointsRecord($result['records']),
- ]);
- }
- /**
- * 获取系统当前全部积分规则(文生文/图片/视频)
- *
- * 供前端积分规则管理页展示:
- * - chat:文生文(对话)扣分规则(默认10,支持按模型配置)
- * - image_models:图片模型计费规则
- * - video_models:视频模型计费规则
- *
- * @param Request $request
- * @return mixed
- */
- public function rules(Request $request)
- {
- return $this->success($this->pointsService->getPointsRules());
- }
- /**
- * 编辑积分规则(文生文/图片/视频)
- *
- * 入参三个区块均可选传,单次至少传一个:
- * - text_rules:文生文规则列表 [{model?, charge_type?, price_json?}],model 可不传,不传时更新全部文生文模型
- * - image_rules:图片规则列表 [{model, charge_type?, price_json?}]
- * - video_rules:视频规则列表 [{model, charge_type?, price_json?}]
- * 图片/视频规则 model 必填;每条规则 charge_type / price_json 至少传一个(未传字段保留原值)。
- *
- * @param Request $request
- * @return mixed
- */
- public function editRules(Request $request)
- {
- $data = $request->all();
- $validator = Validator::make($data, [
- 'text_rules' => 'nullable|array',
- 'image_rules' => 'nullable|array',
- 'video_rules' => 'nullable|array',
- ], [
- 'text_rules.array' => 'text_rules必须为数组',
- 'image_rules.array' => 'image_rules必须为数组',
- 'video_rules.array' => 'video_rules必须为数组',
- ]);
- if ($validator->fails()) {
- Utils::throwError('1002:' . $validator->errors()->first());
- }
- $hasText = isset($data['text_rules']) && !empty($data['text_rules']);
- $hasImage = isset($data['image_rules']) && !empty($data['image_rules']);
- $hasVideo = isset($data['video_rules']) && !empty($data['video_rules']);
- if (!$hasText && !$hasImage && !$hasVideo) {
- Utils::throwError('1002:请至少传入text_rules、image_rules或video_rules之一');
- }
- $updated = $this->pointsService->updatePointsRules($data);
- return $this->success([
- 'updated' => $updated,
- 'remark' => '计费规则已更新,后续生成任务按新规则扣费',
- ]);
- }
- }
|