123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019-04-28
- * Time: 15:31
- */
- namespace App\Http\Controllers\V1;
- use App\Repositories\ProductRepository;
- use App\Repositories\ShopRepository;
- use App\Shop;
- use App\ShopAccount;
- use App\Transformers\ShopsTransformer;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Validation\Rule;
- use League\Fractal\Manager;
- use League\Fractal\Resource\Item;
- use League\Fractal\Resource\Collection;
- use League\Fractal\Pagination\IlluminatePaginatorAdapter;
- use Tymon\JWTAuth\Facades\JWTAuth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class ShopController extends Controller {
- public function __construct(ShopRepository $shopRepository)
- {
- $this->shopRepository = $shopRepository;
- }
- //新增商家信息
- public function addShop(Request $request)
- {
- $data = $request->all();
- $validator = Validator::make($data, [
- 'shop_name' => 'required|max:11',
- 'shop_short_name' => 'required|string|max:100',
- 'mobile' => 'required|max:11',
- 'address' => 'required|string',
- 'province_name' => 'required|string|max:50',
- 'province_id' => 'required|integer|digits_between:1,7',
- 'city_name' => 'required|string:max:50',
- 'city_id' => 'required|integer|digits_between:1,7',
- 'contact_name' => 'string|max:50',
- 'contact_mobile' => 'max:16',
- 'shop_desc' => 'string|max:500',
- 'status' =>['required',Rule::in([0,1])],
- 'logo_img' => 'string',
- 'license_img' => 'required|string',
- 'food_trans_license' => 'required|string',
- 'other_license' => 'string',
- 'proportion'=>'required|digits_between:1,100|integer',
- 'verify_type'=>['required',Rule::in([0,1])],
- 'account'=>'string|max:20',
- 'password'=>'string|min:6|max:18',
- // 'shop_id'=>'integer',
- // 'shop_account_id'=>'integer',
- ]);
- if ($validator->fails()) {
- return $this->response->error($validator->errors()->first(), 500);
- }
- $shop = Shop::where(['shop_name'=>$data['account']])->first();
- if($shop){
- return $this->response->error('该商户已存在,请重新输入', 500);
- }
- $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
- if($shop_account){
- return $this->response->error('该账号已存在,请重新输入', 500);
- }
- $shopRepository = new ShopRepository();
- $res = $shopRepository->saveShopAccount($data);
- if($res){
- return ['message' => '成功','status_code' => 200];
- }else{
- return $this->response->error("失败", 500);
- }
- }
- public function editShop(Request $request)
- {
- $data = $request->all();
- $validator = Validator::make($data, [
- 'shop_name' => 'required|max:11',
- 'shop_short_name' => 'required|string|max:100',
- 'mobile' => 'required|max:11',
- 'address' => 'required|string',
- 'province_name' => 'required|string|max:50',
- 'province_id' => 'required|integer|digits_between:1,7',
- 'city_name' => 'required|string:max:50',
- 'city_id' => 'required|integer|digits_between:1,7',
- 'contact_name' => 'string|max:50',
- 'contact_mobile' => 'max:16',
- 'shop_desc' => 'string|max:500',
- 'status' =>['required',Rule::in([0,1])],
- 'logo_img' => 'string',
- 'license_img' => 'required|string',
- 'food_trans_license' => 'required|string',
- 'other_license' => 'string',
- 'proportion'=>'required|digits_between:1,100|integer',
- 'verify_type'=>['required',Rule::in([0,1])],
- 'account'=>'string|max:20',
- 'password'=>'string|min:6|max:18',
- 'shop_id'=>'integer|required',
- 'shop_account_id'=>'integer|required',
- ]);
- if ($validator->fails()) {
- return $this->response->error($validator->errors()->first(), 500);
- }
- $shop = Shop::where('shop_name', '=', $data['shop_name'])->where('shop_id', '<>', $data['shop_id'])->first();
- if($shop){
- return $this->response->error('该商户已存在,请重新输入', 500);
- }
- $shop_account = ShopAccount::where('account',$data['account'])->where('id', '<>', $data['shop_account_id'])->first();
- if($shop_account){
- return $this->response->error('该账号已存在,请重新输入11', 500);
- }
- $shopRepository = new ShopRepository();
- $res = $shopRepository->editShopAccount($data);
- if($res){
- return ['message' => '成功','status_code' => 200];
- }else{
- return $this->response->error("失败", 500);
- }
- }
- //详情
- public function view(Request $request)
- {
- $data = $request->only('shop_id');
- $validator = Validator::make($data, [
- 'shop_id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return $this->response->error($validator->errors()->first(), 500);
- }
- $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
- $fractal = new Manager();
- $res = new Item($shop,new ShopsTransformer());
- return $fractal->createData($res)->toArray();
- }
- //列表
- public function list(Request $request){
- $shops = $this->shopRepository->shopList($request->all());
- $fractal = new Manager();
- $resource = new Collection($shops, new ShopsTransformer());
- $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
- $data = $fractal->createData($resource)->toArray();
- $data['extra']['filters'] = ['shop_name','status'];
- $data['extra']['columns'] = ['shop_id','shop_name','mobile','province_name','city_name','product_count','proportion','status'];
- return $data;
- }
- /**
- * @param Request $request
- * @return array
- */
- public function isOpen(Request $request){
- $data = $request->only('shop_id','status');
- $validator = Validator::make($data, [
- 'shop_id' => 'required|integer',
- 'status' =>['required',Rule::in([0,1])],
- ]);
- if ($validator->fails()) {
- return $this->response->error($validator->errors()->first(), 500);
- }
- DB::beginTransaction();
- try {
- $res = Shop::where(['shop_id'=>$data['shop_id']])->update(['status'=>$data['status']]);
- if($res){
- //下架商家所有商品
- if($data['status'] == 0){
- $product = new ProductRepository();
- return $product->upProductStatus($data['shop_id']);
- $upStatus = $product->upProductStatus($data['shop_id']);
-
- if(isset($upStatus['status_code']) && $upStatus['status_code'] == 200){
- DB::commit();
- Log::info('商家id'.$data['shop_id'].'商品禁售成功');
- return ['message' => '成功','status_code' => 200];
- }else{
- Log::info('商家id'.$data['shop_id'].'商品禁售失败');
- return ['message' => '商品禁售失败','status_code' => 500];
- }
- }
- DB::commit();
- return ['message' => '成功','status_code' => 200];
- }else{
- return $this->response->error("失败", 500);
- }
- } catch (\Exception $e) {
- DB::rollBack();
- return $this->response->error("失败", 500);
- }
- }
- /**
- * @param Request $request
- * @return mixed
- * 通过token获取用户
- */
- public function getTokenShop(Request $request){
- $data = JWTAuth::decode(JWTAuth::getToken())['shop'];
- if($data->shop_id){
- $shop = Shop::where(['shop_id'=>$data->shop_id])->first();
- $fractal = new Manager();
- $res = new Item($shop,new ShopsTransformer());
- $data = $fractal->createData($res)->toArray();
- return $this->jsonSuccess($data);
- }else{
- return $this->jsonError('操作失败');
- }
- }
- /**
- * @param Request $request
- * @return array
- * 删除账号
- */
- public function deleteShopAccount(Request $request){
- $data = $request->only('shop_id');
- $validator = Validator::make($data, [
- 'id' => 'required|integer',
- 'shop_id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return $this->response->error($validator->errors()->first(), 500);
- }
- $res = ShopAccount::where(['id'=>$data['id'],'shop_id'=>$data['shop_id']])->delete();
- if($res){
- //下架商家所有商品,放入队列
- return ['message' => '成功','status_code' => 200];
- }else{
- return $this->response->error("失败", 500);
- }
- }
- }
|