WeixinService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Service\Weixin;
  3. use App\Service\Service;
  4. use Illuminate\Support\Facades\Redis;
  5. class WeixinService extends Service
  6. {
  7. const APPID = 'wxe048bcdcc21aae6e';
  8. const APPSECRET = '191789c5b4ef2b3d5b9e79bb62428092';
  9. const ACCESS_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
  10. const OPENID = '';
  11. const TOKEN = '';
  12. const KEY = 'weixintnine';
  13. public function getToken(){
  14. $token_key = self::KEY.'_'.'token';
  15. $token = Redis::get($token_key);
  16. if(! $token){
  17. $url = sprintf(self::ACCESS_URL,self::APPID,self::APPSECRET);
  18. $res = $this->curlOpen($url);
  19. $res = json_decode($res,true);
  20. if(isset($res['errmsg'])) return [false,$res['errmsg']];
  21. if(!isset($res['access_token'])) return [false,'request error'];
  22. $token = $res['access_token'];
  23. $expire_time = $res['expires_in']-300;
  24. Redis::set($token_key,$token);
  25. Redis::expire($token_key, $expire_time);
  26. return [true,$token];
  27. }
  28. return [true,$token];
  29. }
  30. public function getOpenid($data){
  31. if(empty($data['code'])) return [false, 'code不能为空'];
  32. $code = $data['code'];
  33. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code';
  34. $url = sprintf($url,self::APPID,self::APPSECRET,$code);
  35. $res = $this->curlOpen($url);
  36. $res = json_decode($res,true);
  37. if(!isset($res['openid'])) return [false,$res['errmsg']??'request error'];
  38. $openid = $res['openid'];
  39. return [true,['openid' => $openid]];
  40. }
  41. public function setWebHook($data){
  42. // file_put_contents('22.txt',json_encode($data));
  43. $uri = isset($data['uri']) ? $data['uri'] : '';
  44. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  45. $param = isset($data['param']) ? $data['param'] : '';
  46. $redirect_uri = urlencode('https://t9api.qingyaokeji.com/wxapi/getUnionid?uri='.$uri.'&param='.$param);
  47. $url = sprintf($url,self::APPID,$redirect_uri);
  48. header("Location:".$url);exit;
  49. echo 'ok';die;
  50. }
  51. public function getUnionid($data){
  52. file_put_contents('22.txt',date('YmdHis').json_encode($data));
  53. // echo $data['code'];
  54. if(isset($data['code'])) {
  55. list($status,$openid) = $this->getOpenid($data);
  56. file_put_contents('222.txt',date('YmdHis').json_encode($openid));
  57. if(!$status) return [false,$openid];
  58. $uri = $data['uri'];
  59. $openid = $openid['openid'];
  60. $param = isset($data['param']) ? $data['param'] : '';
  61. $url = 'https://t9.qingyaokeji.com/#/wxGet?uri='.$uri.'&openid='.$openid.'&param='.$param;
  62. header('Location:'.$url);exit();
  63. }
  64. }
  65. // public function sendTmpMsg($data){
  66. // // $openid = 'okXNa69ggEX61KvHUhCq9PcGrPKI';
  67. // $data = [
  68. // 'openid' => 'o7B4f68DuDlBSevGdctFyP8MD-nw',
  69. // 'tempid' => '5azHlaoAu6MgRzkxn_HL6ygFt_wIkXEz9CklPWEdP70',
  70. // 'reload_url' => '',
  71. // 'first' => '工资发放',
  72. // 'remark' => '请查收',
  73. // 'detail' => [
  74. // 'thing2' => '姓名',
  75. // 'thing6' => '10',
  76. // 'time4' => '2023-09-01',
  77. // 'character_string3' => 'st.1231',
  78. // 'thing1' => '类型',
  79. // ]
  80. // ];
  81. // if(!isset($data['detail'])) return [false,'invalid detail'];
  82. // if(!isset($data['openid'])) return [false,'invalid openid'];
  83. // if(!isset($data['tempid'])) return [false,'invalid tempid'];
  84. // if(!isset($data['reload_url'])) return [false,'invalid reload_url'];
  85. // $templateID = $data['tempid'];
  86. // $reload_url = $data['reload_url'];
  87. // list($status,$token) = $this->getToken();
  88. // if(!$status) return [false,$token];
  89. // $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
  90. // $post = '{
  91. // "touser":"'.$data['openid'].'",
  92. // "template_id":"'.$templateID.'",
  93. // "url":"'.$reload_url.'",
  94. // "data":{
  95. // "first": {
  96. // "value":"'.$data['first'].'",
  97. // "color":"#173177"
  98. // },
  99. // %s
  100. // "remark":{
  101. // "value":"'.$data['remark'].'",
  102. // "color":"#173177"
  103. // }
  104. // }
  105. // }';
  106. // $content = "";
  107. // foreach ($data['detail'] as $k=>$v){
  108. //
  109. // $content .= '"'.$k.'": {
  110. // "value":"'.$v.'",
  111. // "color":"#173177"
  112. // },';
  113. // }
  114. // $post = sprintf($post,$content);
  115. //// var_dump($post);
  116. //// var_dump(json_decode($post));die;
  117. //// var_dump($url);
  118. //// var_dump(json_encode(json_decode($post)));
  119. // $res = $this->curlOpen($url,['post'=>$post]);
  120. // $res = json_decode($res,true);
  121. // if(isset($res['errcode'])&&$res['errcode'] != 0) return [false,$res['errmsg']];
  122. // if(isset($res['errcode'])&&$res['errcode'] === 0) return [true,''];
  123. // return [false,json_encode($res)];
  124. //
  125. // }
  126. }