first commit

This commit is contained in:
2026-01-28 10:16:06 +08:00
parent 9e0555b619
commit 7402558352
83 changed files with 8848 additions and 0 deletions

View File

@@ -0,0 +1,340 @@
<?php
namespace app\app\controller;
use think\Controller;
use think\Db;
/**
* 验证api是否正确
*/
class Base extends controller
{
function __initialize()
{
}
/******************************* 小程序公用 ******************************************/
/**
* 检验数据的真实性,并且获取解密后的明文.
* @param $encryptedData string 加密的用户数据
* @param $iv string 与用户数据一同返回的初始向量
* @param $data string 解密后的原文
* @return int 成功0失败返回对应的错误码
*/
public function decryptData($sessionKey,$appid,$encryptedData, $iv, &$data )
{
$OK = 0;
$IllegalAesKey = -41001;
$IllegalIv = -41002;
$IllegalBuffer = -41003;
$DecodeBase64Error = -41004;
if (strlen($sessionKey) != 24) {
return $IllegalAesKey;
}
$aesKey=base64_decode($sessionKey);
if (strlen($iv) != 24) {
return $IllegalIv;
}
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json_decode( $result );
if( $dataObj == NULL )
{
return $IllegalBuffer;
}
if( $dataObj->watermark->appid != $appid )
{
return $IllegalBuffer;
}
$data = $result;
return $OK;
}
/**
*GET方式请求
*/
function httpGet($url){
$ch = curl_init();
//设置选项包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//$result = json_decode($output);
//$result->subscribe
return $output;
}
/**
* post访问url
*/
function https_post($url,$data){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/*
*加解密
*函数authcode($string, $operation, $key, $expiry)中的$string字符串明文或密文$operationDECODE表示解密其它表示加密$key密匙$expiry密文有效期。
*/
public function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
// 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
$ckey_length = 4;
// 密匙
$key = md5($key ? $key : '' ); //md5($key ? $key : $GLOBALS['discuz_auth_key']);
// 密匙a会参与加解密
$keya = md5(substr($key, 0, 16));
// 密匙b会用来做数据完整性验证
$keyb = md5(substr($key, 16, 16));
// 密匙c用于变化生成的密文
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
// 参与运算的密匙
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
// 明文前10位用来保存时间戳解密时验证数据有效性10到26位用来保存$keyb(密匙b)
//解密时会通过这个密匙验证数据完整性
// 如果是解码的话,会从第$ckey_length位开始因为密文前$ckey_length位保存 动态密匙,以保证解密正确
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
// 产生密匙簿
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
// 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
// 核心加解密部分
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
// 从密匙簿得出密匙进行异或,再转成字符
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
// 验证数据有效性,请看未加密明文的格式
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
// 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
// 因为加密后的密文可能是一些特殊字符复制过程可能会丢失所以用base64编码
return $keyc.str_replace('=', '', base64_encode($result));
}
}
/**
*微信提现,企业付款到零用钱
*$openid 公众号下的用户openid
*$mch_billno 订单号
*$total_amount 提现的金额
*$mark 备注
*@return 错误 false ,成功
*/
function wxBusinessPay($openid,$mch_billno,$total_amount,$mark,$MCHID,$APPID,$Key){
$nonce_str=$this->createNoncestr( $length = 32 );
$sendarry=array();
// 随机字符串
$sendarry["nonce_str"]=$nonce_str;
// 商户订单号
$sendarry["partner_trade_no"]=$mch_billno;
// 商户号
$sendarry["mchid"]=$MCHID;
// 公众账号appid
$sendarry["mch_appid"]=$APPID;
// 用户openid
$sendarry["openid"]=$openid;
// 付款金额
$sendarry["amount"]=$total_amount*100;
// 红包祝福语
$sendarry["check_name"]='NO_CHECK';//不超过20字
// Ip地址
$sendarry["spbill_create_ip"]='62.234.183.107';
// 备注
$sendarry["desc"]=$mark;
// 签名
$sign=$this->getSign($sendarry,$Key);
$sendarry["sign"]=$sign;
//转换XML
$sendxml=$this->arrayToXml($sendarry);
// 调用的接口
$url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
// 使用证书以post方式提交xml到对应的接口url
$data=$this->postXmlSSLCurl($sendxml,$url,$seconed=30);
if($data == false){
return false;
}else{
$obj = $this->xmlToArray($data);
//发送成功
if($obj['return_code'] == 'SUCCESS' && $obj['result_code'] == 'SUCCESS'){
return true;
//金额不足
}else{
return false;
}
}
}
/**
* 作用产生随机字符串不长于32位
*/
public function createNoncestr( $length = 32 )
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
/**
* 作用:格式化参数,签名过程需要使用
*/
function formatBizQueryParaMap($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v)
{
if($urlencode)
{
$v = urlencode($v);
}
//$buff .= strtolower($k) . "=" . $v . "&";
$buff .= $k . "=" . $v . "&";
}
$reqPar;
if (strlen($buff) > 0)
{
$reqPar = substr($buff, 0, strlen($buff)-1);
}
return $reqPar;
}
/**
* 作用:生成签名
*/
public function getSign($Obj,$KEY)
{
foreach ($Obj as $k => $v)
{
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'</br>';
//签名步骤二在string后加入KEY
$String = $String."&key=".$KEY;
//echo "【string2】".$String."</br>";
//签名步骤三MD5加密
$String = md5($String);
//echo "【string3】 ".$String."</br>";
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
//echo "【result】 ".$result_."</br>";
return $result_;
}
/**
* 作用array转xml
*/
function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}
/**
* 作用将xml转为array
*/
public function xmlToArray($xml)
{
//将XML转为array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
/**
* 作用使用证书以post方式提交xml到对应的接口url
*/
function postXmlSSLCurl($xml,$url,$second=30)
{
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置header
curl_setopt($ch,CURLOPT_HEADER,FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
//设置证书
//使用证书cert 与 key 分别属于两个.pem文件
//默认格式为PEM可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, ROOT_PATH."/application/app/cacert/apiclient_cert.pem");
//默认格式为PEM可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, ROOT_PATH."/application/app/cacert/apiclient_key.pem");
//post提交方式
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
/* echo "curl出错错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";*/
curl_close($ch);
return false;
}
}
/******************************* 小程序公用 ******************************************/
}
?>

