WeixinService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Service\Weixin;
  3. use App\Service\Service;
  4. use Illuminate\Support\Facades\Redis;
  5. //微信公众号
  6. class WeixinService extends Service
  7. {
  8. public $config = [];
  9. public function __construct()
  10. {
  11. $config = config('wechatOfficial');
  12. $this->config = $config;
  13. }
  14. public function getToken(){
  15. $config = $this->config;
  16. $token_key = $config['tokenKeyName'] . '_' . 'token';
  17. $token = Redis::get($token_key);
  18. if(! $token){
  19. $url = sprintf($config['getTokenUrl'],$config['appId'],$config['appSecret']);
  20. $res = $this->curlOpen($url);
  21. $res = json_decode($res,true);
  22. if(isset($res['errmsg'])) return [false,$res['errmsg']];
  23. if(!isset($res['access_token'])) return [false,'request error'];
  24. $token = $res['access_token'];
  25. $expire_time = $res['expires_in']-300;
  26. Redis::set($token_key,$token);
  27. Redis::expire($token_key, $expire_time);
  28. return [true,$token];
  29. }
  30. return [true,$token];
  31. }
  32. public function setWebHook($data){
  33. $config = $this->config;
  34. //重定向地址参数
  35. $uri = isset($data['uri']) ? $data['uri'] : '';
  36. $param = isset($data['param']) ? $data['param'] : '';
  37. //重定向地址 重定向到 getUnionid 去获取openid 然后定向到前端页面
  38. $redirectUrl = sprintf($config['redirectUrl'],$uri,$param);
  39. $redirectUrl = urlencode($redirectUrl);
  40. $url = sprintf($config['setWebHookUrl'],$config['appId'],$redirectUrl);
  41. header("Location:".$url);exit;
  42. echo 'ok';die;
  43. }
  44. public function getUnionid($data){
  45. if(isset($data['code'])) {
  46. list($status,$openid) = $this->getOpenid($data);
  47. if(! $status) return [false, $openid];
  48. $config = $this->config;
  49. $uri = isset($data['uri']) ? $data['uri'] : '';
  50. $param = isset($data['param']) ? $data['param'] : '';
  51. $openid = $openid['openid'];
  52. //重定向前端地址
  53. $redirectFrontEndUrl = sprintf($config['redirectFrontEndUrl'],$uri,$openid,$param);
  54. header('Location:'.$redirectFrontEndUrl);
  55. exit();
  56. }
  57. }
  58. public function getOpenid($data){
  59. $config = $this->config;
  60. if(empty($data['code'])) return [false, 'code不能为空'];
  61. $code = $data['code'];
  62. $url = sprintf($config['getOpenIdUrl'],$config['appId'],$config['appSecret'],$code);
  63. $res = $this->curlOpen($url);
  64. $res = json_decode($res,true);
  65. if(!isset($res['openid'])) return [false,$res['errmsg']??'request error'];
  66. $openid = $res['openid'];
  67. return [true,['openid' => $openid]];
  68. }
  69. // public function sendTmpMsg($data){
  70. // // $openid = 'okXNa69ggEX61KvHUhCq9PcGrPKI';
  71. // $data = [
  72. // 'openid' => 'o7B4f68DuDlBSevGdctFyP8MD-nw',
  73. // 'tempid' => '5azHlaoAu6MgRzkxn_HL6ygFt_wIkXEz9CklPWEdP70',
  74. // 'reload_url' => '',
  75. // 'first' => '工资发放',
  76. // 'remark' => '请查收',
  77. // 'detail' => [
  78. // 'thing2' => '姓名',
  79. // 'thing6' => '10',
  80. // 'time4' => '2023-09-01',
  81. // 'character_string3' => 'st.1231',
  82. // 'thing1' => '类型',
  83. // ]
  84. // ];
  85. // if(!isset($data['detail'])) return [false,'invalid detail'];
  86. // if(!isset($data['openid'])) return [false,'invalid openid'];
  87. // if(!isset($data['tempid'])) return [false,'invalid tempid'];
  88. // if(!isset($data['reload_url'])) return [false,'invalid reload_url'];
  89. // $templateID = $data['tempid'];
  90. // $reload_url = $data['reload_url'];
  91. // list($status,$token) = $this->getToken();
  92. // if(!$status) return [false,$token];
  93. // $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
  94. // $post = '{
  95. // "touser":"'.$data['openid'].'",
  96. // "template_id":"'.$templateID.'",
  97. // "url":"'.$reload_url.'",
  98. // "data":{
  99. // "first": {
  100. // "value":"'.$data['first'].'",
  101. // "color":"#173177"
  102. // },
  103. // %s
  104. // "remark":{
  105. // "value":"'.$data['remark'].'",
  106. // "color":"#173177"
  107. // }
  108. // }
  109. // }';
  110. // $content = "";
  111. // foreach ($data['detail'] as $k=>$v){
  112. //
  113. // $content .= '"'.$k.'": {
  114. // "value":"'.$v.'",
  115. // "color":"#173177"
  116. // },';
  117. // }
  118. // $post = sprintf($post,$content);
  119. //// var_dump($post);
  120. //// var_dump(json_decode($post));die;
  121. //// var_dump($url);
  122. //// var_dump(json_encode(json_decode($post)));
  123. // $res = $this->curlOpen($url,['post'=>$post]);
  124. // $res = json_decode($res,true);
  125. // if(isset($res['errcode'])&&$res['errcode'] != 0) return [false,$res['errmsg']];
  126. // if(isset($res['errcode'])&&$res['errcode'] === 0) return [true,''];
  127. // return [false,json_encode($res)];
  128. //
  129. // }
  130. }