123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- <?php
- namespace app\api\servies\wechat;
- use app\api\model\application\ApplicationModel;
- use app\api\model\doctor\DoctorModel;
- use app\api\model\exam\ExamModel;
- use app\api\model\institution\InstitutionModel;
- use app\api\model\message\MessageModel;
- use app\api\model\moneywater\MoneywaterModel;
- use app\api\model\order\OrderModel;
- use app\api\model\patientsearch\PatientsearchModel;
- use app\api\model\paylog\PaylogModel;
- use app\api\model\payprogresslog\PayprogresslogModel;
- use app\api\model\refundlog\RefundlogModel;
- use app\api\model\remote\RemoteModel;
- use app\api\model\report\ReportModel;
- use app\api\model\user\UserModel;
- use app\api\model\userbind\UserbindModel;
- use app\api\model\wechatbind\WechatbindModel;
- use app\api\servies\ZskkDefaultService;
- use app\api\utils\MsgUtils;
- use app\api\utils\UUIDUtils;
- use app\common\library\send_message;
- use think\Db;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Log;
- class WechatService extends ZskkDefaultService
- {
- private const PUSH_TYPE = [
- 'REMOTE_DIAGNOSIS_PATIENT_PAY' => 1,
- 'REMOTE_DIAGNOSIS_FINISH' => 2,
- 'REMOTE_DIAGNOSIS_CANCEL' => 3,
- 'REMOTE_DIAGNOSIS_REFUND' => 4
- ];
- private const SUCCESS = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
- private const BASE_URL = "/wx_patient/api/";
- public function pushWechatOrder($order_id)
- {
- $order = $this->getPushOrder($order_id);
- $orderStr = $this->getPushContent($order);
- $openids = $this->getPushOpenId($order['exam_id']);
- if (!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach ($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_PATIENT_PAY'], $v['wx_openid'], $v['source'], $orderStr);
- // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
- // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
- // 请求
- log::record('---------发送微信通知-发起订单-------');
- log::record($url);
- $res = $this->curl_request($url, 'GET');
- log::record('---------微信通知结果--------');
- log::record($res);
- }
- return true;
- }
- public function pushWechatCancel($order_id)
- {
- $order = $this->getPushOrder($order_id);
- $orderStr = $this->getPushContent($order);
- $openids = $this->getPushOpenId($order['exam_id']);
- if (!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach ($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_CANCEL'], $v['wx_openid'], $v['source'], $orderStr);
- // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
- // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
- // 请求
- log::record('---------发送微信通知-取消订单-------');
- log::record($url);
- $res = $this->curl_request($url, 'GET');
- log::record('---------微信通知结果--------');
- log::record($res);
- }
- return true;
- }
- public function pushWechatComplete($order_id)
- {
- $order = $this->getPushOrder($order_id);
- $orderStr = $this->getPushContent($order);
- $openids = $this->getPushOpenId($order['exam_id']);
- if (!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach ($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_FINISH'], $v['wx_openid'], $v['source'], $orderStr);
- // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
- // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
- // 请求
- log::record('---------发送微信通知-完成订单-------');
- log::record($url);
- $res = $this->curl_request($url, 'GET');
- log::record('---------微信通知结果--------');
- log::record($res);
- }
- return true;
- }
- public function pushWechatRefund($order_id)
- {
- $order = $this->getPushOrder($order_id);
- $orderStr = $this->getPushContent($order);
- $openids = $this->getPushOpenId($order['exam_id']);
- if (!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach ($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['wx_openid'], $v['source'], $orderStr);
- // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
- // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
- // 请求
- log::record('---------发送微信通知-退款-------');
- log::record($url);
- $res = $this->curl_request($url, 'GET');
- log::record('---------微信通知结果--------');
- log::record($res);
- }
- return true;
- }
- private function getPushContent($order)
- {
- $content = [
- 'out_trade_no' => $order['order_id'],
- 'name' => urlencode($order['name']),
- 'total_fee' => $order['order_money']
- ];
- return json_encode($content);
- }
- // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
- // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
- private function getFullUrl($pushType, $openid, $source, $content)
- {
- $wechat = Config::get('wechat_url');
- return $wechat.self::BASE_URL . "sendMsg?pushType=" . $pushType . "&openid=" . $openid . "&source=" . urlencode($source) . "&content=" . urlencode($content);
- }
- private function getPushOrder($order_id)
- {
- $order = OrderModel::where('id', $order_id)->field('application_id, order_money')->find();
- if ($order) {
- $application = RemoteModel::where('id', $order['application_id'])->field('odd_number,exam_id,name')->find();
- if ($application) {
- return [
- 'order_id' => $application['odd_number'],
- 'order_money' => $order['order_money'],
- 'exam_id' => $application['exam_id'],
- 'name' => $application['name']
- ];
- }
- }
- }
- private function getPushOpenId($exam_id)
- {
- $exam = ExamModel::alias('e')
- ->join('studies s', 'e.study_id = s.id')
- ->join('patient_infos pi', 'e.patient_id = pi.id')
- ->field('e.patient_id, s.accession_num, s.studyid, pi.temp_patient_id, pi.card_num, pi.phone, e.institution_id')
- ->where('e.id', $exam_id)
- ->find();
- if (!$exam) {
- log::record('---------没有对应检查--------');
- return false;
- }
- $institution = InstitutionModel::where('id', $exam['institution_id'])->find();
- if (!$institution) {
- log::record('---------没有对应医院--------');
- return false;
- }
- $wx_source = $institution['wx_source'];
- $options = [];
- $accession_num = $exam['accession_num'];
- $studyid = $exam['studyid'];
- $patient_id = $exam['patient_id'];
- $temp_patient_id = $exam['temp_patient_id'];
- $card_num = $exam['card_num'];
- $phone = $exam['phone'];
- $id1 = UserbindModel::where('patient_id', $patient_id)->field('user_id as uid')->select();
- $where1 = "search_criteria ='$temp_patient_id' ";
- if (!empty($accession_num)) {
- $where1 .= " or search_criteria = '$accession_num' ";
- }
- // if (!empty($studyid)) {
- // $where1 .= " or patientCode = '$studyid'";
- // }
- // $where2 = [];
- if ($card_num) {
- $where1 .= " or search_criteria = '$card_num' ";
- }
- $where3 = [];
- if ($phone) {
- $where1 .= " or search_criteria = '$phone' ";
- $where3['phone'] = $phone;
- }
- // $id2 = WechatbindModel::where($where1)
- // ->whereor($where2)
- // ->whereor($where3)
- // ->select();
- $id2 = PatientsearchModel::where($where1)
- ->whereOr($where3)
- ->where('institution_id',$exam['institution_id'])
- ->select();
- $ids = [];
- foreach ($id1 as $k => $v) {
- $ids[] = $v['uid'];
- }
- foreach ($id2 as $k => $v) {
- $ids[] = $v['patient_uid'];
- }
- $wherein[] = ['id', 'in', $ids];
- $data = UserModel::where($wherein)
- ->where('source', $wx_source)
- ->select();
- // log::record('---------微信推送sql--------');
- // log::record(UserModel::where($wherein)->where('source',$wx_source)->buildSql());
- // log::record($options);
- return $data;
- }
- private function curl_request($url, $method = 'get', $data = null, $https = true)
- {
- //1.初识化curl
- $ch = curl_init($url);
- //2.根据实际请求需求进行参数封装
- //返回数据不直接输出
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //如果是https请求
- if ($https === true) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- //如果是post请求
- if ($method === 'post') {
- //开启发送post请求选项
- curl_setopt($ch, CURLOPT_POST, true);
- //发送post的数据
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- //3.发送请求
- $result = curl_exec($ch);
- //4.返回返回值,关闭连接
- curl_close($ch);
- return $result;
- }
- public function wechatRefund($xml)
- {
- $api_key = '9759a66938e8411ad5889e2b5b394d94';
- Log::record('-----微信退款----开始----');
- Log::record('-----微信退款----xml----');
- Log::record($xml);
- Log::record('-----xml转数组--------');
- $data = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
- Log::record($data);
- $req_info = $data['req_info'];
- // $req_info = 'Jsb+/EIaoJhD0BVWMEiFWeRuivKbS7/bs/wpgwHowHyvVNC+Kj3aiCsLHzQobul1mKwuKT/slYfoVgUQibGH+mEFcA2N10r6Qv5v+rwd7IXvMOM7iS7p9ciDD3fn+jZukjJdGME6KajDtD2rFs0MyZTqsKUTGQN1I8Q1w5aK7OISs437bXp5s4VgrxQWKNe5s18+ts2jrMoA91VXmHy024p010BiB1TJHr46cXC4zpZkeLtECkABhVlBGmDQPfw0JknMqaTriXRZPQcz2Ff/WuRPKPImiL9i0nVpcPuiFjIP863lWLfr2Aif7plPA/mJrAZ6K95hGRvAiiHqxEet7OjYMHu50OKnY/8j2pZjtBUX8Yr1fRnY+v8INLL8+CKS1tZRY0dAkilymPVOSN/naNkVILgwDrlYq5XisDMmXz2HzF1UBX0ZWNs5ltarJC8fyhLpZm1cd/prxcTB6KUm1mTnEYhToZLUlB0N2a0dCT9pm58oj8myMC6mJutCW79QCaHCtV6JIxjueErZkbWYEVSZWG+m7hcKcmcgGhqTcnXPLte5vE0imYoABZnw4uMnv9sMSVP0PhMJ8/JeeP/He/6oAHenvhPrh/J4Xpv/vCbDjVJo8Qo5xWFFG9pk/Aj1rlZiCqmJffy+TXkXceDp+wg4lEOIbZfENaqbj7gtN9RWFTjrtPapPBZzxGJQvEQKzF2TNoP7owsTUup3grDMuw/zreVYt+vYCJ/umU8D+YnTBdx9Q0Bog3qHSlEMnQten+v2X7cSwDcIzvf3UJKVD1041tiBKfJTJFvfbiT+q4vHLKcRn5q7QyWowKrGO/ZrWTIotM0m+1IRqVDHeCTGYDUMGDiQ/+1BupV135PlUg2tKHr0wIUS0wThD3z2ZZxcJ3VioCDcFmjPu128k1xqsjfz+1Np4DxiTZFR1W44wpF/E6Hwi11A440fABYGX4AF7UFBSNfStMCTjVAhcHaYY9P5AbNiUcENjwZEBWE5bcCKm2adIsdqT5isumPoFi6M5H7hL5slQoKqLadEPfazBMHE8r+m+UYLTHlCk9L/DLPmrkVk2HJzlATcg9VYTX+IPYqqv2kY/9BYW+q+O+a8gA==';
- // 对加密信息进行解密,需要用到商户秘钥
- $req_info_xml = openssl_decrypt(base64_decode($req_info), 'aes-256-ecb', md5($api_key), OPENSSL_RAW_DATA);
- Log::record($req_info_xml);
- $refund = (array)simplexml_load_string($req_info_xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- $refund['timestamp'] = time();
- Log::record($refund);
- unset($data['return_code']);
- $info = array_merge($refund, $data);
- RefundlogModel::insert($info);
- $order_id = $refund['out_trade_no'];
- $order = OrderModel::where('id', $order_id)->field('status,hospital_id,application_id')->find();
- if (!empty($order) && $order['status'] === Config::get('ORDER_STATUS')['CANCEL']) {
- OrderModel::where('id', $order_id)->update(['status' => Config::get('ORDER_STATUS')['REDUCE']]);
- $this->pushWechatRefund($order_id);
- // todo 微信消息推送
- }
- if(!empty($order['hospital_id'])){
- //存储支出流水
- $exam_id = ApplicationModel::where('id', $order['application_id'])->value('exam_id');
- $money_water = [
- 'money'=>$info['refund_fee'],
- 'order_type'=>Config::get('order_type')['patientPay'],
- 'type'=>'1', //远程诊断
- 'exam_id'=>$exam_id,
- 'institution_id'=>$order['hospital_id'],
- 'pay_way'=>'1', //患者支付
- 'status'=>'2',
- 'pay_type'=>'2' //退款
- ];
- MoneywaterModel::insert($money_water);
- $pay_pregress_log = [
- 'id'=>UUIDUtils::uuid(),
- 'operator_id'=>$info['appid'],
- 'operator_name'=>UserModel::where('wx_openid',$info['appid'])->value('wx_nickname'),
- 'current_step'=>2,
- 'application_id'=>$order['application_id'],
- 'remark'=>'患者支付'
- ];
- PayprogresslogModel::insert($pay_pregress_log);
- }
- $this->pushWechatCancel($order_id);
- Log::record('-----微信结束----xml----');
- return self::SUCCESS;
- }
- public function wechatCallback($xml)
- {
- try {
- Log::record('-----微信支付回调----开始----');
- Log::record('-----微信支付返回----xml----');
- Log::record($xml);
- Log::record('-----xml转数组--------');
- $data = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
- Log::record($data);
- $out_trade_no = $data['out_trade_no'];
- $data['timestamp'] = time();
- Log::record('-----添加流水记录----');
- Log::record($data);
- PaylogModel::insert($data);
- Log::record('-----获取订单信息-----');
- $order = OrderModel::where("id", $out_trade_no)->field("id, status, super_doctor_id, application_id, hospital_id, doctor_id, exam_class, is_urgent,super_hospital_id")->find();
- $aid = $order['application_id'];
- $application = ApplicationModel::where('id', $aid)->field('exam_id,remote_doctor_id,remote_institution_id,remote_doctor_name')->find();
- $exam_id = $application['exam_id'];
- $money_water = [
- 'money'=>$data['cash_fee'],
- 'order_type'=>Config::get('order_type')['patientPay'],
- 'type'=>'1', //远程诊断
- 'exam_id'=>$exam_id,
- 'institution_id'=>$order['hospital_id'],
- 'pay_way'=>'1', //患者支付
- 'status'=>'2',
- 'pay_type'=>'1' //支入
- ];
- MoneywaterModel::insert($money_water);
- Log::record($order);
- if ($order && $order['status'] === Config::get('ORDER_STATUS')['CREATE']) {
- // $order['status'] = Config::get('ORDER_STATUS')['PAYCOMPLTET'];
- $d = $order['super_doctor_id'];
- // todo 消息通知
- Log::record('-----更新订单状态-----');
- $update = ['status'=>Config::get('ORDER_STATUS')['PAYCOMPLTET']];
- OrderModel::where("id", $out_trade_no)->update($update);
- $r_info = ReportModel::where('exam_id', $exam_id)->where('remote_application_id', $aid)->field('id')->find();
- ApplicationModel::where('id', $aid)->update(['report_status'=>'4']);
- $ins = InstitutionModel::where('id', $order['hospital_id'])->field('name,handling_time')->find();
- if($ins['handling_time'] == '0')
- {
- $ins['handling_time'] = 7200;
- }
- $date = date('Y-m-d H:i:s',(time()+$ins['handling_time']));
- $dinfo = DoctorModel::where('id', $d)->find();
- $d_name = $dinfo['realname'];
- $d_phone = $dinfo['phone'];
- $pay_pregress_log = [
- 'id'=>UUIDUtils::uuid(),
- 'operator_id'=>$data['appid'],
- 'operator_name'=>UserModel::where('wx_openid',$data['appid'])->value('wx_nickname'),
- 'current_step'=>2,
- 'application_id'=>$aid,
- 'remark'=>'患者支付'
- ];
- PayprogresslogModel::insert($pay_pregress_log);
- $t = '收到一条远程诊断申请';
- $doctor = DoctorModel::where('id', $order['doctor_id'])->field('realname')->find();
- $c = $ins['name'] . $doctor['realname'] . '医师向您发起一条远程诊断申请';
- $type = '1';
- $url = $this->url(null, $exam_id, 1, $order['exam_class'], $r_info['id'], $aid);
- Log::record('-----系统内消息通知-----');
- $this->MessageInsert($t, $c, $d, $type, $url, $exam_id,1, $aid);
- //保存 申请
- if (empty($d)) {
- Log::record('---微信支付回调---结束---没有远程医生');
- return self::SUCCESS;
- }
- if ($order['is_urgent'] === 1) {
- $d_time = date('H:i', strtotime('+30 min'));
- } else {
- $d_time = date('H:i', strtotime('+1 hour'));
- }
- if ($dinfo['institution_id'] == '22100003' || $dinfo['send_sms'] == 1) { //魏庙
- Log::record('-----短信通知-----');
- send_message::sendSms2Apply($d_phone, $d_time, $d_name);
- }
- }
- Log::record('---微信支付回调---结束---');
- return self::SUCCESS;
- } catch (\Throwable $th) {
- Log::record('支付回调异常');
- Log::record($th->getMessage());
- //throw $th;
- }
- }
- public function url($sessionid,$id,$is_remote,$exam_class,$report_id='',$rid=''){
- $url = '/remotereport/edit/6?id='.$id.'&is_remote='.$is_remote.'&exam_class='.$exam_class.'&report_id='.$report_id.'&rid='.$rid;
- return $url;
- }
- public function MessageInsert($title,$content,$did,$type,$url,$exam_id,$count =1,$aid = ''){
- Cache::rm('messages_'.$did);
- try{
- $info['id'] = UUIDUtils::uuid();
- $info['title'] = $title;
- $info['content'] = $content;
- $info['doctor_id'] = $did;
- $info['type'] = $type;
- $info['ctime'] = date('Y-m-d H:i:s',time());
- $info['is_read'] = 0;
- $info['status'] = 0;
- $info['url'] = $url;
- $info['count'] = $count;
- $info['application_id'] = $aid;
- $info['exam_id'] = $exam_id;
- MessageModel::insert($info);
- }catch(\Exception $e){
- Log::record($e->getMessage());
- }
- }
- public function wechatStudyPayCallback($xml)
- {
- try {
- // $param = $_REQUEST['param'];
- Log::record('---电子胶片--微信支付回调----开始----');
- Log::record('---电子胶片--微信支付返回----xml----');
- Log::record($xml);
- Log::record('-----xml转数组--------');
- $data = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
- Log::record($data);
- $out_trade_no = $data['out_trade_no'];
- $data['timestamp'] = time();
- Log::record('---电子胶片--添加流水记录----');
- Log::record($data);
- PaylogModel::insert($data);
- Log::record('-----获取订单信息-----');
- $order = ExamModel::where("id", $out_trade_no)->field("id,phone,exam_class,exam_datetime,study_id, name,pay_status,institution_id")->find();
- Log::record($order);
- if ($order && $order['pay_status'] === '0') {
- $order = json_decode(json_encode($order,true),true);
- $order['pay_status'] = '1';
- // todo 消息通知
- Log::record('-----更新检查状态-----');
- ExamModel::where("id", $out_trade_no)->update($order);
- }
- $money_water = [
- 'money'=>$data['cash_fee'],
- 'order_type'=>Config::get('order_type')['patientPay'],
- 'type'=>'2', //电子胶片
- 'exam_id'=>$out_trade_no,
- 'institution_id'=>$order['institution_id'],
- 'pay_way'=>'1', //患者支付
- 'status'=>'2',
- 'pay_type'=>'1' //支入
- ];
- $wa = MoneywaterModel::insert($money_water);
- Log::record('--支付流水存储---'.$wa.'----');
- ExamModel::where("id", $out_trade_no)->update(['pay_status'=>1]);
- if(!empty($order['id'])){
- $ids = PatientsearchModel::where('exam_id',$order['id'])->distinct(true)->column('patient_uid');
- $pushType = 5;
- $cont = [
- 'name'=>$order['name'],
- 'modality'=>$order['exam_class'],
- 'examDate'=>$order['exam_datetime'],
- 'study_id'=>$order['study_id']
- ];
- $content = json_encode($cont,true);
- $openids = UserModel::where('id','in',$ids)->column('wx_openid');;
- foreach ($openids as $k=>$v){
- $this->new_wechat_push($v,$pushType,$content);
- }
- }
- Log::record('--电子胶片---微信支付回调---结束---');
- return self::SUCCESS;
- } catch (\Throwable $th) {
- Log::record('支付回调异常');
- Log::record($th->getMessage());
- //throw $th;
- }
- }
- }
|