WeChart.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\api\controller;
  3. use EasyWeChat\Factory;
  4. use think\cache\driver\Redis;
  5. use think\Session;
  6. use think\Db;
  7. class WeChart {
  8. protected $app_id ='wx4ccfaf737dd24004';
  9. protected $app_secret = 'a7a7e0e7e3ce132f751e85202a09e0dc';
  10. /**
  11. * 获取token
  12. * @return mixed|string
  13. */
  14. public function getAccessToken()
  15. {
  16. $session = new Session();
  17. $over_time = $session->get('token_over_time');
  18. if($over_time < time()) {
  19. $url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
  20. $ret = http_curl($url);
  21. $session->set('access_token',$ret['access_token']);
  22. $session->set('token_over_time',time() +$ret['expires_in'] );
  23. }
  24. return $session->get('access_token');
  25. }
  26. public function sendMsg($openid,$jump_url)
  27. {
  28. $access_token = $this->getAccessToken();
  29. $url='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token='.$access_token;
  30. $data=[
  31. 'touser'=>$openid,
  32. 'mp_template_msg'=>[
  33. "appid"=>"wx8e881917d7c4c283",// 公众号
  34. "template_id"=>"2n42cwWat6UsokuOd9uqYAKIOF4Jbezw7hwVht11pz4",
  35. "url"=>$jump_url,
  36. 'miniprogram'=>[
  37. "appid"=>"wx4ccfaf737dd24004",// 小程序
  38. ],
  39. 'data'=>[
  40. 'first'=>[
  41. "value"=>'一剑霜寒',
  42. "color"=>"#173177"
  43. ],
  44. 'keyword1'=>[
  45. "value"=>'临沂',
  46. "color"=>"#173177"
  47. ],
  48. 'keyword2'=>[
  49. "value"=>'2021-10-20',
  50. "color"=>"#173177"
  51. ],
  52. 'keyword3'=>[
  53. "value"=>'20:30',
  54. "color"=>"#173177"
  55. ],
  56. 'keyword4'=>[
  57. "value"=>'李白刷野',
  58. "color"=>"#173177"
  59. ],
  60. 'keyword5'=>[
  61. "value"=>'20:30',
  62. "color"=>"#173177"
  63. ],
  64. 'remark'=>[
  65. "value"=>'屋里割草',
  66. "color"=>"#173177"
  67. ]
  68. ]
  69. ],
  70. ];
  71. $res = http_curl($url,'post',$data);
  72. }
  73. }