GoodsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\S1CartInfoModel;
  4. use App\Models\S1CateAttrModel;
  5. use App\Models\S1CommentInfoModel;
  6. use App\Models\S1GoodsInfoModel;
  7. use App\Models\S1UserFavoriteModel;
  8. use App\Services\Base\ErrorCode;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Validator;
  14. class GoodsController extends Controller
  15. {
  16. /**
  17. * @api {get} /api/goods/show 商品详情
  18. * @apiDescription 商品详情
  19. * @apiGroup Goods
  20. * @apiParam {string} appid appid
  21. * @apiParam {int} id 商品id
  22. * @apiPermission none
  23. * @apiVersion 0.1.0
  24. * @apiSuccessExample {json} Success-Response:
  25. * HTTP/1.1 200 OK
  26. {
  27. "status": true,
  28. "status_code": 0,
  29. "message": "",
  30. "data": {
  31. "id": 1,
  32. "appid": "dsfbdffd32434dfvdf",
  33. "cate_id": 4,
  34. "is_home_cate": 1,
  35. "home_cate_id": 3,
  36. "attr_id": 0,
  37. "name": "商品1",
  38. "number": "0011",
  39. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  40. "weight": 1001,
  41. "stock": 10081,
  42. "status": 1,
  43. "market_price": 100100,
  44. "price": 101100,
  45. "cost_price": 99100,
  46. "junior_price": 0,
  47. "middle_price": 0,
  48. "top_price": 0,
  49. "sale_number": 91,
  50. "info": "好东西1",
  51. "is_address": 1,
  52. "sort": 11,
  53. "is_favorite": 1, 或 0 1已经收藏
  54. "ground_time": "2017-10-13 10:29:19",
  55. "draw_time": "0000-00-00 00:00:00",
  56. "created_at": "2017-10-13 02:29:19",
  57. "updated_at": "2017-10-13 03:02:21",
  58. "deleted_at": null,
  59. 'attr_info':null 已经选择的属性
  60. "attr": { 属性
  61. "规格": [
  62. "ML",
  63. "L",
  64. "XL",
  65. "XXL"
  66. ],
  67. "颜色": [
  68. "黑色",
  69. "白色"
  70. ]
  71. },
  72. "images": [ 图片
  73. {
  74. "url": "/upload/s1/goods/shows/20171013/46072732d071540939acd5e875f383dd.jpg",
  75. },
  76. ],
  77. "comments": [ 评论
  78. {
  79. "id": 1,
  80. "wx_user_id": 1,
  81. "wx_user_avatar": "0", 头像
  82. "wx_user_nickname": "0", 昵称
  83. "comment": "dsgdsfdfsdfbf",
  84. "created_at": null,
  85. "updated_at": null
  86. }
  87. ]
  88. }
  89. }
  90. * @apiErrorExample {json} Error-Response:
  91. * HTTP/1.1 400 Bad Request
  92. */
  93. public function show(Request $request)
  94. {
  95. $user = Auth::guard('api')->user();
  96. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  97. $validator = Validator::make($request->all(),
  98. [
  99. 'id' => 'required',
  100. 'appid' => 'required',
  101. ],
  102. [
  103. 'id.required' => '商品不存在',
  104. 'appid.required' => 'appid不存在',
  105. ]
  106. );
  107. if ($validator->fails()) {
  108. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  109. }
  110. $appid = $request->input('appid');
  111. $id = $request->input('id');
  112. $is_favorite = S1UserFavoriteModel::where('s1_goods_id',$id)->where('wx_user_id',$user->id)->first();
  113. if (empty($is_favorite)){
  114. $is_favorite = 0;
  115. }else{
  116. $is_favorite = 1;
  117. }
  118. $goods = S1GoodsInfoModel::with('images','comments')->find($id);
  119. $attr = S1CateAttrModel::where('cate_id',$goods->home_cate_id)->get()->toArray();
  120. $attr_info = [] ;
  121. foreach ($attr as $key =>$item) {
  122. $attr_info[$key]['attr_group_name'] = $item['name'];
  123. $attr_info[$key]['attr_group_id'] = $item['id'];
  124. $arr = ( explode(',',$item['value']));
  125. foreach ($arr as $k => $v){
  126. $attr_info[$key]['attr_list'][] = ['id'=>$k,'name'=>$v];
  127. }
  128. }
  129. /* $attr_info = array_column($attr,'value','name');
  130. foreach ($attr_info as $key => $value) {
  131. $arr = explode(',',$value);
  132. $attr_info[$key] = $arr;
  133. }*/
  134. $goods->is_favorite = $is_favorite;
  135. $goods->attr = $attr_info;
  136. if (session('attr_info') && session('attr_info')['user_id']==$user->id && session('attr_info')['goods_id']==$id) {
  137. $goods->attr_info = session('attr_info')['attr_info'];
  138. }else{
  139. $goods->attr_info = null;
  140. }
  141. return $this->api($goods);
  142. }
  143. /**
  144. * @api {get} /api/goods/chose_attr 选择属性
  145. * @apiDescription 选择属性
  146. * @apiGroup Goods
  147. * @apiParam {int} id 商品id
  148. * @apiParam {array} attr 商品属性
  149. * @apiPermission none
  150. * @apiVersion 0.1.0
  151. * @apiSuccessExample {json} Success-Response:
  152. * HTTP/1.1 200 OK
  153. * @apiErrorExample {json} Error-Response:
  154. * HTTP/1.1 400 Bad Request
  155. */
  156. public function choseAttr(Request $request)
  157. {
  158. $user = Auth::guard('api')->user();
  159. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  160. $validator = Validator::make($request->all(),
  161. [
  162. 'id' => 'required',
  163. 'attr' => 'required',
  164. ],
  165. [
  166. 'id.required' => '商品不存在',
  167. 'attr.required' => '商品属性不存在',
  168. ]
  169. );
  170. if ($validator->fails()) {
  171. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  172. }
  173. $attr = json_decode($request->input('attr'),true);
  174. $id = $request->input('id');
  175. $str = '';
  176. if (is_array($attr)) {
  177. foreach ($attr as $k => $v){
  178. $str .= $v['attr_group_name'].':'.$v['attr_name'].',';
  179. }
  180. }
  181. $arr = [
  182. 'user_id'=>$user->id,
  183. 'goods_id'=>$id,
  184. 'attr_info'=>$str
  185. ];
  186. Cache::put('attr_info', $arr, 10); //存储十分钟
  187. $id = $request->input('id');
  188. $is_favorite = S1UserFavoriteModel::where('s1_goods_id',$id)->where('wx_user_id',$user->id)->first();
  189. if (empty($is_favorite)){
  190. $is_favorite = 0;
  191. }else{
  192. $is_favorite = 1;
  193. }
  194. $goods = S1GoodsInfoModel::with('images','comments')->find($id);
  195. $attr = S1CateAttrModel::where('cate_id',$goods->home_cate_id)->get()->toArray();
  196. $attr_info = [] ;
  197. foreach ($attr as $key =>$item) {
  198. $attr_info[$key]['attr_group_name'] = $item['name'];
  199. $attr_info[$key]['attr_group_id'] = $item['id'];
  200. $arr = ( explode(',',$item['value']));
  201. foreach ($arr as $k => $v){
  202. $attr_info[$key]['attr_list'][] = ['id'=>$k,'name'=>$v];
  203. }
  204. }
  205. $goods->is_favorite = $is_favorite;
  206. $goods->attr = $attr_info;
  207. if (Cache::get('attr_info') && Cache::get('attr_info')['user_id']==$user->id && Cache::get('attr_info')['goods_id']==$id) {
  208. $goods->attr_info = Cache::get('attr_info')['attr_info'];
  209. }else{
  210. $goods->attr_info = null;
  211. }
  212. return $this->api($goods);
  213. }
  214. /**
  215. * @api {get} /api/goods/addtocar 添加至购物车
  216. * @apiDescription 添加至购物车
  217. * @apiGroup Goods
  218. * @apiParam {string} appid appid
  219. * @apiParam {int} id 商品id
  220. * @apiParam {int} number 商品数量
  221. * @apiParam {array} attr 商品属性
  222. * @apiPermission none
  223. * @apiVersion 0.1.0
  224. * @apiSuccessExample {json} Success-Response:
  225. * HTTP/1.1 200 OK
  226. * @apiErrorExample {json} Error-Response:
  227. * HTTP/1.1 400 Bad Request
  228. */
  229. public function addToCar(Request $request)
  230. {
  231. $user = Auth::guard('api')->user();
  232. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  233. $validator = Validator::make($request->all(),
  234. [
  235. 'id' => 'required',
  236. 'appid' => 'required',
  237. 'number' => 'required',
  238. 'attr' => 'required',
  239. ],
  240. [
  241. 'id.required' => '商品不存在',
  242. 'appid.required' => 'appid不存在',
  243. 'number.required' => '商品数量不能为空',
  244. 'attr.required' => '商品属性不能为空',
  245. ]
  246. );
  247. if ($validator->fails()) {
  248. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  249. }
  250. $appid = $request->input('appid');
  251. $id = $request->input('id');
  252. $number = $request->input('number');
  253. $attr = json_decode($request->input('attr'),true);
  254. $str = '';
  255. if (is_array($attr)) {
  256. foreach ($attr as $k => $v){
  257. $str .= $v['attr_group_name'].':'.$v['attr_name'].',';
  258. }
  259. }
  260. $arr = [
  261. 'appid'=>$appid,
  262. 'wx_user_id'=>$user->id,
  263. 's1_goods_id'=>$id,
  264. 'number'=>$number,
  265. 'attr_info'=>$str,
  266. ];
  267. $data = S1CartInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->where('s1_goods_id',$id)->first();
  268. if(empty($data)){
  269. $ok = S1CartInfoModel::create($arr);
  270. }else{
  271. $data->goods_num += $number;
  272. $ok = $data->save();
  273. }
  274. if($ok) return $this->api('');
  275. return $this->error(ErrorCode::OP_ERROR);
  276. }
  277. /**
  278. * @api {get} /api/goods/collection 收藏/取消
  279. * @apiDescription 收藏/取消
  280. * @apiGroup Goods
  281. * @apiParam {int} id 商品id
  282. * @apiParam {int} value 0或1 0(取消收藏)
  283. * @apiPermission none
  284. * @apiVersion 0.1.0
  285. * @apiSuccessExample {json} Success-Response:
  286. * HTTP/1.1 200 OK
  287. {
  288. "status": true,
  289. "status_code": 0,
  290. "message": "",
  291. "data": ""
  292. }
  293. * @apiErrorExample {json} Error-Response:
  294. * HTTP/1.1 400 Bad Request
  295. */
  296. public function collection(Request $request)
  297. {
  298. $user = Auth::guard('api')->user();
  299. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  300. $validator = Validator::make($request->all(),
  301. [
  302. 'id' => 'required',
  303. 'value' => 'required',
  304. ],
  305. [
  306. 'id.required' => '商品不存在',
  307. 'value.required' => '收藏值不存在',
  308. ]
  309. );
  310. if ($validator->fails()) {
  311. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  312. }
  313. $s1_goods_id = $request->input('id');
  314. $value = $request->input('value');
  315. $wx_user_id = $user->id;
  316. $arr = compact('s1_goods_id','wx_user_id');
  317. if ($value==1) {
  318. $ok = S1UserFavoriteModel::firstOrCreate($arr);
  319. }else{
  320. $ok = S1UserFavoriteModel::where('s1_goods_id',$s1_goods_id)->where('wx_user_id',$wx_user_id)->delete();
  321. }
  322. if ($ok) return $this->api('');
  323. return $this->error(ErrorCode::OP_ERROR);
  324. }
  325. /**
  326. * @api {post} /api/goods/comment 评价/回复评价
  327. * @apiDescription 评价
  328. * @apiGroup Goods
  329. * @apiParam {int} goods_id 商品id
  330. * @apiParam {int} level 评论星级(321好中差)
  331. * @apiParam {string} comment 评论内容不能为空
  332. * @apiParam {int} [comment_id] 评论id
  333. * @apiParam {array} [images] 上传图片
  334. * @apiPermission none
  335. * @apiVersion 0.1.0
  336. * @apiSuccessExample {json} Success-Response:
  337. * HTTP/1.1 200 OK
  338. {
  339. "status": true,
  340. "status_code": 0,
  341. "message": "",
  342. "data": ""
  343. }
  344. * @apiErrorExample {json} Error-Response:
  345. * HTTP/1.1 400 Bad Request
  346. {
  347. "status": false,
  348. "status_code": 2006,
  349. "message": "操作失败",
  350. "data": null
  351. }
  352. */
  353. public function comment(Request $request)
  354. {
  355. $user = Auth::guard('api')->user();
  356. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  357. $validator = Validator::make($request->all(),
  358. [
  359. 'goods_id' => 'required',
  360. 'level' => 'required',
  361. 'comment' => 'required',
  362. ],
  363. [
  364. 'goods_id.required' => '商品不存在',
  365. 'comment.required' => '评论内容不能为空',
  366. 'level.required' => '评论星级不存在',
  367. ]
  368. );
  369. if ($validator->fails()) {
  370. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  371. }
  372. $goods_id = $request->input('goods_id');
  373. $level = $request->input('level');
  374. $images = $request->input('images');
  375. $comment = $request->input('comment');
  376. $comment_id = $request->input('comment_id');
  377. $arr = [];
  378. $arr['wx_user_id'] = $user->id;
  379. $arr['wx_user_avatar'] = $user->avatar;
  380. $arr['wx_user_nickname'] = $user->nickname;
  381. $arr['s1_goods_id'] = $goods_id;
  382. $arr['comment'] = $comment;
  383. $arr['level'] = $level;
  384. if(!empty($images)) $arr['images'] = json_encode($images);
  385. if ($comment_id) { //表示回复
  386. $arr['comment_info_id'] = $comment_id;
  387. }
  388. $ok = S1CommentInfoModel::firstOrCreate($arr);
  389. if($ok) return $this->api('');
  390. return $this->error(ErrorCode::OP_ERROR);
  391. }
  392. }