400 lines
15 KiB
PHP
400 lines
15 KiB
PHP
<?php
|
||
namespace app\app\controller;
|
||
|
||
use think\Request;
|
||
use think\Db;
|
||
|
||
/**
|
||
* CPP生长减速预测模型 小程序API
|
||
*/
|
||
class Cppwechat extends Base
|
||
{
|
||
private $Appid;
|
||
private $Appsecret;
|
||
private $TokenKey;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->Appid = 'wx0847525a15342a46';
|
||
$this->Appsecret = '71cf8c524580ba6b262e6dc52d44da4c';
|
||
$this->TokenKey = 'ruilai340u20k4';
|
||
}
|
||
|
||
/********************************* 首页 *********************************/
|
||
public function selectRealtimeInfoList(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
$page = isset($post['page']) && !empty($post['page']) ? $post['page'] : 1;
|
||
$page_size = isset($post['page_size']) && !empty($post['page_size']) ? $post['page_size'] : 20;
|
||
|
||
$res = Db::name('wechat_real_time_info')
|
||
->field('title_plain,thumbnail,excerpt_plain,url')
|
||
->order('create_time desc')
|
||
->paginate($page_size, false, ['page' => $page]);
|
||
|
||
$result = array();
|
||
$result['lists'] = $res->items();
|
||
$result['lastPage'] = $res->lastPage();
|
||
$result['currentPage'] = $res->currentPage();
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '查询成功';
|
||
return json($result);
|
||
}
|
||
|
||
/********************************* 计算 *********************************/
|
||
public function calculate(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
|
||
if (!isset($post['uid']) || empty($post['uid'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入用户id";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['name']) || empty($post['name'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入姓名";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['bone_age']) || empty($post['bone_age']) || !is_numeric($post['bone_age'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入初诊骨龄或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['uterus_length']) || empty($post['uterus_length']) || !is_numeric($post['uterus_length'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入初诊子宫长或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['uterus_width']) || empty($post['uterus_width']) || !is_numeric($post['uterus_width'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入初诊子宫宽或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['lh_peak']) || empty($post['lh_peak']) || !is_numeric($post['lh_peak'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入LH峰值或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['fsh_peak']) || empty($post['fsh_peak']) || !is_numeric($post['fsh_peak'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入FSH峰值或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['fsh_3m']) || empty($post['fsh_3m']) || !is_numeric($post['fsh_3m'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入治疗3月FSH或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['igf1']) || empty($post['igf1']) || !is_numeric($post['igf1'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入基础IGF-1或数据格式不对";
|
||
return json_encode($result);
|
||
}
|
||
|
||
// 参数取值
|
||
$bone_age = floatval($post['bone_age']);
|
||
$uterus_length = floatval($post['uterus_length']);
|
||
$uterus_width = floatval($post['uterus_width']);
|
||
$lh_peak = floatval($post['lh_peak']);
|
||
$fsh_peak = floatval($post['fsh_peak']);
|
||
$fsh_3m = floatval($post['fsh_3m']);
|
||
$igf1 = floatval($post['igf1']);
|
||
|
||
// 交互项: 骨龄 x 子宫长
|
||
$interaction_ba_ul = $bone_age * $uterus_length;
|
||
// 比值项: LH峰值 / FSH峰值
|
||
$lh_fsh_ratio = $lh_peak / $fsh_peak;
|
||
// log(FSH峰值)
|
||
$log_fsh_peak = log($fsh_peak);
|
||
|
||
// 预测模型
|
||
$logit = -2.8457
|
||
+ 0.1198 * $interaction_ba_ul
|
||
+ 1.0411 * $log_fsh_peak
|
||
- 0.4832 * $fsh_3m
|
||
- 0.0044 * $igf1
|
||
+ 0.4207 * $lh_fsh_ratio
|
||
- 0.8303 * $uterus_width;
|
||
|
||
// sigmoid
|
||
$exp_val = exp($logit);
|
||
$probability = $exp_val / (1 + $exp_val);
|
||
|
||
// 风险分级
|
||
if ($probability < 0.430) {
|
||
$risk_level = '低风险';
|
||
$suggestion = '继续目前GnRHa治疗方案,按常规6个月随访即可,关注身高增长趋势。';
|
||
} elseif ($probability <= 0.572) {
|
||
$risk_level = '中风险';
|
||
$suggestion = '建议缩短随访间隔至3个月,关注GV下降趋势,必要时联合rhGH。';
|
||
} else {
|
||
$risk_level = '高风险';
|
||
$suggestion = '建议每2-3个月密切随访,积极监测生长速率变化,尽早考虑rhGH联合干预。';
|
||
}
|
||
|
||
// 保存记录
|
||
$data = [];
|
||
$data['id'] = md5(time() . rand(100000, 999999));
|
||
$data['uid'] = $post['uid'];
|
||
$data['name'] = $post['name'];
|
||
$data['bone_age'] = $bone_age;
|
||
$data['uterus_length'] = $uterus_length;
|
||
$data['uterus_width'] = $uterus_width;
|
||
$data['lh_peak'] = $lh_peak;
|
||
$data['fsh_peak'] = $fsh_peak;
|
||
$data['fsh_3m'] = $fsh_3m;
|
||
$data['igf1'] = $igf1;
|
||
$data['calculate_result'] = round($probability, 4);
|
||
$data['risk_level'] = $risk_level;
|
||
$data['create_time'] = date('Y-m-d H:i:s');
|
||
|
||
$res = Db::name('cpp_calculate_record')->insert($data);
|
||
if ($res === false) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '计算失败,请重试';
|
||
return json_encode($result);
|
||
} else {
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '计算成功';
|
||
$result['calculate_result'] = round($probability, 4);
|
||
$result['risk_level'] = $risk_level;
|
||
$result['suggestion'] = $suggestion;
|
||
return json_encode($result);
|
||
}
|
||
}
|
||
|
||
/********************************* 记录 *********************************/
|
||
public function selectRecordList(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
if (!isset($post['uid']) || empty($post['uid'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入用户id";
|
||
return json_encode($result);
|
||
}
|
||
$page = isset($post['page']) && !empty($post['page']) ? $post['page'] : 1;
|
||
$page_size = isset($post['page_size']) && !empty($post['page_size']) ? $post['page_size'] : 30;
|
||
|
||
$res = Db::name('cpp_calculate_record')
|
||
->field('id,name,bone_age,uterus_length,uterus_width,lh_peak,fsh_peak,fsh_3m,igf1,calculate_result,risk_level,create_time')
|
||
->where('uid', $post['uid'])
|
||
->order('create_time desc')
|
||
->paginate($page_size, false, ['page' => $page]);
|
||
|
||
$result = array();
|
||
$result['lists'] = $res->items();
|
||
$result['lastPage'] = $res->lastPage();
|
||
$result['currentPage'] = $res->currentPage();
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '查询成功';
|
||
return json($result);
|
||
}
|
||
|
||
public function deleteRecord(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
if (!isset($post['uid']) || empty($post['uid'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入用户id";
|
||
return json($result);
|
||
}
|
||
if (!isset($post['id']) || empty($post['id'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "请选择要删除的记录";
|
||
return json($result);
|
||
}
|
||
$id = Db::name('cpp_calculate_record')
|
||
->where('id', $post['id'])
|
||
->where('uid', $post['uid'])
|
||
->value('id');
|
||
if (empty($id)) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '该记录异常,无法删除';
|
||
return json($result);
|
||
}
|
||
$res = Db::name('cpp_calculate_record')->where('id', $id)->delete();
|
||
if ($res > 0) {
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '删除成功';
|
||
} else {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '删除失败';
|
||
}
|
||
return json($result);
|
||
}
|
||
|
||
/********************************* 反馈 *********************************/
|
||
public function submitFeedback(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
if (!isset($post['uid']) || empty($post['uid'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "请先登录";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['content']) || empty(trim($post['content']))) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "请输入反馈内容";
|
||
return json_encode($result);
|
||
}
|
||
$data = [];
|
||
$data['uid'] = $post['uid'];
|
||
$data['content'] = trim($post['content']);
|
||
$data['create_time'] = date('Y-m-d H:i:s');
|
||
$res = Db::name('cpp_feedback')->insert($data);
|
||
if ($res) {
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '感谢您的反馈';
|
||
} else {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '提交失败,请重试';
|
||
}
|
||
return json_encode($result);
|
||
}
|
||
|
||
/********************************* 登录 *********************************/
|
||
public function wechatLogin(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
if (!isset($post['code']) || empty($post['code'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入code";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['encryptedData']) || empty($post['encryptedData'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入encryptedData";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['iv']) || empty($post['iv'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入iv";
|
||
return json_encode($result);
|
||
}
|
||
|
||
$encryptedData = $post['encryptedData'];
|
||
$iv = $post['iv'];
|
||
$code = $post['code'];
|
||
$URL = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->Appid . "&secret=" . $this->Appsecret . "&js_code=$code&grant_type=authorization_code";
|
||
$apiData = json_decode($this->httpGet($URL), true);
|
||
|
||
if (!isset($apiData['errcode'])) {
|
||
$sessionKey = $apiData['session_key'];
|
||
$errCode = $this->decryptData($sessionKey, $this->Appid, $encryptedData, $iv, $data);
|
||
if ($errCode == 0) {
|
||
$record = json_decode($data, true);
|
||
$res = $this->saveUseInfor($record);
|
||
return json_encode($res);
|
||
} else {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '解析接口返回数据失败,请重试!';
|
||
return json_encode($result);
|
||
}
|
||
} else {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = $apiData['errmsg'];
|
||
return json_encode($result);
|
||
}
|
||
}
|
||
|
||
private function saveUseInfor($record)
|
||
{
|
||
$data = array();
|
||
$data['nickname'] = $record['nickName'];
|
||
$data['headimg'] = $record['avatarUrl'];
|
||
|
||
$rec = Db::name('wechat_user')->field('uid')->where('openid', $record['openId'])->find();
|
||
|
||
if (empty($rec)) {
|
||
$data['openid'] = $record['openId'];
|
||
$data['create_time'] = date('Y-m-d H:i:s');
|
||
$res = Db::name('wechat_user')->insertGetId($data);
|
||
$uid = $res;
|
||
} else {
|
||
$res = Db::name('wechat_user')->where('uid', $rec['uid'])->update($data);
|
||
$uid = $rec['uid'];
|
||
}
|
||
if ($res === false) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '保存信息失败';
|
||
return $result;
|
||
} else {
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '登录成功';
|
||
$result['uid'] = $uid;
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
public function getToken(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
if (!isset($post['uid']) || empty($post['uid'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传登录用户id";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['phone']) || empty($post['phone'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传登录用户账号";
|
||
return json_encode($result);
|
||
}
|
||
$arr = array();
|
||
$arr['phone'] = $post['phone'];
|
||
$arr['status'] = 1;
|
||
$res = Db::name('accounthosting')->where($arr)->value('id');
|
||
if ($res != $post['uid']) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = '账号异常';
|
||
return json_encode($result);
|
||
}
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '登录成功';
|
||
$result['token'] = $this->authcode($post['uid'] . '_' . time(), 'ENCODE', $this->TokenKey, 60 * 60 * 3);
|
||
return json_encode($result);
|
||
}
|
||
|
||
private function checkToken($token, $uid)
|
||
{
|
||
$res = $this->authcode($token, 'DECODE', $this->TokenKey, 60 * 60 * 3);
|
||
if (empty($res)) {
|
||
return false;
|
||
} else {
|
||
$data = explode('_', $res);
|
||
if ($data[0] == $uid) {
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
public function getUserBaseInfo(Request $request)
|
||
{
|
||
$post = $request->param();
|
||
if (!isset($post['uid']) || empty($post['uid'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入登录人id";
|
||
return json_encode($result);
|
||
}
|
||
if (!isset($post['token']) || empty($post['token'])) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = "未传入token";
|
||
return json_encode($result);
|
||
}
|
||
if ($this->checkToken($post['token'], $post['uid']) === false) {
|
||
$result['erro'] = -1;
|
||
$result['msg'] = 'token异常';
|
||
return json_encode($result);
|
||
}
|
||
$result['infor'] = Db::name('accounthosting')
|
||
->field('name,headimg,balance')
|
||
->where('id', $post['uid'])
|
||
->find();
|
||
$result['erro'] = 0;
|
||
$result['msg'] = '获取数据成功';
|
||
return json_encode($result);
|
||
}
|
||
}
|