123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- // 应用公共文件
- /**
- * 随机码
- * @param type $length
- * @return string
- */
- function randString($length, $c = false) {
- if ($c) {
- $_codeSet = '0123456789';
- } else {
- $_codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
- }
- $code = '';
- for ($i = 0; $i < $length; $i++) {
- $code.= $_codeSet[mt_rand(0, strlen($_codeSet) - 1)];
- }
- return $code;
- }
- /**
- * openssl aes 加密
- * @param $data
- * @param $key
- * @param int $options
- * @return string
- */
- function crypto_encrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
- $key = md5($key);
- $iv = substr($key, 0, -16);
- $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, $options, $iv);
- return $encrypted;
- }
- /**
- * openssl aes 解密
- * @param $data
- * @param $key
- * @param int $options
- * @return string
- */
- function crypto_decrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
- $key = md5($key);
- $iv = substr($key, 0, -16);
- return openssl_decrypt($data, 'aes-256-cbc', $key, $options, $iv);
- }
- /**
- * 验证手机号是否正确
- * @author honfei
- * @param number $mobile
- */
- function isMobile($mobile) {
- if (!is_numeric($mobile)) {
- return false;
- }
- return preg_match('/^1[23456789]{1}\d{9}$/', $mobile) ? true : false;
- }
- /**
- * 设置网址参数
- * @param $url
- */
- function setParam($url,$parm){
- $url = explode('?',$url);
- $web = $url[0];
- $parmAr = [];
- $parmStr = count($url) == 2 ? $url[1] : "" ;
- if(!empty($parmStr)) {
- parse_str($parmStr,$parmAr);
- }
- foreach ($parm as $k=>$v){
- $parmAr[$k] = $v;
- }
- $parmStr2 = http_build_query($parmAr);
- return $web .( empty($parmStr2) ? "" : ("?" . $parmStr2));
- }
- /**
- * 几天未消费
- * @param $time
- */
- function getLastTime($time) {
- $nowTime = time();
- $day = 3600 * 24;
- if(empty($time)) {
- return '从无消费';
- }
- if($nowTime - $time < $day) {
- return '今天有消费';
- }
- $lDay = intval(($nowTime - $time) / $day);
- return ($lDay + 1).'天未消费';
- }
- /**
- * 分级排序
- * @param $data
- * @param int $pid
- * @param string $field
- * @param string $pk
- * @param string $html
- * @param int $level
- * @param bool $clear
- * @return array
- */
- function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
- {
- static $list = [];
- if ($clear) $list = [];
- foreach ($data as $k => $res) {
- if ($res[$field] == $pid) {
- $res['html'] = str_repeat($html, $level);
- $list[] = $res;
- unset($data[$k]);
- sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
- }
- }
- return $list;
- }
- /**
- * 上传路径转化,默认路径
- * @param $path
- * @param int $type
- * @param bool $force
- * @return string
- */
- function make_path($path, int $type = 2, bool $force = false)
- {
- $path = DS . ltrim(rtrim($path));
- switch ($type) {
- case 1:
- $path .= DS . date('Y');
- break;
- case 2:
- $path .= DS . date('Y') . DS . date('m');
- break;
- case 3:
- $path .= DS . date('Y') . DS . date('m') . DS . date('d');
- break;
- }
- try {
- if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
- return trim(str_replace(DS, '/', $path), '.');
- } else return '';
- } catch (\Exception $e) {
- if ($force)
- throw new \Exception($e->getMessage());
- return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
- }
- }
- /**
- * 格式化属性
- * @param $arr
- * @return array
- */
- function attr_format($arr)
- {
- $data = [];
- $res = [];
- $count = count($arr);
- if ($count > 1) {
- for ($i = 0; $i < $count - 1; $i++) {
- if ($i == 0) $data = $arr[$i]['detail'];
- //替代变量1
- $rep1 = [];
- foreach ($data as $v) {
- foreach ($arr[$i + 1]['detail'] as $g) {
- //替代变量2
- $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
- $tmp[] = $rep2;
- if ($i == $count - 2) {
- foreach (explode('-$-', $rep2) as $k => $h) {
- //替代变量3
- $rep3 = explode('_$_', $h);
- //替代变量4
- $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
- }
- if($count == count($rep4['detail']))
- $res[] = $rep4;
- }
- }
- }
- $data = isset($tmp) ? $tmp : [];
- }
- } else {
- $dataArr = [];
- foreach ($arr as $k => $v) {
- foreach ($v['detail'] as $kk => $vv) {
- $dataArr[$kk] = $v['value'] . '_' . $vv;
- $res[$kk]['detail'][$v['value']] = $vv;
- }
- }
- $data[] = implode('-', $dataArr);
- }
- return [$data, $res];
- }
- /**
- * 获取图片转为base64
- * @param string $avatar
- * @return bool|string
- */
- function image_to_base64($avatar = '', $timeout = 9)
- {
- try {
- $url = parse_url($avatar);
- $url = $url['host'];
- $header = [
- 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
- 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
- 'Accept-Encoding: gzip, deflate, br',
- 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
- 'Host:' . $url
- ];
- $dir = pathinfo($url);
- $host = $dir['dirname'];
- $refer = $host . '/';
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_REFERER, $refer);
- curl_setopt($curl, CURLOPT_URL, $avatar);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($curl);
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- curl_close($curl);
- if ($code == 200) {
- return "data:image/jpeg;base64," . base64_encode($data);
- } else {
- return false;
- }
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 获取图片转为base64
- * @param string $avatar
- * @return bool|string
- */
- function put_image($url, $filename = '')
- {
- if ($url == '') {
- return false;
- }
- try {
- if ($filename == '') {
- $ext = pathinfo($url);
- if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
- return false;
- }
- $filename = time() . "." . $ext['extension'];
- }
- //文件保存路径
- ob_start();
- readfile($url);
- $img = ob_get_contents();
- ob_end_clean();
- $path = 'uploads/qrcode';
- $fp2 = fopen($path . '/' . $filename, 'a');
- fwrite($fp2, $img);
- fclose($fp2);
- return $path . '/' . $filename;
- } catch (\Exception $e) {
- return false;
- }
- }
- function doRequest($url, $data, $header = null, $post = true, $json = false, $form = false, $format = 0)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- if ($post) {
- curl_setopt($curl, CURLOPT_POST, 1);
- if (!$json && !$form) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
- } else if ($json && !$form) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
- } else {
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- if ($header) {
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- }
- $result = curl_exec($curl);
- if (curl_errno($curl)) {
- return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
- }
- curl_close($curl);
- return $result;
- }
|