407 lines
16 KiB
PHP
407 lines
16 KiB
PHP
<?php
|
||
|
||
namespace app\OutsideInterface\controller\ZhiZhaoTuan;
|
||
|
||
use think\Db;
|
||
|
||
class ApiJk extends Base
|
||
{
|
||
|
||
|
||
|
||
// 智照团保存表单信息
|
||
// 添加菜谱(OK)
|
||
// 根据食材详细查找列表(OK)
|
||
public function activity_list(){
|
||
// 尝试捕获异常
|
||
try {
|
||
$data = input('post.');
|
||
$return_data = $this->activity_list_action($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 json(['status' => 'error', 'message' => '系统错误']);
|
||
}
|
||
}
|
||
public function details_data(){
|
||
// 尝试捕获异常
|
||
try {
|
||
$data = input('post.');
|
||
// $data = ['id'=>3];
|
||
if(!array_key_exists('id', $data)){
|
||
return $this->msg(10001,'id is miss');
|
||
}
|
||
|
||
if(!$this->verify_data_is_ok($data['id'],'intnum')){
|
||
return $this->msg(10005,'id type is error');
|
||
}
|
||
$return_data = $this->details_data_action($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 json(['status' => 'error', 'message' => '系统错误']);
|
||
}
|
||
}
|
||
|
||
public function save_activity_data(){
|
||
// 尝试捕获异常
|
||
try {
|
||
$data = input('post.');
|
||
// $data = '{"id":"3","tel":"13915547489","content":[{"name":"姓名","type":"text","placeholder":"请输入姓名","is_must":"true","value":"大力","error":false},{"name":"电话","type":"text","placeholder":"请输入姓名","is_must":"true","value":"15237988596","error":false},{"name":"性别","type":"select","placeholder":"请选择性别","is_must":"true","options":["男","女"],"value":"男","error":false},{"name":"兴趣","type":"checkbox","placeholder":"请选择兴趣","is_must":"true","options":["跳舞","跳操","跳绳","跳远","跳高"],"value":["跳舞","跳远"],"error":false},{"name":"其他","type":"textarea","placeholder":"","is_must":"false","value":"哈","error":false}],"appid":"wxbbddd1888da43ab0","sessionid":"opv6R67fwcnu7-Rrts_ijylHQLCc"}';
|
||
// dump($data);
|
||
// // dump(json_decode($data,true));
|
||
// die;
|
||
// $data = json_decode($data,true);
|
||
if(!array_key_exists('id', $data)){
|
||
return $this->msg(10001,'id is miss');
|
||
}
|
||
if(!array_key_exists('tel', $data)){
|
||
return $this->msg(10001,'tel is miss');
|
||
}
|
||
if(!array_key_exists('content', $data)){
|
||
return $this->msg(10001,'content is miss');
|
||
}
|
||
|
||
if(!$this->verify_data_is_ok($data['id'],'intnum')){
|
||
return $this->msg(10005,'id type is error');
|
||
}
|
||
// if(!$this->verify_data_is_ok($data['tel'],'intnum')){
|
||
// return $this->msg(10005,'id type is error');
|
||
// }
|
||
$return_data = $this->save_activity_data_action($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 json(['status' => 'error', 'message' => '系统错误']);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
###########################################################################################################################################################
|
||
###########################################################################################################################################################
|
||
###########################################################################################################################################################
|
||
|
||
|
||
public function activity_list_action($data){
|
||
$page_now = array_key_exists('page',$data)?$data['page']:1;
|
||
$page_total = $page_now;
|
||
$page_num = 20;
|
||
$zzt_db = Db::connect('zzt_db');
|
||
$content_num = $zzt_db->table('pc_activity_registration')
|
||
->where("is_del = 0")
|
||
->count(); // 然后获取结果
|
||
// dump($content_num);
|
||
$page_total = ceil($content_num/$page_num);
|
||
|
||
$content_list = $zzt_db->table('pc_activity_registration')
|
||
->alias('a')
|
||
->join('pc_pic_manage b','a.pic = b.id','LEFT')
|
||
->where("a.is_del = 0")
|
||
->field('a.id,a.title,a.people_num,a.start_time,a.end_time,b.url_data')
|
||
->page("$page_now,$page_num")
|
||
->select();
|
||
|
||
return $this->msg([
|
||
'page_now'=>$page_now,
|
||
'page_total'=>$page_total,
|
||
'content_list'=>$content_list
|
||
]);
|
||
|
||
}
|
||
|
||
public function details_data_action($data){
|
||
|
||
$zzt_db = Db::connect('zzt_db');
|
||
$result = $zzt_db->table('pc_activity_registration')
|
||
->alias('a')
|
||
->join('pc_pic_manage b','a.pic = b.id','LEFT')
|
||
->where(["a.id"=>$data['id']])
|
||
->field('a.id,a.title,a.people_num,a.people_num_now,a.start_time,a.end_time,a.text_content,a.form_content,b.url_data as pic')
|
||
->find(); // 然后获取结果
|
||
// dump($result);
|
||
|
||
if($result){
|
||
$result['status'] = 1;
|
||
// $people_num = $zzt_db->table('pc_activity_registration_log')
|
||
// ->where(["par_id"=>$data['id']])
|
||
// ->count();
|
||
|
||
if(date('Y-m-d H:i:s') >= $result['end_time']){
|
||
$result['status'] = 0;
|
||
}
|
||
if($result['people_num_now']>=$result['people_num']){
|
||
$result['status'] = 0;
|
||
}
|
||
$result['form_content'] = json_decode($result['form_content'],true);
|
||
foreach ($result['form_content'] as $key => $value) {
|
||
if($value['type'] == 'checkbox'){
|
||
$result['form_content'][$key]['value'] = [];
|
||
}else{
|
||
$result['form_content'][$key]['value'] = "";
|
||
}
|
||
}
|
||
unset($result['people_num_now']);
|
||
return $this->msg($result);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
}
|
||
|
||
public function save_activity_data_action($data){
|
||
|
||
// $zzt_db = Db::connect('zzt_db');
|
||
|
||
// $result = $zzt_db->table('pc_activity_registration')
|
||
// ->where(["id"=>$data['id']])
|
||
// ->field('id,people_num,people_num_now,start_time,end_time,form_content')
|
||
// ->find(); // 然后获取结果
|
||
// $result['form_content'] = json_decode($result['form_content'],true);
|
||
// dump($result);
|
||
// dump($data);
|
||
// die;
|
||
// if(!$result){
|
||
// return $this->msg(10004);
|
||
// }
|
||
// if(date('Y-m-d H:i:s') < $result['start_time']){
|
||
// return $this->msg(10002,'活动还未开始');
|
||
// }
|
||
// if(date('Y-m-d H:i:s') > $result['end_time']){
|
||
// return $this->msg(10002,'活动已经结束');
|
||
// }
|
||
// if($result['people_num_now']>=$result['people_num']){
|
||
// return $this->msg(10002,'名额已报满');
|
||
// }
|
||
// $people_log = $zzt_db->table('pc_activity_registration_log')
|
||
// ->where(["par_id"=>$data['id'],'tel'=>$data['tel']])
|
||
// ->count(); // 然后获取结果
|
||
// if($people_log>=1){
|
||
// return $this->msg(10002,'您已经提交过,请勿重复提交');
|
||
// }
|
||
|
||
// dump();
|
||
// die;
|
||
|
||
if($data['tel'] == ''){
|
||
$data['tel'] = time();
|
||
}
|
||
|
||
|
||
|
||
$zzt_db = Db::connect('zzt_db');
|
||
|
||
try {
|
||
// 开启事务
|
||
$zzt_db->startTrans();
|
||
|
||
// 查询活动基础信息(使用WITH(UPDLOCK)加锁)
|
||
$result = $zzt_db->table('pc_activity_registration WITH(UPDLOCK)')
|
||
->where(["id" => $data['id']])
|
||
->field('id,people_num,people_num_now,start_time,end_time,form_content,is_only_once')
|
||
->find();
|
||
|
||
if (!$result) {
|
||
return $this->msg(10004);
|
||
}
|
||
|
||
$result['form_content'] = json_decode($result['form_content'], true);
|
||
|
||
// 检查活动时间
|
||
$currentTime = date('Y-m-d H:i:s');
|
||
if ($currentTime < $result['start_time']) {
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '活动还未开始');
|
||
}
|
||
if ($currentTime > $result['end_time']) {
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '活动已经结束');
|
||
}
|
||
|
||
// 检查是否可以重复报名
|
||
if($result['is_only_once'] == 0){ //如果是0不可重复提交
|
||
$people_log = $zzt_db->table('pc_activity_registration_log')
|
||
->where(["par_id" => $data['id'], 'tel' => $data['tel']])
|
||
->count();
|
||
|
||
if ($people_log >= 1) {
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '您已经提交过,请勿重复提交');
|
||
}
|
||
}
|
||
// 检查名额
|
||
if ($result['people_num_now'] >= $result['people_num']) {
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '名额已报满');
|
||
}
|
||
|
||
// 更新当前人数(SQL Server 语法)
|
||
$updateResult = $zzt_db->table('pc_activity_registration')
|
||
->where(["id" => $data['id']])
|
||
->update([
|
||
"people_num_now" => $zzt_db->raw('people_num_now + 1')
|
||
]);
|
||
|
||
// 插入报名记录
|
||
$insertData = [
|
||
'par_id' => $data['id'],
|
||
'tel' => $data['tel'],
|
||
'content' => [],
|
||
'create_time' => $currentTime
|
||
];
|
||
|
||
$tm_data = [];
|
||
foreach ($data['content'] as $key => $value) {
|
||
if($value['type'] == 'checkbox'){
|
||
$tm_data[$value['name']] = implode('CH1',$value['value']);
|
||
}else{
|
||
$tm_data[$value['name']] = $value['value'];
|
||
}
|
||
}
|
||
|
||
// 调试点1:检查数据结构
|
||
// dump($tm_data);
|
||
// dump($result['form_content']);
|
||
|
||
foreach ($result['form_content'] as $key => $value) {
|
||
|
||
if($value['is_must'] == 'true'){// 如果是必填的
|
||
if(array_key_exists($value['name'],$tm_data)){// 如果包含选项
|
||
// 这里可能有问题:$tm_data[$value['name']] 可能是字符串,不是数组
|
||
if(isset($tm_data[$value['name']]) && $tm_data[$value['name']] != ''){// 如果选项有值
|
||
$insertData['content'][] = ['name'=>$value['name'],'type'=>$value['type'],'value'=>$tm_data[$value['name']]];
|
||
}else{
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '必填项目未填('.$value['name'].')');
|
||
}
|
||
}else{
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '项目缺失('.$value['name'].')');
|
||
}
|
||
|
||
}else{
|
||
if(array_key_exists($value['name'],$tm_data)){// 如果包含选项
|
||
// 这里可能有问题:$tm_data[$value['name']] 可能是字符串,不是数组
|
||
if(isset($tm_data[$value['name']]) && $tm_data[$value['name']] != ''){// 如果选项有值
|
||
$insertData['content'][] = ['name'=>$value['name'],'type'=>$value['type'],'value'=>$tm_data[$value['name']]];
|
||
}else{
|
||
$insertData['content'][] = ['name'=>$value['name'],'type'=>$value['type'],'value'=>""];
|
||
}
|
||
}else{
|
||
$insertData['content'][] = ['name'=>$value['name'],'type'=>$value['type'],'value'=>""];
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
// 调试点2:检查最终的插入数据
|
||
// dump($insertData);
|
||
$insertData['content'] = json_encode($insertData['content']);
|
||
// 检测表单内容是否重复
|
||
$people_log2 = $zzt_db->table('pc_activity_registration_log')
|
||
->where(["content" => $insertData['content']])
|
||
->count();
|
||
|
||
if ($people_log2 >= 1) {
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '该表单内容已经提交过,请勿重复提交');
|
||
}
|
||
|
||
$insertResult = $zzt_db->table('pc_activity_registration_log')
|
||
->insert($insertData);
|
||
|
||
if ($insertResult) {
|
||
$zzt_db->commit();
|
||
return $this->msg([]);
|
||
} else {
|
||
$zzt_db->rollback();
|
||
return $this->msg(10002, '报名失败');
|
||
}
|
||
|
||
} catch (\Exception $e) {
|
||
$zzt_db->rollback();
|
||
|
||
// // 输出详细错误信息到日志
|
||
// \think\Log::error('报名系统错误:' . $e->getMessage());
|
||
// \think\Log::error('错误文件:' . $e->getFile() . ' 行号:' . $e->getLine());
|
||
// \think\Log::error('错误追踪:' . $e->getTraceAsString());
|
||
|
||
// // 如果是开发环境,直接显示错误详情
|
||
// if (config('app_debug')) {
|
||
// return $this->msg(10002, '系统错误:' . $e->getMessage() . ' 在文件:' . $e->getFile() . ' 第' . $e->getLine() . '行');
|
||
// }
|
||
|
||
return $this->msg(10002, '系统繁忙,请重试');
|
||
}
|
||
|
||
// foreach ($variable as $key => $value) {
|
||
// # code...
|
||
// }
|
||
// $data = [
|
||
// "id"=>1,
|
||
// "tel"=>15588668888,
|
||
// "content"=>[
|
||
// [
|
||
// 'name'=>'姓名',
|
||
// 'type'=>'text',
|
||
// 'value'=>'Jerry',
|
||
// ],
|
||
// [
|
||
// 'name'=>'备注',
|
||
// 'type'=>'textarea',
|
||
// 'value'=>'xxxxxxxxxxxxxxxxxxxxxxx',
|
||
// ],
|
||
// [
|
||
// 'name'=>'性别',
|
||
// 'type'=>'select',
|
||
// 'value'=>'男',
|
||
// ],
|
||
// [
|
||
// 'name'=>'兴趣',
|
||
// 'type'=>'checkbox',
|
||
// 'value'=>['选项1','选项2','选项3'],
|
||
// ],
|
||
// ],
|
||
// ];
|
||
|
||
// return $this->msg($data);
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |