Home.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service;
  10. use app\common\exception\BaseException;
  11. use mall\utils\Tool;
  12. use think\facade\Config;
  13. use app\common\models\goods\Goods as GoodsModel;
  14. use think\facade\Db;
  15. class Home extends Service {
  16. /**
  17. * 获取装修数据
  18. * @param array $data
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public static function getData(){
  25. $banner = Db::name("data")->where("sign","banner")->find();
  26. $slider = array_map(function($res){
  27. return [
  28. "photo" => Tool::thumb($res["photo"],"",true),
  29. "url" => $res["url"]
  30. ];
  31. },Db::name("data_item")->where("pid",$banner["id"])->order("sort","ASC")->select()->toArray());
  32. $category = Db::name("data")->where("sign","category")->find();
  33. $nav = array_map(function($res){
  34. return [
  35. "url"=>$res["url"],
  36. "name"=>$res["name"],
  37. "image"=>Tool::thumb($res["photo"],"",true)
  38. ];
  39. },Db::name("data_item")->where("pid",$category["id"])->order("sort","ASC")->select()->toArray());
  40. $adOne = Db::name("data")->where("sign","home_ad_one")->find();
  41. $adItemOne = array_map(function($res){
  42. return [
  43. "url"=>$res["url"],
  44. "name"=>$res["name"],
  45. "image"=>Tool::thumb($res["photo"],"",true)
  46. ];
  47. },Db::name("data_item")->where("pid",$adOne["id"])->order("sort","ASC")->select()->toArray());
  48. $adTwo = Db::name("data")->where("sign","home_ad_two")->find();
  49. $adItemTwo = array_map(function($res){
  50. return [
  51. "url"=>$res["url"],
  52. "name"=>$res["name"],
  53. "image"=>Tool::thumb($res["photo"],"",true)
  54. ];
  55. },Db::name("data_item")->where("pid",$adTwo["id"])->order("sort","ASC")->select()->toArray());
  56. $hot = array_map(function ($res){
  57. return [
  58. "id"=> $res["id"],
  59. "url"=>'/goods/view/'.$res["id"],
  60. "name"=>$res["title"],
  61. "image"=>Tool::thumb($res["photo"],"",true),
  62. "price"=>$res["sell_price"]
  63. ];
  64. },
  65. Db::name("goods_extends")
  66. ->alias("e")->field("g.*")->join("goods g","e.goods_id=g.id","LEFT")
  67. ->where('g.status',0)->where("e.attribute","hot")
  68. ->order("e.id","DESC")->limit(3)->select()->toArray()
  69. );
  70. $recommend = array_map(function ($res){
  71. return [
  72. "id"=> $res["id"],
  73. "url"=>'/goods/view/'.$res["id"],
  74. "name"=>$res["title"],
  75. "image"=>Tool::thumb($res["photo"],"",true),
  76. "price"=>$res["sell_price"]
  77. ];
  78. },
  79. Db::name("goods_extends")
  80. ->alias("e")->field("g.*")->join("goods g","e.goods_id=g.id","LEFT")
  81. ->where('g.status',0)->where("e.attribute","recommend")
  82. ->order("e.id","DESC")->limit(5)->select()->toArray()
  83. );
  84. $notice = Db::name("archives")->field('id,title')->where("status",0)->where('pid',71)->find();
  85. return [
  86. "banner"=>$slider,
  87. "nav"=>$nav,
  88. "img_1"=>isset($adItemOne[0]) ? $adItemOne[0] : [],
  89. "img_2"=>$adItemTwo,
  90. "hot"=>$hot,
  91. "recommend"=>$recommend,
  92. "notice"=>isset($notice) ? $notice : []
  93. ];
  94. }
  95. /**
  96. * 获取商品数据列表
  97. * @param array $data
  98. * @return array
  99. * @throws BaseException
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public static function getList($data=[]){
  105. $size = Config::get("website.pageSize");
  106. $page = $data["page"]??1;
  107. $condition = ["status"=>0];
  108. $array = ["list"=>[], "page"=>$page, "total"=>0, "size"=>$size];
  109. $count = GoodsModel::where($condition)->count();
  110. $total = ceil($count/$size);
  111. $array["total"] = $total;
  112. $array["size"] = $size;
  113. if($total == $page -1){
  114. throw new BaseException("empty",-1,$array);
  115. }
  116. $result = GoodsModel::field("id,title,photo,sell_price as price,sale")->where($condition)->order('id','desc')->page($page,$size)->select()->toArray();
  117. $array["list"] = array_map(function ($rs){
  118. $rs["photo"] = Tool::thumb($rs["photo"],"medium",true);
  119. return $rs;
  120. },$result);
  121. return $array;
  122. }
  123. }