uniacid = $uniacid; $this->config = $this->getConfig($this->uniacid); $this->appid = $this->getAppid(); $this->appsecret = $this->getAppsecret(); } /** * 功能说明 获取appid * * @return mixed|null * @author chenniang * @DataTime: 2019-12-25 17:36 */ protected function getAppid() { if(isset($this->config['appid'])) return $this->config['appid']; return null; } /** * 功能说明 获取appsecret * * @return mixed|null * @author chenniang * @DataTime: 2019-12-25 17:36 */ protected function getAppsecret() { if(isset($this->config['appsecret'])) return $this->config['appsecret']; return null; } /** * 功能说明 获取配置信息 * * @param $uniacid * @return array|bool|mixed|\think\Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author chenniang * @DataTime: 2019-12-25 17:35 */ public function getConfig($uniacid) { $config = longbingGetAppConfig($uniacid); //返回数据 return $config; } //检查信息是否存在 public function checkConfig() { $result = true; if(empty($this->uniacid) || empty($this->appid) || empty($this->appsecret)) $result = false; return $result; } /** * 功能说明 获取token * * @return bool|null * @author chenniang * @DataTime: 2019-12-25 17:35 */ public function lbSingleGetAccessToken () { $key = "longbing_card_access_token"; $value = getCache( $key,$this->uniacid ); if ( $value !=false ) { return $value; } $uniacid = $this->uniacid; //基础检查 if(!$this->checkConfig()){ return false; } //appid $appid = $this->appid; //appsecret $appsecret = $this->appsecret; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret={$appsecret}"; $accessToken = file_get_contents( $url ); // dump($accessToken);exit; if ( strstr( $accessToken, 'errcode' ) ) { return 0; } $accessToken = json_decode( $accessToken, true ); $accessToken = $accessToken[ 'access_token' ]; setCache( $key, $accessToken, 7000, $uniacid ); return $accessToken; } /** * User: chenniang * Date: 2019-12-25 11:52 * @param $data * @return bool|string * descrption:添加模版 返回模版消息id */ public function addTmpl($data){ //获取token $access_token = $this->lbSingleGetAccessToken(); // dump($access_token);exit; //添加模版的小程序地址 $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={$access_token}"; //请求 $data = json_encode( $data, JSON_UNESCAPED_UNICODE ); //返回模版消息id 是个array $res = $this->curlPost( $url,$data ); return $res; } /** * @author chenniang * @DataTime: 2019-12-26 11:39 * @功能说明:获取所有模版消息列表 */ public function getAllTpmlList(){ //获取token $access_token = $this->lbSingleGetAccessToken(); //获取小程序所有 模版消息的url $url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={$access_token}"; //请求 $res = $this->curlPost($url,NULL,'GET'); return $res; } /** * @param $tpml_id * @功能说明: 删除模版消息 * @author chenniang * @DataTime: 2019-12-26 14:08 */ public function deleteTmpl($tpml_id){ //获取token $access_token = $this->lbSingleGetAccessToken(); //获取小程序所有 模版消息的url $url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token={$access_token}"; //请求参数 $data['priTmplId'] = $tpml_id; //请求 $data = json_encode( $data, JSON_UNESCAPED_UNICODE ); $res = $this->curlPost($url,$data); return $res; } /** * @author chenniang * @DataTime: 2019-12-26 18:54 * @功能说明:发送模版消息 */ public function sendTmpl($data){ //获取token $access_token = $this->lbSingleGetAccessToken(); //获取小程序所有 模版消息的url $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$access_token}"; //请求 $data = json_encode( $data, JSON_UNESCAPED_UNICODE ); // dump($data);exit; $res = $this->curlPost($url,$data); return $res; } /** * @author chenniang * @DataTime: 2019-12-26 18:54 * @功能说明:获取当前账号下的所有模版消息 */ public function getUserTmpl(){ //获取token $access_token = $this->lbSingleGetAccessToken(); //获取小程序所有 模版消息的url $url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={$access_token}"; //请求 $res = $this->curlPost($url,[],'GET'); return $res; } /** * @param array $data * @功能说明:企业微信消息 * @author chenniang * @DataTime: 2020-04-27 18:33 */ public function sendCompanyMsg(array $data) { if (is_array($data)) { $data = json_encode($data, JSON_UNESCAPED_UNICODE); } $accessTokenWW = $this->companyMsgToken(); $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$accessTokenWW}"; $res = $this->curlPost($url, $data); return $res; } /** * @author chenniang * @DataTime: 2020-04-28 13:40 * @功能说明:获取企业消息token */ public function companyMsgToken(){ $key = "longbing_card_access_token_qy_cn"; $value = getCache($key,$this->uniacid); if ( $value !== false ) { return $value; } $send_model = new SendConfig(); //配置信息 $config = $send_model->configInfo(['uniacid'=>$this->uniacid]); if (!empty($config['yq_corpid'])&& !empty($config['yq_corpsecret'] )) { $key = $config['yq_corpid']; $secret = $config['yq_corpsecret']; $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$key&corpsecret=$secret"; $accessToken = file_get_contents( $url ); $accessToken = json_decode( $accessToken, true ); if($accessToken['errcode'] != 0){ echo json_encode( [ 'code' => 402, 'error' => '获取token失败' ] ); exit; } $accessToken = $accessToken[ 'access_token' ]; setCache( $key, $accessToken, 7200, $this->uniacid ); } return !empty($accessToken)?$accessToken:''; } /** * 功能说明 post 请求 * * @param $url * @param $post * @param $method * @param int $header * @return bool|string * @author chenniang * @DataTime: 2019-12-25 18:40 */ public function curlPost($url,$post=NULL,$post_type='POST'){ $result = $this->crulGetData($url,$post,$post_type); return $result; } /** * 功能说明 post 请求 * * @param $url * @param $post * @param $method * @param int $header * @return bool|string * @author chenniang * @DataTime: 2019-12-25 18:40 */ public function crulGetData($url, $post, $method, $header=1){ $this->_errno = NULL; $this->_error = NULL; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if($post != NULL) curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true); curl_setopt($ch, CURLOPT_AUTOREFERER,true); curl_setopt($ch, CURLOPT_TIMEOUT, 120); if($header) curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json; charset=utf-8", ) ); $result = curl_exec($ch); return $result; } /** * @param $content * @param $access_token * @功能说明: 违禁词检测 * @author chenniang * @DataTime: 2020-01-02 17:07 */ public function wxContentRlue($content) { $access_token = $this->lbSingleGetAccessToken(); if(empty($access_token)){ return ['errcode'=>0]; } $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token={$access_token}"; $tmp = [ 'url' => $url, 'data' => [ 'content' => urlencode($content) ], ]; $rest = $this->curlPost($tmp['url'], urldecode(json_encode($tmp['data']))); $rest = json_decode($rest, true); return $rest; } /** * @param $img_path * @功能说明:图片检测 * @author chenniang * @DataTime: 2020-03-27 11:44 */ function imgSecCheck($img_path){ //token $access_token = $this->lbSingleGetAccessToken(); //地址 $url ='https://api.weixin.qq.com/wxa/img_sec_check?access_token='.$access_token; //请求包 $post_data = [ 'media'=>new \CURLFile($img_path) ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); $output = curl_exec($ch); curl_close($ch); return $output; } /** * @author chenniang * @DataTime: 2022-03-14 18:56 * @功能说明:php二维码 返回base64 */ public function phpQrCode($data){ require_once EXTEND_PATH.'phpqrcode/phpqrcode.php'; $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 10; //生成图片大小 ob_start(); \QRcode::png($data,false,$errorCorrectionLevel,$matrixPointSize,2,false,0xFFFFFF,0x000000); $imgstr = base64_encode(ob_get_contents()); ob_end_clean(); $imgstr = 'data:image/png;base64,'.$imgstr; return $imgstr; } }