p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.1 KiB

<?php
namespace app\admin\controller;
use app\admin\service\ConfigService;
use app\utility\MongoConnection;
use MongoDB\BSON\ObjectId;
use phpDocumentor\Reflection\Types\Object_;
use think\facade\Cache;
use think\facade\Filesystem;
use app\model\ConfigModel;
class Config extends AdminBaseController
{
public function setConfig()
{
try {
$param = $this->request->param();
if (empty($param['name'])) return json(['code' => '1', 'message' => '参数错误', 'data' => []]);
$name = $param['name'];
$type = $param['type'] ?? 'string';
$value = $param['value'] ?? '';
$tip = $param['tip'] ?? '';
$insertData = [
'name' => $name,
'group' => 'config',
'title' => $name,
'tip' => $tip,
'type' => $type,
'value' => $value,
];
$config = ConfigModel::where('name', $name)->find();
if (empty($config)) {
$bool = ConfigModel::insert($insertData);
if (!$bool) return json(['code' => '100500', 'message' => '设置失败', 'data' => []]);
} else {
$bool = ConfigModel::where('name', $name)->update($insertData);
if (!$bool) return json(['code' => '100500', 'message' => '设置失败', 'data' => []]);
}
return json(['code' => '0', 'message' => 'Request successful', 'data' => []]);
} catch (\Exception $exception) {
return json(['code' => '100500', 'message' => '系统繁忙', 'data' => [$exception->getMessage()]]);
}
}
public function getConfig()
{
try {
$param = $this->request->param();
if (empty($param['name'])) return json(['code' => '1', 'message' => '参数错误', 'data' => []]);
$name = $param['name'];
$value = ConfigModel::where('name', $name)->value('value');
return json(['code' => '0', 'message' => 'Request successful', 'data' => ['value' => $value]]);
} catch (\Exception $exception) {
return json(['code' => '100500', 'message' => '系统繁忙', 'data' => [$exception->getMessage()]]);
}
}
public function getConfigList()
{
$returnData = (new ConfigService())->getConfigList($this->request->param());
return json($returnData);
}
// 获取邮箱模板列表
public function emailTemplateList()
{
$returnData = (new ConfigService())->emailTemplateList($this->request->param());
return json($returnData);
}
// 编辑邮箱模板列表
public function editEmailTemplate()
{
$returnData = (new ConfigService())->editEmailTemplate($this->request->param());
return json($returnData);
}
// 短信模板列表
public function smsTemplateList()
{
$returnData = (new ConfigService())->smsTemplateList($this->request->param());
return json($returnData);
}
// 编辑邮短信板列表
public function editSmsTemplate()
{
$returnData = (new ConfigService())->editSmsTemplate($this->request->param());
return json($returnData);
}
// 获取行情数据列表
public function quoteList()
{
$params = $this->request->param();
if (!isset($params['page']) || !isset($params['limit'])) {
return json([
'code' => 400,
'message' => '缺少参数',
'data' => []
]);
}
// 分页计算
$page = (int)$params['page'];
$pageSize = (int)$params['limit'];
$options = [
'skip' => ($page - 1) * $pageSize,
'limit' => $pageSize,
'sort' => ['_id' => -1], // 按创建时间倒序
];
// 构造查询条件
$filter = [];
if (!empty($params['name'])) {
// 使用正则表达式进行模糊匹配(i 表示不区分大小写)
$filter['Name'] = [
'$regex' => $params['name'],
'$options' => 'i'
];
}
// 查询数据
$client = MongoConnection::getClient();
$collection = $client->selectCollection('bourse', 'stockListBak');
$cursor = $collection->find($filter, $options);
$results = iterator_to_array($cursor); // 将 BSON 文档转换为数组
$total = $collection->countDocuments($filter);
$list = [];
foreach ($results as $item) {
$arr = (array)$item;
$list[] = $arr;
}
return json([
'code' => 0,
'message' => 'ok',
'data' => [
'total' => $total,
'page' => $page,
'pageSize' => $pageSize,
'list' => $list,
]
]);
}
// 行情数据置顶
public function quoteTopData()
{
$params = $this->request->param();
if (empty($params['id']) || empty($params['sort'])) {
return json([
'code' => 400,
'message' => '缺少参数',
'data' => []
]);
}
// 获取行情数据
$client = MongoConnection::getClient();
$collection = $client->selectCollection('bourse', 'stockListBak');
$doc = $collection->find(['_id'=>new ObjectId($params['id'])]);
$results = iterator_to_array($doc);
if (!isset($results[0]['Code']) || !isset($results[0]['Type'])) {
return json([
'code' => 400,
'message' => '行情数据错误',
'data' => []
]);
}
$buildArr = [
'Code' => $results[0]['Code'],
'Type' => $results[0]['Type'],
'Sort' => $params['sort'],
];
$jsonStr = json_encode($buildArr);
$res = Cache::store('redis')->hSet(MongoConnection::QUOTE_TOP_DATA_HASH_KEY, $results[0]['Code'], $jsonStr);
return json([
'code' => 0,
'message' => 'ok',
'data' => [$res]
]);
}
}