SchoolPhysicalExamination/application/OutsideInterface/controller/ZhiZhaoTuan/Index.php

242 lines
7.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\OutsideInterface\controller\ZhiZhaoTuan;
use think\Db;
use Picqer\Barcode\BarcodeGeneratorPNG;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
class Index extends Base
{
protected $page_num = 20;
// 智照团表单列表
public function index($page=1)
{
$data = input();
$pd = true;
$parameter = [];
if(array_key_exists('tt', $data)){
$page = $data['page_num'];
unset($data['page_num']);
unset($data['tt']);
$pd = false;
}
$zzt_db = Db::connect('zzt_db');
$num = $zzt_db->table('pc_activity_registration')->where($parameter)->count();
$result = $zzt_db->table('pc_activity_registration')->where($parameter)->order('id desc')->page($page,$this->page_num)->field('id,title,people_num,people_num_now,pic,is_del,start_time,end_time,create_time')->select();
if(!$pd){
$result['num'] = $num;
$result['data'] = $result;
return $this->msg($result);
}
$this->assign([
'result' => $result,
'num' => $num,
]);
return $this->fetch();
}
public function del_action()
{
$data = input('post.');
$zzt_db = Db::connect('zzt_db');
$result = $zzt_db->table('pc_activity_registration')->where(['id'=>$data['id']])->update(['is_del'=>$data['is_del']]);
if($result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
// 智照团添加表单
public function add_form2()
{
return $this->fetch();
}
// 智照团添加表单
public function add_form_action()
{
$data = input('post.');
// dump($data);
// die;
$zzt_db = Db::connect('zzt_db');
$result = $zzt_db->table('pc_activity_registration')->insert([
'title'=>$data['title'],
'people_num'=>$data['people_num'],
'start_time'=>$data['s_time'],
'end_time'=>$data['e_time'],
'text_content'=>$data['content'],
'form_content'=>json_encode($data['form_data']),
'pic'=>$data['cover_image'],
'is_only_once'=>$data['is_only_once'],
'create_time'=>date('Y-m-d H:i:s')
]);
if($result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
// 智照团修改表单
public function edit_form()
{
$data = input();
$zzt_db = Db::connect('zzt_db');
$result = $zzt_db->table('pc_activity_registration')->where(['id'=>$data['id']])->field('id,title,people_num,start_time,end_time,text_content,form_content,pic,is_only_once')->find();
$pic = $zzt_db->table('pc_pic_manage')->where(['id'=>$result['pic']])->field('id,url_data')->find();
$result['pic_url'] = $pic['url_data'];
// dump($result);
// die;
$this->assign([
'result' => $result,
]);
return $this->fetch();
}
// 智照团修改表单
public function edit_form_action()
{
$data = input('post.');
// dump($data);
// die;
$zzt_db = Db::connect('zzt_db');
$people_num = $zzt_db->table('pc_activity_registration_log')->where(['par_id'=>$data['id']])->count();
if($people_num>0){
$yz_data = $zzt_db->table('pc_activity_registration')->where(['id'=>$data['id']])->field('id,form_content,people_num')->find();
if($yz_data['form_content'] != json_encode($data['form_data'])){
return $this->msg(10002,'已有人报名,不可修改表单项目');
}
if($yz_data['people_num'] > $data['people_num']){
return $this->msg(10002,'已有人报名,请不要缩减报名人数');
}
}
$result = $zzt_db->table('pc_activity_registration')
->where(['id'=>$data['id']])
->update([
'title'=>$data['title'],
'people_num'=>$data['people_num'],
'start_time'=>$data['s_time'],
'end_time'=>$data['e_time'],
'text_content'=>$data['content'],
'form_content'=>json_encode($data['form_data']),
'pic'=>$data['cover_image'],
'is_only_once'=>$data['is_only_once'],
]);
if($result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
// 下载报名人员
public function down_data_action(){
$data = input();
$zzt_db = Db::connect('zzt_db');
$down_data = $zzt_db->table('pc_activity_registration_log')->where(['par_id'=>$data['id']])->select();
$activity_registration_data = $zzt_db->table('pc_activity_registration')->where(['id'=>$data['id']])->field('id,title,form_content')->find();
if($activity_registration_data){
$activity_registration_data['form_content'] = json_decode($activity_registration_data['form_content'],true);
}else{
return $this->msg(10002,'未找到对应活动');
}
$config = $activity_registration_data;
// 构建表头
$header = ['序号', '会员电话'];
// 添加表单字段名称到表头
foreach ($config['form_content'] as $field) {
$header[] = $field['name'];
}
if(count($down_data) > 0){
foreach ($down_data as $key => $value) {
$down_data[$key]['content'] = json_decode($value['content'],true);
}
$userData = $down_data;
// 初始化结果数组
$result = [];
$result[] = $header;
// 处理每条用户数据
foreach ($userData as $index => $user) {
$row = [];
// 序号(使用数组索引+1或者用用户数据中的id
$row[] = $index + 1;
// 会员电话处理:如果不是手机号格式,显示"非会员用户"
$tel = $user['tel'];
if (!preg_match('/^1[3-9]\d{9}$/', $tel)) {
$row[] = "非会员用户";
} else {
$row[] = $tel;
}
// 处理每个表单字段的值
foreach ($config['form_content'] as $configField) {
$fieldName = $configField['name'];
$fieldType = $configField['type'];
$fieldValue = '';
// 在用户数据中查找对应的字段值
foreach ($user['content'] as $userField) {
if ($userField['name'] === $fieldName) {
$fieldValue = $userField['value'];
// 如果是checkbox类型替换"CH1"为逗号
if ($fieldType === 'checkbox') {
$fieldValue = str_replace('CH1', ',', $fieldValue);
}
break;
}
}
$row[] = $fieldValue;
}
$result[] = $row;
}
}else{
// 初始化结果数组
$result = [];
$result[] = $header;
}
$excel = new \app\download\controller\Excel();
$excel->export($result, $activity_registration_data['title']);
}
}