CommonServer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace App\Servers;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Ecs\Ecs;
  5. use AlibabaCloud\Green\Green;
  6. use App\Models\Config;
  7. use App\Models\ErrorRecord;
  8. use App\Models\Region;
  9. use Illuminate\Support\Facades\Storage;
  10. use AlibabaCloud\Client\Exception\ClientException;
  11. use AlibabaCloud\Client\Exception\ServerException;
  12. /**
  13. * Redis数据缓存类
  14. */
  15. class CommonServer
  16. {
  17. /**
  18. * 错误信息
  19. * @var string
  20. */
  21. private $errorMsg = '';
  22. static private $server = null;
  23. private function __construct()
  24. {
  25. }
  26. /**
  27. * 创建对象
  28. * @return CommonServer
  29. */
  30. static function creatServer()
  31. {
  32. if (empty(self::$server)) self::$server = new CommonServer();
  33. return self::$server;
  34. }
  35. /**
  36. * 验证手机号码格式
  37. * @param $phone
  38. * @return bool
  39. */
  40. public function verifyPhoneNumber($phone)
  41. {
  42. if (empty($phone)) return false;
  43. if (!preg_match('#^[\d]{11,11}$#', $phone)) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * 字符串去标签过滤及空格过滤
  50. * @param $str_name
  51. * @param $default
  52. * @return string
  53. */
  54. function filtrationStr($str_name, $default = '')
  55. {
  56. $str = request()->input($str_name, $default);
  57. if (empty($str)) return $str;
  58. if (!is_string($str)) {
  59. $str = json_encode($str);
  60. $str = strip_tags($str);
  61. $str = trim($str);
  62. $str = json_decode($str, true);
  63. } else {
  64. $str = strip_tags($str);
  65. $str = trim($str);
  66. }
  67. return $str;
  68. }
  69. /**
  70. * 获取城市名称
  71. * @param string $province_id
  72. * @param string $city_id
  73. * @param string $area_id
  74. * @return string
  75. */
  76. function getCityName($province_id = '', $city_id = '', $area_id = '')
  77. {
  78. $ids = [$province_id, $city_id, $area_id];
  79. $ids = array_filter($ids);
  80. if (empty($ids)) return '';
  81. $city_name = Region::whereIn('id', $ids)->orderBy('id', 'asc')->pluck('name')->toArray();
  82. $city_name = implode(' ', $city_name);
  83. return $city_name;
  84. }
  85. /**
  86. * 字符串屏蔽
  87. * @param $str
  88. * @param int $start_key 开始位置
  89. * @param int $end_key 尾数开始位置
  90. * @param int $start_num 截取位数
  91. * @param int $end_num 倒数截取位数
  92. * @param int $conceal_num 最大填充数量
  93. * @return string
  94. */
  95. function concealStr($str, $start_key = 0, $end_key = 1, $start_num = 1, $end_num = 1, $conceal_num = 3)
  96. {
  97. $str_len = mb_strlen($str);
  98. $start_str = '';
  99. $end_str = '';
  100. if (($str_len - $start_num - $end_num) < $conceal_num) {
  101. $conceal_num = $str_len - $start_num - $end_num;
  102. }
  103. $conceal_str = $this->getRandChar($conceal_num, '*');
  104. if (empty($conceal_str)) $conceal_str = '*';
  105. if ($str_len >= $start_key) {
  106. $start_str = mb_substr($str, $start_key, $start_num);
  107. }
  108. if ($str_len > $end_key) {
  109. $end_str = mb_substr($str, $end_key * -1, $end_num);
  110. }
  111. return $start_str . $conceal_str . $end_str;
  112. }
  113. /**
  114. * 获取随机字符串
  115. * @param int $length
  116. * @param string $strPol
  117. * @return string|null
  118. */
  119. public function getRandChar($length = 6, $strPol = '')
  120. {
  121. if ($length <= 0) $length = 1;
  122. $str = null;
  123. if (empty($strPol)) $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  124. $max = strlen($strPol) - 1;
  125. for ($i = 0; $i < $length; $i++) {
  126. $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
  127. }
  128. return $str;
  129. }
  130. /**
  131. * 获取数据库配置信息
  132. * @param $key
  133. * @return mixed
  134. */
  135. function getConfigValue($key)
  136. {
  137. $key=strtolower($key);
  138. if(in_array($key,['sys_address','sys_private','receive_address','common_address','sediment_address','common_private'])){
  139. $value=env(strtoupper($key));
  140. if($value){
  141. return $value;
  142. }
  143. }
  144. return Config::where('key', $key)->value('value');
  145. }
  146. /**
  147. * 创建文件夹
  148. * @param $dir
  149. * @return bool
  150. */
  151. function creatDir($dir)
  152. {
  153. if ($dir == '') return false;
  154. if (!file_exists(public_path($dir))) {
  155. mkdir(public_path($dir), 0777, true);
  156. chmod(public_path($dir), 0777);
  157. }
  158. return true;
  159. }
  160. /**
  161. * 获取本月1号至月底
  162. * @param $date
  163. * @return array
  164. */
  165. function getNextMonth($date)
  166. {
  167. $first_day = date("Y-m-01 00:00:00", strtotime($date));
  168. $next_day = date("Y-m-d 23:59:59", strtotime("$first_day +1 month -1 day"));
  169. return [$first_day, $next_day];
  170. }
  171. /**
  172. * 获取多少天后的日期
  173. * @param $date
  174. * @param $num
  175. * @return false|string
  176. */
  177. function getManyDate($date, $num)
  178. {
  179. if(!is_numeric($num) || $num==0){
  180. return $date;
  181. }
  182. if($num>0){
  183. $next_day = date("Y-m-d", strtotime("$date +$num day"));
  184. }else{
  185. $next_day = date("Y-m-d", strtotime("$date $num day"));
  186. }
  187. return $next_day;
  188. }
  189. /**
  190. * 验证是否微信浏览器
  191. * @return bool
  192. */
  193. function isWeixin()
  194. {
  195. $http_user_agent = request()->header('user-agent');
  196. if (strpos($http_user_agent, 'MicroMessenger') !== false) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * 格式化小数位数
  203. * @param $num
  204. * @param int $digit
  205. * @return string
  206. */
  207. function setFloatNum($num, $digit = 4)
  208. {
  209. return sprintf("%.{$digit}f", $num);
  210. }
  211. /**
  212. * 获取真实IP
  213. * @return mixed
  214. */
  215. function getClientIp()
  216. {
  217. $http_x_forwarded_for = request()->header('x-forwarded-for');
  218. if (empty($http_x_forwarded_for)) {
  219. return request()->getClientIp();
  220. } else {
  221. $http_x_forwarded_for = explode(',', $http_x_forwarded_for);
  222. return $http_x_forwarded_for[0];
  223. }
  224. }
  225. function delDir($dir)
  226. {
  227. //先删除目录下的文件:
  228. $dh = opendir($dir);
  229. while ($file = readdir($dh)) {
  230. if ($file != "." && $file != "..") {
  231. $fullpath = $dir . "/" . $file;
  232. if (!is_dir($fullpath)) {
  233. unlink($fullpath);
  234. } else {
  235. $this->deldir($fullpath);
  236. }
  237. }
  238. }
  239. closedir($dh);
  240. //删除当前文件夹:
  241. if (rmdir($dir)) {
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. /**
  248. * 图片base64
  249. * @param $image_file
  250. * @return string
  251. */
  252. function base64EncodeImage($image_file)
  253. {
  254. $image_info = getimagesize($image_file);
  255. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  256. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  257. return $base64_image;
  258. }
  259. /**
  260. * 保留2为小数
  261. * @param $num
  262. * @param int $format_num
  263. * @return float|int
  264. */
  265. function setFormatNum($num,$format_num=2){
  266. // return $num;
  267. $format_num=4;
  268. if(!is_numeric($num)){
  269. return 0;
  270. }
  271. if($format_num<0){
  272. return $num;
  273. }elseif ($format_num==0){
  274. return floor($num);
  275. }else{
  276. $num= sprintf("%.{$format_num}f", $num);
  277. // $multiple=pow(10,$format_num);
  278. // $num = floor($num*$multiple)/$multiple;
  279. }
  280. $num=$num*1;
  281. return $num;
  282. }
  283. /**
  284. * 获取文件名
  285. * @param $url
  286. * @return string
  287. */
  288. function getFileName($url)
  289. {
  290. $name = basename($url);
  291. return empty($name) ? '' : $name;
  292. }
  293. /**
  294. * 身份证号码验证
  295. * @param $id
  296. * @return bool
  297. */
  298. public static function isIdcard($id)
  299. {
  300. $id = strtoupper($id);
  301. $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  302. $arr_split = array();
  303. if (!preg_match($regx, $id)) {
  304. return FALSE;
  305. }
  306. if (15 == strlen($id)) //检查15位
  307. {
  308. $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  309. @preg_match($regx, $id, $arr_split);
  310. //检查生日日期是否正确
  311. $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  312. if (!strtotime($dtm_birth)) {
  313. return FALSE;
  314. } else {
  315. return TRUE;
  316. }
  317. } else //检查18位
  318. {
  319. $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
  320. @preg_match($regx, $id, $arr_split);
  321. $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  322. if (!strtotime($dtm_birth)) //检查生日日期是否正确
  323. {
  324. return FALSE;
  325. } else {
  326. //检验18位身份证的校验码是否正确。
  327. //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  328. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  329. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  330. $sign = 0;
  331. for ($i = 0; $i < 17; $i++) {
  332. $b = (int)$id[$i];
  333. $w = $arr_int[$i];
  334. $sign += $b * $w;
  335. }
  336. $n = $sign % 11;
  337. $val_num = $arr_ch[$n];
  338. if ($val_num != substr($id, 17, 1)) {
  339. return FALSE;
  340. }
  341. else {
  342. return TRUE;
  343. }
  344. }
  345. }
  346. }
  347. /**
  348. * 记录错误信息
  349. * @param $msg
  350. * @param array $data
  351. * @param int $mId
  352. */
  353. function addErrorRecord($msg,$data=[],$mId=0){
  354. ErrorRecord::create([
  355. 'm_id' => $mId,
  356. 'msg' => $msg,
  357. 'data' => json_encode($data)
  358. ]);
  359. }
  360. }