ShopController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\ProductRepository;
  10. use App\Repositories\ShopRepository;
  11. use App\Shop;
  12. use App\ShopAccount;
  13. use App\Transformers\ShopsTransformer;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Hash;
  16. use Illuminate\Support\Facades\Validator;
  17. use Illuminate\Validation\Rule;
  18. use League\Fractal\Manager;
  19. use League\Fractal\Resource\Item;
  20. use League\Fractal\Resource\Collection;
  21. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  22. use Tymon\JWTAuth\Facades\JWTAuth;
  23. class ShopController extends Controller {
  24. public function __construct(ShopRepository $shopRepository)
  25. {
  26. $this->shopRepository = $shopRepository;
  27. }
  28. //新增商家信息
  29. public function addShop(Request $request)
  30. {
  31. $data = $request->all();
  32. $validator = Validator::make($data, [
  33. 'shop_name' => 'required|max:11',
  34. 'shop_short_name' => 'required|string|max:100',
  35. 'mobile' => 'required|max:11',
  36. 'address' => 'required|string',
  37. 'province_name' => 'required|string|max:50',
  38. 'province_id' => 'required|integer|digits_between:1,7',
  39. 'city_name' => 'required|string:max:50',
  40. 'city_id' => 'required|integer|digits_between:1,7',
  41. 'contact_name' => 'string|max:50',
  42. 'contact_mobile' => 'max:16',
  43. 'shop_desc' => 'string|max:500',
  44. 'status' =>['required',Rule::in([0,1])],
  45. 'logo_img' => 'string',
  46. 'license_img' => 'required|string',
  47. 'food_trans_license' => 'required|string',
  48. 'other_license' => 'string',
  49. 'proportion'=>'required|digits_between:1,100|integer',
  50. 'verify_type'=>['required',Rule::in([0,1])],
  51. 'account'=>'string|max:20',
  52. 'password'=>'string|min:6|max:18',
  53. // 'shop_id'=>'integer',
  54. // 'shop_account_id'=>'integer',
  55. ]);
  56. if ($validator->fails()) {
  57. return $this->response->error($validator->errors()->first(), 500);
  58. }
  59. $shop = Shop::where(['shop_name'=>$data['account']])->first();
  60. if($shop){
  61. return $this->response->error('该商户已存在,请重新输入', 500);
  62. }
  63. $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
  64. if($shop_account){
  65. return $this->response->error('该账号已存在,请重新输入', 500);
  66. }
  67. $shopRepository = new ShopRepository();
  68. $res = $shopRepository->saveShopAccount($data);
  69. if($res){
  70. return ['message' => '成功','status_code' => 200];
  71. }else{
  72. return $this->response->error("失败", 500);
  73. }
  74. }
  75. public function editShop(Request $request)
  76. {
  77. $data = $request->all();
  78. $validator = Validator::make($data, [
  79. 'shop_name' => 'required|max:11',
  80. 'shop_short_name' => 'required|string|max:100',
  81. 'mobile' => 'required|max:11',
  82. 'address' => 'required|string',
  83. 'province_name' => 'required|string|max:50',
  84. 'province_id' => 'required|integer|digits_between:1,7',
  85. 'city_name' => 'required|string:max:50',
  86. 'city_id' => 'required|integer|digits_between:1,7',
  87. 'contact_name' => 'string|max:50',
  88. 'contact_mobile' => 'max:16',
  89. 'shop_desc' => 'string|max:500',
  90. 'status' =>['required',Rule::in([0,1])],
  91. 'logo_img' => 'string',
  92. 'license_img' => 'required|string',
  93. 'food_trans_license' => 'required|string',
  94. 'other_license' => 'string',
  95. 'proportion'=>'required|digits_between:1,100|integer',
  96. 'verify_type'=>['required',Rule::in([0,1])],
  97. 'account'=>'string|max:20',
  98. 'password'=>'string|min:6|max:18',
  99. 'shop_id'=>'integer|required',
  100. 'shop_account_id'=>'integer|required',
  101. ]);
  102. if ($validator->fails()) {
  103. return $this->response->error($validator->errors()->first(), 500);
  104. }
  105. $shop = Shop::where('shop_name', '=', $data['shop_name'])->where('shop_id', '<>', $data['shop_id'])->first();
  106. if($shop){
  107. return $this->response->error('该商户已存在,请重新输入', 500);
  108. }
  109. $shop_account = ShopAccount::where('account',$data['account'])->where('id', '<>', $data['shop_account_id'])->first();
  110. if($shop_account){
  111. return $this->response->error('该账号已存在,请重新输入11', 500);
  112. }
  113. $shopRepository = new ShopRepository();
  114. $res = $shopRepository->editShopAccount($data);
  115. if($res){
  116. return ['message' => '成功','status_code' => 200];
  117. }else{
  118. return $this->response->error("失败", 500);
  119. }
  120. }
  121. //详情
  122. public function view(Request $request)
  123. {
  124. $data = $request->only('shop_id');
  125. $validator = Validator::make($data, [
  126. 'shop_id' => 'required|integer',
  127. ]);
  128. if ($validator->fails()) {
  129. return $this->response->error($validator->errors()->first(), 500);
  130. }
  131. $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
  132. $fractal = new Manager();
  133. $res = new Item($shop,new ShopsTransformer());
  134. return $fractal->createData($res)->toArray();
  135. }
  136. //列表
  137. public function list(Request $request){
  138. $shops = $this->shopRepository->shopList($request->all());
  139. $fractal = new Manager();
  140. $resource = new Collection($shops, new ShopsTransformer());
  141. $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
  142. $data = $fractal->createData($resource)->toArray();
  143. $data['extra']['filters'] = ['shop_name','status'];
  144. $data['extra']['columns'] = ['shop_id','shop_name','mobile','province_name','city_name','product_count','proportion','status'];
  145. return $data;
  146. }
  147. /**
  148. * @param Request $request
  149. * @return array
  150. */
  151. public function isOpen(Request $request){
  152. $data = $request->only('shop_id','status');
  153. $validator = Validator::make($data, [
  154. 'shop_id' => 'required|integer',
  155. 'status' =>['required',Rule::in([0,1])],
  156. ]);
  157. if ($validator->fails()) {
  158. return $this->response->error($validator->errors()->first(), 500);
  159. }
  160. $res = Shop::where(['shop_id'=>$data['shop_id']])->update(['status'=>$data['status']]);
  161. if($res){
  162. //下架商家所有商品
  163. if($data['status'] == 0){
  164. $product = new ProductRepository();
  165. $upStatus = $product->upProductStatus($data['shop_id']);
  166. if($upStatus['status_code'] == 200){
  167. return ['message' => '成功','status_code' => 200];
  168. }
  169. }
  170. }else{
  171. return $this->response->error("失败", 500);
  172. }
  173. }
  174. /**
  175. * @param Request $request
  176. * @return mixed
  177. * 通过token获取用户
  178. */
  179. public function getTokenShop(Request $request){
  180. $data = JWTAuth::decode(JWTAuth::getToken())['shop'];
  181. if($data->shop_id){
  182. $shop = Shop::where(['shop_id'=>$data->shop_id])->first();
  183. $fractal = new Manager();
  184. $res = new Item($shop,new ShopsTransformer());
  185. $data = $fractal->createData($res)->toArray();
  186. return $this->jsonSuccess($data);
  187. }else{
  188. return $this->jsonError('操作失败');
  189. }
  190. }
  191. /**
  192. * @param Request $request
  193. * @return array
  194. * 删除账号
  195. */
  196. public function deleteShopAccount(Request $request){
  197. $data = $request->only('shop_id');
  198. $validator = Validator::make($data, [
  199. 'id' => 'required|integer',
  200. 'shop_id' => 'required|integer',
  201. ]);
  202. if ($validator->fails()) {
  203. return $this->response->error($validator->errors()->first(), 500);
  204. }
  205. $res = ShopAccount::where(['id'=>$data['id'],'shop_id'=>$data['shop_id']])->delete();
  206. if($res){
  207. //下架商家所有商品,放入队列
  208. return ['message' => '成功','status_code' => 200];
  209. }else{
  210. return $this->response->error("失败", 500);
  211. }
  212. }
  213. }