1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace app\api\controller;
- use EasyWeChat\Factory;
- use think\cache\driver\Redis;
- use think\Session;
- class WeChart {
- protected $app_id ='wx7a5f15ddeda9ef5a';
- protected $app_secret = 'a4c33b42917949652c31447e42132de0';
- /**
- * 获取token
- * @return mixed|string
- */
- public function getAccessToken()
- {
- $session = new Session();
- $over_time = $session->get('token_over_time');
- if($over_time < time()) {
- $url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
- $ret = http_curl($url);
- $session->set('access_token',$ret['access_token']);
- $session->set('token_over_time',time() +$ret['expires_in'] );
- }
- return $session->get('access_token');
- }
- public function send_message($order_sn=''){
- $url='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token='.$access_token;
- $order = Db::name('StoreOrder')->field('id,order_sn,create_at,num,take_time,user_id,goods_id')->where('order_sn',$order_sn)->find();
- $member = Db::name('storeMember')->field('id,vip_level,openid,username,nickname')->where('id',$order['user_id'])->find();
- $book_book = Db::name('book_book')->field('id,title')->where('id',$order['goods_id'])->find();
- $data=[
- 'touser'=>$member['openid'],
- 'mp_template_msg'=>[
- "appid"=>"wx3346d52f010b611d",
- "template_id"=>"SH6KcRTVDiU0-7cSDujHsCg8MhbgzGpHb8US1QiPYW0",
- "url"=>"http://www.baidu.com",
- 'miniprogram'=>[
- "appid"=>"wx2b03c73ff0547cc3",
- ],
- 'data'=>[
- 'first'=>[
- "value"=>'恭喜你已借阅到该图书',
- "color"=>"#173177"
- ],
- 'keyword1'=>[
- "value"=>$book_book['title'],
- "color"=>"#173177"
- ],
- 'keyword2'=>[
- "value"=>$member['username']?$member['username']:$member['nickname'],
- "color"=>"#173177"
- ],
- 'keyword3'=>[
- "value"=>date('Y-m-d H:i:s',time()),
- "color"=>"#173177"
- ],
- 'remark'=>[
- "value"=>'谢谢你对哩哈教育的支持!',
- "color"=>"#173177"
- ]
- ]
- ],
- ];
- $res = requestPost($url,json_encode($data));
- }
- }
|