ShopController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Upload;
  4. use Cookie;
  5. use Illuminate\Http\Request;
  6. use App\Models\Shop;
  7. use App\Models\User;
  8. use App\Models\SellerPackage;
  9. use App\Models\BusinessSetting;
  10. use App\Models\AffiliateLog;
  11. use App\Models\SellerPackagePayment;
  12. use Auth;
  13. use Hash;
  14. use App\Notifications\EmailVerificationNotification;
  15. use function dd;
  16. use function view;
  17. class ShopController extends Controller
  18. {
  19. public function __construct() {
  20. $this->middleware('user', [ 'only' => [ 'index' ] ]);
  21. }
  22. /**
  23. * Display a listing of the resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function index() {
  28. $shop = Auth::user()->shop;
  29. return view('seller.shop', compact('shop'));
  30. }
  31. /**
  32. * Show the form for creating a new resource.
  33. *
  34. * @return \Illuminate\Http\Response
  35. */
  36. public function create( Request $request ) {
  37. Upload::where('user_id', 0)->delete();
  38. if ( Auth::check() )
  39. {
  40. if ( Auth::user()->user_type == 'admin' )
  41. {
  42. return view('backend.sellers.seller_form');
  43. }
  44. else if ( Auth::user()->user_type == 'salesman' )
  45. {
  46. return view('salesman.sellers.seller_form');
  47. }
  48. else if ( Auth::user()->user_type == 'customer' )
  49. {
  50. flash(translate('Customer can not be a seller'))->error();
  51. return back();
  52. }
  53. else if ( Auth::user()->user_type == 'seller' )
  54. {
  55. flash(translate('This user already a seller'))->error();
  56. return back();
  57. }
  58. }
  59. else
  60. {
  61. if ( $request->has('leader_id') )
  62. {
  63. Cookie::queue('leader_id', $request->leader_id, 720);
  64. }
  65. return view('frontend.seller_form');
  66. }
  67. }
  68. /**
  69. * Store a newly created resource in storage.
  70. *
  71. * @param \Illuminate\Http\Request $request
  72. *
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function store( Request $request ) {
  76. $package = SellerPackage::where(['is_default' => 1 ])->first();
  77. $package_id = $package['id'];
  78. $user = NULL;
  79. if ( $request->identity_card_front == NULL ){
  80. flash(translate('Identity Card Front Not Allow Empty!'))->error();
  81. return back();
  82. }
  83. if ( $request->identity_card_back == NULL ){
  84. flash(translate('Identity Card Back Not Allow Empty!'))->error();
  85. return back();
  86. }
  87. if(!empty($request->create) && $request->create==2){ //用户转卖家
  88. $user = Auth::user();
  89. if($user->status==1 && $user->user_type == 'customer'){
  90. $user->status = 2;
  91. $user->user_type == 'customer';
  92. $user->identity_card_front = $request->identity_card_front;
  93. $user->identity_card_back = $request->identity_card_back;
  94. $user->identity_card_num = $request->identity_card_num;
  95. $user->line = $request->line;
  96. $user->save();
  97. // flash(translate('Store application successful, waiting for review!'))->success();
  98. // return redirect()->route('dashboard');
  99. }else if($user->status==2 && $user->user_type == 'customer'){
  100. flash(translate('You have already applied. Please do not apply again'))->error();
  101. return back();
  102. }
  103. }else{
  104. if($request->invit==null){
  105. flash(translate('The invitation code must not be empty'));
  106. return back();
  107. }
  108. if ( !Auth::check() ){
  109. if ( User::where('email', $request->email)->first() != NULL ){
  110. flash(translate('Email already exists!'))->error();
  111. return back();
  112. }
  113. if ( $request->password == $request->password_confirmation ){
  114. $user = new User;
  115. $userlist=(new User)->where('id',$request->invit)->get();
  116. $li=[];
  117. if($userlist){ //查询上级ID是否存在
  118. $userlist=$userlist[0];
  119. $user->leader_id=$userlist['id'];
  120. $user->tpath=!empty($userlist['tpath']) ? $userlist['tpath'].','.$userlist['id'] : $userlist['id'];
  121. $user->invit_1=$userlist['id'];
  122. $user->invit_2=!empty($userlist['invit_1']) ? $userlist['invit_1'] : '';
  123. $user->invit_3=!empty($userlist['invit_2']) ? $userlist['invit_2'] : '';
  124. $user->pid=$userlist['id'];
  125. }
  126. $user->name = $request->name;
  127. $user->email = $request->email;
  128. $user->identity_card_front = $request->identity_card_front;
  129. $user->identity_card_back = $request->identity_card_back;
  130. $user->identity_card_num = $request->identity_card_num;
  131. $user->line = $request->line;
  132. $user->user_type = "customer";
  133. $user->status = 2;
  134. $user->password = Hash::make($request->password);
  135. $user->save();
  136. }else{
  137. flash(translate('Sorry! Password did not match.'))->error();
  138. return back();
  139. }
  140. }else{//如果有登录用户
  141. if ( Auth::user()->user_type == 'admin' ){//管理后台
  142. if ( User::where('email', $request->email)->first() != NULL )
  143. {
  144. flash(translate('Email already exists!'))->error();
  145. return back();
  146. }
  147. if ( $request->password == $request->password_confirmation )
  148. {
  149. $user = new User;
  150. $user->name = $request->name;
  151. $user->email = $request->email;
  152. $user->user_type = "seller";
  153. $user->is_virtual = "1";
  154. $user->password = Hash::make($request->password);
  155. $user->email_verified_at = date('Y-m-d H:m:s');
  156. $user->save();
  157. }
  158. else
  159. {
  160. flash(translate('Sorry! Password did not match.'))->error();
  161. return back();
  162. }
  163. }else if ( Auth::user()->user_type == 'salesman' ){//推销员
  164. if ( User::where('email', $request->email)->first() != NULL )
  165. {
  166. flash(translate('Email already exists!'))->error();
  167. return back();
  168. }
  169. if ( $request->password == $request->password_confirmation )
  170. {
  171. $user = new User;
  172. $user->name = $request->name;
  173. $user->email = $request->email;
  174. $user->user_type = "seller";
  175. $user->is_virtual = "1";
  176. $user->pid = Auth::user()->id;
  177. $user->password = Hash::make($request->password);
  178. $user->email_verified_at = date('Y-m-d H:m:s');
  179. $user->save();
  180. }
  181. else
  182. {
  183. flash(translate('Sorry! Password did not match.'))->error();
  184. return back();
  185. }
  186. }else{
  187. $user = Auth::user();
  188. if($user->status==1 && $user->user_type == 'customer'){
  189. $user->status = 2;
  190. $user->user_type == 'customer';
  191. $user->save();
  192. flash(translate('Store application successful, waiting for review!'))->success();
  193. return redirect()->route('dashboard');
  194. }else if($user->status==2 && $user->user_type == 'customer'){
  195. flash(translate('You have already applied. Please do not apply again'))->error();
  196. return back();
  197. }
  198. }
  199. }
  200. }
  201. if(Shop::where('user_id', $user->id)->first() == NULL ){
  202. $shop = new Shop;
  203. $shop->user_id = $user->id;
  204. $shop->name = $request->name;
  205. $shop->address = $request->address;
  206. $shop->slug = preg_replace('/\s+/', '-', $request->name);
  207. $user->identity_card_front = $request->identity_card_front;
  208. $user->identity_card_back = $request->identity_card_back;
  209. $user->identity_card_num = $request->identity_card_num;
  210. $user->line = $request->line;
  211. $user->certtype = $request->certtype;
  212. $shop->seller_package_id = $package_id;
  213. if ( Cookie::get('leader_id') ){
  214. $user->pid = Cookie::get('leader_id');
  215. }
  216. Upload::where('user_id', 0)->where('id', 'NOTIN', [ $user->identity_card_front, $user->identity_card_back ])->update([ 'user_id' => $user->id ]);
  217. if ( $shop->save() ){
  218. $shop->seller_package_id = $package_id;
  219. $seller_package = SellerPackage::findOrFail( $package_id );
  220. $shop->product_upload_limit = $seller_package->product_upload_limit;
  221. $shop->package_invalid_at = date('Y-m-d', strtotime( ' +' . $seller_package->duration . 'days'));
  222. $shop->save();
  223. $seller_package = new SellerPackagePayment;
  224. $seller_package->user_id = $user->id;
  225. $seller_package->seller_package_id = $package_id;
  226. $seller_package->payment_method = 'free';
  227. $seller_package->payment_details = '';
  228. $seller_package->approval = 1;
  229. $seller_package->offline_payment = 0;
  230. $seller_package->save();
  231. if ( Auth::check() ){
  232. if ( Auth::user()->user_type == 'admin' )
  233. {//管理后台
  234. flash(translate('Virtual Seller has been created successfully!'))->success();
  235. return redirect()->route('sellers.index');
  236. }
  237. else if ( Auth::user()->user_type == 'salesman' )
  238. {//推销员
  239. flash(translate('Virtual Seller has been created successfully!'))->success();
  240. return redirect()->route('salesman.sellers_index');
  241. }
  242. }else{
  243. auth()->login($user, false);
  244. }
  245. if ( BusinessSetting::where('type', 'email_verification')->first()->value != 1 ){
  246. $user->email_verified_at = date('Y-m-d H:m:s');
  247. $user->save();
  248. }else{
  249. $user->notify(new EmailVerificationNotification());
  250. }
  251. flash(translate('Your store has been successfully created and is awaiting review!'))->success();
  252. return redirect()->route('dashboard');
  253. }else{
  254. $user->user_type == 'customer';
  255. $user->save();
  256. }
  257. }
  258. flash(translate('Sorry! Something went wrong.'))->error();
  259. return back();
  260. }
  261. /**
  262. * Display the specified resource.
  263. *
  264. * @param int $id
  265. *
  266. * @return \Illuminate\Http\Response
  267. */
  268. public function show( $id ) {
  269. //
  270. }
  271. /**
  272. * Show the form for editing the specified resource.
  273. *
  274. * @param int $id
  275. *
  276. * @return \Illuminate\Http\Response
  277. */
  278. public function edit( $id ) {
  279. //
  280. }
  281. public function destroy( $id ) {
  282. //
  283. }
  284. }