View File

@@ -0,0 +1,388 @@
<?php
namespace app\app\controller;
use think\Request;
use think\Db;
/**
*生长激素缺乏症小程序
*/
class Ghdwechat extends Base{
//小程序appid
private $Appid;
//小程序秘钥
private $Appsecret;
//商户号
//private $Mch_id;
//商户key
//private $Key;
//访问令牌加密秘钥
private $TokenKey;
public function __construct(){
$this->Appid='wxa75a76964ae7ce73';
$this->Appsecret='e14af51b729cabdbb232147ce9ad8b30';
// $this->Mch_id='1611903889';
// $this->Key='b046hjZnS6ioMacttGA66ffae887e55f';
$this->TokenKey='ghd340u20k4se309sa';
}
/********************************* 首页 *********************************/
/**
*查询资讯列表
*/
public function selectRealtimeInfoList(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'] : 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['total'] = $res->total();//数据总条数
$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['age']) || empty($post['age']) || !is_numeric($post['age'])){
$result['erro'] = -1;
$result['msg'] = "未传入年龄";
return json_encode($result);
}
if(!isset($post['height']) || empty($post['height']) || !is_numeric($post['height'])){
$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['father_height']) || empty($post['father_height']) || !is_numeric($post['father_height'])){
$result['erro'] = -1;
$result['msg'] = "未传入父亲身高";
return json_encode($result);
}
if(!isset($post['mother_height']) || empty($post['mother_height']) || !is_numeric($post['mother_height'])){
$result['erro'] = -1;
$result['msg'] = "未传入母亲身高";
return json_encode($result);
}
if(!isset($post['LH']) || empty($post['LH']) || !is_numeric($post['LH'])){
$result['erro'] = -1;
$result['msg'] = "未传入LH或数据格式不对";
return json_encode($result);
}
if(!isset($post['IGF']) || empty($post['IGF']) || !is_numeric($post['IGF'])){
$result['erro'] = -1;
$result['msg'] = "未传入IGF或数据格式不对";
return json_encode($result);
}
//子宫厚度
if(!isset($post['uterus_thickness']) || empty($post['uterus_thickness']) || !is_numeric($post['uterus_thickness'])){
$result['erro'] = -1;
$result['msg'] = "未传入uterus_thickness或数据格式不对";
return json_encode($result);
}
//计算
//计算公式 P= (exp(-5.908+0.008*IGF-1+2.146*LH+0.286*子宫(厚)))/(1 + exp(-5.908+0.008*IGF-1+2.146*LH+0.286*子宫(厚)))
$exp = exp(-5.908 + 0.008*$post['IGF'] + 2.146*$post['LH'] + 0.286*$post['uterus_thickness']);
$probability = $exp / (1 + $exp);
//保存记录数据
$data=[];
$data['id']=md5(time().rand(100000,999999));
$data['uid']=$post['uid'];
$data['name']=$post['name'];
$data['age']=$post['age'];
$data['height']=$post['height'];
$data['bone_age']=$post['bone_age'];
$data['father_height']=$post['father_height'];
$data['mother_height']=$post['mother_height'];
$data['IGF']=$post['IGF'];
$data['LH']=$post['LH'];
$data['uterus_thickness']=$post['uterus_thickness'];
$data['calculate_resutlt']=round($probability,3);
$data['create_time']=date('Y-m-d H:i:s');
$res=Db::name('wechat_calculate_record')->insert($data);
if($res===false){
$result['erro'] = -1;
$result['msg'] = "计算失败";
return json_encode($result);
}else{
$result['erro'] = 0;
$result['msg'] = "计算成功";
$result['calculate_resutlt'] = $data['calculate_resutlt'];
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('wechat_calculate_record')
->field('id,name,age,height,bone_age,father_height,mother_height,IGF,LH,uterus_thickness,calculate_resutlt,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['total'] = $res->total();//数据总条数
$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('wechat_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('wechat_calculate_record')->where('id',$id)->delete();
if($res > 0){
$result['erro'] = 0;
$result['msg'] = '删除成功';
}else{
$result['erro'] = -1;
$result['msg'] = '删除失败';
}
return json($result);
}
/********************************* 计算 *********************************/
/********************************* 登录 *********************************/
/**
*小程序授权登录,获取微信用户信息
* code: wx.login()获取到的code
* encryptedDatawx.getUserInfo获取到的encryptedData
* iv:wx.getUserInfo获取到的iv
*/
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);
// $apiData=['session_key'=>'dsQb6qITD2CZyBilb22dag==','openid'=>'oXcSb66vSO0_aGbL0NXJ_cxXnt5g'];
// var_dump($apiData);
if(!isset($apiData['errcode'])){
$sessionKey =$apiData['session_key'];//$post['sessionKey'];//
$errCode =$this->decryptData($sessionKey,$this->Appid,$encryptedData, $iv, $data );
// LogUtils::error('小程序解析:'.$data, __METHOD__);
if ($errCode == 0) {
//保存用户信息
$record=json_decode($data,true);
$res=$this->saveUseInfor($record);
return json_encode($res);
} else {
//LogUtils::error('解析数据错误:'.$errCode, __METHOD__);
$result['erro'] = -1;
$result['msg'] = "解析接口返回数据失败,请重试!";
return json_encode($result);
}
}else{
// LogUtils::error('错误码:'.$apiData['errcode'].',错误信息:'.$apiData['errmsg'], __METHOD__);
$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'];
//var_dump(strlen($data['headimg']));
//是否已经保存信息
$rec=Db::name('ghd_wechat_user')->field('uid')->where('openid',$record['openId'])->find();
if(empty($rec)){
//用户openid
$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['version_number']) || empty($post['version_number'])){
$result['erro'] = -1;
$result['msg'] = "未传入版本号";
return json_encode($result);
}
switch($post['version_number']){
case '1.0.0':
return $this->getUserBaseInfo_1_0_0($post);
break;
default:
return $this->getUserBaseInfo_1_0_0($post);
break;
}
}
private function getUserBaseInfo_1_0_0($post){
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);
}
}
?>

