SchoolPhysicalExamination/application/app/controller/Msginformation.php

283 lines
12 KiB
PHP
Raw Normal View History

2024-11-23 16:46:38 +08:00
<?php
namespace app\app\controller;
use think\Db;
class Msginformation extends Base{
protected $msginformation_use_db_name = [
'1'=>'admin_editor_text_content',
'2'=>'admin_editor_text_like_up_log',
];
protected $page_num = 10;
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 获取板块,及板块下类型标签
public function get_sector_label_msg(){
try {
$return_data = $this->get_sector_label_msg_action();
return $return_data;
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
$this->record_api_log([], $logContent, null);
return $this->msg(99999);
}
}
// 获取板块下信息
public function get_sector_content_msg($data = ['token'=>'6441bf7dabea7b3360a30240d3b19fc5','sector_id'=>1,'type'=>4,'page'=>1]){
try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data) || !array_key_exists('sector_id', $data) || !array_key_exists('type', $data) || !array_key_exists('page', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['token'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['sector_id'],'intnum')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['type'],'intnum')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['page'],'intnum')){
return $this->msg(10005);
}
$return_data = $this->get_sector_content_msg_action($data);
// 成功
$this->record_api_log($data, null, $return_data);
return $return_data;
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
$this->record_api_log($data, $logContent, null);
return $this->msg(99999);
}
}
// 资讯点赞
public function user_like_it($data = ['token'=>'0dafb98a10995c98b5a33b7d59d986ca','id'=>'44']){
try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('id', $data) || !array_key_exists('token', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['token'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['id'],'intnum')){
return $this->msg(10005);
}
$return_data = $this->user_like_it_action($data);
// 成功
$this->record_api_log($data, null, $return_data);
return $return_data;
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
$this->record_api_log($data, $logContent, null);
return $this->msg(99999);
}
}
################################################################业务################################################################
################################################################get_sector_label_msg
public function get_sector_label_msg_action(){
$Template_arr = [
1=>[
'id'=>'1',
'name'=>'推荐',
'loop_data'=>[],
'list'=>[
['id'=>0,'name'=>'全部'],
['id'=>1,'name'=>'身高管理'],
['id'=>2,'name'=>'体重管理'],
['id'=>3,'name'=>'肺活训练'],
['id'=>4,'name'=>'跳绳训练'],
['id'=>5,'name'=>'中考体测'],
]
],
];
// 获取需要版块id start
$sector = Db::query("
SELECT
sector
FROM ".$this->msginformation_use_db_name['1']."
group by sector
");
$sector_all = [];
foreach ($sector as $key => $value) {
foreach (explode(',', $value['sector']) as $k => $v) {
if(!in_array($v, $sector_all)){
array_push($sector_all, $v);
}
}
}
// 获取需要版块id end
// 填充进去版块的轮播start
$return_data = [];
foreach ($sector_all as $key => $value) {
$loop_result = Db::query("
SELECT
id,
title,
CONCAT('https://tc.pcxbc.com/', cover_image) AS cover_image
FROM ".$this->msginformation_use_db_name['1']."
WHERE
is_del = 0 AND loop_img > 0 AND sector LIKE '%".$value."%'
ORDER BY
loop_img desc
");
$Template_arr[$value]['loop_data'] = $loop_result;
array_push($return_data, $Template_arr[$value]);
}
// 填充进去版块的轮播end
return $this->msg($return_data);
}
################################################################get_sector_content_msg
public function get_sector_content_msg_action($data){
$return_result = [
'page_now'=>$data['page'],
'page_num'=>$this->page_num,
'content_data'=>[]
];
if($data['type'] != 0){
$type_str = " AND type LIKE '%".$data['type']."%'";
}else{
$type_str = "";
}
$content_result = Db::query("
SELECT
id,
title,
create_time,
i_like,
reading,
CONCAT('https://tc.pcxbc.com/', cover_image) AS cover_image,
top_up
FROM ".$this->msginformation_use_db_name['1']."
WHERE
is_del = 0 AND sector LIKE '%".$data['sector_id']."%'".$type_str."
ORDER BY
top_up desc,id
OFFSET
(".$data['page']." - 1) * ".$this->page_num." ROWS
FETCH NEXT
".$this->page_num." ROWS ONLY
");
$return_result['content_data'] = $content_result;
2024-11-29 18:46:37 +08:00
$user_like = Db::table($this->msginformation_use_db_name['2'])->where(['token'=>$data['token'],'is_like'=>0])->column('aetc_id');
2024-11-23 16:46:38 +08:00
foreach ($return_result['content_data'] as $key => $value) {
if(array_key_exists($value['id'], $user_like)){
$return_result['content_data'][$key]['is_like'] = 1;
}else{
$return_result['content_data'][$key]['is_like'] = 0;
}
}
return $this->msg($return_result);
}
################################################################user_like_it
public function user_like_it_action($data){
$user_like_data = Db::table($this->msginformation_use_db_name['2'])->where(['token'=>$data['token'],'aetc_id'=>$data['id']])->find();
if($user_like_data){
// 如果找到有点赞记录
if($user_like_data['is_del'] == 0){
// 如果已经点了
// 启动事务
Db::startTrans();
try{
Db::table($this->msginformation_use_db_name['1'])->where(['id'=>$data['id']])->setDec('i_like');
Db::table($this->msginformation_use_db_name['2'])->where(['aetc_id'=>$data['id'],'token'=>$data['token']])->update(['is_del'=>1,'update_time'=>date('Y-m-d H:i:s')]);
// 提交事务
Db::commit();
return $this->msg(['user_like'=>1]);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002);
}
}else{
// 如果之前点过又取消了
// 启动事务
Db::startTrans();
try{
Db::table($this->msginformation_use_db_name['1'])->where(['id'=>$data['id']])->setInc('i_like');
Db::table($this->msginformation_use_db_name['2'])->where(['aetc_id'=>$data['id'],'token'=>$data['token']])->update(['is_del'=>0,'update_time'=>date('Y-m-d H:i:s')]);
// 提交事务
Db::commit();
return $this->msg(['user_like'=>0]);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002);
}
}
}else{
// 如果没有记录
// 启动事务
Db::startTrans();
try{
Db::table($this->msginformation_use_db_name['1'])->where(['id'=>$data['id']])->setInc('i_like');
Db::table($this->msginformation_use_db_name['2'])->insert([
'aetc_id'=>$data['id'],
'token'=>$data['token'],
'create_time'=>date('Y-m-d H:i:s'),
'update_time'=>date('Y-m-d H:i:s')
]);
// 提交事务
Db::commit();
return $this->msg(['user_like'=>0]);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002);
}
}
}
}