| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // 在 application/common.php 中添加
- //本站点请求地址
- function commonURL(){
- // return 'http://127.0.0.1:3335';
- return 'http://192.168.10.115:3335';
- }
- //开放平台请求地址(仅限本机请求)
- function commonOpenURL(){
- return 'http://192.168.10.115:3338';
- }
- //apiHTTP公网版
- function commonOpenURLHTTP(){
- // return 'http://127.0.0.1:3337';
- return 'http://192.168.10.115:3340';
- }
- //apiSSE公网版
- function commonOpenURLSSE(){
- // return 'http://127.0.0.1:3336';
- return 'http://192.168.10.115:3339';
- }
- //开放平台请求地址(内网)
- function commonOpenURLLocal(){
- // return 'http://127.0.0.1:3338';
- return 'http://192.168.10.115:3338';
- }
- //apiHTTP内网版
- function commonOpenURLLocalHTTP(){
- return 'http://192.168.10.115:3340';
- }
- //apiSSE内网版
- function commonOpenURLLocalSSE(){
- return 'http://192.168.10.115:3339';
- }
- //websocket
- function commonWebsocket(){
- // return 'ws://127.0.0.1:8033';
- return 'ws://192.168.10.115:8023';
- }
- //内网数据集请求地址
- function commonDataURL(){
- // 3334
- return 'http://192.168.10.102:3334';
- }
- //创建项目密钥
- function commonPublicKey(){
- return '6c5McfXGHnB0lmX2ir4sRvx6YBXlgxew';
- }
- //创建项目sm4的key
- function commonPrivateKey(){
- return 'kdOVYNCDa50ZaKQF';
- }
- //生成随机16位key字符串做sm4的iv
- function getSMIV() {
- $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $randomString = '';
- for ($i = 0; $i < 16; $i++) {
- $index = rand(0, strlen($characters) - 1);
- $randomString .= $characters[$index];
- }
- return $randomString;
- }
- //判断是否是手机端还是电脑端
- function isMobile() {
- // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
- if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
- return true;
-
- //此条摘自TPM智能切换模板引擎,适合TPM开发
- if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
- return true;
- //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
- if (isset ($_SERVER['HTTP_VIA']))
- //找不到为flase,否则为true
- return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
- //判断手机发送的客户端标志,兼容性有待提高
- if (isset ($_SERVER['HTTP_USER_AGENT'])) {
- $clientkeywords = array(
- 'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
- );
- //从HTTP_USER_AGENT中查找手机浏览器的关键字
- if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
- return true;
- }
- }
- //协议法,因为有可能不准确,放到最后判断
- if (isset ($_SERVER['HTTP_ACCEPT'])) {
- // 如果只支持wml并且不支持html那一定是移动设备
- // 如果支持wml和html但是wml在html之前则是移动设备
- if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
- return true;
- }
- }
- return false;
- }
- //判断用户设备是ios还是安卓
- function mobileType(){
- $userAgent = $_SERVER['HTTP_USER_AGENT'];
- if (preg_match('/(iPhone|iPod|iPad)/', $userAgent)) {
- return "ios";
- } elseif (preg_match('/Android/', $userAgent)) {
- return "android";
- } else {
- return "unknow";
- }
- }
- //公共请求方法
- function curlPost($url, $data = null, $headers = [], $json = false) {
- $curl = curl_init($url);
- $options = [
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_POST => !empty($data) || $json,
- ];
- if ($json) {
- $headers = array_filter($headers, function($h) {
- return stripos($h, 'Content-Type:') === false;
- });
- $headers[] = 'Content-Type: application/json';
- $data = json_encode($data, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($headers)) {
- $options[CURLOPT_HTTPHEADER] = $headers;
- }
- if (!empty($data)) {
- $options[CURLOPT_POSTFIELDS] = $data;
- }
- curl_setopt_array($curl, $options);
- $result = curl_exec($curl);
- curl_close($curl);
- return $result;
- }
- //将对象转为数组
- function stdClassObjToArray($array) {
- if(is_object($array)) {
- $array = (array)$array;
- }
- if(is_array($array)) {
- foreach($array as $key=>$value) {
- $array[$key] = stdClassObjToArray($value);
- }
- }
- return $array;
- }
- //公共公众号appid
- function commonAPPID(){
- return 'wxbd4f2db9777efe2e';
- }
- // 生成签名的随机串
- function nonceStr($length){
- $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJK1NGJBQRSTUVWXYZ';//随即串,62个字符
- $strlen = 62;
- while($length > $strlen){
- $str .= $str;
- $strlen += 62;
- }
- $str = str_shuffle($str);
- return substr($str,0,$length);
- }
-
- // 获取微信公众号accessToken
- function getOfficialAccessToken() {
- // 构建查询条件判断accesstoken接口是否存在
- $mapWxOfficial['api'] = array('eq','accessToken');
- $accessTokenExist = db('Wxofficial') -> where($mapWxOfficial) -> value('id');
- // 如果accessToken接口不存在
- if(!$accessTokenExist){
- // 获取公众号的accessToken
- $accessTokenRequest = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbd4f2db9777efe2e&secret=78937f347edd0a7e8882ce3ac3b7fe94');
- $accessTokenRequestJson = json_decode($accessTokenRequest,true);
- $accessToken = $accessTokenRequestJson['access_token'];
- $time = time();
- $data = ['api' => 'accessToken','info' => $accessToken,'time' => $time];
- db('Wxofficial') -> insert($data);
- }
- // 如果accessToken接口存在
- else{
- // 获取accessToken存储的最后时间
- $accessTokenTime = db('Wxofficial') -> where($mapWxOfficial) -> value('time');
- $time = time();
- // 如果当前时间比accessToken存储时间超出60秒
- if(($time-$accessTokenTime) > 60){
- // 获取公众号的accessToken
- $accessTokenRequest = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbd4f2db9777efe2e&secret=78937f347edd0a7e8882ce3ac3b7fe94');
- $accessTokenRequestJson = json_decode($accessTokenRequest,true);
- $accessToken = $accessTokenRequestJson['access_token'];
- db('Wxofficial') -> where($mapWxOfficial) -> update(['info' => $accessToken,'time' => $time]);
- }
- else{
- $accessToken = db('Wxofficial') -> where($mapWxOfficial) -> value('info');
- }
- }
- return $accessToken;
- }
- // 获取微信公众号jsapiTicket
- function getOfficialJsapiTicket() {
- // 构建查询条件判断jsapiTicket接口是否存在
- // $mapWxOfficialJsapiTicket['api'] = array('eq','jsapiTicket');
- // $jsapiTicketExist = db('Wxofficial') -> where($mapWxOfficialJsapiTicket) -> value('id');
- // // 如果jsapiTicket接口不存在
- // if(!$jsapiTicketExist){
- // $accessToken = getOfficialAccessToken();
- // // 获取公众号的jsapiTicket
- // $jsapiTicketRequest = file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$accessToken.'&type=jsapi');
- // $jsapiTicketRequestJson = json_decode($jsapiTicketRequest,true);
- // $jsapiTicket = $jsapiTicketRequestJson['ticket'];
- // $time = time();
- // $data = ['api' => 'jsapiTicket','info' => $jsapiTicket,'time' => $time];
- // db('Wxofficial') -> insert($data);
- // }
- // // 如果jsapiTicket接口存在
- // else{
- // // 获取jsapiTicket存储的最后时间
- // $jsapiTicketTime = db('Wxofficial') -> where($mapWxOfficialJsapiTicket) -> value('time');
- // $time = time();
- // // 如果当前时间比jsapiTicket存储时间超出60秒
- // if(($time-$jsapiTicketTime) > 60){
- // $accessToken = getOfficialAccessToken();
- // // 获取公众号的jsapiTicket
- // $jsapiTicketRequest = file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$accessToken.'&type=jsapi');
- // $jsapiTicketRequestJson = json_decode($jsapiTicketRequest,true);
- // $jsapiTicket = $jsapiTicketRequestJson['ticket'];
- // db('Wxofficial') -> where($mapWxOfficialJsapiTicket) -> update(['info' => $jsapiTicket,'time' => $time]);
- // }
- // else{
- // $jsapiTicket = db('Wxofficial') -> where($mapWxOfficialJsapiTicket) -> value('info');
- // }
- // }
- return "dasds";
- }
- function curlPostRAG($url, $data = null, $headers = [], $json = true) {
- $curl = curl_init($url);
- $options = [
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_POST => true, // 强制启用 POST(JSON 模式下始终 POST)
- ];
-
- if ($json) {
- // 移除已有的 Content-Type 和 Accept 头,避免冲突
- $headers = array_filter($headers, function($h) {
- return stripos($h, 'Content-Type:') === false && stripos($h, 'Accept:') === false;
- });
- // 添加 JSON 专用头
- $headers[] = 'Content-Type: application/json';
- $headers[] = 'Accept: application/json';
- // 编码数据为 JSON(空数据编码为 "{}" 避免服务器解析错误)
- $data = $data !== null ? json_encode($data, JSON_UNESCAPED_UNICODE) : '{}';
- }
-
- // 设置请求头和数据
- if (!empty($headers)) {
- $options[CURLOPT_HTTPHEADER] = $headers;
- }
- if ($data !== null) {
- $options[CURLOPT_POSTFIELDS] = $data;
- }
-
- curl_setopt_array($curl, $options);
- $result = curl_exec($curl);
-
- // 错误处理(调试时启用)
- if ($result === false) {
- $error = curl_error($curl);
- $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- throw new Exception("CURL请求失败: HTTP状态码 $httpCode, 错误信息: $error");
- }
-
- curl_close($curl);
- return $result;
- }
|