Goods.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\controller\mall;
  3. use app\admin\model\MallCate;
  4. use app\admin\model\MallGoods;
  5. use app\admin\service\annotation\MiddlewareAnnotation;
  6. use app\common\controller\AdminController;
  7. use app\admin\service\annotation\ControllerAnnotation;
  8. use app\admin\service\annotation\NodeAnnotation;
  9. use app\Request;
  10. use think\App;
  11. use think\response\Json;
  12. use Wolfcode\Ai\Enum\AiType;
  13. use Wolfcode\Ai\Service\AiChatService;
  14. #[ControllerAnnotation(title: '商城商品管理')]
  15. class Goods extends AdminController
  16. {
  17. #[NodeAnnotation(ignore: ['export'])] // 过滤不需要生成的权限节点 默认 CURD 中会自动生成部分节点 可以在此处过滤
  18. protected array $ignoreNode;
  19. public function __construct(App $app)
  20. {
  21. parent::__construct($app);
  22. self::$model = new MallGoods();
  23. $this->assign('cate', MallCate::column('title', 'id'));
  24. }
  25. #[NodeAnnotation(title: '列表', auth: true)]
  26. public function index(Request $request): Json|string
  27. {
  28. if ($request->isAjax()) {
  29. if (input('selectFields')) return $this->selectList();
  30. list($page, $limit, $where) = $this->buildTableParams();
  31. $count = self::$model::where($where)->count();
  32. $list = self::$model::with(['cate'])->where($where)->page($page, $limit)->order($this->sort)->select()->toArray();
  33. $data = [
  34. 'code' => 0,
  35. 'msg' => '',
  36. 'count' => $count,
  37. 'data' => $list,
  38. ];
  39. return json($data);
  40. }
  41. return $this->fetch();
  42. }
  43. #[NodeAnnotation(title: '入库', auth: true)]
  44. public function stock(Request $request, $id): string
  45. {
  46. $row = self::$model::find($id);
  47. empty($row) && $this->error('数据不存在');
  48. if ($request->isPost()) {
  49. $post = $request->post();
  50. $rule = [];
  51. $this->validate($post, $rule);
  52. try {
  53. $post['total_stock'] = $row->total_stock + $post['stock'];
  54. $post['stock'] = $row->stock + $post['stock'];
  55. $save = $row->save($post);
  56. }catch (\Exception $e) {
  57. $this->error('保存失败');
  58. }
  59. $save ? $this->success('保存成功') : $this->error('保存失败');
  60. }
  61. $this->assign('row', $row);
  62. return $this->fetch();
  63. }
  64. #[MiddlewareAnnotation(ignore: MiddlewareAnnotation::IGNORE_LOGIN)]
  65. public function no_check_login(Request $request): string
  66. {
  67. return '这里演示方法不需要经过登录验证';
  68. }
  69. #[NodeAnnotation(title: 'AI优化', auth: true)]
  70. public function aiOptimization(Request $request): void
  71. {
  72. $message = $request->post('message');
  73. if (empty($message)) $this->error('请输入内容');
  74. // 演示环境下 默认返回的内容
  75. if ($this->isDemo) {
  76. $content = <<<EOF
  77. 演示环境中 默认返回的内容
  78. 我来帮你优化这个标题,让它更有吸引力且更符合电商平台的搜索逻辑:
  79. "商务男士高端定制马克杯 | 办公室精英必备 | 优质陶瓷防烫手柄"
  80. 这个优化后的标题:
  81. 1. 突出了目标用户群体(商务男士)
  82. 2. 强调了产品定位(高端定制)
  83. 3. 点明了使用场景(办公室)
  84. 4. 添加了材质和功能特点(优质陶瓷、防烫手柄)
  85. 5. 使用了吸引人的关键词(精英必备)
  86. 这样的标题不仅更具体,也更容易被搜索引擎识别,同时能精准触达目标客户群。您觉得这个版本如何?
  87. EOF;
  88. $choices = [['message' => [
  89. 'role' => 'assistant',
  90. 'content' => $content,
  91. ]]];
  92. $this->success('success', compact('choices'));
  93. }
  94. try {
  95. $result = AiChatService::instance()
  96. // 当使用推理模型时,可能存在超时的情况,所以需要设置超时时间为 0
  97. // ->setTimeLimit(0)
  98. // 请替换为您需要的模型类型
  99. ->setAiType(AiType::QWEN)
  100. // 如果需要指定模型的 API 地址,可自行设置
  101. // ->setAiUrl('https://xxx.com')
  102. // 请替换为您的模型
  103. ->setAiModel('qwen-plus')
  104. // 请替换为您的 API KEY
  105. ->setAiKey('sk-1234567890')
  106. // 此内容会作为系统提示,会影响到回答的内容 当前仅作为测试使用
  107. ->setSystemContent('你现在是一位资深的海外电商产品经理')
  108. ->chat($message);
  109. $choices = $result['choices'];
  110. }catch (\Throwable $exception) {
  111. $choices = [['message' => [
  112. 'role' => 'assistant',
  113. 'content' => $exception->getMessage(),
  114. ]]];
  115. }
  116. $this->success('success', compact('choices'));
  117. }
  118. }