WeChart.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\api\controller;
  3. use EasyWeChat\Factory;
  4. use think\cache\driver\Redis;
  5. use think\Session;
  6. class WeChart {
  7. protected $app_id ='wx7a5f15ddeda9ef5a';
  8. protected $app_secret = 'a4c33b42917949652c31447e42132de0';
  9. /**
  10. * 获取token
  11. * @return mixed|string
  12. */
  13. public function getAccessToken()
  14. {
  15. $session = new Session();
  16. $over_time = $session->get('token_over_time');
  17. if($over_time < time()) {
  18. $url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
  19. $ret = http_curl($url);
  20. $session->set('access_token',$ret['access_token']);
  21. $session->set('token_over_time',time() +$ret['expires_in'] );
  22. }
  23. return $session->get('access_token');
  24. }
  25. public function send_message($order_sn=''){
  26. $url='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token='.$access_token;
  27. $order = Db::name('StoreOrder')->field('id,order_sn,create_at,num,take_time,user_id,goods_id')->where('order_sn',$order_sn)->find();
  28. $member = Db::name('storeMember')->field('id,vip_level,openid,username,nickname')->where('id',$order['user_id'])->find();
  29. $book_book = Db::name('book_book')->field('id,title')->where('id',$order['goods_id'])->find();
  30. $data=[
  31. 'touser'=>$member['openid'],
  32. 'mp_template_msg'=>[
  33. "appid"=>"wx3346d52f010b611d",
  34. "template_id"=>"SH6KcRTVDiU0-7cSDujHsCg8MhbgzGpHb8US1QiPYW0",
  35. "url"=>"http://www.baidu.com",
  36. 'miniprogram'=>[
  37. "appid"=>"wx2b03c73ff0547cc3",
  38. ],
  39. 'data'=>[
  40. 'first'=>[
  41. "value"=>'恭喜你已借阅到该图书',
  42. "color"=>"#173177"
  43. ],
  44. 'keyword1'=>[
  45. "value"=>$book_book['title'],
  46. "color"=>"#173177"
  47. ],
  48. 'keyword2'=>[
  49. "value"=>$member['username']?$member['username']:$member['nickname'],
  50. "color"=>"#173177"
  51. ],
  52. 'keyword3'=>[
  53. "value"=>date('Y-m-d H:i:s',time()),
  54. "color"=>"#173177"
  55. ],
  56. 'remark'=>[
  57. "value"=>'谢谢你对哩哈教育的支持!',
  58. "color"=>"#173177"
  59. ]
  60. ]
  61. ],
  62. ];
  63. $res = requestPost($url,json_encode($data));
  64. }
  65. }