Base.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. class Base extends Controller
  5. {
  6. public function _initialize(){
  7. $data = db('column')->order('sort desc')->select();//导航栏列表
  8. $good =db('good')->select();//工具信息列表
  9. $link =db('links')->order('sort desc')->select();//友情链接列表
  10. //标签栏一个工具对应多个标签处理-----------------------------------
  11. $tags_id = input('id');
  12. // 找到所有标签,对其进行格式化处理
  13. $tags_data = db('tags')->select();
  14. $tags = [];
  15. foreach ($tags_data as $value) {
  16. $tags[$value['id']] = ['tagname'=>$value['tagname'], 'children'=>[]];
  17. }
  18. // 找到所有商品,判断商品是否在标签内
  19. $ta = db('good')->field('id, title, tags_id')->where('tags_id','<>','')->order('sort desc')->select();//查询出tags_id不为空的所有数据
  20. foreach ($ta as $val) {
  21. $tag_ids = explode(',', $val['tags_id']);
  22. if ($tag_ids) {
  23. foreach($tag_ids as $tid) {
  24. if($tags[$tid]) {
  25. $tags[$tid]['children'][] = $val;
  26. }
  27. }
  28. }
  29. }
  30. //end---------------------------------------------------------
  31. $getip = getIp();//获取ip
  32. // $getip = "218.70.26.255"; //重庆IP地址
  33. if($getip == ''||$getip=='127.0.0.1'||$getip=='::1'){//nginx环境为本地ip为127.0.0.1,apache本地环境为::1
  34. $getip="210.51.167.169";//默认为北京ip地址
  35. }
  36. $content = json_decode(file_get_contents("https://api.seniverse.com/v3/weather/now.json?key=S3OSd1xyPevThEbnB&location=$getip"),true);
  37. $city=array();
  38. $city['name'] = $content['results'][0]['location']['name'];//获取城市
  39. $tq = tianqi($city['name']);//获取当前城市天气
  40. $this->assign([
  41. 'data'=>$data,
  42. 'tags'=>$tags,
  43. 'good'=>$good,
  44. 'getip'=>$getip,//ip
  45. 'city'=>$city,//城市
  46. 'tq'=>$tq,//天气
  47. 'link'=>$link,//友情链接
  48. ]);
  49. return view();
  50. }
  51. public function praise(){//点赞
  52. $id = input('id');//获取当前点赞id
  53. $res = db('good')->where('id',$id)->setInc('likes');//setInc:对指定字段进行加操作,setDec:对指定字段进行减操作
  54. if($res){
  55. return 1;//点赞成功
  56. }else{
  57. return 2;//失败
  58. }
  59. }
  60. public function alls(){//所有工具
  61. $cid=db('good')
  62. ->alias('a')
  63. ->field('a.*,b.catename')
  64. ->join('column b','a.column_id=b.id')
  65. ->order('sort desc')
  66. ->select();
  67. $this->assign([
  68. 'cid'=>$cid,
  69. ]);
  70. return view('indexs/index');
  71. }
  72. }