Products.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 app\common\models\goods\GoodsExtends as GoodsExtendsModel;
  12. use mall\utils\Tool;
  13. use think\facade\Config;
  14. class Products extends Service {
  15. /**
  16. * 商品列表排序
  17. * @param $data
  18. * @return array
  19. */
  20. protected static function getOrder($data){
  21. $type = $data["type"]??0;
  22. $sort = $data["sort"]??1;
  23. switch($type){
  24. case '2':
  25. $order = 'g.sell_price';
  26. $text = $sort == 1 ? "ASC" : "DESC";
  27. break;
  28. case '1':
  29. $order = 'g.sale';
  30. $text = 'DESC';
  31. break;
  32. case '0':
  33. default :
  34. $order = 'g.id';
  35. $text = 'DESC';
  36. break;
  37. }
  38. return [ "field"=>$order, "order"=>$text ];
  39. }
  40. /**
  41. * 获取商品数据
  42. * @param $data
  43. * @param array $condition
  44. * @return array
  45. * @throws BaseException
  46. */
  47. public static function getList($data,$condition=[]){
  48. $sort = self::getOrder($data);
  49. $size = Config::get("website.pageSize");
  50. $page = $data["page"]??1;
  51. $count = GoodsExtendsModel::alias("e")->join("goods g","e.goods_id=g.id","LEFT")->where($condition)->count();
  52. $result = GoodsExtendsModel::field("g.id,g.title,g.photo,g.sell_price as price,g.sale")->alias("e")->join("goods g","e.goods_id=g.id","LEFT")->where($condition)->order($sort["field"],$sort["order"])->page($page,$size)->select()->toArray();
  53. $array = [ "list"=>array_map(function ($res){
  54. $res["photo"] = Tool::thumb($res["photo"],"medium",true);
  55. return $res;
  56. },$result), "page"=>$page, "total"=>0, "size"=>$size ];
  57. $total = ceil($count / $size);
  58. $array["total"] = $total;
  59. if($total == $page -1){
  60. throw new BaseException("没有数据了哦!",-1,$array);
  61. }
  62. return $array;
  63. }
  64. }