fuyu 5 lat temu
rodzic
commit
d044fcbf29

+ 19 - 12
application/inter/controller/Application.php

@@ -201,7 +201,7 @@ class Application extends Base
         }
     }
     /**取消订单 */
-    public function cancel(){
+    public function cancel(WechatService $wechatService){
         try{
             log::record($_REQUEST);
             $sessionid = $_REQUEST['sessionid'];
@@ -220,13 +220,16 @@ class Application extends Base
             if(!($remote_order['status'] === self::ORDER_STATUS['CREATE'] || $remote_order['status'] === self::ORDER_STATUS['PAYCOMPLTET'])) {
                 return json_encode(['status'=>'fail','code'=>'2102','msg'=> '订单状态不可取消']);
             }
-            $application = DB::table('remote_application')->where('id', $remote_order['application_id'])->field('report_status')->find();
+            $application = DB::table('remote_application')->where('id', $remote_order['application_id'])->field('report_status, exam_id')->find();
             if(!$application) {
                 return json_encode(['status'=>'fail','code'=>'2103','msg'=> '没有找到远程诊断申请单']);
             }
             if(!($application['report_status'] === '5' || $application['report_status'] === '12')) {
                 return json_encode(['status'=>'fail','code'=>'2103','msg'=> '进行中的远程诊断订单不可取消']);
             }
+            // 微信消息推送
+            // $wechatService->pushWechatCancel($application['exam_id']);
+            $wechatService->pushWechatCancel($order_id);
             if($remote_order['pay_type'] === self::PAY_TYPE['HOSPITAL']) {
                 return $this->cancelHospitalOrder($order_id, $remote_order['hospital_id'], $remote_order['order_money']);
             }
@@ -441,21 +444,23 @@ class Application extends Base
                         if(!$contact) {
                             return json_encode(['status'=>'ok','code'=>'2017','msg'=> '远程机构获取失败']);
                         }
+                        $res = false;
                         if($contact['pay_type'] === 0) { //医院挂账
                             // $hospital_id, $super_hospital_id, $doctor_id, $super_doctor_id, $exam_class, $application_id, $order_money
-                            $res = $this->createHospitalOrder($local_institution_id, $remote_institution_id, $req_doctor_id, $remote_doctor_id, $exam_info['exam_class'], $application['id'], $cost['money'], $is_urgent);
-                            if($res !== true) {
-                                return $res;
-                            }
+                            $res = $this->createHospitalOrder($local_institution_id, $remote_institution_id, $req_doctor_id, $remote_doctor_id, $exam_info['exam_class'], $application['id'], $cost['money'], $is_urgent, $wechatService);
                         }
                         if($contact['pay_type'] === 1) { // 患者支付
                             // var_dump(2);
                             // var_dump($wechatService);
                             // $push_res = WechatService::pushWechatOrder($param['id']);
-                            $wechatService->pushWechatOrder($param['id']);
-                            $res = $this->createPatientsOrder($local_institution_id, $remote_institution_id, $req_doctor_id, $remote_doctor_id, $exam_info['exam_class'], $application['id'], $cost['money'], $is_urgent);
+                            // $wechatService->pushWechatOrder($param['id']);
+                            $res = $this->createPatientsOrder($local_institution_id, $remote_institution_id, $req_doctor_id, $remote_doctor_id, $exam_info['exam_class'], $application['id'], $cost['money'], $is_urgent, $wechatService);
                             return json_encode(['status'=>'ok','code'=>'0000']);
                         }
+                        if($res === true) {
+                            return ['status'=>'ok','code'=>'0000'];
+                        }
+                        return $res;
                     }
                 } else {
                     DB::table('remote_order')->insert([
@@ -477,7 +482,7 @@ class Application extends Base
                 }
                 // 添加messages信息
 
-                $wechatService->pushWechatOrder($param['id']);
+                // $wechatService->pushWechatOrder($param['id']);
                 $t = '收到一条远程诊断申请';
                 $ins = DB::table('institution')->where('id',$doctor['institution_id'])->field('name')->find();
                 $c = $ins['name'].$doctor['realname'].'医师向您发起一条远程诊断申请';
@@ -504,7 +509,7 @@ class Application extends Base
         }
     }
 
-    private function createHospitalOrder($hospital_id, $super_hospital_id, $doctor_id, $super_doctor_id, $exam_class, $application_id, $order_money, $is_urgent = 0) {
+    private function createHospitalOrder($hospital_id, $super_hospital_id, $doctor_id, $super_doctor_id, $exam_class, $application_id, $order_money, $is_urgent = 0, $wechatService) {
         DB::startTrans();
         $pay_type = self::PAY_TYPE['HOSPITAL'];
         $pay_way = self::PAY_TYPE['HOSPITAL'];
@@ -565,6 +570,7 @@ class Application extends Base
             ]);
             // 提交事务
             DB::commit();
+            $wechatService->pushWechatOrder($order_id);
             return true;
         } catch (\Exception $e) {
             // 回滚事务
@@ -572,7 +578,7 @@ class Application extends Base
         }
         return json_encode(['status'=>'fail','code'=>'2018','远程诊断发起失败']);
     }
