Base.php 2.8 KB

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