CommonServer.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. return Config::where('key', $key)->value('value');
  138. }
  139. /**
  140. * 创建文件夹
  141. * @param $dir
  142. * @return bool
  143. */
  144. function creatDir($dir)
  145. {
  146. if ($dir == '') return false;
  147. if (!file_exists(public_path($dir))) {
  148. mkdir(public_path($dir), 0777, true);
  149. chmod(public_path($dir), 0777);
  150. }
  151. return true;
  152. }
  153. /**
  154. * 获取本月1号至月底
  155. * @param $date
  156. * @return array
  157. */
  158. function getNextMonth($date)
  159. {
  160. $first_day = date("Y-m-01 00:00:00", strtotime($date));
  161. $next_day = date("Y-m-d 23:59:59", strtotime("$first_day +1 month -1 day"));
  162. return [$first_day, $next_day];
  163. }
  164. /**
  165. * 获取多少天后的日期
  166. * @param $date
  167. * @param $num
  168. * @return false|string
  169. */
  170. function getManyDate($date, $num)
  171. {
  172. if(!is_numeric($num) || $num==0){
  173. return $date;
  174. }
  175. if($num>0){
  176. $next_day = date("Y-m-d", strtotime("$date +$num day"));
  177. }else{
  178. $next_day = date("Y-m-d", strtotime("$date $num day"));
  179. }
  180. return $next_day;
  181. }
  182. /**
  183. * 验证是否微信浏览器
  184. * @return bool
  185. */
  186. function isWeixin()
  187. {
  188. $http_user_agent = request()->header('user-agent');
  189. if (strpos($http_user_agent, 'MicroMessenger') !== false) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. /**
  195. * 格式化小数位数
  196. * @param $num
  197. * @param int $digit
  198. * @return string
  199. */
  200. function setFloatNum($num, $digit = 4)
  201. {
  202. return sprintf("%.{$digit}f", $num);
  203. }
  204. /**
  205. * 获取真实IP
  206. * @return mixed
  207. */
  208. function getClientIp()
  209. {
  210. $http_x_forwarded_for = request()->header('x-forwarded-for');
  211. if (empty($http_x_forwarded_for)) {
  212. return request()->getClientIp();
  213. } else {
  214. $http_x_forwarded_for = explode(',', $http_x_forwarded_for);
  215. return $http_x_forwarded_for[0];
  216. }
  217. }
  218. function delDir($dir)
  219. {
  220. //先删除目录下的文件:
  221. $dh = opendir($dir);
  222. while ($file = readdir($dh)) {
  223. if ($file != "." && $file != "..") {
  224. $fullpath = $dir . "/" . $file;
  225. if (!is_dir($fullpath)) {
  226. unlink($fullpath);
  227. } else {
  228. $this->deldir($fullpath);
  229. }
  230. }
  231. }
  232. closedir($dh);
  233. //删除当前文件夹:
  234. if (rmdir($dir)) {
  235. return true;
  236. } else {
  237. return false;
  238. }
  239. }
  240. /**
  241. * 图片base64
  242. * @param $image_file
  243. * @return string
  244. */
  245. function base64EncodeImage($image_file)
  246. {
  247. $image_info = getimagesize($image_file);
  248. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  249. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  250. return $base64_image;
  251. }
  252. /**
  253. * 保留2为小数
  254. * @param $num
  255. * @param int $format_num
  256. * @return float|int
  257. */
  258. function setFormatNum($num,$format_num=2){
  259. // return $num;
  260. $format_num=4;
  261. if(!is_numeric($num)){
  262. return 0;
  263. }
  264. if($format_num<0){
  265. return $num;
  266. }elseif ($format_num==0){
  267. return floor($num);
  268. }else{
  269. $num= sprintf("%.{$format_num}f", $num);
  270. // $multiple=pow(10,$format_num);
  271. // $num = floor($num*$multiple)/$multiple;
  272. }
  273. $num=$num*1;
  274. return $num;
  275. }
  276. /**
  277. * 获取文件名
  278. * @param $url
  279. * @return string
  280. */
  281. function getFileName($url)
  282. {
  283. $name = basename($url);
  284. return empty($name) ? '' : $name;
  285. }
  286. /**
  287. * 身份证号码验证
  288. * @param $id
  289. * @return bool
  290. */
  291. public static function isIdcard($id)
  292. {
  293. $id = strtoupper($id);
  294. $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  295. $arr_split = array();
  296. if (!preg_match($regx, $id)) {
  297. return FALSE;
  298. }
  299. if (15 == strlen($id)) //检查15位
  300. {
  301. $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  302. @preg_match($regx, $id, $arr_split);
  303. //检查生日日期是否正确
  304. $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  305. if (!strtotime($dtm_birth)) {
  306. return FALSE;
  307. } else {
  308. return TRUE;
  309. }
  310. } else //检查18位
  311. {
  312. $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
  313. @preg_match($regx, $id, $arr_split);
  314. $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  315. if (!strtotime($dtm_birth)) //检查生日日期是否正确
  316. {
  317. return FALSE;
  318. } else {
  319. //检验18位身份证的校验码是否正确。
  320. //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  321. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  322. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  323. $sign = 0;
  324. for ($i = 0; $i < 17; $i++) {
  325. $b = (int)$id[$i];
  326. $w = $arr_int[$i];
  327. $sign += $b * $w;
  328. }
  329. $n = $sign % 11;
  330. $val_num = $arr_ch[$n];
  331. if ($val_num != substr($id, 17, 1)) {
  332. return FALSE;
  333. }
  334. else {
  335. return TRUE;
  336. }
  337. }
  338. }
  339. }
  340. /**
  341. * 记录错误信息
  342. * @param $msg
  343. * @param array $data
  344. * @param int $mId
  345. */
  346. function addErrorRecord($msg,$data=[],$mId=0){
  347. ErrorRecord::create([
  348. 'm_id' => $mId,
  349. 'msg' => $msg,
  350. 'data' => json_encode($data)
  351. ]);
  352. }
  353. }