123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?php
- namespace App\Service;
- use App\Jobs\OperationLog;
- use App\Model\Employee;
- use Illuminate\Support\Facades\Redis;
- /**
- * 公用的公共服务
- * @package App\Models
- */
- class Service
- {
- public $log;
- public $total = 0;
- public function __construct()
- {
- }
- //分页共用
- public function limit($db, $columns, $request)
- {
- $per_page = $request['page_size'] ?? 9999;
- $page = $request['page_index'] ?? 1;
- $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
- $data['total'] = $return['total'];
- $data['data'] = $return['data'];
- return $data;
- }
- //抽象一个查询条件,省的每天写很多行
- protected function is_exist_db($data, $word, $words, $db, $formula = '=', $type = '')
- {
- if (isset($data[$word]) && ($data[$word] !== null && $data[$word] !== '' && $data[$word] !== [])) {
- switch ($type) {
- case 'time':
- $data[$word] = ctype_digit($data[$word]) ? $data[$word] : strtotime($data[$word]);
- if ($formula == '<'||$formula == '<=') $data[$word] += 86400;
- $db = $db->where($words, $formula, $data[$word]);
- break;
- case 'like':
- $db = $db->where($words, $type, '%' . $data[$word] . '%');
- break;
- case 'in':
- if (is_array($data[$word])) {
- $db = $db->wherein($words, $data[$word]);
- } else {
- $db = $db->wherein($words, explode(',', $data[$word]));
- }
- break;
- default:
- $db = $db->where($words, $formula, $data[$word]);
- break;
- }
- return $db;
- }
- return $db;
- }
- //判断是否为空
- protected function isEmpty($data, $word)
- {
- if (isset($data[$word]) && (!empty($data[$word]) || $data[$word] === '0'|| $data[$word] === 0)) return false;
- return true;
- }
- //递归处理数据成子父级关系
- public function makeTree($parentId, &$node)
- {
- $tree = [];
- foreach ($node as $key => $value) {
- if ($value['parent_id'] == $parentId) {
- $tree[$value['id']] = $value;
- unset($node[$key]);
- if (isset($tree[$value['id']]['children'])) {
- $tree[$value['id']]['children'] = array_merge($tree[$value['id']]['children'], $this->makeTree($value['id'], $node));
- } else {
- $tree[$value['id']]['children'] = $this->makeTree($value['id'], $node);
- }
- }
- }
- return $tree;
- }
- //进行递归处理数组的排序问题
- public function set_sort_circle($data){
- foreach ($data as $k=>$v){
- // var_dump($v);die;
- if(isset($v['children'])&&!empty($v['children'])){
- $data[$k]['children'] = $this->set_sort_circle($v['children']);
- }
- }
- $data = array_merge($data);
- return $data;
- }
- //根据编码规则获取每一级的code
- public function get_rule_code($code, $rule)
- {
- $rules = [];
- $num = 0;
- foreach ($rule as $v) {
- $num += $v['num'];
- $code = substr($code, 0, $num);
- if (strlen($code) != $num) continue;
- $rules[] = $code;
- }
- return $rules;
- }
- function curlOpen($url, $config = array())
- {
- $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');
- $arr = array_merge($arr, $config);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
- curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
- curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
- curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
- curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);//��ȡheader
- if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
- if($arr['ssl'])
- {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- if(!empty($arr['cookie']))
- {
- curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
- }
- if(!empty($arr['proxy']))
- {
- //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
- if(!empty($arr['userpwd']))
- {
- curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
- }
- }
- //ip�Ƚ����⣬�ü�ֵ��ʾ
- if(!empty($arr['header']['ip']))
- {
- array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
- unset($arr['header']['ip']);
- }
- $arr['header'] = array_filter($arr['header']);
- if(!empty($arr['header']))
- {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
- }
- if($arr['request'] === 'put'){
- curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($ch, CURLOPT_POSTFIELDS,$arr['post']);
- }elseif($arr['post'] != false)
- {
- curl_setopt($ch, CURLOPT_POST, true);
- if(is_array($arr['post']) && $arr['isupfile'] === false)
- {
- $post = http_build_query($arr['post']);
- }
- else
- {
- $post = $arr['post'];
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- }
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- function getAllIdsArr($data, $pid = 0, $prefix = '', &$result = []) {
- foreach ($data as $node) {
- if ($node['parent_id'] == $pid) {
- $id = $prefix . $node['id'];
- $result[] = $id;
- $this->getAllIdsArr($data, $node['id'], $id . ',', $result);
- }
- }
- return $result;
- }
- function getLongestStr($arr = [], $searchStr){
- if(empty($arr) || ! $searchStr) return '';
- if(! is_string($searchStr)) $searchStr = (string)$searchStr;
- $longest = '';
- foreach ($arr as $str) {
- if (strpos($str, $searchStr) !== false && strlen($str) > strlen($longest)) {
- $longest = $str;
- }
- }
- return $longest;
- }
- function getAllDescendants($data, $id) {
- $result = array(); // 存储结果的数组
- foreach ($data as $node) {
- if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
- $result[] = $node['id'];
- // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
- $result = array_merge($result, $this->getAllDescendants($data, $node['id']));
- }
- }
- return $result;
- }
- //后台端 某些需要限制请求频率的接口
- //需要主动删除 Redis::del($key)
- public function limitingSendRequestBackg($key){
- // 使用Redis Facade设置,当键名不存在时才设置成功
- if (Redis::setnx($key, 1)) return [true, ''];
- return [false,'操作频繁!'];
- }
- public function dellimitingSendRequestBackg($key){
- Redis::del($key);
- }
- //后台端 某些需要限制请求频率的接口 有过期时间
- public function limitingSendRequestBackgExpire($key,$ttl = 5){
- if($ttl < 5) $ttl = 5;
- // 使用Redis Facade设置,当键名不存在时才设置成功
- if (Redis::setnx($key, 1)) {
- Redis::expire($key, $ttl); //多少秒后过期
- return [true, ''];
- }
- return [false,'操作频繁, 请在 ' . $ttl . '秒后重试'];
- }
- //前端传来的时间段 转换为时间戳
- //精确到秒
- function changeDateToTimeStampAboutRange($time_range){
- if(empty($time_range[0]) || empty($time_range[1])) return [];
- // 创建一个 DateTime 对象并设置时区为 UTC
- $dateTime = new \DateTime($time_range[0], new \DateTimeZone('UTC'));
- $dateTime1 = new \DateTime($time_range[1], new \DateTimeZone('UTC'));
- // 将时区设置为 PRC
- $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
- $dateTime1->setTimezone(new \DateTimeZone('Asia/Shanghai'));
- // 将日期时间格式化为特定格式
- $formattedDate = $dateTime->format('Y-m-d');
- $formattedDate1 = $dateTime1->format('Y-m-d');
- $return = [];
- $return[] = strtotime($formattedDate . " 00:00:00");
- $return[] = strtotime($formattedDate1 . " 23:59:59");
- return $return;
- }
- //前端传来的时间段 转换为时间戳
- //精确到日
- function changeDateToNewDate($time_range){
- if(empty($time_range[0]) || empty($time_range[1])) return [];
- // 创建一个 DateTime 对象并设置时区为 UTC
- $dateTime = new \DateTime($time_range[0], new \DateTimeZone('UTC'));
- $dateTime1 = new \DateTime($time_range[1], new \DateTimeZone('UTC'));
- // 将时区设置为 PRC
- $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
- $dateTime1->setTimezone(new \DateTimeZone('Asia/Shanghai'));
- // 将日期时间格式化为特定格式
- $formattedDate = $dateTime->format('Y-m-d');
- $formattedDate1 = $dateTime1->format('Y-m-d');
- $return = [];
- $return[] = strtotime($formattedDate);
- $return[] = strtotime($formattedDate1);
- return $return;
- }
- //前端传来的时间 转为时间戳
- //精确到分
- function changeDateToDateMin($time){
- if(empty($time)) return '';
- // 创建一个 DateTime 对象并设置时区为 UTC
- $dateTime = new \DateTime($time, new \DateTimeZone('UTC'));
- // 将时区设置为 PRC
- $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
- // 将日期时间格式化为特定格式
- $formattedDate = $dateTime->format('Y-m-d H:i');
- return strtotime($formattedDate);
- }
- /**
- * 用于用户行为操作日志记录
- * @param $insert
- * @return void
- */
- public function insertLog($insert){
- OperationLog::dispatch($insert);
- }
- public function checkNumber($number, $decimals = 2){
- if(! is_numeric($number) || $number < 0) return false;
- $formattedNumber = number_format($number, $decimals, '.', '');
- if($formattedNumber != $number) return false;
- return true;
- }
- public function getDepart($user){
- if(empty($user)) return 0;
- $depart = array_shift($user['rule_depart']);
- return $depart['depart_id'];
- }
- public function getTopId($id, $data) {
- foreach ($data as $item) {
- if ($item['id'] == $id) {
- if ($item['parent_id'] == 0) {
- // 找到最顶级的id
- return $item['id'];
- } else {
- // 继续递归查找父级
- return $this->getTopId($item['parent_id'], $data);
- }
- }
- }
- // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
- return 0;
- }
- // 递归查找所有父级id
- public function findParentIds($id, $data) {
- $parentIds = [];
- foreach ($data as $item) {
- if ($item['id'] == $id) {
- $parentId = $item['parent_id'];
- if ($parentId) {
- $parentIds[] = $parentId;
- $parentIds = array_merge($parentIds, $this->findParentIds($parentId, $data));
- }
- break;
- }
- }
- return $parentIds;
- }
- // 递归查找所有子级id
- public function findChildIds($id, $data) {
- $childIds = [];
- foreach ($data as $item) {
- if ($item['parent_id'] == $id) {
- $childId = $item['id'];
- $childIds[] = $childId;
- $childIds = array_merge($childIds, $this->findChildIds($childId, $data));
- }
- }
- return $childIds;
- }
- public function getDataFile($data){
- foreach ($data as $key => $value){
- if($key == 'depart_id' || $key == 'top_depart_id') unset($data[$key]);
- }
- return $data;
- }
- }
|