ShopController.php 9.0 KB

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