-    private function createPatientsOrder($hospital_id, $super_hospital_id, $doctor_id, $super_doctor_id, $exam_class, $application_id, $order_money, $is_urgent = 0) {
+    private function createPatientsOrder($hospital_id, $super_hospital_id, $doctor_id, $super_doctor_id, $exam_class, $application_id, $order_money, $is_urgent = 0, $wechatService) {
         $pay_type = self::PAY_TYPE['PATIENT'];
         $pay_way = self::PAY_WAY['WECHAT'];
         DB::startTrans();
@@ -607,7 +613,8 @@ class Application extends Base
                 'status' => self::ORDER_STATUS['CREATE']
             ]);
             DB::commit();
-            return json_encode(['status'=>'ok','code'=>'0000','oid'=>$order_id,'aid'=>$application_id]);
+            $wechatService->pushWechatOrder($order_id);
+            return true;
         } catch (\Exception $e) {
             var_dump($e);
             DB::rollback();

+ 7 - 4
application/inter/controller/Wechat.php

@@ -11,11 +11,11 @@ use app\common\library\Verify;
 use think\File;
 use app\common\library\send_message;
 use app\common\library\Message;
+use app\inter\service\WechatService;
+
 class Wechat extends Base {
-    public function _initialize() {
-    }
 
-    public function wechatRefund() {
+    public function wechatRefund(WechatService $wechatService) {
         $repsone = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
         $api_key = '9759a66938e8411ad5889e2b5b394d94';
         Log::record('-----微信退款----开始----');
@@ -36,9 +36,12 @@ class Wechat extends Base {
         DB::table('wechat_refund_log')->insert( array_merge($refund, $data));
 
         $order_id = $refund['out_trade_no'];
-        $order = DB::table('remote_order')->where('id', $order_id)->field('status, ')-find();
+        $order = DB::table('remote_order')->where('id', $order_id)->field('status, application_id')-find();
         if(!empty($order) && $order['status'] === Application::ORDER_STATUS['CANCEL']) {
             DB::table('remote_order')->where('id', $order_id)->update(['status' => Application::ORDER_STATUS['REDUCE']]);
+            $remote_application = DB::table('remote_application')->where('id', $order['application_id'])->field('exam_id')-find();
+            // $wechatService->pushWechatRefund($remote_application['exam_id']);
+            $wechatService->pushWechatRefund($order_id);
             // todo 微信消息推送
         }
         Log::record('-----微信结束----xml----');

+ 5 - 4
application/inter/controller/Writereport.php

@@ -11,6 +11,7 @@ use think\Cache;
 use think\Log;
 use app\common\library\Verify;
 use app\common\library\send_message;
+use app\inter\service\WechatService;
 
 class Writereport extends Base
 {
@@ -462,7 +463,7 @@ class Writereport extends Base
     }
      *")
      **/
-    public function confirm(){
+    public function confirm(WechatService $wechatService){
         try{
             Log::record('confirm');
             $is_remote = $_REQUEST['is_remote'];
@@ -510,8 +511,7 @@ class Writereport extends Base
                 Message::read($doctor['id'],$report_info['remote_application_id'],4);
                 //远程
                 
-                $this->confirmReport($report_info['remote_application_id']);
-               
+                $this->confirmReport($report_info['remote_application_id'],  $wechatService);
                 // DB::table('remote_application')->where('id',$report_info['remote_application_id'])->update(['report_status'=>'9']);
                 // 添加messages消息
                 $t = '您的报告已确认';
@@ -545,7 +545,7 @@ class Writereport extends Base
 
     }
 
-    private function confirmReport($application_id) {
+    private function confirmReport($application_id, $wechatService) {
         Db::startTrans();
         try{
             DB::table('remote_application')->where('id', $application_id)->update(['report_status'=>'9']);
@@ -592,6 +592,7 @@ class Writereport extends Base
             ]);
             // 提交事务
             Db::commit();
+            $wechatService->pushWechatComplete($order_id);
             // todo 消息推送
             return true;
         } catch (\Exception $e) {

+ 93 - 21
application/inter/service/WechatService.php

@@ -6,67 +6,139 @@ 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
+  ];
 
-  public function pushWechatOrder($exam_id) {
-    $openids = $this->getPushOpenId($exam_id);
+  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   = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
-      $full_url = $url . $v['openid'];
+      $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($full_url);
-      $res = $this->curl_request($full_url, 'GET');
+      log::record('---------发送微信通知-完成订单-------');
+      log::record($url);
+      $res = $this->curl_request($url, 'GET');
       log::record('---------微信通知结果--------');
       log::record($res);
     }
     return true;
   }
 
-  public function pushWechatCancel($exam_id) {
-    $openids = $this->getPushOpenId($exam_id);
+  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   = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
-      $full_url = $url . $v['openid'];
+      $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($full_url);
-      $res = $this->curl_request($full_url, 'GET');
+      log::record('---------发送微信通知-完成订单-------');
+      log::record($url);
+      $res = $this->curl_request($url, 'GET');
       log::record('---------微信通知结果--------');
       log::record($res);
     }
     return true;
   }
 
-  public function pushWechatComplete($exam_id) {
-    $openids = $this->getPushOpenId($exam_id);
+  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   = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
-      $full_url = $url . $v['openid'];
+      $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($full_url);
-      $res = $this->curl_request($full_url, 'GET');
+      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')
@@ -110,7 +182,7 @@ class WechatService {
       $options[] = $phone;
     }
 
-    $sql = "select wx_openid as openid from user where id in (".$sql1." union ".$sql2.")";
+    $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);