common.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. // 应用公共文件
  3. /**
  4. * 随机码
  5. * @param type $length
  6. * @return string
  7. */
  8. function randString($length, $c = false) {
  9. if ($c) {
  10. $_codeSet = '0123456789';
  11. } else {
  12. $_codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
  13. }
  14. $code = '';
  15. for ($i = 0; $i < $length; $i++) {
  16. $code.= $_codeSet[mt_rand(0, strlen($_codeSet) - 1)];
  17. }
  18. return $code;
  19. }
  20. /**
  21. * openssl aes 加密
  22. * @param $data
  23. * @param $key
  24. * @param int $options
  25. * @return string
  26. */
  27. function crypto_encrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
  28. $key = md5($key);
  29. $iv = substr($key, 0, -16);
  30. $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, $options, $iv);
  31. return $encrypted;
  32. }
  33. /**
  34. * openssl aes 解密
  35. * @param $data
  36. * @param $key
  37. * @param int $options
  38. * @return string
  39. */
  40. function crypto_decrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
  41. $key = md5($key);
  42. $iv = substr($key, 0, -16);
  43. return openssl_decrypt($data, 'aes-256-cbc', $key, $options, $iv);
  44. }
  45. /**
  46. * 验证手机号是否正确
  47. * @author honfei
  48. * @param number $mobile
  49. */
  50. function isMobile($mobile) {
  51. if (!is_numeric($mobile)) {
  52. return false;
  53. }
  54. return preg_match('/^1[23456789]{1}\d{9}$/', $mobile) ? true : false;
  55. }
  56. /**
  57. * 设置网址参数
  58. * @param $url
  59. */
  60. function setParam($url,$parm){
  61. $url = explode('?',$url);
  62. $web = $url[0];
  63. $parmAr = [];
  64. $parmStr = count($url) == 2 ? $url[1] : "" ;
  65. if(!empty($parmStr)) {
  66. parse_str($parmStr,$parmAr);
  67. }
  68. foreach ($parm as $k=>$v){
  69. $parmAr[$k] = $v;
  70. }
  71. $parmStr2 = http_build_query($parmAr);
  72. return $web .( empty($parmStr2) ? "" : ("?" . $parmStr2));
  73. }
  74. /**
  75. * 几天未消费
  76. * @param $time
  77. */
  78. function getLastTime($time) {
  79. $nowTime = time();
  80. $day = 3600 * 24;
  81. if(empty($time)) {
  82. return '从无消费';
  83. }
  84. if($nowTime - $time < $day) {
  85. return '今天有消费';
  86. }
  87. $lDay = intval(($nowTime - $time) / $day);
  88. return ($lDay + 1).'天未消费';
  89. }
  90. /**
  91. * 分级排序
  92. * @param $data
  93. * @param int $pid
  94. * @param string $field
  95. * @param string $pk
  96. * @param string $html
  97. * @param int $level
  98. * @param bool $clear
  99. * @return array
  100. */
  101. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  102. {
  103. static $list = [];
  104. if ($clear) $list = [];
  105. foreach ($data as $k => $res) {
  106. if ($res[$field] == $pid) {
  107. $res['html'] = str_repeat($html, $level);
  108. $list[] = $res;
  109. unset($data[$k]);
  110. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  111. }
  112. }
  113. return $list;
  114. }
  115. /**
  116. * 上传路径转化,默认路径
  117. * @param $path
  118. * @param int $type
  119. * @param bool $force
  120. * @return string
  121. */
  122. function make_path($path, int $type = 2, bool $force = false)
  123. {
  124. $path = DS . ltrim(rtrim($path));
  125. switch ($type) {
  126. case 1:
  127. $path .= DS . date('Y');
  128. break;
  129. case 2:
  130. $path .= DS . date('Y') . DS . date('m');
  131. break;
  132. case 3:
  133. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  134. break;
  135. }
  136. try {
  137. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  138. return trim(str_replace(DS, '/', $path), '.');
  139. } else return '';
  140. } catch (\Exception $e) {
  141. if ($force)
  142. throw new \Exception($e->getMessage());
  143. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  144. }
  145. }
  146. /**
  147. * 格式化属性
  148. * @param $arr
  149. * @return array
  150. */
  151. function attr_format($arr)
  152. {
  153. $data = [];
  154. $res = [];
  155. $count = count($arr);
  156. if ($count > 1) {
  157. for ($i = 0; $i < $count - 1; $i++) {
  158. if ($i == 0) $data = $arr[$i]['detail'];
  159. //替代变量1
  160. $rep1 = [];
  161. foreach ($data as $v) {
  162. foreach ($arr[$i + 1]['detail'] as $g) {
  163. //替代变量2
  164. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  165. $tmp[] = $rep2;
  166. if ($i == $count - 2) {
  167. foreach (explode('-$-', $rep2) as $k => $h) {
  168. //替代变量3
  169. $rep3 = explode('_$_', $h);
  170. //替代变量4
  171. $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
  172. }
  173. if($count == count($rep4['detail']))
  174. $res[] = $rep4;
  175. }
  176. }
  177. }
  178. $data = isset($tmp) ? $tmp : [];
  179. }
  180. } else {
  181. $dataArr = [];
  182. foreach ($arr as $k => $v) {
  183. foreach ($v['detail'] as $kk => $vv) {
  184. $dataArr[$kk] = $v['value'] . '_' . $vv;
  185. $res[$kk]['detail'][$v['value']] = $vv;
  186. }
  187. }
  188. $data[] = implode('-', $dataArr);
  189. }
  190. return [$data, $res];
  191. }
  192. /**
  193. * 获取图片转为base64
  194. * @param string $avatar
  195. * @return bool|string
  196. */
  197. function image_to_base64($avatar = '', $timeout = 9)
  198. {
  199. try {
  200. $url = parse_url($avatar);
  201. $url = $url['host'];
  202. $header = [
  203. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  204. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  205. 'Accept-Encoding: gzip, deflate, br',
  206. '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',
  207. 'Host:' . $url
  208. ];
  209. $dir = pathinfo($url);
  210. $host = $dir['dirname'];
  211. $refer = $host . '/';
  212. $curl = curl_init();
  213. curl_setopt($curl, CURLOPT_REFERER, $refer);
  214. curl_setopt($curl, CURLOPT_URL, $avatar);
  215. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  216. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  217. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  218. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  219. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  220. $data = curl_exec($curl);
  221. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  222. curl_close($curl);
  223. if ($code == 200) {
  224. return "data:image/jpeg;base64," . base64_encode($data);
  225. } else {
  226. return false;
  227. }
  228. } catch (\Exception $e) {
  229. return false;
  230. }
  231. }
  232. /**
  233. * 获取图片转为base64
  234. * @param string $avatar
  235. * @return bool|string
  236. */
  237. function put_image($url, $filename = '')
  238. {
  239. if ($url == '') {
  240. return false;
  241. }
  242. try {
  243. if ($filename == '') {
  244. $ext = pathinfo($url);
  245. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  246. return false;
  247. }
  248. $filename = time() . "." . $ext['extension'];
  249. }
  250. //文件保存路径
  251. ob_start();
  252. readfile($url);
  253. $img = ob_get_contents();
  254. ob_end_clean();
  255. $path = 'uploads/qrcode';
  256. $fp2 = fopen($path . '/' . $filename, 'a');
  257. fwrite($fp2, $img);
  258. fclose($fp2);
  259. return $path . '/' . $filename;
  260. } catch (\Exception $e) {
  261. return false;
  262. }
  263. }
  264. function doRequest($url, $data, $header = null, $post = true, $json = false, $form = false, $format = 0)
  265. {
  266. $curl = curl_init();
  267. curl_setopt($curl, CURLOPT_URL, $url);
  268. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  269. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  270. if ($post) {
  271. curl_setopt($curl, CURLOPT_POST, 1);
  272. if (!$json && !$form) {
  273. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  274. } else if ($json && !$form) {
  275. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
  276. } else {
  277. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  278. }
  279. }
  280. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  281. if ($header) {
  282. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  283. curl_setopt($curl, CURLOPT_HEADER, 0);
  284. }
  285. $result = curl_exec($curl);
  286. if (curl_errno($curl)) {
  287. return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
  288. }
  289. curl_close($curl);
  290. return $result;
  291. }