<?php function apiReturn($errCode = 0, $errMsg = 'ok', $data = []) { $data = [ 'err_code' => $errCode, 'err_msg' => $errMsg, 'code' => $errCode, 'msg' => $errMsg, ]; if (!empty($data['total'])) { $data['count'] = $data['total']; } if (!empty($data)) { foreach ($data as $k => $v) { $data[$k] = $v; } } return response()->json($data, 200); } function curl($url, $params = false, $isPost = 0, $https = 0, $cookie = '', $timeout = 1000) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_REFERER, Config('website.data')); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 if ($isPost) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_URL, $url); } else { if ($params) { if (is_array($params)) { $params = http_build_query($params); } curl_setopt($ch, CURLOPT_URL, $url . '?' . $params); } else { curl_setopt($ch, CURLOPT_URL, $url); } } $response = curl_exec($ch); if ($response === false) { echo "cURL Error: " . curl_error($ch); return false; } curl_close($ch); return $response; } //检查权限,仅支持验证单个权限 function perm($userId, $perm1 = '') { $permArr = config('perm.perm'); $NotAuth = $permArr['notAuth']; $AdminID = $permArr['adminGroup']; if ((!in_array($perm1, $NotAuth)) && !in_array($userId, $AdminID)) {//过滤不用鉴权的方法与用户 $permID = $permArr['id']; $url = $permArr['url'] . '/' . $userId . '/' . $permID . '?perms=' . $perm1; $result = json_decode(curl($url, '', 0), true); if (!isset($result['retcode']) || $result['retcode'] !== 0 || $result['data']['perms'][$perm1] == false) { return false; } else { return true; } } else { return true; } } //根据权限生成菜单 function menu($menu, $user) { $perm = chils($menu); $perm1 = implode(',', $perm); $permArr = config('perm.perm'); $perm = $permArr['id']; $url = $permArr['url'] . '/' . $user . '/' . $perm . '?perms=' . $perm1; $result = json_decode(curl($url, '', false), true); if ($result['retcode'] === 0) { $find = $result['data']['perms']; $menu = DeleteMenu($menu, $find); if (!empty($menu) && is_array($menu)) { foreach ($menu as $k => $v) { $v = (array)$v; if ($v['title'] != 'Dashboard' && count($v['childs']) == 0 && empty($v['href'])) { unset($menu[$k]); } } } return array_merge($menu); } else { return []; } } //提取菜单 function chils($chils) { $perms = []; foreach ($chils as $k => $v) { $perm_a = []; $perm = []; if (strlen($v['href']) > 2) { $action = explode('/', $v['href']); $perm[] = end($action); } if (count($v['childs']) > 0) { $perm_a = chils($v['childs']); } $perms = array_merge($perms, array_merge($perm, $perm_a)); } return $perms; } //删除没有权限的菜单 function DeleteMenu($menu, $find) { foreach ($menu as $k => $v) { if (strlen($v['href']) > 2) { $action = explode('/', $v['href']); $key = end($action); if (empty($find[$key]) || $find[$key] == false) { unset($menu[$k]); } } else { if (count($v['childs']) > 0) { $menu[$k]['childs'] = array_values(DeleteMenu($v['childs'], $find)); } if (!count($v['childs']) > 0 && $v['title'] != '首页') { unset($menu[$k]); }//为了删除没有下级的目录 } } return array_values($menu); } //处理iframe的Url function iframeUrl($Url) { if (strstr($Url, '?') === false) { $Url .= '?view=iframe'; } else { $Url .= '&view=iframe'; } return $Url; } //上传文件接口签名 function Autograph() { $url = Config('website.UploadUrl'); $data['k1'] = time(); $data['k2'] = MD5(MD5($data['k1']) . Config('fixed.UploadKey')); echo '<script> k1="' . $data['k1'] . '"; k2="' . $data['k2'] . '"; UploadImgUrl="' . $url . '" </script>'; } //判断是否有对应的权限 //request()->perms是CheckLogin中间件过来的 function checkPerm($perm) { $perms = request()->perms; if ($perms === null) { return true; } return in_array($perm, $perms); } //批量清楚前后空格 function BatchTrim($data) { if (!empty($data)) { foreach ($data as &$item) { $item = trim($item); } unset($item); } return $data; } //判断一个数组是否所有key都是空的 function checkArrayAllValueNull($array, $excludeField = []) { foreach ($array as $key => $value) { if (in_array($key, $excludeField)) { continue; } if (!empty($value)) { return false; } } return true; } function generateMobile($count) { $arr = [ 130,131,132,133,134,135,136,137,138,139, 144,147, 150,151,152,153,155,156,157,158,159, 176,177,178, 180,181,182,183,184,185,186,187,188,189, ]; $phone = []; for($i = 0; $i < $count; $i++) { $phone[] = $arr[array_rand($arr)].''.mt_rand(1000,9999).''.mt_rand(1000,9999); } return array_unique($phone); } function makePassword($length) { // 密码字符集,可任意添加你需要的字符 // $str = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', // 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', // 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D', // 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z', // '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); $str = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); // 在 $str 中随机取 $length 个数组元素键名 $keys = array_rand($str, $length); $password = ''; for($i = 0; $i < $length; $i++) { // 将 $length 个数组元素连接成字符串 $password .= $str[$keys[$i]]; } return $password; } function isDateTime($dateTime){ $ret = strtotime($dateTime); return $ret !== FALSE && $ret != -1; } function fputcsv2($handle, array $fields, $delimiter = ",", $enclosure = '"', $escape_char = "\\") { foreach ($fields as $k => $v) { $fields[$k] = iconv("UTF-8", "GB2312//IGNORE", $v); // 这里将UTF-8转为GB2312编码 } fputcsv($handle, $fields, $delimiter, $enclosure, $escape_char); } //导出csv function exportCsv($data, $filename, $column) { header('Content-Description: File Transfer'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="' . $filename . '.csv"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); $file = fopen('php://output', 'w'); fputcsv2($file, $column); foreach ($data as $row) { fputcsv2($file, $row); } exit(); } /* * 解析SPU和SKU的ID * @$id sku 或者 spu id * @return array db 数据库,table 表名 */ function getSpuSkuDb($id) { $data['db'] = 'sku_0'; $data['table'] = 'lie_sku_0'; $type = substr($id, 0, 1); $dbcode = substr($id, -2, 1); $tablecode = substr($id, -1); if ($type == 1) { $data['db'] = 'sku_' . $dbcode; $data['table'] = 'sku_' . $tablecode; } if ($type == 2) { $data['db'] = 'spu'; $data['table'] = 'spu_' . $tablecode; } return $data; } //去除特殊空格 function replaceSpace($str) { $str = str_replace(" ", " ", $str); //头尾的空格去掉 $str = trim($str); return $str; }