WechatService.php 2.9 KB

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