ShopController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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|digits_between:1,7',
  38. 'city_name' => 'required|string:max:50',
  39. 'city_id' => 'required|integer|digits_between:1,7',
  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. // 'shop_account_id'=>'integer',
  54. ]);
  55. if ($validator->fails()) {
  56. return $this->response->error($validator->errors()->first(), 500);
  57. }
  58. $shop = Shop::where(['shop_name'=>$data['account']])->first();
  59. if($shop){
  60. return $this->response->error('该商户已存在,请重新输入', 500);
  61. }
  62. $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
  63. if($shop_account){
  64. return $this->response->error('该账号已存在,请重新输入', 500);
  65. }
  66. $shopRepository = new ShopRepository();
  67. $res = $shopRepository->saveShopAccount($data);
  68. if($res){
  69. return ['message' => '成功','status_code' => 200];
  70. }else{
  71. return $this->response->error("失败", 500);
  72. }
  73. }
  74. public function editShop(Request $request)
  75. {
  76. $data = $request->all();
  77. $validator = Validator::make($data, [
  78. 'shop_name' => 'required|max:11',
  79. 'shop_short_name' => 'required|string|max:100',
  80. 'mobile' => 'required|max:11',
  81. 'address' => 'required|string',
  82. 'province_name' => 'required|string|max:50',
  83. 'province_id' => 'required|integer|digits_between:1,7',
  84. 'city_name' => 'required|string:max:50',
  85. 'city_id' => 'required|integer|digits_between:1,7',
  86. 'contact_name' => 'string|max:50',
  87. 'contact_mobile' => 'max:16',
  88. 'shop_desc' => 'string|max:500',
  89. 'status' =>['required',Rule::in([0,1])],
  90. 'logo_img' => 'string',
  91. 'license_img' => 'required|string',
  92. 'food_trans_license' => 'required|string',
  93. 'other_license' => 'string',
  94. 'proportion'=>'required|digits_between:1,100|integer',
  95. 'verify_type'=>['required',Rule::in([0,1])],
  96. 'account'=>'string|max:20',
  97. 'password'=>'string|min:6|max:18',
  98. 'shop_id'=>'integer|required',
  99. 'shop_account_id'=>'integer|required',
  100. ]);
  101. if ($validator->fails()) {
  102. return $this->response->error($validator->errors()->first(), 500);
  103. }
  104. $shop = Shop::where('shop_name', '=', $data['shop_name'])->where('shop_id', '<>', $data['shop_id'])->first();
  105. if($shop){
  106. return $this->response->error('该商户已存在,请重新输入', 500);
  107. }
  108. $shop_account = ShopAccount::where('account',$data['account'])->where('id', '<>', $data['shop_account_id'])->first();
  109. if($shop_account){
  110. return $this->response->error('该账号已存在,请重新输入11', 500);
  111. }
  112. $shopRepository = new ShopRepository();
  113. $res = $shopRepository->editShopAccount($data);
  114. if($res){
  115. return ['message' => '成功','status_code' => 200];
  116. }else{
  117. return $this->response->error("失败", 500);
  118. }
  119. }
  120. //详情
  121. public function view(Request $request)
  122. {
  123. $data = $request->only('shop_id');
  124. $validator = Validator::make($data, [
  125. 'shop_id' => 'required|integer',
  126. ]);
  127. if ($validator->fails()) {
  128. return $this->response->error($validator->errors()->first(), 500);
  129. }
  130. $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
  131. $fractal = new Manager();
  132. $res = new Item($shop,new ShopsTransformer());
  133. return $fractal->createData($res)->toArray();
  134. }
  135. //列表
  136. public function list(Request $request){
  137. $shops = $this->shopRepository->shopList($request->all());
  138. $fractal = new Manager();
  139. $resource = new Collection($shops, new ShopsTransformer());
  140. $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
  141. $data = $fractal->createData($resource)->toArray();
  142. $data['extra']['filters'] = ['shop_name','status'];
  143. $data['extra']['columns'] = ['shop_id','shop_name','mobile','province_name','city_name','product_count','proportion','status'];
  144. return $data;
  145. }
  146. /**
  147. * @param Request $request
  148. * @return mixed
  149. * 通过token获取用户
  150. */
  151. public function getTokenShop(Request $request){
  152. $data = JWTAuth::decode(JWTAuth::getToken())['shop'];
  153. if($data->shop_id){
  154. $shop = Shop::where(['shop_id'=>$data->shop_id])->first();
  155. $fractal = new Manager();
  156. $res = new Item($shop,new ShopsTransformer());
  157. $data = $fractal->createData($res)->toArray();
  158. return $this->jsonSuccess($data);
  159. }else{
  160. return $this->jsonError('操作失败');
  161. }
  162. }
  163. }