ShopController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. use Tymon\JWTAuth\Facades\JWTAuth;
  22. class ShopController extends Controller {
  23. public function __construct(ShopRepository $shopRepository)
  24. {
  25. $this->shopRepository = $shopRepository;
  26. }
  27. //新增商家信息
  28. public function addShop(Request $request)
  29. {
  30. $data = $request->all();
  31. $validator = Validator::make($data, [
  32. 'shop_name' => 'required|max:11',
  33. 'shop_short_name' => 'required|string|max:100',
  34. 'mobile' => 'required|max:11',
  35. 'address' => 'required|string',
  36. 'province_name' => 'required|string|max:50',
  37. 'province_id' => 'required|integer|max:6',
  38. 'city_name' => 'required|string:max:50',
  39. 'city_id' => 'required|integer|max:6',
  40. 'contact_name' => 'string|max:50',
  41. 'contact_mobile' => 'max:16',
  42. 'shop_desc' => 'string|max:500',
  43. 'status' =>['required',Rule::in([0,1])],
  44. 'logo_img' => 'string',
  45. 'license_img' => 'required|string',
  46. 'food_trans_license' => 'required|string',
  47. 'other_license' => 'string',
  48. 'proportion'=>'required|digits_between:1,100|integer',
  49. 'verify_type'=>['required',Rule::in([0,1])],
  50. 'account'=>'string|max:20',
  51. 'password'=>'string|min:6|max:18',
  52. 'shop_id'=>'integer',
  53. ]);
  54. if ($validator->fails()) {
  55. return $this->jsonError($validator->errors()->first());
  56. }
  57. // if($data['account']){
  58. // $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
  59. // if($shop_account){
  60. // return $this->jsonError('该账号已存在,请重新输入');
  61. // }
  62. // }
  63. $shopRepository = new ShopRepository();
  64. $res = $shopRepository->saveShopAccount($data);
  65. if($res){
  66. return $this->jsonSuccess();
  67. }else{
  68. return $this->jsonError('添加失败');
  69. }
  70. }
  71. //详情
  72. public function view(Request $request)
  73. {
  74. $data = $request->only('shop_id');
  75. $validator = Validator::make($data, [
  76. 'shop_id' => 'required|integer',
  77. ]);
  78. if ($validator->fails()) {
  79. return $this->jsonError($validator->errors()->first());
  80. }
  81. $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
  82. $fractal = new Manager();
  83. $res = new Item($shop,new ShopsTransformer());
  84. $data = $fractal->createData($res)->toArray();
  85. if($data){
  86. return $this->jsonSuccess($data);
  87. }else{
  88. return $this->jsonError("操作失败");
  89. }
  90. }
  91. //列表
  92. public function list(Request $request){
  93. $shops = $this->shopRepository->shopList($request->all());
  94. $fractal = new Manager();
  95. $resource = new Collection($shops, new ShopsTransformer());
  96. $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
  97. $data = $fractal->createData($resource)->toArray();
  98. $data['extra']['filters'] = ['shop_name','status'];
  99. $data['extra']['columns'] = ['shop_id','shop_name','mobile','province_name','city_name','product_count','proportion','status'];
  100. return $data;
  101. }
  102. /**
  103. * @param Request $request
  104. * @return mixed
  105. * 通过token获取用户
  106. */
  107. public function getTokenShop(Request $request){
  108. $data = JWTAuth::decode(JWTAuth::getToken())['shop'];
  109. if($data->shop_id){
  110. $shop = Shop::where(['shop_id'=>$data->shop_id])->first();
  111. $fractal = new Manager();
  112. $res = new Item($shop,new ShopsTransformer());
  113. $data = $fractal->createData($res)->toArray();
  114. return $this->jsonSuccess($data);
  115. }else{
  116. return $this->jsonError('操作失败');
  117. }
  118. }
  119. }