function.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. use Illuminate\Support\Facades\Redis;
  3. if (!function_exists('res_422')) {
  4. function res_422($msg='参数有误!'){
  5. return json_encode(['code' => 422, 'data' => [], 'count' => 0,'msg'=>$msg]);
  6. }
  7. }
  8. //这个只能用在方法末尾
  9. if (!function_exists('opt_return')){
  10. function opt_return($res,$msg=null){
  11. if (!$res) {
  12. if (!$msg){
  13. $msg = '操作失败';
  14. }
  15. die(json_encode(array('status' => 2, 'message' => $msg)));
  16. }
  17. die(json_encode(array('status' => 1, 'message' => '操作成功')));
  18. }
  19. }
  20. if (!function_exists('opt_ok')){
  21. function opt_ok(){
  22. die(json_encode(array('status' => 1, 'message' => '操作成功'),JSON_UNESCAPED_UNICODE));
  23. }
  24. }
  25. if (!function_exists('opt_error')){
  26. function opt_error($msg=null){
  27. if (!empty($msg)){
  28. die(json_encode(array('status' => 2, 'message' => $msg)));
  29. }
  30. die(json_encode(array('status' => 2, 'message' => '操作失败')));
  31. }
  32. }
  33. if (!function_exists('table_error')){
  34. function table_error($msg=null){
  35. if (!$msg)
  36. $msg = '数据查询异常,请检查参数!';
  37. die(json_encode(['code'=>-1,'msg'=>$msg]));
  38. }
  39. }
  40. if (!function_exists('format_time')){
  41. function format_time(&$param,$format){
  42. if (isset($param['stime']))
  43. $param['stime'][1] = date($format,$param['stime'][1]);
  44. if (isset($param['etime']))
  45. $param['etime'][1] = date($format,$param['etime'][1]+86400);
  46. }
  47. }
  48. function nextDay($data)
  49. {
  50. return date('Y-m-d',strtotime('+1 day', strtotime($data)));
  51. }
  52. function nowDate()
  53. {
  54. return date('Y-m-d H:i:s');
  55. }
  56. if (!function_exists('getTree')){
  57. function getTree($data, $pId=0)
  58. {
  59. $tree = array();
  60. foreach ($data as $k => $v) {
  61. if ($v->parentId == $pId) { //父亲找到儿子
  62. $v->children = getTree($data, $v->id);
  63. $tree[] = $v;
  64. //unset($data[$k]);
  65. }
  66. }
  67. return $tree;
  68. }
  69. }
  70. /**
  71. * 把数字1-1亿换成汉字表述,如:123->一百二十三
  72. * @param [num] $num [数字]
  73. * @return [string] [string]
  74. */
  75. if (!function_exists('numToWord')) {
  76. function numToWord($num)
  77. {
  78. $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  79. $chiUni = array('','十', '百', '千', '万', '亿', '十', '百', '千');
  80. $chiStr = '';
  81. $num_str = (string)$num;
  82. $count = strlen($num_str);
  83. $last_flag = true; //上一个 是否为0
  84. $zero_flag = true; //是否第一个
  85. $temp_num = null; //临时数字
  86. $chiStr = '';//拼接结果
  87. if ($count == 2) {//两位数
  88. $temp_num = $num_str[0];
  89. $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
  90. $temp_num = $num_str[1];
  91. $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
  92. }else if($count > 2){
  93. $index = 0;
  94. for ($i=$count-1; $i >= 0 ; $i--) {
  95. $temp_num = $num_str[$i];
  96. if ($temp_num == 0) {
  97. if (!$zero_flag && !$last_flag ) {
  98. $chiStr = $chiNum[$temp_num]. $chiStr;
  99. $last_flag = true;
  100. }
  101. }else{
  102. $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;
  103. $zero_flag = false;
  104. $last_flag = false;
  105. }
  106. $index ++;
  107. }
  108. }else{
  109. $chiStr = $chiNum[$num_str[0]];
  110. }
  111. return $chiStr;
  112. }
  113. }
  114. if (!function_exists('createNo')) {
  115. function createNo($prefix = '')
  116. {
  117. return $prefix . date('YmdHis') . sprintf("%03d", rand(1, 99));
  118. }
  119. }
  120. if (!function_exists('llog')) {
  121. function llog($content, $channel = '', $level = 'info')
  122. {
  123. if ($channel == '') {
  124. \Log::info($content);
  125. } else {
  126. switch ($level) {
  127. case 'info':
  128. \Log::channel($channel)->info($content);
  129. break;
  130. case 'error':
  131. \Log::channel($channel)->error($content);
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. if (!function_exists('complete_pic')) {
  138. function complete_pic($val) {
  139. // $url = $remote == 'server' ? env('APP_URL') : env('API_URL');
  140. if ($val != '' && strpos($val, 'http') === false) {
  141. return env('OSS_URL') . $val;
  142. }
  143. return $val;
  144. }
  145. }
  146. /**
  147. * API请求
  148. * @param $url 链接
  149. * @param $data 参数
  150. */
  151. function Get_Curl($url, $data = null, array $heders = [], $time=6)
  152. {
  153. $ch = curl_init();
  154. curl_setopt($ch, CURLOPT_URL, $url);
  155. curl_setopt($ch, CURLOPT_HEADER, 0);
  156. curl_setopt($ch, CURLOPT_TIMEOUT, $time); //单位 秒,也可以使用
  157. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  158. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //这个是重点,规避ssl的证书检查。
  159. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 跳过host验证
  160. if(!empty($heders)){
  161. curl_setopt($ch, CURLOPT_HTTPHEADER, $heders);
  162. }
  163. if(!empty($data)){
  164. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  165. }
  166. $content = curl_exec($ch);
  167. curl_close($ch);
  168. return $content;
  169. }
  170. /**
  171. * API请求2
  172. * @param $url 链接
  173. * @param $data 参数
  174. */
  175. function curl_post_https($url,$data,$headers=null,$cookie=null){ // 模拟提交数据函数
  176. $curl = curl_init(); // 启动一个CURL会话
  177. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  178. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  179. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  180. // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  181. // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  182. // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  183. if(!empty($headers)){
  184. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
  185. }
  186. if(!empty($cookie)){
  187. curl_setopt($curl, CURLOPT_COOKIE, $cookie); // 带上COOKIE请求
  188. }
  189. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  190. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  191. curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 设置超时限制防止死循环
  192. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  193. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  194. $tmpInfo = curl_exec($curl); // 执行操作
  195. curl_close($curl); // 关闭CURL会话
  196. return $tmpInfo; // 返回数据
  197. }
  198. /**
  199. * API请求3
  200. * @param $url 链接
  201. * @param $data 参数
  202. */
  203. function curl_get_https($url,$headers=null,$raw=null,$time=6){
  204. $curl = curl_init();
  205. curl_setopt($curl, CURLOPT_URL, $url);
  206. curl_setopt($curl, CURLOPT_TIMEOUT, $time);
  207. curl_setopt($curl, CURLOPT_HEADER, 0);
  208. if(!empty($headers)){
  209. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
  210. }
  211. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  212. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  213. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  214. if($raw){
  215. curl_setopt($curl, CURLOPT_POSTFIELDS, $raw); // Post提交的数据包
  216. }
  217. $tmpInfo = curl_exec($curl); //返回api的json对象
  218. curl_close($curl);
  219. return $tmpInfo;
  220. }
  221. /**
  222. * post请求
  223. * @param $url string api链接
  224. * @param $data array 参数
  225. * @param $time_out int 超时时间
  226. */
  227. function post_multi(string $url, array $data = [],$time_out = 5)
  228. {
  229. $headerArray = array("Content-Type: multipart/form-data;charset='utf-8'");
  230. $curl = curl_init();
  231. curl_setopt($curl, CURLOPT_URL, $url);
  232. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT , $time_out);
  233. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  234. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  235. curl_setopt($curl, CURLOPT_POST, 1);
  236. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  237. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
  238. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  239. $output = curl_exec($curl);
  240. curl_close($curl);
  241. return json_decode($output,true);
  242. }
  243. /**
  244. * 转为十进制
  245. * @param string $number 十六进制的数
  246. * @return string
  247. */
  248. function hexToDec($number)
  249. {
  250. // 如果有0x去除它
  251. $number = remove0x(strtolower($number));
  252. $decvalues = [
  253. '0' => '0', '1' => '1', '2' => '2',
  254. '3' => '3', '4' => '4', '5' => '5',
  255. '6' => '6', '7' => '7', '8' => '8',
  256. '9' => '9', 'a' => '10', 'b' => '11',
  257. 'c' => '12', 'd' => '13', 'e' => '14',
  258. 'f' => '15'];
  259. $decval = '0';
  260. $number = strrev($number);
  261. for($i = 0; $i < strlen($number); $i++) {
  262. $decval = bcadd(bcmul(bcpow('16', $i, 0), $decvalues[$number[$i]]), $decval);
  263. }
  264. return $decval;
  265. }
  266. /**
  267. * 如果有0x去除它
  268. * @param $value
  269. * @return false|string
  270. */
  271. function remove0x($value)
  272. {
  273. if (strtolower(substr($value, 0, 2)) == '0x') {
  274. return substr($value, 2);
  275. }
  276. return $value;
  277. }
  278. /**
  279. * 高精度计算相除
  280. * @param $price [金额]
  281. * @param $number [多少个0]
  282. */
  283. function calculationExcept($price,$number){
  284. $multiple = 1; //倍数
  285. if($number > 0){
  286. for ($i=0; $i < $number; $i++) {
  287. $multiple = $multiple.'0';
  288. }
  289. $multiple = (int)$multiple;
  290. }
  291. $data = bcdiv($price,$multiple,$number);
  292. if(strpos(floatval($data),'-') == false){
  293. // 精度未失效
  294. $data = floatval($data);
  295. }else{
  296. // 精度失效
  297. $data = rtrim(rtrim($data, '0'), '.');
  298. }
  299. return $data;
  300. }
  301. /**
  302. * 获取Redis
  303. * @param $field 字段
  304. */
  305. function getRedis($field){
  306. $prefix = env('REDIS_PREFIX','sdadmin_');
  307. return Redis::get($prefix.$field);
  308. }
  309. /**
  310. * 存储Redis
  311. * @param $field 字段
  312. * @param $data 值
  313. */
  314. function setRedis($field,$data){
  315. $prefix = env('REDIS_PREFIX','sdadmin_');
  316. return Redis::set($prefix.$field,$data);
  317. }
  318. /**
  319. * 存储Redis,含有效期
  320. * @param $field 字段
  321. * @param $data 值
  322. * @param $expireSeconds 有效期,单位秒
  323. */
  324. function setexRedis($field,$expireSeconds = 60,$data){
  325. $prefix = env('REDIS_PREFIX','sdadmin_');
  326. return Redis::setex($prefix.$field,$expireSeconds,$data);
  327. }
  328. /**
  329. * 删除Redis
  330. * @param $data 值
  331. */
  332. function deleteRedis($data){
  333. $prefix = env('REDIS_PREFIX','sdadmin_');
  334. return Redis::del($prefix.$data);
  335. }
  336. /**
  337. * 获取Redis数组key
  338. * @param $data 值
  339. */
  340. function getKeyRedis(){
  341. return Redis::keys("_*");
  342. }
  343. /**
  344. * 获取真实IP
  345. * @param
  346. * @return string
  347. */
  348. function getClientIP()
  349. {
  350. $ip_address = '';
  351. if(getenv('HTTP_CLIENT_IP'))
  352. $ip_address = getenv('HTTP_CLIENT_IP');
  353. else if(getenv('HTTP_X_FORWARDED_FOR'))
  354. $ip_address = getenv('HTTP_X_FORWARDED_FOR');
  355. else if(getenv('HTTP_X_FORWARDED'))
  356. $ip_address = getenv('HTTP_X_FORWARDED');
  357. else if(getenv('HTTP_FORWARDED_FOR'))
  358. $ip_address = getenv('HTTP_FORWARDED_FOR');
  359. else if(getenv('HTTP_FORWARDED'))
  360. $ip_address = getenv('HTTP_FORWARDED');
  361. else if(getenv('REMOTE_ADDR'))
  362. $ip_address = getenv('REMOTE_ADDR');
  363. else
  364. $ip_address = 'UNKNOWN';
  365. return $ip_address;
  366. }