ShopController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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:11',
  35. 'shop_short_name' => 'required|string|max:100',
  36. 'mobile' => 'required|max:11',
  37. 'address' => 'required|string',
  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:50',
  43. 'contact_mobile' => 'max:16',
  44. 'shop_desc' => 'string|max:500',
  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|digits_between:1,100|integer',
  51. 'verify_type'=>['required',Rule::in([0,1])],
  52. 'account'=>'string|max:20',
  53. 'password'=>'string|min:6|max:18',
  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:11',
  81. 'shop_short_name' => 'required|string|max:100',
  82. 'mobile' => 'required|max:11',
  83. 'address' => 'required|string',
  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:50',
  89. 'contact_mobile' => 'max:16',
  90. 'shop_desc' => 'string|max:500',
  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|digits_between:1,100|integer',
  97. 'verify_type'=>['required',Rule::in([0,1])],
  98. 'account'=>'string|max:20',
  99. 'password'=>'string|min:6|max:18',
  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','product_count','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. Log::info('商家id'.$data['shop_id'].'商品禁售成功');
  172. return ['message' => '成功','status_code' => 200];
  173. }else{
  174. Log::info('商家id'.$data['shop_id'].'商品禁售失败');
  175. }
  176. }
  177. return ['message' => '成功','status_code' => 200];
  178. }else{
  179. return $this->response->error("失败", 500);
  180. }
  181. } catch (\Exception $e) {
  182. DB::rollBack();
  183. return $this->response->error("失败", 500);
  184. }
  185. }
  186. /**
  187. * @param Request $request
  188. * @return mixed
  189. * 通过token获取用户
  190. */
  191. public function getTokenShop(Request $request){
  192. $data = JWTAuth::decode(JWTAuth::getToken())['shop'];
  193. if($data->shop_id){
  194. $shop = Shop::where(['shop_id'=>$data->shop_id])->first();
  195. $fractal = new Manager();
  196. $res = new Item($shop,new ShopsTransformer());
  197. $data = $fractal->createData($res)->toArray();
  198. return $this->jsonSuccess($data);
  199. }else{
  200. return $this->jsonError('操作失败');
  201. }
  202. }
  203. /**
  204. * @param Request $request
  205. * @return array
  206. * 删除账号
  207. */
  208. public function deleteShopAccount(Request $request){
  209. $data = $request->only('shop_id');
  210. $validator = Validator::make($data, [
  211. 'id' => 'required|integer',
  212. 'shop_id' => 'required|integer',
  213. ]);
  214. if ($validator->fails()) {
  215. return $this->response->error($validator->errors()->first(), 500);
  216. }
  217. $res = ShopAccount::where(['id'=>$data['id'],'shop_id'=>$data['shop_id']])->delete();
  218. if($res){
  219. //下架商家所有商品,放入队列
  220. return ['message' => '成功','status_code' => 200];
  221. }else{
  222. return $this->response->error("失败", 500);
  223. }
  224. }
  225. }