Product.php 16 KB

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