123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Service\Weixin;
- use App\Service\Service;
- use Illuminate\Support\Facades\Redis;
- class WeixinService extends Service
- {
- const APPID = 'wxe048bcdcc21aae6e';
- const APPSECRET = '191789c5b4ef2b3d5b9e79bb62428092';
- const ACCESS_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
- const OPENID = '';
- const TOKEN = '';
- const KEY = 'weixintnine';
- public function getToken(){
- $token_key = self::KEY.'_'.'token';
- $token = Redis::get($token_key);
- if(! $token){
- $url = sprintf(self::ACCESS_URL,self::APPID,self::APPSECRET);
- $res = $this->curlOpen($url);
- $res = json_decode($res,true);
- if(isset($res['errmsg'])) return [false,$res['errmsg']];
- if(!isset($res['access_token'])) return [false,'request error'];
- $token = $res['access_token'];
- $expire_time = $res['expires_in']-300;
- Redis::set($token_key,$token);
- Redis::expire($token_key, $expire_time);
- return [true,$token];
- }
- return [true,$token];
- }
- public function getOpenid($data){
- if(empty($data['code'])) return [false, 'code不能为空'];
- $code = $data['code'];
- $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code';
- $url = sprintf($url,self::APPID,self::APPSECRET,$code);
- $res = $this->curlOpen($url);
- $res = json_decode($res,true);
- if(!isset($res['openid'])) return [false,$res['errmsg']??'request error'];
- $openid = $res['openid'];
- return [true,['openid' => $openid]];
- }
- public function setWebHook($data){
- // file_put_contents('22.txt',json_encode($data));
- $uri = isset($data['uri']) ? $data['uri'] : '';
- $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
- $param = isset($data['param']) ? $data['param'] : '';
- $redirect_uri = urlencode('https://t9api.qingyaokeji.com/wxapi/getUnionid?uri='.$uri.'¶m='.$param);
- $url = sprintf($url,self::APPID,$redirect_uri);
- header("Location:".$url);exit;
- echo 'ok';die;
- }
- public function getUnionid($data){
- file_put_contents('22.txt',date('YmdHis').json_encode($data));
- // echo $data['code'];
- if(isset($data['code'])) {
- list($status,$openid) = $this->getOpenid($data);
- file_put_contents('222.txt',date('YmdHis').json_encode($openid));
- if(!$status) return [false,$openid];
- $uri = $data['uri'];
- $openid = $openid['openid'];
- $param = isset($data['param']) ? $data['param'] : '';
- $url = 'https://t9.qingyaokeji.com/#/wxGet?uri='.$uri.'&openid='.$openid.'¶m='.$param;
- header('Location:'.$url);exit();
- }
- }
- // public function sendTmpMsg($data){
- // // $openid = 'okXNa69ggEX61KvHUhCq9PcGrPKI';
- // $data = [
- // 'openid' => 'o7B4f68DuDlBSevGdctFyP8MD-nw',
- // 'tempid' => '5azHlaoAu6MgRzkxn_HL6ygFt_wIkXEz9CklPWEdP70',
- // 'reload_url' => '',
- // 'first' => '工资发放',
- // 'remark' => '请查收',
- // 'detail' => [
- // 'thing2' => '姓名',
- // 'thing6' => '10',
- // 'time4' => '2023-09-01',
- // 'character_string3' => 'st.1231',
- // 'thing1' => '类型',
- // ]
- // ];
- // if(!isset($data['detail'])) return [false,'invalid detail'];
- // if(!isset($data['openid'])) return [false,'invalid openid'];
- // if(!isset($data['tempid'])) return [false,'invalid tempid'];
- // if(!isset($data['reload_url'])) return [false,'invalid reload_url'];
- // $templateID = $data['tempid'];
- // $reload_url = $data['reload_url'];
- // list($status,$token) = $this->getToken();
- // if(!$status) return [false,$token];
- // $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
- // $post = '{
- // "touser":"'.$data['openid'].'",
- // "template_id":"'.$templateID.'",
- // "url":"'.$reload_url.'",
- // "data":{
- // "first": {
- // "value":"'.$data['first'].'",
- // "color":"#173177"
- // },
- // %s
- // "remark":{
- // "value":"'.$data['remark'].'",
- // "color":"#173177"
- // }
- // }
- // }';
- // $content = "";
- // foreach ($data['detail'] as $k=>$v){
- //
- // $content .= '"'.$k.'": {
- // "value":"'.$v.'",
- // "color":"#173177"
- // },';
- // }
- // $post = sprintf($post,$content);
- //// var_dump($post);
- //// var_dump(json_decode($post));die;
- //// var_dump($url);
- //// var_dump(json_encode(json_decode($post)));
- // $res = $this->curlOpen($url,['post'=>$post]);
- // $res = json_decode($res,true);
- // if(isset($res['errcode'])&&$res['errcode'] != 0) return [false,$res['errmsg']];
- // if(isset($res['errcode'])&&$res['errcode'] === 0) return [true,''];
- // return [false,json_encode($res)];
- //
- // }
- }
|