123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Upload;
- use Cookie;
- use Illuminate\Http\Request;
- use App\Models\Shop;
- use App\Models\User;
- use App\Models\SellerPackage;
- use App\Models\BusinessSetting;
- use App\Models\AffiliateLog;
- use App\Models\SellerPackagePayment;
- use Auth;
- use Hash;
- use App\Notifications\EmailVerificationNotification;
- use function dd;
- use function view;
- class ShopController extends Controller
- {
- public function __construct() {
- $this->middleware('user', [ 'only' => [ 'index' ] ]);
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index() {
- $shop = Auth::user()->shop;
- return view('seller.shop', compact('shop'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create( Request $request ) {
- Upload::where('user_id', 0)->delete();
- if ( Auth::check() )
- {
- if ( Auth::user()->user_type == 'admin' )
- {
- return view('backend.sellers.seller_form');
- }
- else if ( Auth::user()->user_type == 'salesman' )
- {
- return view('salesman.sellers.seller_form');
- }
- else if ( Auth::user()->user_type == 'customer' )
- {
- flash(translate('Customer can not be a seller'))->error();
- return back();
- }
- else if ( Auth::user()->user_type == 'seller' )
- {
- flash(translate('This user already a seller'))->error();
- return back();
- }
- }
- else
- {
- if ( $request->has('leader_id') )
- {
- Cookie::queue('leader_id', $request->leader_id, 720);
- }
- return view('frontend.seller_form');
- }
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- *
- * @return \Illuminate\Http\Response
- */
- public function store( Request $request ) {
- $package = SellerPackage::where(['is_default' => 1 ])->first();
- $package_id = $package['id'];
- $user = NULL;
- if ( $request->identity_card_front == NULL ){
- flash(translate('Identity Card Front Not Allow Empty!'))->error();
- return back();
- }
- if ( $request->identity_card_back == NULL ){
- flash(translate('Identity Card Back Not Allow Empty!'))->error();
- return back();
- }
- if(!empty($request->create) && $request->create==2){ //用户转卖家
- $user = Auth::user();
- if($user->status==1 && $user->user_type == 'customer'){
- $user->status = 2;
- $user->user_type == 'customer';
- $user->identity_card_front = $request->identity_card_front;
- $user->identity_card_back = $request->identity_card_back;
- $user->identity_card_num = $request->identity_card_num;
- $user->line = $request->line;
- $user->save();
- // flash(translate('Store application successful, waiting for review!'))->success();
- // return redirect()->route('dashboard');
- }else if($user->status==2 && $user->user_type == 'customer'){
- flash(translate('You have already applied. Please do not apply again'))->error();
- return back();
- }
-
- }else{
- if($request->invit==null){
- flash(translate('The invitation code must not be empty'));
- return back();
- }
- if ( !Auth::check() ){
- if ( User::where('email', $request->email)->first() != NULL ){
- flash(translate('Email already exists!'))->error();
- return back();
- }
- if ( $request->password == $request->password_confirmation ){
- $user = new User;
- $userlist=(new User)->where('id',$request->invit)->get();
- $li=[];
- if($userlist){ //查询上级ID是否存在
- $userlist=$userlist[0];
- $user->leader_id=$userlist['id'];
- $user->tpath=!empty($userlist['tpath']) ? $userlist['tpath'].','.$userlist['id'] : $userlist['id'];
- $user->invit_1=$userlist['id'];
- $user->invit_2=!empty($userlist['invit_1']) ? $userlist['invit_1'] : '';
- $user->invit_3=!empty($userlist['invit_2']) ? $userlist['invit_2'] : '';
- $user->pid=$userlist['id'];
- }
- $user->name = $request->name;
- $user->email = $request->email;
- $user->identity_card_front = $request->identity_card_front;
- $user->identity_card_back = $request->identity_card_back;
- $user->identity_card_num = $request->identity_card_num;
- $user->line = $request->line;
- $user->user_type = "customer";
- $user->status = 2;
- $user->password = Hash::make($request->password);
- $user->save();
-
-
- }else{
- flash(translate('Sorry! Password did not match.'))->error();
- return back();
- }
- }else{//如果有登录用户
- if ( Auth::user()->user_type == 'admin' ){//管理后台
- if ( User::where('email', $request->email)->first() != NULL )
- {
- flash(translate('Email already exists!'))->error();
- return back();
- }
- if ( $request->password == $request->password_confirmation )
- {
- $user = new User;
- $user->name = $request->name;
- $user->email = $request->email;
- $user->user_type = "seller";
- $user->is_virtual = "1";
- $user->password = Hash::make($request->password);
- $user->email_verified_at = date('Y-m-d H:m:s');
- $user->save();
- }
- else
- {
- flash(translate('Sorry! Password did not match.'))->error();
- return back();
- }
- }else if ( Auth::user()->user_type == 'salesman' ){//推销员
- if ( User::where('email', $request->email)->first() != NULL )
- {
- flash(translate('Email already exists!'))->error();
- return back();
- }
- if ( $request->password == $request->password_confirmation )
- {
- $user = new User;
- $user->name = $request->name;
- $user->email = $request->email;
- $user->user_type = "seller";
- $user->is_virtual = "1";
- $user->pid = Auth::user()->id;
- $user->password = Hash::make($request->password);
- $user->email_verified_at = date('Y-m-d H:m:s');
- $user->save();
- }
- else
- {
- flash(translate('Sorry! Password did not match.'))->error();
- return back();
- }
- }else{
- $user = Auth::user();
- if($user->status==1 && $user->user_type == 'customer'){
- $user->status = 2;
- $user->user_type == 'customer';
- $user->save();
- flash(translate('Store application successful, waiting for review!'))->success();
- return redirect()->route('dashboard');
- }else if($user->status==2 && $user->user_type == 'customer'){
- flash(translate('You have already applied. Please do not apply again'))->error();
- return back();
- }
- }
- }
- }
- if(Shop::where('user_id', $user->id)->first() == NULL ){
- $shop = new Shop;
- $shop->user_id = $user->id;
- $shop->name = $request->name;
- $shop->address = $request->address;
- $shop->slug = preg_replace('/\s+/', '-', $request->name);
- $user->identity_card_front = $request->identity_card_front;
- $user->identity_card_back = $request->identity_card_back;
- $user->identity_card_num = $request->identity_card_num;
- $user->line = $request->line;
- $user->certtype = $request->certtype;
- $shop->seller_package_id = $package_id;
- if ( Cookie::get('leader_id') ){
- $user->pid = Cookie::get('leader_id');
- }
- Upload::where('user_id', 0)->where('id', 'NOTIN', [ $user->identity_card_front, $user->identity_card_back ])->update([ 'user_id' => $user->id ]);
- if ( $shop->save() ){
- $shop->seller_package_id = $package_id;
- $seller_package = SellerPackage::findOrFail( $package_id );
- $shop->product_upload_limit = $seller_package->product_upload_limit;
- $shop->package_invalid_at = date('Y-m-d', strtotime( ' +' . $seller_package->duration . 'days'));
- $shop->save();
-
- $seller_package = new SellerPackagePayment;
- $seller_package->user_id = $user->id;
- $seller_package->seller_package_id = $package_id;
- $seller_package->payment_method = 'free';
- $seller_package->payment_details = '';
- $seller_package->approval = 1;
- $seller_package->offline_payment = 0;
- $seller_package->save();
- if ( Auth::check() ){
- if ( Auth::user()->user_type == 'admin' )
- {//管理后台
- flash(translate('Virtual Seller has been created successfully!'))->success();
- return redirect()->route('sellers.index');
- }
- else if ( Auth::user()->user_type == 'salesman' )
- {//推销员
- flash(translate('Virtual Seller has been created successfully!'))->success();
- return redirect()->route('salesman.sellers_index');
- }
- }else{
- auth()->login($user, false);
- }
- if ( BusinessSetting::where('type', 'email_verification')->first()->value != 1 ){
- $user->email_verified_at = date('Y-m-d H:m:s');
- $user->save();
- }else{
- $user->notify(new EmailVerificationNotification());
- }
- flash(translate('Your store has been successfully created and is awaiting review!'))->success();
- return redirect()->route('dashboard');
- }else{
- $user->user_type == 'customer';
- $user->save();
- }
- }
- flash(translate('Sorry! Something went wrong.'))->error();
- return back();
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function show( $id ) {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function edit( $id ) {
- //
- }
- public function destroy( $id ) {
- //
- }
- }
|