Service.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\OperationLog;
  4. use App\Model\Employee;
  5. use Illuminate\Support\Facades\Redis;
  6. /**
  7. * 公用的公共服务
  8. * @package App\Models
  9. */
  10. class Service
  11. {
  12. public $log;
  13. public $total = 0;
  14. public function __construct()
  15. {
  16. }
  17. //分页共用
  18. public function limit($db, $columns, $request)
  19. {
  20. $per_page = $request['page_size'] ?? 9999;
  21. $page = $request['page_index'] ?? 1;
  22. $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
  23. $data['total'] = $return['total'];
  24. $data['data'] = $return['data'];
  25. return $data;
  26. }
  27. //抽象一个查询条件,省的每天写很多行
  28. protected function is_exist_db($data, $word, $words, $db, $formula = '=', $type = '')
  29. {
  30. if (isset($data[$word]) && ($data[$word] !== null && $data[$word] !== '' && $data[$word] !== [])) {
  31. switch ($type) {
  32. case 'time':
  33. $data[$word] = ctype_digit($data[$word]) ? $data[$word] : strtotime($data[$word]);
  34. if ($formula == '<'||$formula == '<=') $data[$word] += 86400;
  35. $db = $db->where($words, $formula, $data[$word]);
  36. break;
  37. case 'like':
  38. $db = $db->where($words, $type, '%' . $data[$word] . '%');
  39. break;
  40. case 'in':
  41. if (is_array($data[$word])) {
  42. $db = $db->wherein($words, $data[$word]);
  43. } else {
  44. $db = $db->wherein($words, explode(',', $data[$word]));
  45. }
  46. break;
  47. default:
  48. $db = $db->where($words, $formula, $data[$word]);
  49. break;
  50. }
  51. return $db;
  52. }
  53. return $db;
  54. }
  55. //判断是否为空
  56. protected function isEmpty($data, $word)
  57. {
  58. if (isset($data[$word]) && (!empty($data[$word]) || $data[$word] === '0'|| $data[$word] === 0)) return false;
  59. return true;
  60. }
  61. //递归处理数据成子父级关系
  62. public function makeTree($parentId, &$node)
  63. {
  64. $tree = [];
  65. foreach ($node as $key => $value) {
  66. if ($value['parent_id'] == $parentId) {
  67. $tree[$value['id']] = $value;
  68. unset($node[$key]);
  69. if (isset($tree[$value['id']]['children'])) {
  70. $tree[$value['id']]['children'] = array_merge($tree[$value['id']]['children'], $this->makeTree($value['id'], $node));
  71. } else {
  72. $tree[$value['id']]['children'] = $this->makeTree($value['id'], $node);
  73. }
  74. }
  75. }
  76. return $tree;
  77. }
  78. //进行递归处理数组的排序问题
  79. public function set_sort_circle($data){
  80. foreach ($data as $k=>$v){
  81. // var_dump($v);die;
  82. if(isset($v['children'])&&!empty($v['children'])){
  83. $data[$k]['children'] = $this->set_sort_circle($v['children']);
  84. }
  85. }
  86. $data = array_merge($data);
  87. return $data;
  88. }
  89. //根据编码规则获取每一级的code
  90. public function get_rule_code($code, $rule)
  91. {
  92. $rules = [];
  93. $num = 0;
  94. foreach ($rule as $v) {
  95. $num += $v['num'];
  96. $code = substr($code, 0, $num);
  97. if (strlen($code) != $num) continue;
  98. $rules[] = $code;
  99. }
  100. return $rules;
  101. }
  102. function curlOpen($url, $config = array())
  103. {
  104. $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 100, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>true,'isupfile'=>false,'returnheader'=>false,'request'=>'post');
  105. $arr = array_merge($arr, $config);
  106. $ch = curl_init();
  107. curl_setopt($ch, CURLOPT_URL, $url);
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
  109. curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
  110. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  111. curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
  112. curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
  113. curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
  114. curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);//��ȡheader
  115. if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  116. if($arr['ssl'])
  117. {
  118. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  120. }
  121. if(!empty($arr['cookie']))
  122. {
  123. curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
  124. curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
  125. }
  126. if(!empty($arr['proxy']))
  127. {
  128. //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  129. curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
  130. if(!empty($arr['userpwd']))
  131. {
  132. curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
  133. }
  134. }
  135. //ip�Ƚ����⣬�ü�ֵ��ʾ
  136. if(!empty($arr['header']['ip']))
  137. {
  138. array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
  139. unset($arr['header']['ip']);
  140. }
  141. $arr['header'] = array_filter($arr['header']);
  142. if(!empty($arr['header']))
  143. {
  144. curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
  145. }
  146. if($arr['request'] === 'put'){
  147. curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  148. curl_setopt($ch, CURLOPT_POSTFIELDS,$arr['post']);
  149. }elseif($arr['post'] != false)
  150. {
  151. curl_setopt($ch, CURLOPT_POST, true);
  152. if(is_array($arr['post']) && $arr['isupfile'] === false)
  153. {
  154. $post = http_build_query($arr['post']);
  155. }
  156. else
  157. {
  158. $post = $arr['post'];
  159. }
  160. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  161. }
  162. $result = curl_exec($ch);
  163. curl_close($ch);
  164. return $result;
  165. }
  166. function getAllIdsArr($data, $pid = 0, $prefix = '', &$result = []) {
  167. foreach ($data as $node) {
  168. if ($node['parent_id'] == $pid) {
  169. $id = $prefix . $node['id'];
  170. $result[] = $id;
  171. $this->getAllIdsArr($data, $node['id'], $id . ',', $result);
  172. }
  173. }
  174. return $result;
  175. }
  176. function getLongestStr($arr = [], $searchStr){
  177. if(empty($arr) || ! $searchStr) return '';
  178. if(! is_string($searchStr)) $searchStr = (string)$searchStr;
  179. $longest = '';
  180. foreach ($arr as $str) {
  181. if (strpos($str, $searchStr) !== false && strlen($str) > strlen($longest)) {
  182. $longest = $str;
  183. }
  184. }
  185. return $longest;
  186. }
  187. function getAllDescendants($data, $id) {
  188. $result = array(); // 存储结果的数组
  189. foreach ($data as $node) {
  190. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  191. $result[] = $node['id'];
  192. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  193. $result = array_merge($result, $this->getAllDescendants($data, $node['id']));
  194. }
  195. }
  196. return $result;
  197. }
  198. //后台端 某些需要限制请求频率的接口
  199. //需要主动删除 Redis::del($key)
  200. public function limitingSendRequestBackg($key){
  201. // 使用Redis Facade设置,当键名不存在时才设置成功
  202. if (Redis::setnx($key, 1)) return [true, ''];
  203. return [false,'操作频繁!'];
  204. }
  205. public function dellimitingSendRequestBackg($key){
  206. Redis::del($key);
  207. }
  208. //后台端 某些需要限制请求频率的接口 有过期时间
  209. public function limitingSendRequestBackgExpire($key,$ttl = 5){
  210. if($ttl < 5) $ttl = 5;
  211. // 使用Redis Facade设置,当键名不存在时才设置成功
  212. if (Redis::setnx($key, 1)) {
  213. Redis::expire($key, $ttl); //多少秒后过期
  214. return [true, ''];
  215. }
  216. return [false,'操作频繁, 请在 ' . $ttl . '秒后重试'];
  217. }
  218. //前端传来的时间段 转换为时间戳
  219. //精确到秒
  220. function changeDateToTimeStampAboutRange($time_range){
  221. if(empty($time_range[0]) || empty($time_range[1])) return [];
  222. // 创建一个 DateTime 对象并设置时区为 UTC
  223. $dateTime = new \DateTime($time_range[0], new \DateTimeZone('UTC'));
  224. $dateTime1 = new \DateTime($time_range[1], new \DateTimeZone('UTC'));
  225. // 将时区设置为 PRC
  226. $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
  227. $dateTime1->setTimezone(new \DateTimeZone('Asia/Shanghai'));
  228. // 将日期时间格式化为特定格式
  229. $formattedDate = $dateTime->format('Y-m-d');
  230. $formattedDate1 = $dateTime1->format('Y-m-d');
  231. $return = [];
  232. $return[] = strtotime($formattedDate . " 00:00:00");
  233. $return[] = strtotime($formattedDate1 . " 23:59:59");
  234. return $return;
  235. }
  236. //前端传来的时间段 转换为时间戳
  237. //精确到日
  238. function changeDateToNewDate($time_range){
  239. if(empty($time_range[0]) || empty($time_range[1])) return [];
  240. // 创建一个 DateTime 对象并设置时区为 UTC
  241. $dateTime = new \DateTime($time_range[0], new \DateTimeZone('UTC'));
  242. $dateTime1 = new \DateTime($time_range[1], new \DateTimeZone('UTC'));
  243. // 将时区设置为 PRC
  244. $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
  245. $dateTime1->setTimezone(new \DateTimeZone('Asia/Shanghai'));
  246. // 将日期时间格式化为特定格式
  247. $formattedDate = $dateTime->format('Y-m-d');
  248. $formattedDate1 = $dateTime1->format('Y-m-d');
  249. $return = [];
  250. $return[] = strtotime($formattedDate);
  251. $return[] = strtotime($formattedDate1);
  252. return $return;
  253. }
  254. //前端传来的时间 转为时间戳
  255. //精确到分
  256. function changeDateToDateMin($time){
  257. if(empty($time)) return '';
  258. // 创建一个 DateTime 对象并设置时区为 UTC
  259. $dateTime = new \DateTime($time, new \DateTimeZone('UTC'));
  260. // 将时区设置为 PRC
  261. $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
  262. // 将日期时间格式化为特定格式
  263. $formattedDate = $dateTime->format('Y-m-d H:i');
  264. return strtotime($formattedDate);
  265. }
  266. /**
  267. * 用于用户行为操作日志记录
  268. * @param $insert
  269. * @return void
  270. */
  271. public function insertLog($insert){
  272. OperationLog::dispatch($insert);
  273. }
  274. public function checkNumber($number, $decimals = 2){
  275. if(! is_numeric($number) || $number < 0) return false;
  276. $formattedNumber = number_format($number, $decimals, '.', '');
  277. if($formattedNumber != $number) return false;
  278. return true;
  279. }
  280. public function getDepart($user){
  281. if(empty($user)) return 0;
  282. $depart = array_shift($user['rule_depart']);
  283. return $depart['depart_id'];
  284. }
  285. public function getTopId($id, $data) {
  286. foreach ($data as $item) {
  287. if ($item['id'] == $id) {
  288. if ($item['parent_id'] == 0) {
  289. // 找到最顶级的id
  290. return $item['id'];
  291. } else {
  292. // 继续递归查找父级
  293. return $this->getTopId($item['parent_id'], $data);
  294. }
  295. }
  296. }
  297. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  298. return 0;
  299. }
  300. // 递归查找所有父级id
  301. public function findParentIds($id, $data) {
  302. $parentIds = [];
  303. foreach ($data as $item) {
  304. if ($item['id'] == $id) {
  305. $parentId = $item['parent_id'];
  306. if ($parentId) {
  307. $parentIds[] = $parentId;
  308. $parentIds = array_merge($parentIds, $this->findParentIds($parentId, $data));
  309. }
  310. break;
  311. }
  312. }
  313. return $parentIds;
  314. }
  315. // 递归查找所有子级id
  316. public function findChildIds($id, $data) {
  317. $childIds = [];
  318. foreach ($data as $item) {
  319. if ($item['parent_id'] == $id) {
  320. $childId = $item['id'];
  321. $childIds[] = $childId;
  322. $childIds = array_merge($childIds, $this->findChildIds($childId, $data));
  323. }
  324. }
  325. return $childIds;
  326. }
  327. public function getDataFile($data){
  328. foreach ($data as $key => $value){
  329. if($key == 'depart_id' || $key == 'top_depart_id') unset($data[$key]);
  330. }
  331. return $data;
  332. }
  333. }