ShopController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-04-28
  6. * Time: 15:31
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Repositories\ShopRepository;
  10. use App\Shop;
  11. use App\ShopAccount;
  12. use App\Transformers\ShopsTransformer;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Hash;
  15. use Illuminate\Support\Facades\Validator;
  16. use Illuminate\Validation\Rule;
  17. use League\Fractal\Manager;
  18. use League\Fractal\Resource\Item;
  19. use League\Fractal\Resource\Collection;
  20. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  21. class ShopController extends Controller {
  22. public function __construct(ShopRepository $shopRepository)
  23. {
  24. $this->shopRepository = $shopRepository;
  25. }
  26. //新增商家信息
  27. public function addShop(Request $request)
  28. {
  29. $data = $request->all();
  30. $validator = Validator::make($data, [
  31. 'shop_name' => 'required|max:11',
  32. 'shop_short_name' => 'required|string|max:100',
  33. 'mobile' => 'required|max:11',
  34. 'address' => 'required|string',
  35. 'province_name' => 'required|string|max:50',
  36. 'province_id' => 'required|integer|max:6',
  37. 'city_name' => 'required|string:max:50',
  38. 'city_id' => 'required|integer|max:6',
  39. 'contact_name' => 'string|max:50',
  40. 'contact_mobile' => 'max:16',
  41. 'shop_desc' => 'string|max:500',
  42. 'status' =>['required',Rule::in([0,1])],
  43. 'logo_img' => 'string',
  44. 'license_img' => 'required|string',
  45. 'food_trans_license' => 'required|string',
  46. 'other_license' => 'string',
  47. 'proportion'=>'required|digits_between:1,100|integer',
  48. 'verify_type'=>['required',Rule::in([0,1])],
  49. 'account'=>'string|max:20',
  50. 'password'=>'string|min:6|max:18'
  51. ]);
  52. if ($validator->fails()) {
  53. return $this->jsonError($validator->errors()->first());
  54. }
  55. if($data['account']){
  56. $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
  57. if($shop_account){
  58. return $this->jsonError('该账号已存在,请重新输入');
  59. }
  60. }
  61. $shopRepository = new ShopRepository();
  62. $res = $shopRepository->saveShopAccount($data);
  63. if($res){
  64. return $this->jsonSuccess();
  65. }else{
  66. return $this->jsonError('添加失败');
  67. }
  68. }
  69. //详情
  70. public function view(Request $request)
  71. {
  72. $data = $request->all();
  73. $validator = Validator::make($data, [
  74. 'shop_id' => 'required|integer',
  75. ]);
  76. if ($validator->fails()) {
  77. return $this->jsonError($validator->errors()->first());
  78. }
  79. $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
  80. $fractal = new Manager();
  81. $res = new Item($shop,new ShopsTransformer());
  82. $data = $fractal->createData($res)->toArray();
  83. if($data){
  84. return $this->jsonSuccess($data);
  85. }else{
  86. return $this->jsonError("操作失败");
  87. }
  88. }
  89. //列表
  90. public function list(Request $request){
  91. $shops = $this->shopRepository->shopList($request->all());
  92. $fractal = new Manager();
  93. $resource = new Collection($shops, new ShopsTransformer());
  94. $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
  95. $data = $fractal->createData($resource)->toArray();
  96. $data['extra']['filters'] = ['shop_name','status'];
  97. $data['extra']['columns'] = ['shop_id','mobile','province_name','city_name','product_count','proportion','status'];
  98. return $data;
  99. }
  100. }