CommonServer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace App\Servers\System;
  3. use App\Models\Config;
  4. use App\Models\Region;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Storage;
  7. use QCloud\COSSTS\Sts;
  8. /**
  9. * Redis数据缓存类
  10. */
  11. class CommonServer
  12. {
  13. /**
  14. * 错误信息
  15. * @var string
  16. */
  17. private $errorMsg = '';
  18. static private $server = null;
  19. private function __construct()
  20. {
  21. }
  22. /**
  23. * 创建对象
  24. * @return CommonServer
  25. */
  26. static function creatServer()
  27. {
  28. if (empty(self::$server)) self::$server = new CommonServer();
  29. return self::$server;
  30. }
  31. /**
  32. * 验证手机号码格式
  33. * @param $phone
  34. * @return bool
  35. */
  36. public function verifyPhoneNumber($phone)
  37. {
  38. if (empty($phone)) return false;
  39. if (!preg_match('#^[\d]{11,11}$#', $phone)) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. /**
  45. * 字符串去标签过滤及空格过滤
  46. * @param $str_name
  47. * @param $default
  48. * @return string
  49. */
  50. function filtrationStr($str_name, $default = '')
  51. {
  52. $str = request()->input($str_name, $default);
  53. if (empty($str)) return $str;
  54. if (!is_string($str)) {
  55. $str = json_encode($str);
  56. $str = strip_tags($str);
  57. $str = trim($str);
  58. $str = json_decode($str, true);
  59. } else {
  60. $str = strip_tags($str);
  61. $str = trim($str);
  62. }
  63. return $str;
  64. }
  65. /**
  66. * 获取城市名称
  67. * @param string $province_id
  68. * @param string $city_id
  69. * @param string $area_id
  70. * @return string
  71. */
  72. function getCityName($province_id = '', $city_id = '', $area_id = '')
  73. {
  74. $ids = [$province_id, $city_id, $area_id];
  75. $ids = array_filter($ids);
  76. if (empty($ids)) return '';
  77. $city_name = Region::whereIn('id', $ids)->orderBy('id', 'asc')->pluck('name')->toArray();
  78. $city_name = implode(' ', $city_name);
  79. return $city_name;
  80. }
  81. /**
  82. * 字符串屏蔽
  83. * @param $str
  84. * @param int $start_key 开始位置
  85. * @param int $end_key 尾数开始位置
  86. * @param int $start_num 截取位数
  87. * @param int $end_num 倒数截取位数
  88. * @param int $conceal_num 最大填充数量
  89. * @return string
  90. */
  91. function concealStr($str, $start_key = 0, $end_key = 1, $start_num = 1, $end_num = 1, $conceal_num = 3)
  92. {
  93. $str_len = mb_strlen($str);
  94. $start_str = '';
  95. $end_str = '';
  96. if (($str_len - $start_num - $end_num) < $conceal_num) {
  97. $conceal_num = $str_len - $start_num - $end_num;
  98. }
  99. $conceal_str = $this->getRandChar($conceal_num, '*');
  100. if (empty($conceal_str)) $conceal_str = '*';
  101. if ($str_len >= $start_key) {
  102. $start_str = mb_substr($str, $start_key, $start_num);
  103. }
  104. if ($str_len > $end_key) {
  105. $end_str = mb_substr($str, $end_key * -1, $end_num);
  106. }
  107. return $start_str . $conceal_str . $end_str;
  108. }
  109. /**
  110. * 获取随机字符串
  111. * @param int $length
  112. * @param string $strPol
  113. * @return string|null
  114. */
  115. public function getRandChar($length = 6, $strPol = '')
  116. {
  117. if ($length <= 0) $length = 1;
  118. $str = null;
  119. if (empty($strPol)) $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  120. $max = strlen($strPol) - 1;
  121. for ($i = 0; $i < $length; $i++) {
  122. $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
  123. }
  124. return $str;
  125. }
  126. /**
  127. * 获取数据库配置信息
  128. * @param $key
  129. * @return mixed
  130. */
  131. function getConfigValue($key)
  132. {
  133. return Config::where('key', $key)->value('value');
  134. }
  135. /**
  136. * 创建文件夹
  137. * @param $dir
  138. * @return bool
  139. */
  140. function creatDir($dir)
  141. {
  142. if ($dir == '') return false;
  143. if (!file_exists(public_path($dir))) {
  144. mkdir(public_path($dir), 0777, true);
  145. chmod(public_path($dir), 0777);
  146. }
  147. return true;
  148. }
  149. /**
  150. * 验证是否微信浏览器
  151. * @return bool
  152. */
  153. function isWeixin()
  154. {
  155. $http_user_agent = request()->header('user-agent');
  156. if (strpos($http_user_agent, 'MicroMessenger') !== false) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. /**
  162. * 格式化小数位数
  163. * @param $num
  164. * @param int $digit
  165. * @return string
  166. */
  167. function setFloatNum($num, $digit = 4)
  168. {
  169. return sprintf("%.{$digit}f", $num);
  170. }
  171. /**
  172. * 获取真实IP
  173. * @return mixed
  174. */
  175. function getClientIp()
  176. {
  177. $http_x_forwarded_for = request()->header('x-forwarded-for');
  178. if (empty($http_x_forwarded_for)) {
  179. return request()->getClientIp();
  180. } else {
  181. $http_x_forwarded_for = explode(',', $http_x_forwarded_for);
  182. return $http_x_forwarded_for[0];
  183. }
  184. }
  185. function delDir($dir)
  186. {
  187. //先删除目录下的文件:
  188. $dh = opendir($dir);
  189. while ($file = readdir($dh)) {
  190. if ($file != "." && $file != "..") {
  191. $fullpath = $dir . "/" . $file;
  192. if (!is_dir($fullpath)) {
  193. unlink($fullpath);
  194. } else {
  195. $this->deldir($fullpath);
  196. }
  197. }
  198. }
  199. closedir($dh);
  200. //删除当前文件夹:
  201. if (rmdir($dir)) {
  202. return true;
  203. } else {
  204. return false;
  205. }
  206. }
  207. /**
  208. * 图片base64
  209. * @param $image_file
  210. * @return string
  211. */
  212. function base64EncodeImage($image_file)
  213. {
  214. $image_info = getimagesize($image_file);
  215. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  216. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  217. return $base64_image;
  218. }
  219. /**
  220. * 获取文件名
  221. * @param $url
  222. * @return string
  223. */
  224. function getFileName($url)
  225. {
  226. $name = basename($url);
  227. return empty($name) ? '' : $name;
  228. }
  229. /**
  230. * @param $url
  231. * @param string $type 请求方式
  232. * @param string $data 数据 数组格式
  233. * @param $head
  234. * @param $retType
  235. * @return mixed
  236. */
  237. function sendRequest($url, $data = '', $type = 'get',$head=[],$retType='json')
  238. {
  239. $ch = curl_init();
  240. if ($type == 'get' && $data) {
  241. $url = $url . '?' . http_build_query($data);
  242. }
  243. // dump(date('Y-m-d H:i:s'));
  244. // dump($url);
  245. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  246. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  247. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  248. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  249. if(empty($head)){
  250. $head = [
  251. 'User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
  252. 'Content-Type: application/json'
  253. ];
  254. }
  255. // curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  256. curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
  257. if ($type == 'post') {
  258. curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
  259. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  260. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置传递的参数
  261. }
  262. $request = curl_exec($ch);
  263. // $header_size = curl_getinfo($ch,);
  264. // dump($request);
  265. if($request){
  266. if($retType=='gzip'){
  267. $request=gzdecode($request);
  268. }
  269. $request=json_decode($request,true);
  270. }
  271. // dump($request);
  272. // dump(date('Y-m-d H:i:s'));
  273. curl_close($ch);
  274. return $request;
  275. }
  276. function lockCapitalRoutes($mId,$routeName,$type){
  277. $routeArr= [
  278. 'withdraw.add',//提现
  279. ];
  280. if(in_array($routeName,$routeArr)){
  281. $redisName=$mId.'lockCapital';
  282. if($type){
  283. $row= RedisDataServer::creatServer()->getData($redisName);
  284. $isOutput= RedisDataServer::creatServer()->getData('dateOutput');
  285. if($row || $isOutput){
  286. return false;
  287. }
  288. RedisDataServer::creatServer()->setData($redisName,time(),'str',3,false);
  289. }else{
  290. RedisDataServer::creatServer()->delData($redisName);
  291. }
  292. }
  293. return true;
  294. }
  295. function getTxySts()
  296. {
  297. $data = RedisDataServer::creatServer()->getData('txy_sts_data');
  298. if ($data) {
  299. $data = json_decode($data, true);
  300. } else {
  301. $sts = new Sts();
  302. $accessKeyId = env('TXY_OSS_ACCESS_ID');
  303. $accessSecret = env('TXY_OSS_ACCESS_KEY');
  304. $config = array(
  305. 'url' => 'https://sts.tencentcloudapi.com/', // url和domain保持一致
  306. 'domain' => 'sts.tencentcloudapi.com', // 域名,非必须,默认为 sts.tencentcloudapi.com
  307. 'proxy' => '',
  308. 'secretId' => $accessKeyId, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
  309. 'secretKey' => $accessSecret, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
  310. 'bucket' => env('COSV5_BUCKET'), // 换成你的 bucket
  311. 'region' => env('COSV5_REGION'), // 换成 bucket 所在园区
  312. 'durationSeconds' => 1800, // 密钥有效期
  313. 'allowPrefix' => ['/'], // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
  314. // 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
  315. 'allowActions' => array(
  316. // 简单上传
  317. 'name/cos:PutObject',
  318. 'name/cos:PostObject',
  319. // 分片上传
  320. 'name/cos:InitiateMultipartUpload',
  321. 'name/cos:ListMultipartUploads',
  322. 'name/cos:ListParts',
  323. 'name/cos:UploadPart',
  324. 'name/cos:CompleteMultipartUpload'
  325. )
  326. );
  327. try {
  328. $result = $sts->getTempKeys($config);
  329. if (empty($result['credentials'])) {
  330. return ['code' => 0, 'msg' => '权限生成失败'];
  331. }
  332. $data = $result['credentials'];
  333. $data['StartTime'] = time();
  334. $data['ExpiredTime'] = time() + 1700;
  335. RedisDataServer::creatServer()->setData('txy_sts_data', $data, 'json', 800);
  336. } catch (\Exception $e) {
  337. return ['code' => 0, 'msg' => $e->getMessage()];
  338. }
  339. }
  340. return ['code' => 1, 'msg' => '获取信息成功', 'data' => $data];
  341. }
  342. /**
  343. * 微信签名
  344. */
  345. function wxSign($data, $keys)
  346. {
  347. ksort($data);
  348. $str = '';
  349. foreach ($data as $key => $val) {
  350. if ($val) {
  351. if ($str != '') $str .= '&';
  352. $str .= "{$key}={$val}";
  353. }
  354. }
  355. $str .= '&key=' . $keys;
  356. $sign = strtoupper(MD5($str));
  357. return $sign;
  358. }
  359. }