WechatService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\inter\service;
  3. use think\Db;
  4. use think\Log;
  5. class WechatService {
  6. private const PUSH_TYPE = [
  7. 'REMOTE_DIAGNOSIS_PATIENT_PAY' => 1,
  8. 'REMOTE_DIAGNOSIS_FINISH' => 2,
  9. 'REMOTE_DIAGNOSIS_CANCEL' => 3,
  10. 'REMOTE_DIAGNOSIS_REFUND' => 4
  11. ];
  12. private const BASE_URL = "http://wechat.pacsonline.cn/wx_patient/api/";
  13. public function pushWechatOrder($order_id) {
  14. $order = $this->getPushOrder($order_id);
  15. $orderStr = $this->getPushContent($order);
  16. $openids = $this->getPushOpenId($order['exam_id']);
  17. if(!$openids) {
  18. log::record('---------没有找到对应openid--------');
  19. return false;
  20. }
  21. foreach($openids as $v) {
  22. $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['openid'], $v['source'], $orderStr);
  23. // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
  24. // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
  25. // 请求
  26. log::record('---------发送微信通知-完成订单-------');
  27. log::record($url);
  28. $res = $this->curl_request($url, 'GET');
  29. log::record('---------微信通知结果--------');
  30. log::record($res);
  31. }
  32. return true;
  33. }
  34. public function pushWechatCancel($order_id) {
  35. $order = $this->getPushOrder($order_id);
  36. $orderStr = $this->getPushContent($order);
  37. $openids = $this->getPushOpenId($order['exam_id']);
  38. if(!$openids) {
  39. log::record('---------没有找到对应openid--------');
  40. return false;
  41. }
  42. foreach($openids as $v) {
  43. $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['openid'], $v['source'], $orderStr);
  44. // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
  45. // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
  46. // 请求
  47. log::record('---------发送微信通知-取消订单-------');
  48. log::record($url);
  49. $res = $this->curl_request($url, 'GET');
  50. log::record('---------微信通知结果--------');
  51. log::record($res);
  52. }
  53. return true;
  54. }
  55. public function pushWechatComplete($order_id) {
  56. $order = $this->getPushOrder($order_id);
  57. $orderStr = $this->getPushContent($order);
  58. $openids = $this->getPushOpenId($order['exam_id']);
  59. if(!$openids) {
  60. log::record('---------没有找到对应openid--------');
  61. return false;
  62. }
  63. foreach($openids as $v) {
  64. $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['openid'], $v['source'], $orderStr);
  65. // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
  66. // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
  67. // 请求
  68. log::record('---------发送微信通知-完成订单-------');
  69. log::record($url);
  70. $res = $this->curl_request($url, 'GET');
  71. log::record('---------微信通知结果--------');
  72. log::record($res);
  73. }
  74. return true;
  75. }
  76. public function pushWechatRefund($order_id) {
  77. $order = $this->getPushOrder($order_id);
  78. $orderStr = $this->getPushContent($order);
  79. $openids = $this->getPushOpenId($order['exam_id']);
  80. if(!$openids) {
  81. log::record('---------没有找到对应openid--------');
  82. return false;
  83. }
  84. foreach($openids as $v) {
  85. $url = $this->getFullUrl(self::PUSH_TYPE['REMOTE_DIAGNOSIS_REFUND'], $v['openid'], $v['source'], $orderStr);
  86. // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
  87. // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
  88. // 请求
  89. log::record('---------发送微信通知-完成订单-------');
  90. log::record($url);
  91. $res = $this->curl_request($url, 'GET');
  92. log::record('---------微信通知结果--------');
  93. log::record($res);
  94. }
  95. return true;
  96. }
  97. private function getPushContent($order) {
  98. $content = [
  99. 'out_trade_no' => $order['order_id'],
  100. 'name' => urlencode($order['name']),
  101. 'total_fee' => $order['order_money']
  102. ];
  103. return json_encode($content);
  104. }
  105. // $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=".."&openid=";
  106. // $full_url = $url . $v['openid']."&source=".$v['source']."&content=".$orderStr;
  107. private function getFullUrl($pushType, $openid, $source, $content) {
  108. return self::BASE_URL."sendMsg?pushType=".$pushType."&openid=".$openid."&source=".urlencode($source)."&content=".$content;
  109. }
  110. private function getPushOrder($order_id) {
  111. $order = DB::table('remote_order')->where('id', $order_id)->field('application_id, order_money')->find();
  112. if($order) {
  113. $application = DB::table('remote_application')->where('id', $order['application_id'])->field('exam_id')->find();
  114. $exam = DB::table('exams')->where('id', $application['exam_id'])->field('patient_id')->find();
  115. $patient = DB::table('patient_infos')->where('id', $exam['patient_id'])->field('name')->find();
  116. if($application) {
  117. return [
  118. 'order_id' => $order_id,
  119. 'order_money' => $order['order_money'],
  120. 'exam_id' => $application['exam_id'],
  121. 'name' => $patient['name']
  122. ];
  123. }
  124. }
  125. }
  126. private function getPushOpenId($exam_id) {
  127. $exams = DB::table("exams")
  128. ->alias('e')
  129. ->join('studies s', 'e.study_id = s.id')
  130. ->join('patient_infos pi', 'e.patient_id = pi.id')
  131. ->field('e.patient_id, s.accession_num, s.studyid, pi.temp_patient_id, pi.card_num, pi.phone')
  132. ->where('e.id', $exam_id)
  133. ->find();
  134. if(!$exams) {
  135. return false;
  136. }
  137. $options = [];
  138. $accession_num = $exams['accession_num'];
  139. $studyid = $exams['studyid'];
  140. $patient_id = $exams['patient_id'];
  141. $temp_patient_id = $exams['temp_patient_id'];
  142. $card_num = $exams['card_num'];
  143. $phone = $exams['phone'];
  144. $sql1 = "select user_id as uid from user_bind where patient_id = ?";
  145. $options[] = $patient_id;
  146. $sql2 = "select uid from wechat_bind where ";
  147. $sql2 .= "(patientCode = ?";
  148. $options[] = $temp_patient_id;
  149. if(!empty($accession_num)) {
  150. $sql2 .= " or patientCode = ?";
  151. $options[] = $accession_num;
  152. }
  153. if(!empty($studyid)) {
  154. $sql2 .= " or patientCode = ?";
  155. $options[] = $studyid;
  156. }
  157. $sql2 .=")";
  158. if($card_num) {
  159. $sql2 .= " or idcard = ?";
  160. $options[] = $card_num;
  161. }
  162. if($card_num) {
  163. $sql2 .= " or phone = ?";
  164. $options[] = $phone;
  165. }
  166. $sql = "select wx_openid as openid, source from user where id in (".$sql1." union ".$sql2.")";
  167. log::record('---------微信推送sql--------');
  168. log::record($sql);
  169. return Db::query($sql, $options);
  170. }
  171. private function curl_request($url,$method='get',$data=null,$https=true){
  172. //1.初识化curl
  173. $ch = curl_init($url);
  174. //2.根据实际请求需求进行参数封装
  175. //返回数据不直接输出
  176. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  177. //如果是https请求
  178. if($https === true){
  179. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  180. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  181. }
  182. //如果是post请求
  183. if($method === 'post'){
  184. //开启发送post请求选项
  185. curl_setopt($ch,CURLOPT_POST,true);
  186. //发送post的数据
  187. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  188. }
  189. //3.发送请求
  190. $result = curl_exec($ch);
  191. //4.返回返回值,关闭连接
  192. curl_close($ch);
  193. return $result;
  194. }
  195. }