Goods.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 mall\library\tools\jwt\Token;
  11. use mall\utils\Tool;
  12. use think\facade\Config;
  13. use app\common\exception\BaseException;
  14. use mall\basic\Users;
  15. use app\common\models\users\Users as UsersModel;
  16. use app\common\models\goods\Goods as GoodsModel;
  17. use app\common\models\goods\GoodsItem as GoodsItemModel;
  18. use app\common\models\goods\GoodsImage as GoodsImageModel;
  19. use app\common\models\goods\GoodsAttribute as GoodsAttributeModel;
  20. use app\common\models\users\UsersFavorite as UsersFavoriteModel;
  21. use app\api\service\Comments as CommentsService;
  22. class Goods extends Service {
  23. /**
  24. * 商品列表排序
  25. * @param $data
  26. * @return array
  27. */
  28. protected static function getOrder($data){
  29. $type = $data["type"]??0;
  30. $sort = $data["sort"]??1;
  31. switch($type){
  32. case '2':
  33. $order = 'sell_price';
  34. $text = $sort == 1 ? "ASC" : "DESC";
  35. break;
  36. case '1':
  37. $order = 'sale';
  38. $text = 'DESC';
  39. break;
  40. case '0':
  41. default :
  42. $order = 'id';
  43. $text = 'DESC';
  44. break;
  45. }
  46. return [ "field"=>$order, "order"=>$text ];
  47. }
  48. /**
  49. * 获取商品数据
  50. * @param $data
  51. * @return array
  52. * @throws BaseException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public static function getList($data){
  58. $condition = [
  59. ["cat_id","=",$data["id"]??""],
  60. ["status","=",0]
  61. ];
  62. $sort = self::getOrder($data);
  63. $size = Config::get("website.pageSize");
  64. $page = $data["page"]??1;
  65. $count = GoodsModel::where($condition)->count();
  66. $result = GoodsModel::field("id,title,photo,sell_price as price,sale")->where($condition)->order($sort["field"],$sort["order"])->page($page,$size)->select()->toArray();
  67. $array = [ "list"=>array_map(function ($res){
  68. $res["photo"] = Tool::thumb($res["photo"],"medium",true);
  69. return $res;
  70. },$result), "page"=>$page, "total"=>0, "size"=>$size ];
  71. $total = ceil($count / $size);
  72. $array["total"] = $total;
  73. if($total == $page -1){
  74. throw new BaseException("没有数据了哦!",-1,$array);
  75. }
  76. return $array;
  77. }
  78. /**
  79. * 详情
  80. * @param $id
  81. * @return array
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public static function detail($id){
  87. if(!$goods = GoodsModel::where("id",$id)->where("status",0)->find()){
  88. throw new \Exception("商品不存在",0);
  89. }
  90. $data = [];
  91. $data["collect"] = false;
  92. try{
  93. $token = Token::check();
  94. $result = Token::parse($token,"id");
  95. if(is_array($result)){
  96. if($row=UsersModel::where("id",$result["value"])->find()){
  97. $data["collect"] = UsersFavoriteModel::where([ "user_id"=>$row["id"], "goods_id"=>$id ])->count() ? true : false;
  98. }
  99. }
  100. }catch(\Exception $ex){}
  101. $data["photo"] = array_map(function ($result){
  102. return Tool::thumb($result["photo"],"",true);
  103. }, GoodsImageModel::field("path as photo")->where([
  104. "goods_id"=>$id,
  105. ])->select()->toArray());
  106. // 商品规格
  107. $attr = GoodsAttributeModel::where("goods_id",$id)->select()->toArray();
  108. $goods_attr = [];
  109. foreach ($attr as $key=>$val){
  110. if(!in_array($val["attr_id"],array_keys($goods_attr))){
  111. $goods_attr[$val["attr_id"]] = [
  112. "id"=>$val["attr_id"],
  113. "name"=>$val["name"],
  114. "list"=>[]
  115. ];
  116. }
  117. $goods_attr[$val["attr_id"]]["list"][$val["attr_data_id"]] = [
  118. "id"=>$val["attr_data_id"],
  119. "pid"=>$val["attr_id"],
  120. "value"=>$val["value"],
  121. "selected"=>false,
  122. "disable"=>false
  123. ];
  124. }
  125. $goods_attr = array_values($goods_attr);
  126. foreach($goods_attr as $key=>$val){
  127. $goods_attr[$key]['list'] = array_values($val["list"]);
  128. }
  129. $data["attr"] = $goods_attr;
  130. $item = GoodsItemModel::where("goods_id",$id)->select()->toArray();
  131. $goods_item = [];
  132. foreach($item as $key=>$val){
  133. $sku_id = str_replace([",",":"],["_","_"], $val["spec_key"]);
  134. $goods_item[$sku_id]["key"] = $val["spec_key"];
  135. $goods_item[$sku_id]["sell_price"] = $val["sell_price"];
  136. $goods_item[$sku_id]["goods_weight"] = $val["goods_weight"];
  137. $goods_item[$sku_id]["store_nums"] = $val["store_nums"];
  138. $goods_item[$sku_id]["goods_no"] = $val["goods_number"];
  139. $goods_item[$sku_id]["product_id"] = $val["id"];
  140. }
  141. $data['item'] = $goods_item;
  142. $goods["content"] = Tool::replaceContentImage(Tool::removeContentAttr($goods["content"]));
  143. $data["goods"] = [
  144. "id"=>$id,
  145. "title"=>$goods["title"],
  146. "briefly"=>$goods["briefly"],
  147. "briefly_color"=>$goods["briefly_color"],
  148. "photo"=>Tool::thumb($goods["photo"],'medium ',true),
  149. "sell_price"=>$goods["sell_price"],
  150. "market_price"=>$goods["market_price"],
  151. "store_nums"=>$goods["store_nums"],
  152. "sale"=>$goods["sale"],
  153. "content"=>$goods["content"]
  154. ];
  155. $data["comments"] = [];
  156. try{
  157. $comments = CommentsService::getList(["id"=>$id, "type"=>"goods"]);
  158. $data["comments"] = $comments["data"];
  159. }catch (\Exception $e){}
  160. return $data;
  161. }
  162. /**
  163. * 用户收藏操作
  164. * @param $id
  165. * @return int
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public static function favorite($id){
  171. if(Users::isEmpty("id")){
  172. throw new \Exception("您还没有登录,请先登录",0);
  173. }
  174. if(($row = GoodsModel::where("id",$id)->find()) == false){
  175. throw new \Exception("非法操作",0);
  176. }
  177. $condition = [ "user_id"=>Users::get("id"), "goods_id"=>$id ];
  178. if(UsersFavoriteModel::where($condition)->count()){
  179. UsersFavoriteModel::where($condition)->delete();
  180. return 2;
  181. }
  182. UsersFavoriteModel::create([ "user_id"=>Users::get("id"), "goods_id"=>$id, "create_time"=>time() ]);
  183. return 1;
  184. }
  185. }