123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace app\inter\service;
- use think\Db;
- use think\Log;
- class WechatService {
- private const PUSH_TYPE = [
- 'REMOTE_DIAGNOSIS_PATIENT_PAY' => 1,
- 'REMOTE_DIAGNOSIS_FINISH' => 2,
- 'REMOTE_DIAGNOSIS_CANCEL' => 3,
- 'REMOTE_DIAGNOSIS_REFUND' => 4
- ];
- private const BASE_URL = "http://wechat.pacsonline.cn/wx_patient/api/";
- public function pushWechatOrder($order_id) {
- $order = $this->getPushOrder($order_id);
- $orderStr = $this->getPushContent($order);
- $openids = $this->getPushOpenId($order_id['exam_id']);
- if(!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['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_id['exam_id']);
- if(!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['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_id['exam_id']);
- if(!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['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_id['exam_id']);
- if(!$openids) {
- log::record('---------没有找到对应openid--------');
- return false;
- }
- foreach($openids as $v) {
- $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['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) {
- return self::BASE_URL."sendMsg?pushType=".$pushType."&openid=".$openid."&source=".source."&content=".$content;
- }
-
- private function getPushOrder($order_id) {
- $order = DB::table('remote_order')->where('id', $order_id)->field('application_id, order_money')->find();
- if($order) {
- $application = DB::table('remote_application')->where('id', $order['application_id'])->field('exam_id')->find();
- $exam = DB::table('exams')->where('id', $application['exam_id'])->field('patient_id')->find();
- $patient = DB::table('patient_infos')->where('id', $exam['patient_id'])->field('name')->find();
- if($application) {
- return [
- 'order_id' => $order_id,
- 'order_money' => $order['order_money'],
- 'exam_id' => $application['exam_id'],
- 'name' => $patient['name']
- ];
- }
- }
- }
- private function getPushOpenId($exam_id) {
- $exams = DB::table("exams")
- ->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')
- ->where('e.id', $exam_id)
- ->find();
- if(!$exams) {
- return false;
- }
- $options = [];
- $accession_num = $exams['accession_num'];
- $studyid = $exams['studyid'];
- $patient_id = $exams['patient_id'];
- $temp_patient_id = $exams['temp_patient_id'];
- $card_num = $exams['card_num'];
- $phone = $exams['phone'];
- $sql1 = "select user_id as uid from user_bind where patient_id = ?";
- $options[] = $patient_id;
- $sql2 = "select uid from wechat_bind where ";
- $sql2 .= "(patientCode = ?";
- $options[] = $temp_patient_id;
- if(!empty($accession_num)) {
- $sql2 .= " or patientCode = ?";
- $options[] = $accession_num;
- }
- if(!empty($studyid)) {
- $sql2 .= " or patientCode = ?";
- $options[] = $studyid;
- }
- $sql2 .=")";
- if($card_num) {
- $sql2 .= " or idcard = ?";
- $options[] = $card_num;
- }
- if($card_num) {
- $sql2 .= " or phone = ?";
- $options[] = $phone;
- }
- $sql = "select wx_openid as openid, source from user where id in (".$sql1." union ".$sql2.")";
- log::record('---------微信推送sql--------');
- log::record($sql);
- return Db::query($sql, $options);
- }
- 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;
- }
- }
|