WechatService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var_dump($exam_id);
  8. $openids = $this->getPushOpenId($exam_id);
  9. var_dump($openids);
  10. if(!$openids) {
  11. log::record('---------没有找到对应openid--------');
  12. return false;
  13. }
  14. foreach($openids as $v) {
  15. $url = "http://wechat.pacsonline.cn/wx_patient/api/sendMsg?pushType=1&openid=";
  16. $full_url = $url . $v['openid'];
  17. // 请求
  18. $res = $this->curl_request($full_url, 'GET');
  19. var_dump($res);
  20. }
  21. return true;
  22. }
  23. public function pushWechatCancel() {
  24. }
  25. public function pushWechatComplete() {
  26. }
  27. private function getPushOpenId($exam_id) {
  28. $exams = DB::table("exams")
  29. ->alias('e')
  30. ->join('studies s', 'e.study_id = s.id')
  31. ->join('patient_infos pi', 'e.patient_id = pi.id')
  32. ->field('e.patient_id, s.accession_num, s.studyid, pi.temp_patient_id, pi.card_num, pi.phone')
  33. ->where('e.id', $exam_id)
  34. ->find();
  35. if(!$exams) {
  36. return false;
  37. }
  38. $options = [];
  39. $accession_num = $exams['accession_num'];
  40. $studyid = $exams['studyid'];
  41. $patient_id = $exams['patient_id'];
  42. $temp_patient_id = $exams['temp_patient_id'];
  43. $card_num = $exams['card_num'];
  44. $phone = $exams['phone'];
  45. $sql1 = "select user_id as uid from user_bind where patient_id = ?";
  46. $options[] = $patient_id;
  47. $sql2 = "select uid from wechat_bind where ";
  48. $sql2 .= "(patientCode = ?";
  49. $options[] = $temp_patient_id;
  50. if(!empty($accession_num)) {
  51. $sql2 .= " or patientCode = ?";
  52. $options[] = $accession_num;
  53. }
  54. if(!empty($studyid)) {
  55. $sql2 .= " or patientCode = ?";
  56. $options[] = $studyid;
  57. }
  58. $sql2 .=")";
  59. if($card_num) {
  60. $sql2 .= " or idcard = ?";
  61. $options[] = $card_num;
  62. }
  63. if($card_num) {
  64. $sql2 .= " or phone = ?";
  65. $options[] = $phone;
  66. }
  67. $sql = "select wx_openid as openid from user where id in (".$sql1." union ".$sql2.")";
  68. log::record('---------微信推送sql--------');
  69. log::record($sql);
  70. return Db::query($sql, $options);
  71. }
  72. private function curl_request($url,$method='get',$data=null,$https=true){
  73. //1.初识化curl
  74. $ch = curl_init($url);
  75. //2.根据实际请求需求进行参数封装
  76. //返回数据不直接输出
  77. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  78. //如果是https请求
  79. if($https === true){
  80. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  81. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  82. }
  83. //如果是post请求
  84. if($method === 'post'){
  85. //开启发送post请求选项
  86. curl_setopt($ch,CURLOPT_POST,true);
  87. //发送post的数据
  88. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  89. }
  90. //3.发送请求
  91. $result = curl_exec($ch);
  92. //4.返回返回值,关闭连接
  93. curl_close($ch);
  94. return $result;
  95. }
  96. }