Files
2024-09-27 01:32:49 +08:00

463 lines
17 KiB
PHP

<?php
namespace app\common;
use PhpParser\Node\NullableType;
use think\Exception;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Log;
class HyData
{
const EXTEND_SITE_URL='https://www.hyey.cn';
const LOGIN_URL='https://www.hyey.cn/ashx/Login.ashx';
const SEARCH_URL='https://www.hyey.cn/ashx/GetList2.ashx';
const DETAIL_URL='https://www.hyey.cn/YpXq.aspx';
const PRICE_DOM=['<input type="hidden" name="ctl00$ContentPlaceHolder1$hfdj" id="ContentPlaceHolder1_hfdj" value="','" />'];
const SHOW_PRICE_DOM=['<span id="ContentPlaceHolder1_spLSJ" class="price" style="font-weight: 900">','</span>'];
const NAME_DOM=['<span id="ContentPlaceHolder1_spSPMC">','</span>'];
const MANUFACTURER_NAME_DOM=['<span id="ContentPlaceHolder1_spCDMC">','</span>'];
const COUNTRY_CODE_DOM=['<span id="ContentPlaceHolder1_spPZWH">','</span>'];
const TERM_VALIDITY_DOM=['<span id="ContentPlaceHolder1_spYXQ">','</span>'];
const SPEC_DESC_DOM=['<span id="ContentPlaceHolder1_spGG">','</span>'];
const RETAIL_NUM_DOM=['<span id="ContentPlaceHolder1_spZBZ">','</span>'];
const ENTIRETY_NUM_DOM=['<span id="ContentPlaceHolder1_spBZ">','</span>'];
const RETAIL_FLAG_DOM=['<span id="ContentPlaceHolder1_spisRetail">','</span>'];
const STOCK_NUM_DOM=['<span id="ContentPlaceHolder1_spSL">','</span>'];
const DRUG_FORM_DOM=['<span id="ContentPlaceHolder1_spJX">','</span>'];
const BATCH_NUMBER_DOM=['<span id="ContentPlaceHolder1_spPH">','</span>'];
const PC_CONTENT_DOM=['<span id="ContentPlaceHolder1_spYPSM">','</span>
</div>'];
const IMG_DOM=["<img src='/uploadpic/50","'"];
public function makeLogin() {
if (!Cache::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_DATA']){
\app\facade\SystemDeal::initConstant();
}
$post_fields['UserName'] = Cache::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_DATA'][array_rand(Cache::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_DATA'],1)];
$post_fields['UserPwd'] = Cache::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_PASSWORD'];
if (!($post_fields['UserName'] && $post_fields['UserPwd'])){
//190819 : hy账号密码修改*/
$post_fields['UserName'] = Config::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_DATA'][array_rand(Config::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_DATA'],1)];
$post_fields['UserPwd'] = Config::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_PASSWORD'];
}
// $post_fields['UserName'] = 'hajhcj6';
// $post_fields['UserPwd'] = 'wasd123456';
$post_fields['Yzm'] = '';
$post_fields['origURL'] = self::EXTEND_SITE_URL;
$post_fields['domain'] = 'www.hyey.cn';
$response=curlCookie(self::LOGIN_URL,[],$post_fields,1);
//return $response;
$json_value=json_decode(getJsonValue($response),true);
if ($json_value['isOk']==1) {
$cookie=getCookieValue($response);
//存储到缓存
Cache::set('HY_LOGIN_COOKIE',$cookie);
return true;
}
return false;
}
public function search($data,$need_price=false) {
//return $this->makeLogin();
//return array_rand(Cache::get('SYSTEM_CONSTANT')['EXTEND_PLAT_USER_DATA']);
$send_param=[];
$send_param['pageIndex']=$data['now_page']??1;
$search_words=$data['search_words']??'';
$send_param['keys']=htmlspecialchars($search_words);
//exit($send_param['keys']);
$send_param['Lx']=3;
$send_param['jg']=$data['price_order']??'';
$send_param['isck']=$data['retail_flag']??'';
$send_param['xqid']=$data['term_validity_type']??0;
//20190926 新增参数
$send_param['FGSID']='';
list($usec, $sec) = explode(" ", microtime());
//$time = (int)(((float)$usec + (float)$sec) * 1000);
$time = (float)sprintf('%.0f',(floatval($usec)+floatval($sec))*1000);
$send_param['_'] = $time;
$search_url = self::SEARCH_URL.'?'.http_build_query($send_param);
//return $search_url;
//return $need_price;
//return curlCookie($search_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
//return Cache::get('HY_LOGIN_COOKIE');
if ($need_price) {
//登录状态设置cookie
$response=curlCookie($search_url,[],[],0,Cache::get('HY_LOGIN_COOKIE')??[],false);
} else {
$response=curlCookie($search_url,[],[],0,'',false);
}
//return $response;
$json_value=$this->dealData($response);
//return $json_value;
if ($need_price) {
if (isset($json_value['data'][0]) && isset($json_value['data'][0]['DJ']) && ($json_value['data'][0]['DJ'] == '会员可查')) {
//说明没有初始化登录,再做一次登录
if ($this->makeLogin()) {
$response=curlCookie($search_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
$json_value=$this->dealData($response);
if (isset($json_value['data'][0]) && isset($json_value['data'][0]['DJ']) && ($json_value['data'][0]['DJ'] == '会员可查')) {
//还是读不到价格的话,给空数据
$json_value=$this->emptyResult();
}
} else {
$json_value=$this->emptyResult();
}
}
}
return $json_value;
}
private function dealData($response) {
$json_value=json_decode($response,true);
if (!isset($json_value['total'])) {
$json_value['total']=[];
$json_value['total']['record']=0;
$json_value['total']['pcount']=0;
}
if (isset($json_value['data'])) {
$data_value=json_decode($json_value['data'],true);
} else {
$data_value=[];
}
if (isset($data_value['tables'])) {
//处理中包装数量可能存在字符串问题
foreach ($data_value['tables'] as $k=>$v) {
if (isset($v['ZBZ'])) {
$data_value['tables'][$k]['ZBZ'] =(int)$v['ZBZ'];
}
if (isset($v['BZ'])) {
$data_value['tables'][$k]['BZ'] =(int)$v['BZ'];
}
}
//--
$json_value['data']=$data_value['tables'];
} else {
$json_value['data']=[];
}
return $json_value;
}
private function emptyResult() {
$json_value['total']=[];
$json_value['total']['record']=0;
$json_value['total']['pcount']=0;
$json_value['data']=[];
return $json_value;
}
//单个药品详情
//$need_price是否需要读取价格
//$only_price是否只需要读取价格
public function detail($data,$need_price=true,$only_price=false) {
$send_param=[];
$extend_goods_id=$data['extend_goods_id']??'';
if ($extend_goods_id=='') {
return false;
}
$send_param['Drugid']=$extend_goods_id;
$detial_url = self::DETAIL_URL.'?'.http_build_query($send_param);
if ($need_price) {
$response=curlCookie($detial_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
} else {
$response=curlCookie($detial_url,[],[],0,'',false);
}
$price=getBetween($response,self::PRICE_DOM[0],self::PRICE_DOM[1]);
if ($price===false) {
return false;
}
$result['can_show'] = 0;
if ($need_price) {
$result['can_show'] = 1;
if ($price['value'] == '会员可查') {
//说明没有初始化登录,再做一次登录
if ($this->makeLogin()) {
$response=curlCookie($detial_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
$price=getBetween($response,self::PRICE_DOM[0],self::PRICE_DOM[1]);
if ($price===false) {
return false;
}
if ($price['value'] == '会员可查') {
return false;
}
} else {
return false;
}
}
}
$result['extend_goods_id']=$extend_goods_id;
//7.3号价格放大0.01 by tianyihao
//$result['price']=$price['value'];
//$result['price'] = @number_format(($price['value']+$price['value']*0.05),2,'.','');
$result['price'] = @number_format($price['value'],2,'.','');
//库存都要
$stock_num=getBetween($response,self::STOCK_NUM_DOM[0],self::STOCK_NUM_DOM[1]);
if ($stock_num['value']=='充足') {
$result['stock_num']='99999';
} else {
$result['stock_num']=$stock_num['value'];
}
//由于存在折扣价,价格处理部分放到前端处理
if (!$only_price) {
//根据具体需求,取其他的详细信息
//图片
$gallery=getMutiBetween($response,self::IMG_DOM[0],self::IMG_DOM[1]);
foreach ($gallery as $k=>$v) {
$gallery[$k]=self::EXTEND_SITE_URL.'/uploadpic/400'.$v;
}
$result['gallery']=$gallery;
//建议零售价和单位
$show_price=getBetween($response,self::SHOW_PRICE_DOM[0],self::SHOW_PRICE_DOM[1]);
$tmp_show_price=explode('元/',$show_price['value']);
$result['show_price']=number_format($tmp_show_price[0],2,'.','');
$result['unit']=$tmp_show_price[1];
if ($result['show_price']) {
$tmp_price=explode('.',$result['show_price']);
$result['show_price_int'] = $tmp_price[0];
$result['show_price_float'] = $tmp_price[1];
} else {
$result['show_price_int']='0';
$result['show_price_float']='00';
}
//商品名称
$name=getBetween($response,self::NAME_DOM[0],self::NAME_DOM[1]);
$result['name']=$name['value'];
//商品简述先用名称代替
$result['sketch']=$name['value'];
//生产厂家
$manufacturer_name=getBetween($response,self::MANUFACTURER_NAME_DOM[0],self::MANUFACTURER_NAME_DOM[1]);
$result['manufacturer_name']=$manufacturer_name['value'];
//国药准字
$country_code=getBetween($response,self::COUNTRY_CODE_DOM[0],self::COUNTRY_CODE_DOM[1]);
$result['country_code']=$country_code['value'];
//有效期
$term_validity=getBetween($response,self::TERM_VALIDITY_DOM[0],self::TERM_VALIDITY_DOM[1]);
$result['term_validity']=$term_validity['value'];
//规格
$spec_desc=getBetween($response,self::SPEC_DESC_DOM[0],self::SPEC_DESC_DOM[1]);
$result['spec_desc']=$spec_desc['value'];
//中包装
$retail_num=getBetween($response,self::RETAIL_NUM_DOM[0],self::RETAIL_NUM_DOM[1]);
$result['retail_num']=(int)$retail_num['value'];
//大包装
$entirety_num=getBetween($response,self::ENTIRETY_NUM_DOM[0],self::ENTIRETY_NUM_DOM[1]);
$result['entirety_num']=(int)$entirety_num['value'];
//整零
$retail_flag=getBetween($response,self::RETAIL_FLAG_DOM[0],self::RETAIL_FLAG_DOM[1]);
if ($retail_flag['value']=='零') {
$result['retail_flag']=0;
} else {
$result['retail_flag']=1;
}
//库存
/*$stock_num=getBetween($response,self::STOCK_NUM_DOM[0],self::STOCK_NUM_DOM[1]);
if ($stock_num['value']=='充足') {
$result['stock_num']='99999+';
} else {
$result['stock_num']=$stock_num['value'];
}*/
//剂型
$drug_form=getBetween($response,self::DRUG_FORM_DOM[0],self::DRUG_FORM_DOM[1]);
$result['drug_form']=$drug_form['value'];
//批号
$batch_number=getBetween($response,self::BATCH_NUMBER_DOM[0],self::BATCH_NUMBER_DOM[1]);
$result['batch_number']=$batch_number['value'];
//详情介绍
$pc_content=getBetween($response,self::PC_CONTENT_DOM[0],self::PC_CONTENT_DOM[1]);
$result['pc_content']=preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $pc_content['value']);
//--
}
return $result;
}
//api专用-仅更新价格
public function apiPriceDetail($data,$need_price=true) {
$send_param=[];
$extend_goods_id=$data['extend_goods_id']??'';
if ($extend_goods_id=='') {
return false;
}
$send_param['Drugid']=$extend_goods_id;
$detial_url = self::DETAIL_URL.'?'.http_build_query($send_param);
if ($need_price) {
$response=curlCookie($detial_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
} else {
$response=curlCookie($detial_url,[],[],0,'',false);
}
$price=getBetween($response,self::PRICE_DOM[0],self::PRICE_DOM[1]);
if ($price===false) {
return false;
}
$result['can_show'] = 0;
if ($need_price) {
$result['can_show'] = 1;
if ($price['value'] == '会员可查') {
//说明没有初始化登录,再做一次登录
if ($this->makeLogin()) {
$response=curlCookie($detial_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
$price=getBetween($response,self::PRICE_DOM[0],self::PRICE_DOM[1]);
if ($price===false) {
return false;
}
if ($price['value'] == '会员可查') {
return false;
}
} else {
return false;
}
}
}
$result['extend_goods_id']=$extend_goods_id;
$result['price'] = ceil($price['value']*100)/100;
return $result['price'];
}
/**
* hy商品下单时校验库存+价格;$extend_goods_id:hy商品id对应ydyd_goods_batch表supplier_sn字段
* @param string $extend_goods_id
* @param bool $check_more
* @return array|bool
* 默认校验价格+库存,支持校验更多字段
* by dingchao 20200514
*/
public function checkHyGoods($extend_goods_id='',$check_more = false){
$send_param=[];
if ($extend_goods_id=='') {
return false;
}
$send_param['Drugid']=$extend_goods_id;
$detial_url = self::DETAIL_URL.'?'.http_build_query($send_param);
$response=curlCookie($detial_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
$price=getBetween($response,self::PRICE_DOM[0],self::PRICE_DOM[1]);
if ($price===false) {
return false;
}
//注意同步hy官网显示文字变更;旧版:$price['value']== '会员可查'
if (!is_numeric($price['value'])) {
//说明没有初始化登录,再做一次登录
if ($this->makeLogin()) {
$response=curlCookie($detial_url,[],[],0,Cache::get('HY_LOGIN_COOKIE'),false);
$price=getBetween($response,self::PRICE_DOM[0],self::PRICE_DOM[1]);
if ($price===false) {
return false;
}
if (!is_numeric($price['value'])) {
return false;
}
} else {
return false;//登录失败
}
}
$result['extend_goods_id']=$extend_goods_id;
$result['price'] = ceil($price*100)/100;
//库存
$stock_num=getBetween($response,self::STOCK_NUM_DOM[0],self::STOCK_NUM_DOM[1]);
//old-version:$stock_num['value']=='充足'
if (!is_numeric($stock_num['value'])) {
$result['stock_num']='99999';
} else {
$result['stock_num']=$stock_num['value'];
}
//根据具体需求,取其他的详细信息
if ($check_more){
$more = $this->checkMoreInfo($response);
if ($result){
$result = array_merge($result,$more);
}
}
return $result;
}
public function checkMoreInfo($response=[]){
if ($response){
//图片
$gallery=getMutiBetween($response,self::IMG_DOM[0],self::IMG_DOM[1]);
foreach ($gallery as $k=>$v) {
$gallery[$k]=self::EXTEND_SITE_URL.'/uploadpic/400'.$v;
}
$result['gallery']=$gallery;
//建议零售价和单位
$show_price=getBetween($response,self::SHOW_PRICE_DOM[0],self::SHOW_PRICE_DOM[1]);
$tmp_show_price=explode('元/',$show_price['value']);
$result['show_price']=number_format($tmp_show_price[0],2,'.','');
$result['unit']=$tmp_show_price[1];
if ($result['show_price']) {
$tmp_price=explode('.',$result['show_price']);
$result['show_price_int'] = $tmp_price[0];
$result['show_price_float'] = $tmp_price[1];
} else {
$result['show_price_int']='0';
$result['show_price_float']='00';
}
//商品名称
$name=getBetween($response,self::NAME_DOM[0],self::NAME_DOM[1]);
$result['name']=$name['value'];
//商品简述先用名称代替
$result['sketch']=$name['value'];
//生产厂家
$manufacturer_name=getBetween($response,self::MANUFACTURER_NAME_DOM[0],self::MANUFACTURER_NAME_DOM[1]);
$result['manufacturer_name']=$manufacturer_name['value'];
//国药准字
$country_code=getBetween($response,self::COUNTRY_CODE_DOM[0],self::COUNTRY_CODE_DOM[1]);
$result['country_code']=$country_code['value'];
//有效期
$term_validity=getBetween($response,self::TERM_VALIDITY_DOM[0],self::TERM_VALIDITY_DOM[1]);
$result['term_validity']=$term_validity['value'];
//规格
$spec_desc=getBetween($response,self::SPEC_DESC_DOM[0],self::SPEC_DESC_DOM[1]);
$result['spec_desc']=$spec_desc['value'];
//中包装
$retail_num=getBetween($response,self::RETAIL_NUM_DOM[0],self::RETAIL_NUM_DOM[1]);
$result['retail_num']=(int)$retail_num['value'];
//大包装
$entirety_num=getBetween($response,self::ENTIRETY_NUM_DOM[0],self::ENTIRETY_NUM_DOM[1]);
$result['entirety_num']=(int)$entirety_num['value'];
//整零
$retail_flag=getBetween($response,self::RETAIL_FLAG_DOM[0],self::RETAIL_FLAG_DOM[1]);
if ($retail_flag['value']=='零') {
$result['retail_flag']=0;
} else {
$result['retail_flag']=1;
}
//剂型
$drug_form=getBetween($response,self::DRUG_FORM_DOM[0],self::DRUG_FORM_DOM[1]);
$result['drug_form']=$drug_form['value'];
//批号
$batch_number=getBetween($response,self::BATCH_NUMBER_DOM[0],self::BATCH_NUMBER_DOM[1]);
$result['batch_number']=$batch_number['value'];
//详情介绍
$pc_content=getBetween($response,self::PC_CONTENT_DOM[0],self::PC_CONTENT_DOM[1]);
$result['pc_content']=preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $pc_content['value']);
}else{
$result = false;
}
return $result;
}
}