common.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. header('Content-Type:text/html;Charset=utf-8');
  13. /**
  14. * 保存后台用户行为
  15. * @param string $remark 日志备注
  16. */
  17. function insert_admin_log($remark)
  18. {
  19. db('user_log')->insert([
  20. 'user_id' => session('id'),
  21. 'uname' => session('uname'),
  22. 'useragent' => request()->server('HTTP_USER_AGENT'),
  23. 'ip' => request()->ip(),
  24. 'url' => request()->url(true),
  25. 'method' => request()->method(),
  26. 'type' => request()->type(),
  27. 'param' => json_encode(request()->param()),
  28. 'remark' => $remark,
  29. 'create_time' => time(),
  30. ]);
  31. }
  32. /**
  33. * 获取用户真实 IP
  34. */
  35. function getIP()
  36. {
  37. static $realip;
  38. if (isset($_SERVER)){
  39. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
  40. $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  41. } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
  42. $realip = $_SERVER["HTTP_CLIENT_IP"];
  43. } else {
  44. $realip = $_SERVER["REMOTE_ADDR"];
  45. }
  46. } else {
  47. if (getenv("HTTP_X_FORWARDED_FOR")){
  48. $realip = getenv("HTTP_X_FORWARDED_FOR");
  49. } else if (getenv("HTTP_CLIENT_IP")) {
  50. $realip = getenv("HTTP_CLIENT_IP");
  51. } else {
  52. $realip = getenv("REMOTE_ADDR");
  53. }
  54. }
  55. return $realip;
  56. }
  57. //获取城市所在天气情况
  58. function tianqi($chengshi)
  59. {
  60. $url = 'http://wthrcdn.etouch.cn/weather_mini?city='.urlencode($chengshi);
  61. $html = file_get_contents($url);
  62. $jsondata = gzdecode($html);
  63. $data=json_decode($jsondata,true);
  64. $arr=array();
  65. $arr['chengshi']=$data['data']['city'];
  66. $dangtian=$data['data']['forecast'][0];
  67. $arr['gaowen']= str_replace("高温 ",null,$dangtian['high']);
  68. $arr['diwen']= str_replace("低温 ",null,$dangtian['low']);
  69. $arr['tianqi']=$dangtian['type'];
  70. $arr['fengxiang'] = $dangtian['fengxiang'];
  71. $arr['time'] =date("Y-m-d H:i:s");
  72. return $arr;
  73. }