Product.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Http\Requests\Api\Product as Request;
  4. use App\Models\Business;
  5. use App\Models\Custom;
  6. use App\Models\Product as Model;
  7. use App\Models\Product\Attr as ProductAttr;
  8. use App\Models\Product\City as ProductCity;
  9. use App\Models\Product\Spec as ProductSpec;
  10. use App\Models\Product\Skus as ProductSkus;
  11. use App\Models\ProductPhoto;
  12. use App\Models\PromoProduct;
  13. use App\Models\PromoRebate;
  14. use App\Models\RegimentActive;
  15. use App\Models\Regiment;
  16. use App\Models\WeiBan\Tags as WeiBanTags;
  17. /**
  18. * 产品接口
  19. *
  20. * @author 刘相欣
  21. *
  22. * */
  23. class Product extends Api{
  24. /**
  25. * 获取产品列表 /api/product/get_list
  26. *
  27. * @param string $name 产品名称
  28. * @param int $page 页码,默认1
  29. * @param int $limit 每页条数,默认10条
  30. *
  31. * */
  32. public function get_list(Request $request,Model $Model,Custom $Custom,RegimentActive $RegimentActive,WeiBanTags $WeiBanTags){
  33. // 接口验签
  34. // $this->verify_sign();
  35. // 验证参数
  36. $request->scene('get_list')->validate();
  37. // 检查登录
  38. $uid = $this->getUid();
  39. // 获取客户信息
  40. $custom = $uid ? $Custom->getOne($uid) : [];
  41. // 接收参数
  42. $name = request('name','');
  43. $limit = request('limit',10);
  44. $cityId = empty($custom['city_id']) ? 0 : $custom['city_id'];
  45. // 显示
  46. $map = [['status','=','0'],['stock','>',0]];
  47. // 分类ID
  48. if( $name ) $map[] = ['name','like','%'.$name.'%'];
  49. // 是否有城市
  50. $wherIn = empty($custom['city_id']) ? [1] : [1,$custom['city_id']];
  51. // 获取分页信息
  52. $Paginator = $Model->query()
  53. ->join('product_city','product_city.product_id','=','product.id')
  54. ->where($map)
  55. ->whereIn('product_city.city_id',$wherIn)
  56. ->groupBy('product_id')
  57. ->orderBy('product.sort')
  58. ->orderBy('product.id')
  59. ->paginate($limit,['product.id','product.sort','product.name','product.tag_scope','product.tag_exclude','product.thumb','product.spec','product.price','product.market_price','product.stock']);
  60. // 获取数据
  61. $data['total'] = $Paginator->total();
  62. $data['current_page'] = $Paginator->currentPage();
  63. $data['per_page'] = $Paginator->perPage();
  64. $data['last_page'] = $Paginator->lastPage();
  65. $data['data'] = $Paginator->items();
  66. //产品促销活动
  67. $productIds = array_column($Paginator->items(),'id');
  68. $time = time();
  69. $where = [
  70. ['promo.status','=','0'],
  71. ['promo.start_time','<=',$time],
  72. ['promo.end_time','>=',$time],
  73. ['promo_product.status','=',0],
  74. ];
  75. // 查询促销活动
  76. $promoList = PromoProduct::query()
  77. ->join('promo','promo.id','=','promo_product.promo_id')
  78. ->where($where)
  79. ->whereIn('promo_product.product_id',$productIds)
  80. ->select('promo.*','promo_product.product_id','promo_product.id as promo_product_id')
  81. ->get()
  82. ->toArray();
  83. // 查询促销活动
  84. if ($promoList) $promoList = array_column($promoList,NULL,'product_id');
  85. // 查询用户标签
  86. $tags = $custom ? $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']) : [];
  87. $tagsList = array_column($tags,'name');
  88. // 处理请求
  89. foreach ( $data['data'] as $key => $value ) {
  90. // 判断是不是可以参与
  91. if( $value['tag_scope'] ) {
  92. // 解析数组
  93. $value['tag_scope'] = explode(',',$value['tag_scope']);
  94. // 如果没有交集,不在参与范围内
  95. if( !array_intersect($value['tag_scope'],$tagsList) ) {
  96. unset($data['data'][$key]);
  97. continue;
  98. }
  99. }
  100. // 判断是不是可以参与
  101. if( $value['tag_exclude'] ) {
  102. // 解析数组
  103. $value['tag_exclude'] = explode(',',$value['tag_exclude']);
  104. // 如果有交集,在排除范围内
  105. if( array_intersect($value['tag_exclude'],$tagsList) ) {
  106. unset($data['data'][$key]);
  107. continue;
  108. }
  109. }
  110. // 处理数据
  111. $value['thumb'] = path_compat($value['thumb']);
  112. $value['regiment_active_id'] = null;
  113. $value['regiment_title'] = '';
  114. // 查询产品拼团信息
  115. $regiment = $RegimentActive::query()->where([['status','=',1],['start_time','<=',$time],['end_time','>=',$time],['product_id','=',$value['id']]])->first(['id as active_id','number','automatic']);
  116. // 如果有拼团信息
  117. if ( $regiment ) {
  118. $value['regiment_active_id']= $regiment['id'];
  119. $value['regiment_title'] = $regiment['automatic'] == 1 ? '多人团' : $regiment['number'].'人团';
  120. }
  121. if ($promoList && $cityId){
  122. if (isset($promoList[$value['id']])){
  123. $promoInfo = $promoList[$value['id']];
  124. $promoTitle = '';
  125. if ($promoInfo['rebate_type'] == 1){
  126. $promoTitle = "满减";
  127. }elseif ($promoInfo['rebate_type'] == 2){
  128. $promoTitle .= "满折";
  129. }elseif ($promoInfo['rebate_type'] == 3){
  130. $promoTitle .= "满赠";
  131. }
  132. $promoInfoCity = [];
  133. if ($promoInfo['city_ids']){
  134. $promoInfoCity = explode(',',$promoInfo['city_ids']);
  135. }
  136. // 判断是不是可以参与
  137. if (!$promoInfoCity || in_array($cityId,$promoInfoCity)){
  138. if( $promoInfo['tag_scope']) {
  139. // 解析数组
  140. $promoInfo['tag_scope'] = explode(',',$promoInfo['tag_scope']);
  141. // 标签范围限定时,默认不能参与
  142. $allowJoin = 0;
  143. // 判断标签是不是存在
  144. if ($tags){
  145. foreach ($tags as $v) {
  146. // 标签范围内,允许参加
  147. if( in_array($v['name'],$promoInfo['tag_scope']) ) $allowJoin = 1;
  148. }
  149. // 在范围
  150. if( $allowJoin ) {
  151. $value['promo_title'] = $promoTitle;
  152. }
  153. }
  154. }else{
  155. $value['promo_title'] = $promoTitle;
  156. }
  157. }
  158. }
  159. }
  160. // 重组数据
  161. $data['data'][$key] = $value;
  162. }
  163. // 数据重组
  164. $data['data'] = array_values($data['data']);
  165. // 返回结果
  166. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  167. }
  168. /**
  169. * 获取产品列表 /api/product/get_detail
  170. *
  171. * @param int $id 产品id
  172. *
  173. * */
  174. public function get_detail(Request $request, Model $Model, ProductPhoto $ProductPhoto,ProductCity $ProductCity, Business $Business, ProductAttr $ProductAttr, ProductSpec $ProductSpec, ProductSkus $ProductSkus, RegimentActive $RegimentActive, Regiment $Regiment, Custom $Custom, WeiBanTags $WeiBanTags, PromoProduct $PromoProduct){
  175. // 接口验签
  176. // $this->verify_sign();
  177. // 验证参数
  178. $request->scene('get_detail')->validate();
  179. // 检查登录
  180. $uid = $this->getUid();
  181. // 获取客户信息
  182. $custom = $uid ? $Custom->getOne($uid) : [];
  183. // 接收参数
  184. $id = request('id',0);
  185. // 显示
  186. $map[] = ['status','=','0'];
  187. // 查询
  188. $data = $Model->query()->where($map)->find($id,['id','hide_orderbtn','name','thumb','stock','spec','poster','price','business_id','market_price']);
  189. // 如果没有数据
  190. if( !$data ) return json_send(['code'=>'error','msg'=>'产品已下架','data'=>['error'=>'产品已下架或不存在']]);
  191. // 允许城市
  192. $allowCity = $ProductCity->where([['product_id','=',$id]])->pluck('city_id')->toArray();
  193. // 如果不是全国城市 也不是服务城市范围
  194. if ( !in_array(1,$allowCity) && !in_array($custom['city_id'],$allowCity) ) return json_send(['code'=>'error','msg'=>'您的城市不在该产品服务范围','data'=>['error'=>'您的城市不在该产品服务范围']]);
  195. // 转数组
  196. $data = $data->toArray();
  197. // 处理数据
  198. $data['thumb'] = path_compat($data['thumb']);
  199. $data['poster'] = $data['poster'] ? path_compat($data['poster']) : '';
  200. $data['description'] = $Model->getDesc($id);
  201. $data['business_info'] = $Business->getOne($data['business_id']);
  202. $data['photo_list'] = $ProductPhoto->getListByProductId($id);
  203. // 缩略图处理
  204. foreach ($data['photo_list'] as $key => $value) {
  205. $value['thumb'] = path_compat($value['thumb']);
  206. $data['photo_list'][$key] = $value;
  207. }
  208. // 主图追加进去
  209. if( $data['photo_list'] ) array_unshift($data['photo_list'],['id'=>0,'sort'=>0,'thumb'=>$data['thumb']]);
  210. // 获取产品属性
  211. $attr = $ProductAttr->getListByProductId($id);
  212. // 规格属性
  213. $specAttr = [];
  214. // 获取数据
  215. foreach ($attr as $value) {
  216. // 默认未选中
  217. $value['active'] = 0;
  218. $specAttr[$value['spec_id']]['spec_id'] = $value['spec_id'];
  219. $specAttr[$value['spec_id']]['spec_name'] = $ProductSpec->getOne($value['spec_id'],'name');
  220. $specAttr[$value['spec_id']]['attr_list'][] = $value;
  221. }
  222. // 获取规格详情数据
  223. $data['product_attr'] = array_values($specAttr);
  224. // 获取SKU数据
  225. $data['product_sku'] = $ProductSkus->getListByProductId($id);
  226. // 获取数据
  227. foreach ($data['product_sku'] as $key=>$value) {
  228. // 默认未选中
  229. $value['sku_thumb'] = $value['sku_thumb'] ? path_compat($value['sku_thumb']) : '';
  230. $data['product_sku'][$key]= $value;
  231. }
  232. // 手机号
  233. if( isset($data['business_info']['phone']) ) unset($data['business_info']['phone']);
  234. if( isset($data['business_info']['logopic']) ) $data['business_info']['logopic'] = path_compat($data['business_info']['logopic']);
  235. //拼团数据
  236. $time = time();
  237. $regimentWhere = [
  238. ['status','=',1],
  239. ['start_time','<=',$time],
  240. ['end_time','>=',$time],
  241. ['product_id','=',$data['id']]
  242. ];
  243. $regimentActive = $RegimentActive::query()
  244. ->where($regimentWhere)
  245. ->first();
  246. $data['regiment_list'] = [];
  247. $data['automatic_info'] = [];
  248. if ( $regimentActive ){
  249. $data['regiment_number'] = $regimentActive['number'];
  250. $data['regiment_active_id'] = $regimentActive['id'];
  251. $data['regiment_price'] = $regimentActive['regiment_price'];
  252. $data['regiment_quota'] = $regimentActive['quota'];
  253. $data['regiment_active'] = $regimentActive;
  254. $data['automatic_info'] = [];
  255. if ($regimentActive['automatic'] == 1){
  256. $automaticInfo = $Regiment::query()->where([['status','=',0],['active_id','=',$regimentActive['id']],['start_time','<=',$time],['end_time','>=',$time]])->first();
  257. if ($automaticInfo){
  258. $data['automatic_info'] = $automaticInfo;
  259. $data['regiment_type'] = 1;
  260. }else{
  261. $data['regiment_type'] = 2;
  262. }
  263. $data['regiment_title'] = '多人团';
  264. }else{
  265. $data['regiment_title'] = $regimentActive['number'].'人团';
  266. $data['regiment_type'] = 2;
  267. $regiment = $Regiment::query()
  268. ->join('custom','custom.uid','=','regiment.custom_uid')
  269. ->where([['regiment.active_id','=',$data['regiment_active_id']],['regiment.product_id','=',$data['id']],['regiment.status','=',0],['regiment.start_time','<=',$time],['regiment.end_time','>=',$time]])
  270. ->select(['regiment.*','custom.username','custom.userpic'])
  271. ->get();
  272. if ($regiment){
  273. foreach ($regiment as &$value) {
  274. $value['userpic'] = $value['userpic'] ? path_compat($value['userpic']) : '';
  275. $surplusNumber = $data['regiment_number'] - $value['people_number'];
  276. if ($surplusNumber > 0){
  277. $value['surplus_number'] = $surplusNumber;
  278. }else{
  279. $value['surplus_number'] = 0;
  280. }
  281. }
  282. $data['regiment_list'] = $regiment;
  283. }
  284. }
  285. }else{
  286. $data['regiment_active_id'] = null;
  287. $data['regiment_type'] = 0;
  288. }
  289. // 查询用户标签
  290. $tags = [];
  291. $cityId = '';
  292. if ($custom){
  293. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  294. $cityId = $custom['city_id'];
  295. }
  296. //促销活动信息
  297. $promoInfo = $PromoProduct->getListById($data['id']);
  298. if ($promoInfo && $cityId){
  299. $promoTitle = "满". $promoInfo['std_pay']. "元";
  300. if ($promoInfo['rebate_type'] == 1){
  301. $promoTitle .= "减". $promoInfo['rebate']. "元";
  302. }elseif ($promoInfo['rebate_type'] == 2){
  303. $promoTitle .= "打". $promoInfo['rebate']. "折";
  304. }elseif ($promoInfo['rebate_type'] == 3){
  305. $rebate = (string) PromoRebate::query()->join('product','promo_rebate.product_id','=','product.id')->where([['promo_id','=',$promoInfo['id']]])->value('product.name');
  306. $promoTitle .= "赠送". $rebate;
  307. }
  308. $promoInfoCity = [];
  309. if ($promoInfo['city_ids']){
  310. $promoInfoCity = explode(',',$promoInfo['city_ids']);
  311. }
  312. // 判断是不是可以参与
  313. if (!$promoInfoCity || in_array($cityId,$promoInfoCity)){
  314. if( $promoInfo['tag_scope']) {
  315. // 解析数组
  316. $promoInfo['tag_scope'] = explode(',',$promoInfo['tag_scope']);
  317. // 标签范围限定时,默认不能参与
  318. $allowJoin = 0;
  319. // 判断标签是不是存在
  320. if ($tags){
  321. foreach ($tags as $v) {
  322. // 标签范围内,允许参加
  323. if( in_array($v['name'],$promoInfo['tag_scope']) ) $allowJoin = 1;
  324. }
  325. // 在范围
  326. if( $allowJoin ) {
  327. $data['promo_title'] = $promoTitle;
  328. }
  329. }
  330. }else{
  331. $data['promo_title'] = $promoTitle;
  332. }
  333. }
  334. }
  335. // 返回结果
  336. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  337. }
  338. /**
  339. * 获取产品列表 /api/product/get_sku
  340. *
  341. * @param int $id 产品id
  342. *
  343. * */
  344. public function get_sku(Request $request,ProductAttr $ProductAttr,ProductSpec $ProductSpec,ProductSkus $ProductSkus ){
  345. // 接口验签
  346. // $this->verify_sign();
  347. // 验证参数
  348. $request->scene('get_sku')->validate();
  349. // 检查登录
  350. $uid = $this->checkLogin();
  351. // 接收参数
  352. $id = request('id',0);
  353. // 获取产品属性
  354. $attr = $ProductAttr->getListByProductId($id);
  355. // 规格属性
  356. $specAttr = [];
  357. // 获取数据
  358. foreach ($attr as $value) {
  359. // 默认未选中
  360. $value['active'] = 0;
  361. $specAttr[$value['spec_id']]['spec_id'] = $value['spec_id'];
  362. $specAttr[$value['spec_id']]['spec_name'] = $ProductSpec->getOne($value['spec_id'],'name');
  363. $specAttr[$value['spec_id']]['attr_list'][] = $value;
  364. }
  365. // 获取规格详情数据
  366. $data['product_attr'] = $specAttr;
  367. // 获取SKU数据
  368. $data['product_sku'] = $ProductSkus->getListByProductId($id);
  369. // 返回结果
  370. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  371. }
  372. }