WechatService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\inter\service;
  3. use think\Db;
  4. use think\Log;
  5. class WechatService {
  6. public function pushWechatOrder($exam_id) {
  7. $openids = $this->getPushOpenId($exam_id);
  8. if(!$openids) {
  9. log::record('---------没有找到对应openid--------');
  10. return false;
  11. }
  12. foreach($openids as $v) {
  13. $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
  14. $full_url = $url . $v['openid'];
  15. // 请求
  16. log::record('---------发送微信通知-发起订单-------');
  17. log::record($full_url);
  18. $res = $this->curl_request($full_url, 'GET');
  19. log::record('---------微信通知结果--------');
  20. log::record($res);
  21. }
  22. return true;
  23. }
  24. public function pushWechatCancel($exam_id) {
  25. $openids = $this->getPushOpenId($exam_id);
  26. if(!$openids) {
  27. log::record('---------没有找到对应openid--------');
  28. return false;
  29. }
  30. foreach($openids as $v) {
  31. $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
  32. $full_url = $url . $v['openid'];
  33. // 请求
  34. log::record('---------发送微信通知-取消订单-------');
  35. log::record($full_url);
  36. $res = $this->curl_request($full_url, 'GET');
  37. log::record('---------微信通知结果--------');
  38. log::record($res);
  39. }
  40. return true;
  41. }
  42. public function pushWechatComplete($exam_id) {
  43. $openids = $this->getPushOpenId($exam_id);
  44. if(!$openids) {
  45. log::record('---------没有找到对应openid--------');
  46. return false;
  47. }
  48. foreach($openids as $v) {
  49. $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
  50. $full_url = $url . $v['openid'];
  51. // 请求
  52. log::record('---------发送微信通知-完成订单-------');
  53. log::record($full_url);
  54. $res = $this->curl_request($full_url, 'GET');
  55. log::record('---------微信通知结果--------');
  56. log::record($res);
  57. }
  58. return true;
  59. }
  60. private function getPushOpenId($exam_id) {
  61. $exams = DB::table("exams")
  62. ->alias('e')
  63. ->join('studies s', 'e.study_id = s.id')
  64. ->join('patient_infos pi', 'e.patient_id = pi.id')
  65. ->field('e.patient_id, s.accession_num, s.studyid, pi.temp_patient_id, pi.card_num, pi.phone')
  66. ->where('e.id', $exam_id)
  67. ->find();
  68. if(!$exams) {
  69. return false;
  70. }
  71. $options = [];
  72. $accession_num = $exams['accession_num'];
  73. $studyid = $exams['studyid'];
  74. $patient_id = $exams['patient_id'];
  75. $temp_patient_id = $exams['temp_patient_id'];
  76. $card_num = $exams['card_num'];
  77. $phone = $exams['phone'];
  78. $sql1 = "select user_id as uid from user_bind where patient_id = ?";
  79. $options[] = $patient_id;
  80. $sql2 = "select uid from wechat_bind where ";
  81. $sql2 .= "(patientCode = ?";
  82. $options[] = $temp_patient_id;
  83. if(!empty($accession_num)) {
  84. $sql2 .= " or patientCode = ?";
  85. $options[] = $accession_num;
  86. }
  87. if(!empty($studyid)) {
  88. $sql2 .= " or patientCode = ?";
  89. $options[] = $studyid;
  90. }
  91. $sql2 .=")";
  92. if($card_num) {
  93. $sql2 .= " or idcard = ?";
  94. $options[] = $card_num;
  95. }
  96. if($card_num) {
  97. $sql2 .= " or phone = ?";
  98. $options[] = $phone;
  99. }
  100. $sql = "select wx_openid as openid from user where id in (".$sql1." union ".$sql2.")";
  101. log::record('---------微信推送sql--------');
  102. log::record($sql);
  103. return Db::query($sql, $options);
  104. }
  105. private function curl_request($url,$method='get',$data=null,$https=true){
  106. //1.初识化curl
  107. $ch = curl_init($url);
  108. //2.根据实际请求需求进行参数封装
  109. //返回数据不直接输出
  110. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  111. //如果是https请求
  112. if($https === true){
  113. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  114. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  115. }
  116. //如果是post请求
  117. if($method === 'post'){
  118. //开启发送post请求选项
  119. curl_setopt($ch,CURLOPT_POST,true);
  120. //发送post的数据
  121. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  122. }
  123. //3.发送请求
  124. $result = curl_exec($ch);
  125. //4.返回返回值,关闭连接
  126. curl_close($ch);
  127. return $result;
  128. }
  129. }