作者 lihan

疫苗小程序接口文件

<?php
namespace app\api\controller;
use cmf\controller\HomeBaseController;
use think\Db;
/**
* @title 疫苗助手小程序
*/
class VaccineController extends HomeBaseController {
function _initialize() {
header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求
header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
}
/**
* @title 小程序授权
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/auth
* @method POST
* @module 授权模块
*
* @param name:code type:char require:1 default: other: desc:授权code
* @param name:nickname type:char require:1 default: other: desc:昵称
* @param name:head_img type:text require:1 default: other: desc:头像
*/
public function auth() {
$request=request();
if($request->isPost()) {
$code=$request->param('code');
$url='https://api.weixin.qq.com/sns/jscode2session?appid='.config('AppID').
'&secret='.config('AppSecret').'&js_code='.$code.'&grant_type=authorization_code';
$content = $this->_request($url);
$data['content'] = $content;
$temp=json_decode($data['content'], true);
$openid=$temp['openid'];
if(empty($openid)) {
echo json_encode(["code"=>1, "message"=>"code error"]);
exit();
}else {
if (Db::name('users')->where(['openid' => $openid])->count()) {
$user_id = Db::name('users')->field('id')->where(['openid' => $openid])->find();
$result = [
'user_id' => $user_id['id'],
'openid' => $openid,
'go' => 'login'
];
echo json_encode(["code" => 0, "message" => "success", "data" => $result]);
exit();
} else {
if(empty($request->param('nickname') || empty($request->param('head_img')))) {
echo json_encode(["code" => 1, "message" => "info wrong"]);
exit();
}
$insert = [
'openid' => $openid,
'nickname' => $request->param('nickname'),
'head_img' => $request->param('head_img'),
'child_num' => 0,
'register_time' => time(),
'status' => 1,
'from' => 'public'
];
if (Db::name('users')->insert($insert)) {
$user_id = Db::name('users')->getLastInsID();
$result = [
'user_id' => $user_id,
'openid' => $openid,
'go' => 'register'
];
echo json_encode(["code" => 0, "message" => "success", "data" => $result]);
exit();
} else {
echo json_encode(["code" => 1, "message" => "register failed"]);
exit();
}
}
}
}
}
public function auth0() {
$request=request();
if($request->isPost()) {
$code=$request->param('code');
$url='https://api.weixin.qq.com/sns/jscode2session?appid='.config('AppID').
'&secret='.config('AppSecret').'&js_code='.$code.'&grant_type=authorization_code';
$content = $this->_request($url);
$data['content'] = $content;
$temp=json_decode($data['content'], true);
$openid=$temp['openid'];
if (Db::name('users')->where(['openid' => $openid])->count()) {
$user_id=Db::name('users')->field('id')->where(['openid' => $openid])->find();
$result = [
'user_id' => $user_id['id'],
'openid' => $openid,
'go' => 'login'
];
}else {
$result = [
'openid' => $openid,
'go' => 'register'
];
}
echo json_encode(["code" => 0, "message" => "success", "data"=>$result]);
exit();
}
}
/**
* @title 家长注册宝宝
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/registerChild
* @method POST
* @module 宝宝模块
*
* @param name:parent_id type:int require:1 default: other: desc:家长id
* @param name:baby_name type:char require:1 default: other: desc:宝宝姓名
* @param name:baby_sex type:int require:1 default: other: desc:宝宝性别1男孩0女孩
* @param name:baby_birthday type:char require:1 default: other: desc:宝宝出生日期
*/
public function registerChild() {
$request=request();
Db::startTrans();
if($request->isPost()) {
$parent_id=$request->param('parent_id');
if($parent_id != null) {
$parent=Db::name('users')->field('openid,nickname')->where(['id'=>$parent_id])->find();
$data=$request->param();
if(empty($data['baby_name'])) {
echo json_encode(["code"=>1, "message"=>"baby_name is null"]);
exit();
}
if(!isset($data['baby_sex'])) {
echo json_encode(["code"=>1, "message"=>"baby_sex is null"]);
exit();
}
if(empty($data['baby_birthday'])) {
echo json_encode(["code"=>1, "message"=>"baby_birthday is null"]);
exit();
}
$map=[
'parent_id' => ['eq', $parent_id],
'baby_name' => ['eq', $data['baby_name']],
'baby_sex' => ['eq', $data['baby_sex']],
'baby_birthday' => ['eq', $data['baby_birthday']]
];
if(Db::name('child')->where($map)->count()) {
echo json_encode(["code"=>1, "message"=>"The baby already exists"]);
exit();
}
$res=[
'parent_id' => $parent_id,
'openid' => $parent['openid'],
'baby_name' => $data['baby_name'],
'baby_sex' => $data['baby_sex'],
'baby_birthday' => strtotime($data['baby_birthday']),
'register_time' => time()
];
if(Db::name('child')->insert($res)) {
$child_id = Db::name('child')->getLastInsID();
if(Db::name('users')->where(['id'=>$parent_id])->setInc('child_num', 1)) {
$vaccineList=$this->getAllVaccineList();
$result=[];
foreach ($vaccineList as $key => $item) {
$result[$key]=[
'baby_id' => $child_id,
'vaccine_id' => $item['id'],
'is_handle' => 0,
'should_vaccine_time' => $this->getShouldVaccineTime($res['baby_birthday'], $item['vaccine_time'])
];
}
if(Db::name('vaccine_list')->insertAll($result)) {
Db::commit();
echo json_encode(["code"=>0, "message"=>"success", "data"=>['child_id'=>$child_id]]);
exit();
}else {
Db::rollback();
}
}else {
Db::rollback();
}
}else {
Db::rollback();
}
}else {
echo json_encode(["code"=>1, "message"=>"parent_id is null"]);
exit();
}
}
}
/**
* @title 宝宝列表(我的)
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/getChildList
* @method POST
* @module 宝宝模块
*
* @param name:parent_id type:int require:1 default: other: desc:家长id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getChildList() {
$request=request();
if($request->isPost()) {
$parent_id=$request->param('parent_id');
if(!empty($parent_id)) {
if(Db::name('users')->where(['id'=>$parent_id])->count()) {
$res=Db::name('child')->field('id as baby_id,baby_name,baby_birthday,baby_sex')->where(['parent_id'=>$parent_id])->order('register_time DESC')->select()->toArray();
if(!empty($res)) {
foreach ($res as $k => $v) {
$v['age']=$this->calcAge($v['baby_birthday']);
$v['next']=$this->getNextVaccineDay($v['baby_id']);
unset($v['baby_birthday']);
$res[$k]=$v;
}
$result=[
'user_info' => Db::name('users')->field('nickname,head_img')->where(['id'=>$parent_id])->find(),
'child_list' => $res
];
echo json_encode(["code"=>0, "message"=>"success", "data"=>$result]);
exit();
}else {
$result=[
'user_info' => Db::name('users')->field('nickname,head_img')->where(['id'=>$parent_id])->find(),
'child_list' => null
];
echo json_encode(["code"=>0, "message"=>"success", "data"=>$result]);
exit();
}
}else {
echo json_encode(["code"=>1, "message"=>"parent_id is wrong"]);
exit();
}
}else {
echo json_encode(["code"=>1, "message"=>"parent_id is null"]);
exit();
}
}
}
/**
* @title 获取宝宝信息
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/getChildInfo
* @method POST
* @module 宝宝模块
*
* @param name:child_id type:int require:1 default: other: desc:宝宝id
*/
public function getChildInfo() {
$request=request();
if($request->isPost()) {
$child_id=$request->param('child_id');
if(!empty($child_id)) {
$result=Db::name('child')->field('baby_name,baby_sex,baby_birthday')->where(['id'=>$child_id])->find();
if(!empty($result)) {
$result['baby_birthday']=date('Y-m-d', $result['baby_birthday']);
echo json_encode(["code"=>0, "message"=>"success", "data"=>$result]);
exit();
}else {
echo json_encode(["code"=>1, "message"=>"return null"]);
exit();
}
}else {
echo json_encode(["code"=>1, "message"=>"child_id is null"]);
exit();
}
}
}
/**
* @title 更改宝宝信息
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/updateChildInfo
* @method POST
* @module 宝宝模块
*
* @param name:child_id type:int require:1 default: other: desc:宝宝id
* @param name:baby_name type:char require:1 default: other: desc:宝宝姓名
* @param name:baby_sex type:int require:1 default: other: desc:宝宝性别
* @param name:baby_birthday type:char require:1 default: other: desc:宝宝生日
*/
public function updateChildInfo() {
$request=request();
if($request->isPost()) {
$child_id=$request->param('child_id');
if(!empty($child_id)) {
if(empty($request->param('baby_name'))) {
echo json_encode(["code"=>1, "message"=>"baby_name is null"]);
exit();
}
if($request->param('baby_sex') === false) {
echo json_encode(["code"=>1, "message"=>"baby_sex is null"]);
exit();
}
if(empty($request->param('baby_birthday'))) {
echo json_encode(["code"=>1, "message"=>"baby_birthday is null"]);
exit();
}
$data=[
'id' => $child_id,
'baby_name' => $request->param('baby_name'),
'baby_sex' => $request->param('baby_sex'),
'baby_birthday' => strtotime($request->param('baby_birthday'))
];
Db::startTrans();
if(Db::name('child')->update($data)) {
$list=$this->getAllVaccineList();
$res=Db::name('vaccine_list')->field('id')->where(['baby_id'=>$child_id])->select();
$m='';
foreach ($res as $k => $v) {
$update=[
'id' => $v['id'],
'should_vaccine_time' => $this->getShouldVaccineTime(strtotime($request->param('baby_birthday')), $list[$k]['vaccine_time'])
];
if(Db::name('vaccine_list')->update($update) >= 0) {
$m='commit';
}else {
$m='rollback';
break;
}
}
if($m == 'commit') {
Db::commit();
echo json_encode(["code"=>0, "message"=>"success"]);
exit();
}else {
Db::rollback();
echo json_encode(["code"=>1, "message"=>"failed"]);
exit();
}
}else {
echo json_encode(["code"=>1, "message"=>"failed"]);
exit();
}
}else {
echo json_encode(["code"=>1, "message"=>"child_id is null"]);
exit();
}
}
}
/**
* @title 删除宝宝信息
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/delChildInfo
* @method POST
* @module 宝宝模块
*
* @param name:child_id type:int require:1 default: other: desc:宝宝id
* @param name:parent_id type:int require:1 default: other: desc:家长id
*/
public function delChildInfo() {
$request=request();
if($request->isPost()) {
$parent_id=$request->param('parent_id');
$child_id=$request->param('child_id');
if(!empty($parent_id) && !empty($child_id)) {
if(Db::name('child')->where(['parent_id'=>$parent_id,'id'=>$child_id])->count()) {
Db::startTrans();
if(Db::name('child')->delete($child_id)) {
if(Db::name('vaccine_list')->where(['baby_id'=>$child_id])->delete()) {
if(Db::name('users')->where(['id'=>$parent_id])->setDec('child_num', 1)) {
Db::commit();
echo json_encode(["code"=>0, "message"=>"success"]);
exit();
}else {
Db::rollback();
}
}else {
Db::rollback();
}
}else {
DB::rollback();
}
}
}
}
}
/**
* @title 选择疫苗
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/choiceVaccine
* @method POST
* @module 疫苗模块
*
* @param name:child_id type:int require:1 default: other: desc:宝宝id
* @param name:parent_id type:int require:1 default: other: desc:家长id
* @param name:list_id type:int require:1 default: other: desc:疫苗列表id
*/
public function choiceVaccine() {
$request=request();
if($request->isPost()) {
$parent_id=$request->param('parent_id');
$child_id=$request->param('child_id');
if(!empty($parent_id) && !empty($child_id)) {
if(Db::name('child')->where(['parent_id'=>$parent_id,'id'=>$child_id])->count()) {
$hand=Db::name('vaccine_list')->field('is_handle')->where(['id'=>$request->param('list_id')])->find();
$is_handle=$hand['is_handle'];
if($is_handle == 1) {
$is_handle=0;
}else {
$is_handle=1;
}
$data=[
'id' => $request->param('list_id'),
'is_handle' => $is_handle
];
if(Db::name('vaccine_list')->update($data)) {
echo json_encode(["code"=>0, "message"=>"success", "data"=>['is_handle'=>$is_handle]]);
exit();
}else {
echo json_encode(["code"=>1, "message"=>"failed"]);
exit();
}
}
}
}
}
/**
* @title 疫苗详情
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/vaccineDetail
* @method POST
* @module 疫苗模块
*
* @param name:vaccine_id type:int require:1 default: other: desc:疫苗id
*/
public function vaccineDetail() {
$request=request();
if($request->isPost()) {
$vaccine_id=$request->param('vaccine_id');
$info=Db::name('vaccine')->field('vaccine_name,tag,vaccine_content')->where(['id'=>$vaccine_id])->find();
$info['tag']=explode('、', $info['tag']);
echo json_encode(["code"=>0, "message"=>"success", "data"=>$info]);
exit();
}
}
/**
* @title 宝宝个人专属疫苗列表
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/vaccineList
* @method POST
* @module 疫苗模块
*
* @param name:child_id type:int require:1 default: other: desc:宝宝id
*/
public function vaccineList() {
$request=request();
if($request->isPost()) {
$child_id=$request->param('child_id');
if(!empty($child_id)) {
if(Db::name('child')->where(['id'=>$child_id])->count()) {
$child_info = Db::name('child')->field('baby_name,baby_birthday')->where(['id' => $child_id])->find();
$child_info['age'] = $this->calcAge($child_info['baby_birthday']);
$child_info['next_vaccine_date'] = $this->getNextVaccineDate($child_id);
unset($child_info['baby_birthday']);
$res = Db::name('vaccine')
->alias('v')
->field('v.id as vaccine_id,vv.id as list_id,v.vaccine_name,v.needle_num,v.type,v.vaccine_desc,v.vaccine_time,vv.is_handle,vv.should_vaccine_time')
->where(['vv.baby_id'=>$child_id])
->join('vaccine_list vv', 'v.id=vv.vaccine_id')
->select();
$temp = [];
$temp_time = 0;
$i = 0;
$j = 0;
foreach ($res as $k => $v) {
$v['should_vaccine_time'] = date('Y-m-d', $v['should_vaccine_time']);
if ($v['vaccine_time'] != $temp_time) {
$i++;
$j = 0;
}
if($v['vaccine_time'] == 0) {
$temp[$i]['age_tag']='出生时';
}elseif($v['vaccine_time'] < 12) {
$temp[$i]['age_tag']=$v['vaccine_time'].'月龄';
}else {
$age_y=intval($v['vaccine_time']/12);
$age_m=$v['vaccine_time']%12;
if($age_m == 0) {
$temp[$i]['age_tag']=$age_y.'岁';
}else {
$temp[$i]['age_tag']=$age_y.'岁'.$age_m.'月龄';
}
}
$temp[$i]['should_vaccine_time']=$v['should_vaccine_time'];
$temp_time = $v['vaccine_time'];
unset($v['vaccine_time']);
unset($v['should_vaccine_time']);
$temp[$i]['vaccine_list'][$j] = $v;
$j++;
}
$result = [
'child_info' => $child_info,
'list' => $temp
];
echo json_encode(["code" => 0, "message" => "success", "data" => $result]);
exit();
}else {
echo json_encode(["code"=>1, "message"=>"child_id is wrong"]);
exit();
}
}else {
echo json_encode(["code"=>1, "message"=>"child_id is null"]);
exit();
}
}
}
/**
* @title 提交反馈
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/postFeedBack
* @method POST
* @module 意见模块
*
* @param name:user_id type:int require:1 default: other: desc:家长user_id
* @param name:content type:text require:1 default: other: desc:意见内容
*/
public function postFeedBack() {
$request=request();
if($request->isPost()) {
if(empty($request->param('content'))) {
echo json_encode(["code"=>1, "message"=>"content is null"]);
exit();
}
$data=[
'user_id' => $request->param('user_id'),
'content' => $request->param('content'),
'add_time' => time(),
'is_read' => 0,
'status' => 1
];
if(Db::name('feedback')->insert($data)) {
echo json_encode(["code"=>0, "message"=>"success"]);
exit();
}
}
}
/**
* @title 用户协议
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/userAgreement
* @method POST
* @module 文章模块
*/
public function userAgreement() {
$info=Db::name('portal_post')->field('post_content')->where(['id'=>1])->find();
$article=$info['post_content'];
echo json_encode(["code"=>0, "message"=>"success", "data"=>$article]);
exit();
}
/**
* @title 关于我们
* @description 接口说明
* @author Soul of Cinder
* @url /index.php/api/Vaccine/about
* @method POST
* @module 文章模块
*/
public function about() {
$info=Db::name('portal_post')->field('post_content')->where(['id'=>2])->find();
$article=$info['post_content'];
echo json_encode(["code"=>0, "message"=>"success", "data"=>$article]);
exit();
}
/******************************************************************************************************************/
/**
* 根据出生日期计算孩子年龄(格式:几岁零几个月)
* @param $birthday
* @return string
*/
private function calcAge($birthday) {
$baby_Y=date('Y', $birthday);
$baby_M=date('m', $birthday);
$baby_D=date('d', $birthday);
$now_Y=date('Y');
$now_M=date('m');
$now_D=date('d');
$age_Y=$now_Y-$baby_Y;
$age_M=$now_M-$baby_M;
if($age_M < 0) {
$age_Y--;
$age_M=12-$baby_M+$now_M;
}
$age_D=$now_D-$baby_D;
if($age_D < 0) {
$age_M--;
}
$age_M=($age_M < 0) ? 0 : $age_M;
return $age_Y.'岁'.$age_M.'个月';
}
/**
* 根据出生日期计算孩子年龄(格式:几岁零几个月)
* @param $birthday
* @return string
*/
private function calcMonth($birthday) {
$baby_Y=date('Y', $birthday);
$baby_M=date('m', $birthday);
$baby_D=date('d', $birthday);
$now_Y=date('Y');
$now_M=date('m');
$now_D=date('d');
$age_Y=$now_Y-$baby_Y;
$age_M=$now_M-$baby_M;
if($age_M < 0) {
$age_Y--;
$age_M=12-$baby_M+$now_M;
}
$age_D=$now_D-$baby_D;
if($age_D < 0) {
$age_M--;
}
$age_M=($age_M < 0) ? 0 : $age_M;
$m=$age_Y*12+$age_M;
return $m;
}
/**
* 获取所有疫苗列表
*/
private function getAllVaccineList() {
return Db::name('vaccine')->field('id,vaccine_time')->select();
}
/**
* 根据生日和月龄计算疫苗接种日期
* @param $baby_birthday
* @param $vaccine_time
* @return false|int
*/
private function getShouldVaccineTime($baby_birthday, $vaccine_time) {
$should_vaccine_time=strtotime("+$vaccine_time month", $baby_birthday);
return $should_vaccine_time;
}
/**
* 计算下次疫苗距离天数
* @param $baby_id
* @return null|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function getNextVaccineDay($baby_id) {
$map=[
'baby_id' => ['eq', $baby_id],
'should_vaccine_time' => ['egt', strtotime(date('Y-m-d 00:00:00'))]
];
$res=Db::name('vaccine_list')->where($map)->order('should_vaccine_time ASC')->select()->toArray();
if(!empty($res)) {
$next=$res[0];
$dec=$next['should_vaccine_time']-strtotime(date('Y-m-d 00:00:00'));
return '下次疫苗在'.ceil($dec/3600/24).'天后';
}else {
return null;
}
}
private function getNextVaccineDate($baby_id) {
$map=[
'baby_id' => ['eq', $baby_id],
'should_vaccine_time' => ['egt', strtotime(date('Y-m-d 00:00:00'))]
];
$res=Db::name('vaccine_list')->where($map)->order('should_vaccine_time ASC')->select()->toArray();
if(!empty($res)) {
$next=$res[0];
return date('Y-m-d', $next['should_vaccine_time']);
}else {
return null;
}
}
private function _request($curl,$https=true,$method='GET',$data=null) {
// 创建一个新cURL资源
$ch = curl_init();
// 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $curl); //要访问的网站
curl_setopt($ch, CURLOPT_HEADER, false); //启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //将curl_exec()获取的信息以字符串返回,而不是直接输出。
if($https){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //FALSE 禁止 cURL 验证对等证书(peer's certificate)。
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //验证主机
}
if($method == 'POST'){
curl_setopt($ch, CURLOPT_POST, true); //发送 POST 请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //全部数据使用HTTP协议中的 "POST" 操作来发送。
}
// 抓取URL并把它传递给浏览器
$content = curl_exec($ch);
if ($content === false) {
return "网络请求出错: " . curl_error($ch);
exit();
}
//关闭cURL资源,并且释放系统资源
curl_close($ch);
return $content;
}
}
\ No newline at end of file
... ...