CmsContentTemplateSetController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\CmsSubject;
  4. use App\Repositories\CmsContentTemplateSetRepository;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Validator;
  7. /**
  8. * Created by PhpStorm.
  9. * User: qinyaer
  10. * Date: 2019/5/5
  11. * Time: 上午9:30
  12. */
  13. class CmsContentTemplateSetController extends BaseController
  14. {
  15. public function __construct(CmsContentTemplateSetRepository $cmsContentTemplateSetRepository)
  16. {
  17. $this->cmsContentSetTemplate = $cmsContentTemplateSetRepository;
  18. }
  19. //内容预览
  20. public function preview(Request $request)
  21. {
  22. $validator = Validator::make($request->all(), [
  23. 'city_id' => 'required|integer',
  24. 'type' => 'required|integer',
  25. ]);
  26. if ($validator->fails()) {
  27. return $this->jsonError($validator->errors()->first());
  28. }
  29. return $this->jsonSuccess($this->cmsContentSetTemplate->preview($request->all()));
  30. }
  31. //兑换商城首页预览
  32. public function exchangeMall(Request $request)
  33. {
  34. return $this->jsonSuccess($this->cmsContentSetTemplate->exchangeMall($request->all()));
  35. }
  36. /**
  37. * 获取城市模板名称及类型
  38. * @param Request $request
  39. * @return array
  40. */
  41. public function template(Request $request){
  42. $validator = Validator::make($request->all(), [
  43. 'city_id' => 'required|integer'
  44. ]);
  45. if ($validator->fails()) {
  46. return $this->jsonError($validator->errors()->first());
  47. }
  48. return $this->jsonSuccess($this->cmsContentSetTemplate->getTemplate($request->only('city_id')));
  49. }
  50. //获取商品列表
  51. public function productList(Request $request)
  52. {
  53. $validator = Validator::make($request->all(), [
  54. 'subject_id' => 'required|integer',
  55. ]);
  56. if ($validator->fails()) {
  57. return $this->jsonError($validator->errors()->first());
  58. }
  59. $product_list = $this->cmsContentSetTemplate->productList($request->all());
  60. if ($product_list){
  61. return $this->jsonSuccess($product_list);
  62. }else{
  63. return $this->jsonError('没有找到对应商品');
  64. }
  65. }
  66. //获取某专题下的列表
  67. public function getProducts(Request $request)
  68. {
  69. $validator = Validator::make($request->all(), [
  70. 'subject_id' => 'required|integer',
  71. ]);
  72. if ($validator->fails()) {
  73. return $this->jsonError($validator->errors()->first());
  74. }
  75. $get_subject = CmsSubject::select('title', 'subject_img')->where('id', $request['subject_id'])->first();
  76. if (!$get_subject) {
  77. return $this->jsonError('专题ID不正确');
  78. }
  79. $product_list = $this->cmsContentSetTemplate->getProducts($request->all());
  80. if (count($product_list) > 0) {
  81. foreach ($product_list->toArray() as $k => $v) {
  82. $product_list[$k]['subject_title'] = $get_subject->title ?? '';
  83. $product_list[$k]['subject_img'] = $get_subject->subject_img ?? '';
  84. }
  85. return $this->jsonSuccess($product_list);
  86. } else {
  87. return $this->jsonError('没有找到对应商品');
  88. }
  89. }
  90. }