View File

@@ -0,0 +1,388 @@
<?php
namespace app\app\controller;
use think\Request;
use think\Db;
/**
*瑞莱医疗小程序
*/
class Ruilaiwechat extends Base{
//小程序appid
private $Appid;
//小程序秘钥
private $Appsecret;
//商户号
//private $Mch_id;
//商户key
//private $Key;
//访问令牌加密秘钥
private $TokenKey;
public function __construct(){
$this->Appid='wx0847525a15342a46';
$this->Appsecret='71cf8c524580ba6b262e6dc52d44da4c';
// $this->Mch_id='1611903889';
// $this->Key='b046hjZnS6ioMacttGA66ffae887e55f';
$this->TokenKey='ruilai340u20k4';
}
/********************************* 首页 *********************************/
/**
*查询资讯列表
*/
public function selectRealtimeInfoList(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'] : 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['total'] = $res->total();//数据总条数
$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['age']) || empty($post['age']) || !is_numeric($post['age'])){
$result['erro'] = -1;
$result['msg'] = "未传入年龄";
return json_encode($result);
}
if(!isset($post['height']) || empty($post['height']) || !is_numeric($post['height'])){
$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['father_height']) || empty($post['father_height']) || !is_numeric($post['father_height'])){
$result['erro'] = -1;
$result['msg'] = "未传入父亲身高";
return json_encode($result);
}
if(!isset($post['mother_height']) || empty($post['mother_height']) || !is_numeric($post['mother_height'])){
$result['erro'] = -1;
$result['msg'] = "未传入母亲身高";
return json_encode($result);
}
if(!isset($post['LH']) || empty($post['LH']) || !is_numeric($post['LH'])){
$result['erro'] = -1;
$result['msg'] = "未传入LH或数据格式不对";
return json_encode($result);
}
if(!isset($post['IGF']) || empty($post['IGF']) || !is_numeric($post['IGF'])){
$result['erro'] = -1;
$result['msg'] = "未传入IGF或数据格式不对";
return json_encode($result);
}
//子宫厚度
if(!isset($post['uterus_thickness']) || empty($post['uterus_thickness']) || !is_numeric($post['uterus_thickness'])){
$result['erro'] = -1;
$result['msg'] = "未传入uterus_thickness或数据格式不对";
return json_encode($result);
}
//计算
//计算公式 P= (exp(-5.908+0.008*IGF-1+2.146*LH+0.286*子宫(厚)))/(1 + exp(-5.908+0.008*IGF-1+2.146*LH+0.286*子宫(厚)))
$exp = exp(-5.908 + 0.008*$post['IGF'] + 2.146*$post['LH'] + 0.286*$post['uterus_thickness']);
$probability = $exp / (1 + $exp);
//保存记录数据
$data=[];
$data['id']=md5(time().rand(100000,999999));
$data['uid']=$post['uid'];
$data['name']=$post['name'];
$data['age']=$post['age'];
$data['height']=$post['height'];
$data['bone_age']=$post['bone_age'];
$data['father_height']=$post['father_height'];
$data['mother_height']=$post['mother_height'];
$data['IGF']=$post['IGF'];
$data['LH']=$post['LH'];
$data['uterus_thickness']=$post['uterus_thickness'];
$data['calculate_resutlt']=round($probability,3);
$data['create_time']=date('Y-m-d H:i:s');
$res=Db::name('wechat_calculate_record')->insert($data);
if($res===false){
$result['erro'] = -1;
$result['msg'] = "计算失败";
return json_encode($result);
}else{
$result['erro'] = 0;
$result['msg'] = "计算成功";
$result['calculate_resutlt'] = $data['calculate_resutlt'];
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('wechat_calculate_record')
->field('id,name,age,height,bone_age,father_height,mother_height,IGF,LH,uterus_thickness,calculate_resutlt,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['total'] = $res->total();//数据总条数
$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('wechat_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('wechat_calculate_record')->where('id',$id)->delete();
if($res > 0){
$result['erro'] = 0;
$result['msg'] = '删除成功';
}else{
$result['erro'] = -1;
$result['msg'] = '删除失败';
}
return json($result);
}
/********************************* 计算 *********************************/
/********************************* 登录 *********************************/
/**
*小程序授权登录,获取微信用户信息
* code: wx.login()获取到的code
* encryptedDatawx.getUserInfo获取到的encryptedData
* iv:wx.getUserInfo获取到的iv
*/
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);
// $apiData=['session_key'=>'dsQb6qITD2CZyBilb22dag==','openid'=>'oXcSb66vSO0_aGbL0NXJ_cxXnt5g'];
// var_dump($apiData);
if(!isset($apiData['errcode'])){
$sessionKey =$apiData['session_key'];//$post['sessionKey'];//
$errCode =$this->decryptData($sessionKey,$this->Appid,$encryptedData, $iv, $data );
// LogUtils::error('小程序解析:'.$data, __METHOD__);
if ($errCode == 0) {
//保存用户信息
$record=json_decode($data,true);
$res=$this->saveUseInfor($record);
return json_encode($res);
} else {
//LogUtils::error('解析数据错误:'.$errCode, __METHOD__);
$result['erro'] = -1;
$result['msg'] = "解析接口返回数据失败,请重试!";
return json_encode($result);
}
}else{
// LogUtils::error('错误码:'.$apiData['errcode'].',错误信息:'.$apiData['errmsg'], __METHOD__);
$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'];
//var_dump(strlen($data['headimg']));
//是否已经保存信息
$rec=Db::name('wechat_user')->field('uid')->where('openid',$record['openId'])->find();
if(empty($rec)){
//用户openid
$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['version_number']) || empty($post['version_number'])){
$result['erro'] = -1;
$result['msg'] = "未传入版本号";
return json_encode($result);
}
switch($post['version_number']){
case '1.0.0':
return $this->getUserBaseInfo_1_0_0($post);
break;
default:
return $this->getUserBaseInfo_1_0_0($post);
break;
}
}
private function getUserBaseInfo_1_0_0($post){
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);
}
}
?>