代码提交优化
This commit is contained in:
@@ -46,13 +46,35 @@ class AiKnowledgeBaseController extends BaseController
|
||||
$where[] = ['type', '=', AiKnowledgeBaseType::TYPE_USER];
|
||||
}
|
||||
|
||||
// 统计开启的类型总数
|
||||
$enabledCountWhere = $where;
|
||||
$enabledCountWhere[] = ['status', '=', 1];
|
||||
$enabledCount = AiKnowledgeBaseType::where($enabledCountWhere)->count();
|
||||
|
||||
// 查询数据
|
||||
$list = AiKnowledgeBaseType::where($where)
|
||||
->order('type', 'asc') // 系统类型排在前面
|
||||
->order('createTime', 'desc')
|
||||
->paginate($pageSize, false, ['page' => $page]);
|
||||
|
||||
return ResponseHelper::success($list, '获取成功');
|
||||
// 为每个类型添加素材数量统计
|
||||
$listData = $list->toArray();
|
||||
foreach ($listData['data'] as &$item) {
|
||||
// 统计该类型下的知识库数量(素材数量)
|
||||
$item['materialCount'] = AiKnowledgeBase::where([
|
||||
['typeId', '=', $item['id']],
|
||||
['isDel', '=', 0]
|
||||
])->count();
|
||||
}
|
||||
|
||||
// 重新构造返回数据
|
||||
$result = [
|
||||
'total' => $listData['total'],
|
||||
'data' => $listData['data'],
|
||||
'enabledCount' => $enabledCount, // 开启的类型总数
|
||||
];
|
||||
|
||||
return ResponseHelper::success($result, '获取成功');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('系统异常:' . $e->getMessage());
|
||||
@@ -419,6 +441,11 @@ class AiKnowledgeBaseController extends BaseController
|
||||
->order('createTime', 'desc')
|
||||
->paginate($pageSize, false, ['page' => $page]);
|
||||
|
||||
foreach ($list as &$v){
|
||||
$v['size'] = 0;
|
||||
}
|
||||
unset($v);
|
||||
|
||||
return ResponseHelper::success($list, '获取成功');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -40,6 +40,7 @@ class AiSettingsController extends BaseController
|
||||
|
||||
// 确保智能体已创建
|
||||
if (empty($settings->botId)) {
|
||||
$settings->releaseTime = 0;
|
||||
$botCreated = $this->createBot($settings);
|
||||
if (!$botCreated) {
|
||||
return ResponseHelper::error('智能体创建失败');
|
||||
@@ -48,12 +49,13 @@ class AiSettingsController extends BaseController
|
||||
|
||||
// 确保知识库已创建
|
||||
if (empty($settings->datasetId)) {
|
||||
$settings->releaseTime = 0;
|
||||
$knowledgeCreated = $this->createKnowledge($settings);
|
||||
if (!$knowledgeCreated) {
|
||||
return ResponseHelper::error('知识库创建失败');
|
||||
}
|
||||
}
|
||||
if (!empty($settings->botId) && !empty($settings->datasetId)) {
|
||||
if (!empty($settings->botId) && !empty($settings->datasetId) && $settings->releaseTime <= 0) {
|
||||
$cozeAI = new CozeAI();
|
||||
$config = json_decode($settings->config,true);
|
||||
$config['bot_id'] = $settings->botId;
|
||||
@@ -133,7 +135,8 @@ class AiSettingsController extends BaseController
|
||||
## 限制
|
||||
- 仅依据知识库内容回答问题,对于知识库中没有的信息,如实告知用户无法回答。
|
||||
- 回答必须严格遵循中国法律法规,不得出现任何违法违规内容。
|
||||
- 回答需简洁明了,避免冗长复杂的表述。';
|
||||
- 回答需简洁明了,避免冗长复杂的表述(尽量在100字内)。
|
||||
- 适当加些表情点缀。';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -341,4 +344,90 @@ class AiSettingsController extends BaseController
|
||||
$settings->save();
|
||||
return ResponseHelper::success('', '发布成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存统一提示词
|
||||
* 先更新数据库,再调用CozeAI接口更新智能体
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function savePrompt()
|
||||
{
|
||||
try {
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
if (empty($companyId)) {
|
||||
return ResponseHelper::error('公司信息获取失败');
|
||||
}
|
||||
|
||||
// 获取提示词参数
|
||||
$promptInfo = $this->request->param('promptInfo', '');
|
||||
if (empty($promptInfo)) {
|
||||
return ResponseHelper::error('提示词内容不能为空');
|
||||
}
|
||||
|
||||
// 查找AI设置
|
||||
$settings = AiSettingsModel::where(['companyId' => $companyId])->find();
|
||||
if (empty($settings)) {
|
||||
return ResponseHelper::error('AI设置不存在,请先初始化');
|
||||
}
|
||||
|
||||
// 检查智能体是否已创建
|
||||
if (empty($settings->botId)) {
|
||||
return ResponseHelper::error('智能体未创建,请先初始化AI设置');
|
||||
}
|
||||
|
||||
// 解析现有配置
|
||||
$config = json_decode($settings->config, true);
|
||||
if (!is_array($config)) {
|
||||
$config = [];
|
||||
}
|
||||
|
||||
// 更新提示词
|
||||
$config['prompt_info'] = $promptInfo;
|
||||
|
||||
// 第一步:更新数据库
|
||||
$settings->config = json_encode($config, JSON_UNESCAPED_UNICODE);
|
||||
$settings->isRelease = 0; // 标记为未发布状态
|
||||
$settings->updateTime = time();
|
||||
|
||||
if (!$settings->save()) {
|
||||
return ResponseHelper::error('数据库更新失败');
|
||||
}
|
||||
|
||||
// 第二步:调用CozeAI接口更新智能体
|
||||
try {
|
||||
$cozeAI = new CozeAI();
|
||||
|
||||
// 参考 init 方法的参数格式,传递完整的 config
|
||||
$updateData = $config;
|
||||
$updateData['bot_id'] = $settings->botId;
|
||||
|
||||
// 如果有知识库,也一并传入
|
||||
if (!empty($settings->datasetId)) {
|
||||
$updateData['dataset_ids'] = [$settings->datasetId];
|
||||
}
|
||||
|
||||
$result = $cozeAI->updateBot($updateData);
|
||||
$result = json_decode($result, true);
|
||||
|
||||
if ($result['code'] != 200) {
|
||||
\think\facade\Log::error('更新智能体提示词失败:' . json_encode($result));
|
||||
return ResponseHelper::error('更新智能体失败:' . ($result['msg'] ?? '未知错误'));
|
||||
}
|
||||
|
||||
return ResponseHelper::success([
|
||||
'prompt_info' => $promptInfo,
|
||||
'isRelease' => 0
|
||||
], '提示词保存成功,请重新发布智能体');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('调用CozeAI更新接口异常:' . $e->getMessage());
|
||||
return ResponseHelper::error('更新智能体接口调用失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('保存提示词异常:' . $e->getMessage());
|
||||
return ResponseHelper::error('系统异常:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ namespace app\cunkebao\controller;
|
||||
use app\common\controller\PaymentService;
|
||||
use app\common\model\Order;
|
||||
use app\cunkebao\model\TokensPackage;
|
||||
use app\chukebao\model\TokensCompany;
|
||||
use app\chukebao\model\TokensRecord;
|
||||
use library\ResponseHelper;
|
||||
use think\facade\Env;
|
||||
|
||||
@@ -23,9 +25,9 @@ class TokensController extends BaseController
|
||||
$list = $query->where($where)->page($page, $limit)->order('sort ASC,id desc')->select();
|
||||
foreach ($list as &$item) {
|
||||
$item['description'] = json_decode($item['description'], true);
|
||||
$item['discount'] = round(((($item['originalPrice'] - $item['price']) / $item['originalPrice']) * 100),2);
|
||||
$item['price'] = round( $item['price'], 2);
|
||||
$item['unitPrice'] = round( $item['price'] / $item['tokens'],6);
|
||||
$item['discount'] = round(((($item['originalPrice'] - $item['price']) / $item['originalPrice']) * 100), 2);
|
||||
$item['price'] = round($item['price'], 2);
|
||||
$item['unitPrice'] = round($item['price'] / $item['tokens'], 6);
|
||||
$item['originalPrice'] = round($item['originalPrice'] / 100, 2);
|
||||
$item['tokens'] = number_format($item['tokens']);
|
||||
}
|
||||
@@ -40,6 +42,12 @@ class TokensController extends BaseController
|
||||
$price = $this->request->param('price', '');
|
||||
$userId = $this->getUserInfo('id');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$payType = $this->request->param('payType', '');
|
||||
|
||||
if (!in_array($payType, ['wechat', 'alipay', 'qrCode'])) {
|
||||
return ResponseHelper::error('付款类型不正确');
|
||||
}
|
||||
|
||||
|
||||
if (empty($id) && empty($price)) {
|
||||
return ResponseHelper::error('套餐和自定义购买金额必须选一个');
|
||||
@@ -73,6 +81,7 @@ class TokensController extends BaseController
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
$orderNo = date('YmdHis') . rand(100000, 999999);
|
||||
$order = [
|
||||
'companyId' => $companyId,
|
||||
@@ -82,7 +91,8 @@ class TokensController extends BaseController
|
||||
'goodsName' => $specs['name'],
|
||||
'goodsSpecs' => $specs,
|
||||
'orderType' => 1,
|
||||
'money' => $specs['price']
|
||||
'money' => $specs['price'],
|
||||
'service' => $payType
|
||||
];
|
||||
$paymentService = new PaymentService();
|
||||
$res = $paymentService->createOrder($order);
|
||||
@@ -106,16 +116,18 @@ class TokensController extends BaseController
|
||||
$res = $paymentService->queryOrder($orderNo);
|
||||
$res = json_decode($res, true);
|
||||
if ($res['code'] == 200) {
|
||||
return ResponseHelper::success('','订单已支付');
|
||||
return ResponseHelper::success('', '订单已支付');
|
||||
} else {
|
||||
$errorMsg = !empty($order['payInfo']) ? $order['payInfo'] : '订单未支付';
|
||||
return ResponseHelper::error($errorMsg);
|
||||
}
|
||||
} else {
|
||||
return ResponseHelper::success('','订单已支付');
|
||||
return ResponseHelper::success('', '订单已支付');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取订单列表
|
||||
* @return \think\response\Json
|
||||
@@ -164,7 +176,7 @@ class TokensController extends BaseController
|
||||
|
||||
// 分页查询
|
||||
$query = Order::where($where)
|
||||
->where(function($query) {
|
||||
->where(function ($query) {
|
||||
$query->whereNull('deleteTime')->whereOr('deleteTime', 0);
|
||||
});
|
||||
$total = $query->count();
|
||||
@@ -178,12 +190,12 @@ class TokensController extends BaseController
|
||||
foreach ($list as &$item) {
|
||||
// 金额转换(分转元)
|
||||
$item['money'] = round($item['money'] / 100, 2);
|
||||
|
||||
|
||||
// 解析商品规格
|
||||
if (!empty($item['goodsSpecs'])) {
|
||||
$specs = is_string($item['goodsSpecs']) ? json_decode($item['goodsSpecs'], true) : $item['goodsSpecs'];
|
||||
$item['goodsSpecs'] = $specs;
|
||||
|
||||
|
||||
// 添加算力数量
|
||||
if (isset($specs['tokens'])) {
|
||||
$item['tokens'] = number_format($specs['tokens']);
|
||||
@@ -229,4 +241,75 @@ class TokensController extends BaseController
|
||||
return ResponseHelper::error('获取订单列表失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公司算力统计信息
|
||||
* 包括:总算力、今日使用、本月使用、剩余算力
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getTokensStatistics()
|
||||
{
|
||||
try {
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
if (empty($companyId)) {
|
||||
return ResponseHelper::error('公司信息获取失败');
|
||||
}
|
||||
|
||||
// 获取公司算力余额
|
||||
$tokensCompany = TokensCompany::where('companyId', $companyId)->find();
|
||||
$remainingTokens = $tokensCompany ? intval($tokensCompany->tokens) : 0;
|
||||
|
||||
// 获取今日开始和结束时间戳
|
||||
$todayStart = strtotime(date('Y-m-d 00:00:00'));
|
||||
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
|
||||
|
||||
// 获取本月开始和结束时间戳
|
||||
$monthStart = strtotime(date('Y-m-01 00:00:00'));
|
||||
$monthEnd = strtotime(date('Y-m-t 23:59:59'));
|
||||
|
||||
// 统计今日消费(type=0表示消费)
|
||||
$todayUsed = TokensRecord::where([
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0], // 0为减少(消费)
|
||||
['createTime', '>=', $todayStart],
|
||||
['createTime', '<=', $todayEnd]
|
||||
])->sum('tokens');
|
||||
$todayUsed = intval($todayUsed);
|
||||
|
||||
// 统计本月消费
|
||||
$monthUsed = TokensRecord::where([
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0], // 0为减少(消费)
|
||||
['createTime', '>=', $monthStart],
|
||||
['createTime', '<=', $monthEnd]
|
||||
])->sum('tokens');
|
||||
$monthUsed = intval($monthUsed);
|
||||
|
||||
// 计算总算力(当前剩余 + 历史总消费)
|
||||
$totalConsumed = TokensRecord::where([
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0]
|
||||
])->sum('tokens');
|
||||
$totalConsumed = intval($totalConsumed);
|
||||
|
||||
// 总充值算力
|
||||
$totalRecharged = TokensRecord::where([
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 1] // 1为增加(充值)
|
||||
])->sum('tokens');
|
||||
$totalRecharged = intval($totalRecharged);
|
||||
|
||||
return ResponseHelper::success([
|
||||
'totalTokens' => $totalRecharged, // 总算力(累计充值)
|
||||
'todayUsed' => $todayUsed, // 今日使用
|
||||
'monthUsed' => $monthUsed, // 本月使用
|
||||
'remainingTokens' => $remainingTokens, // 剩余算力
|
||||
'totalConsumed' => $totalConsumed, // 累计消费
|
||||
], '获取成功');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('获取算力统计失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,17 +12,28 @@ use think\Db;
|
||||
|
||||
class PosterWeChatMiniProgram extends Controller
|
||||
{
|
||||
|
||||
protected $config;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// 从环境变量获取配置
|
||||
$this->config = [
|
||||
'app_id' => Env::get('weChat.appidMiniApp','wx789850448e26c91d'),
|
||||
'secret' => Env::get('weChat.secretMiniApp','d18f75b3a3623cb40da05648b08365a1'),
|
||||
'response_type' => 'array'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function index()
|
||||
{
|
||||
return 'Hello, World!';
|
||||
}
|
||||
|
||||
const MINI_PROGRAM_CONFIG = [
|
||||
'app_id' => 'wx789850448e26c91d',
|
||||
'secret' => 'd18f75b3a3623cb40da05648b08365a1',
|
||||
'response_type' => 'array'
|
||||
];
|
||||
|
||||
|
||||
// 生成小程序码,存客宝-操盘手调用
|
||||
public function generateMiniProgramCodeWithScene($taskId = '')
|
||||
@@ -34,7 +45,7 @@ class PosterWeChatMiniProgram extends Controller
|
||||
|
||||
|
||||
try {
|
||||
$app = Factory::miniProgram(self::MINI_PROGRAM_CONFIG);
|
||||
$app = Factory::miniProgram($this->config);
|
||||
// scene参数长度限制为32位
|
||||
//$scene = 'taskId=' . $taskId;
|
||||
$scene = sprintf("%s", $taskId);
|
||||
@@ -83,7 +94,7 @@ class PosterWeChatMiniProgram extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
$app = Factory::miniProgram(self::MINI_PROGRAM_CONFIG);
|
||||
$app = Factory::miniProgram($this->config);
|
||||
|
||||
$result = $app->phone_number->getUserPhoneNumber($code);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user