SchoolPhysicalExamination/application/app/controller/Index.php

207 lines
8.4 KiB
PHP
Raw 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\app\controller;
use think\Controller;
use think\Db;
use app\bj\controller\Common;
use think\Log;
use \think\Validate;
class Index extends Base{
protected $db_name = ['2'=>'app_card_body_data','6'=>'app_card_skip_data','8'=>'app_card_vitalcapacity_data'];
// protected $card_key = ['2'=>'body','6'=>'skip','8'=>'vitalcapacity'];
protected $card_data = [
'2'=>['身体数据',['height','weight','bmi','record_time']],
// 'skip'=>['跳绳数据',['height,weight,bmi,record_time']],
'8'=>['肺活量',['average','record_time']],
];
################################################################个人资料卡################################################################
################################################################个人资料卡################################################################
################################################################个人资料卡################################################################
// // 个人信息
// public function personal_information(){
// // phpinfo();
// dump(123);
// $result = Db::table('admin_user')->select();
// dump($result);
// }
// 创建用户
public function create_user_data(){
$data = input();
$verify_result = $this->verify_parameters($data,'register');
if(!is_array($verify_result)){
return $this->msg(10002,$verify_result);
}
$result = Db::table('app_user_data')->insert($verify_result);
if($result){
return $this->msg(0,'success');
}else{
return $this->msg(10001,'创建失败');
}
}
// 获取账号下用户列表
// $type 1获取列表2获取详细信息
public function user_card_list($aan_id,$type=1){
$result = Db::table('app_user_data')->where(['aan_id'=>$aan_id])->select();
// $result = Db::table('app_user_data')->where(['aan_id'=>$aan_id])->field('id,nickname')->select();
$data = [];
if($type == 1){
for ($i=0; $i < count($result); $i++) {
array_push($data,['id'=>$result[$i]['id'],'nickname'=>$result[$i]['nickname']]);
}
}else{
$data = $result;
}
return $this->msg(0,'success',$data);
}
// 获取指定用户详细信息
public function user_data_information($id=11){
$result = Db::table('app_user_data')->where(['id'=>$id])->field('id,aan_id,nickname,birthday,gender,card_order')->find();
unset($result['ROW_NUMBER']);
if($result['card_order'] === ''){
$result['age'] = $this->calculate_age($result['birthday']);
$result['card_order'] = [];
$result['card_data_list'] = [];
}else{
$result['card_order'] = explode(',',$result['card_order']);
$result['card_data_list'] = $this->get_user_card_data_list($result['card_order'],$result['id']);
}
return $this->msg(0,'success',$result);
}
// 获取所有卡片列表信息
public function get_card_all_list($id = 11){
$user_card_list = Db::table('app_user_data')->where(['id'=>$id])->field('id,card_order')->find();
unset($user_card_list['ROW_NUMBER']);
$user_card_list['card_order'] = explode(',',$user_card_list['card_order']);
$all_card_list = Db::table('app_card_data')->field('id,name')->select();
// dump($user_card_list);
// dump($all_card_list);
$result = ['user'=>[],'all'=>[]];
foreach ($all_card_list as $key => $value) {
if(in_array($value['id'],$user_card_list['card_order'])){
$result['user'][array_search($value['id'], $user_card_list['card_order'])] = ['id'=>$value['id'],'name'=>$value['name']];
}else{
array_push($result['all'],['id'=>$value['id'],'name'=>$value['name']]);
}
}
ksort($result['user']);
return $this->msg(0,'success',$result);
}
// 保存用户的卡片排序
public function save_user_card_order($data=['id'=>11,'card_order'=>[2,8]]){
$data = input();
if(!array_key_exists('order_list', $data) || !array_key_exists('user_token', $data)){
return $this->msg(10001,'数据格式错误');
}
if(!$this->isNumericArray($data['order_list'])){
return $this->msg(10002,'数据内参数格式或值错误');
}
$data['card_order'] = json_encode($data['card_order']);
$result = Db::table('app_user_data')->where(['id'=>$data['id']])->update(['card_order'=>implode(',',$data['card_order'])]);
return $this->msg(0,'success');
}
// 存储卡片顺序
// $data = ['id'=>'xxxxxxxxxxxxxxx','card_order'=>[1,2,3,4,5]]
public function save_card_order(){
$data = input();
if(!array_key_exists('order_list', $data) || !array_key_exists('user_token', $data)){
return $this->msg(10001,'数据格式错误');
}
if(!$this->isNumericArray($data['order_list'])){
return $this->msg(10002,'数据内参数格式或值错误');
}
$data['order_list'] = json_encode($data['order_list']);
$result = Db::table('app_user_data')->where(['id'=>$data['aud_id']])->update(['card_order'=>$data['order_list']]);
if($result){
return $this->msg(0,'success');
}else{
return $this->msg(10003,'修改失败');
}
}
################################获取账号下信息操作################################
// 获取账号下首页卡片的基础数据
public function get_user_card_data_list($data,$aud_id){
$result = [];
$temporary_arr = [];
foreach ($data as $key => $value) {
$temporary_arr[$value] = Db::table($this->db_name[$value])->where(['aud_id'=>$aud_id])->order('id desc')->limit(1)->select();
}
foreach ($temporary_arr as $key => $value) {
if(!array_key_exists($key, $this->card_data)){
// dump($key.'====in');
continue;
}
foreach ($value[0] as $k => $v) {
// dump($k);
if(in_array($k, $this->card_data[$key][1])){
$result[$key][$k] = $v;
}
}
$result[$key]['name'] = $this->card_data[$key][0];
}
return $result;
}
################################################################other################################################################
################################################################other################################################################
################################################################other################################################################
public function verify_parameters($data,$type){
// 设置验证
$rule = [
'aan_id' => 'require|number',
'nickname' => 'require|chsAlpha',
'birthday' => 'require|date',
'gender' => 'require|number|in:0,1,2',
'grade' => 'require',
];
$msg = [
'aan_id.require' => '账号信息缺失',
'nickname.require' => '昵称缺失',
'birthday.require' => '生日缺失',
'gender.require' => '性别缺失',
'grade.require' => '年级缺失',
'aan_id.number' => '账号信息格式错误',
'nickname.chsAlpha' => '昵称只能是只能是汉字、字母',
'birthday.date' => '生日信息格式错误',
'gender.number' => '性别格式错误',
'gender.in' => '性别信息错误',
];
$validate = new Validate($rule,$msg);
$result = $validate->check($data);
if(!$result){
return $validate->getError();
}
$parameter['aan_id'] = $data['aan_id'];
$parameter['nickname'] = $data['nickname'];
$parameter['birthday'] = $data['birthday'];
$parameter['gender'] = $data['gender'];
$parameter['grade'] = $data['grade'];
$parameter['create_time'] = date('Y-m-d H:i:s');
$parameter['last_update_time'] = date('Y-m-d H:i:s');
return $parameter;
}
public function msg($code,$msg='',$data=[]){
return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
}
}