ShopController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ]);
  53. if ($validator->fails()) {
  54. return $this->jsonError($validator->errors()->first());
  55. }
  56. if($data['account']){
  57. $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
  58. if($shop_account){
  59. return $this->jsonError('该账号已存在,请重新输入');
  60. }
  61. }
  62. $shopRepository = new ShopRepository();
  63. $res = $shopRepository->saveShopAccount($data);
  64. if($res){
  65. return $this->jsonSuccess();
  66. }else{
  67. return $this->jsonError('添加失败');
  68. }
  69. }
  70. //详情
  71. public function view(Request $request)
  72. {
  73. $data = $request->all();
  74. $validator = Validator::make($data, [
  75. 'shop_id' => 'required|integer',
  76. ]);
  77. if ($validator->fails()) {
  78. return $this->jsonError($validator->errors()->first());
  79. }
  80. $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
  81. $fractal = new Manager();
  82. $res = new Item($shop,new ShopsTransformer());
  83. $data = $fractal->createData($res)->toArray();
  84. if($data){
  85. return $this->jsonSuccess($data);
  86. }else{
  87. return $this->jsonError("操作失败");
  88. }
  89. }
  90. //列表
  91. public function list(Request $request){
  92. $shops = $this->shopRepository->shopList($request->all());
  93. $fractal = new Manager();
  94. $resource = new Collection($shops, new ShopsTransformer());
  95. $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
  96. $data = $fractal->createData($resource)->toArray();
  97. $data['extra']['filters'] = ['shop_name','status'];
  98. $data['extra']['columns'] = ['shop_id','mobile','province_name','city_name','product_count','proportion','status'];
  99. return $data;
  100. }
  101. /**
  102. * @param Request $request
  103. * @return mixed
  104. * 通过token获取用户
  105. */
  106. public function getTokenShop(Request $request){
  107. $data = JWTAuth::decode(JWTAuth::getToken())['shop'];
  108. if($data->shop_id){
  109. $shop = Shop::where(['shop_id'=>$data->shop_id])->first();
  110. $fractal = new Manager();
  111. $res = new Item($shop,new ShopsTransformer());
  112. $data = $fractal->createData($res)->toArray();
  113. return $this->jsonSuccess($data);
  114. }else{
  115. return $this->jsonError('操作失败');
  116. }
  117. }
  118. }