|
@@ -0,0 +1,132 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace App\Service\Weixin;
|
|
|
|
+
|
|
|
|
+use App\Service\Service;
|
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
|
+
|
|
|
|
+class WeixinService extends Service
|
|
|
|
+{
|
|
|
|
+ const APPID = '';
|
|
|
|
+ const APPSECRET = '';
|
|
|
|
+ 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 = 'weixin';
|
|
|
|
+
|
|
|
|
+ public function getToken(){
|
|
|
|
+ $token_key = self::KEY.'_'.'token';
|
|
|
|
+ $token = Redis::get($token_key);
|
|
|
|
+ if(! empty($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){
|
|
|
|
+ $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('http://hdapi.qingyaokeji.com/wx/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);
|
|
|
|
+ if(!$status) return [false,$openid];
|
|
|
|
+ $uri = $data['uri'];
|
|
|
|
+ $openid = $openid['openid'];
|
|
|
|
+ $param = isset($data['param']) ? $data['param'] : '';
|
|
|
|
+ $url = 'http://hd.qingyaokeji.com/#/wxGet?uri='.$uri.'&openid='.$openid.'¶m='.$param;
|
|
|
|
+ header('Location:'.$url);exit();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function sendTmpMsg($data){
|
|
|
|
+ // $openid = 'okXNa69ggEX61KvHUhCq9PcGrPKI';
|
|
|
|
+ $data = [
|
|
|
|
+ 'openid' => 'okXNa69ggEX61KvHUhCq9PcGrPKI',
|
|
|
|
+ 'tempid' => 'qaD9NBasjTNKDBzIuinSpLDMy6zWbPtRb1HN8ZOjY-Y',
|
|
|
|
+ 'reload_url' => '',
|
|
|
|
+ 'first' => '工资发放',
|
|
|
|
+ 'remark' => '请查收',
|
|
|
|
+ 'detail' => [
|
|
|
|
+ 'thing1' => '姓名',
|
|
|
|
+ 'amount2' => '10',
|
|
|
|
+ 'time3' => '2023-09-01',
|
|
|
|
+ 'thing4' => '类型',
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+ 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)];
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|