<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace Modules\Member\Services;

use Modules\Member\Models\Member;
use Modules\Member\Events\LoginBefore;
use Modules\Member\Events\LoginEvent;
use Modules\Member\Events\Registered;

class AuthService {

	/**
	 * @title 会员登录
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function userLogin($request){
		$request->validate([
			'username' => 'required',
			'password' => 'required',
		]);

		$username = $request->input('username');
		$password = $request->input('password');
		LoginBefore::dispatch($username, $password);

		$token = auth('api')->attempt(['username' => $username, 'password' => $password]);

		if (!$token) {
			throw new \Exception("登录失败！", 1000);
		}else{
			LoginEvent::dispatch(auth('api')->user(), $request->input('openid', ''), $request->input('type'));
			// 判断是否到期
			$user = auth('api')->user();
			if(isset($user['level_expire_time']) && $user['level_expire_time'] != ''){
				$level_expire_time = strtotime($user['level_expire_time']);
				if($level_expire_time < time()){
					throw new \Exception("您的會員已到期，請聯系管理員！", 1004);
				}
			}

			return [
				'access_token' => $token,
				'token_type' => 'bearer',
				'expires_in' => auth('api')->factory()->getTTL() * 60
			];
		}
	}

	/**
	 * @title UID登录
	 * @description 提供给第三方登录接口
	 *
	 * @param [type] $uid
	 * @param array $params    第三方登录参数
	 * @param string $type     第三方登录类型
	 * @return void
	 */
	public function userLoginByUid($uid, $params = [], $type = 'wechat'){
		$member = Member::find($uid);
		if(!$member){
			throw new \Exception("用户不存在！", 1000);
		}

		$token = auth('api')->login($member);

		if (!$token) {
			throw new \Exception("登录失败！", 1000);
		}else{
			LoginEvent::dispatch(auth('api')->user(), $params, $type);
			return [
				'access_token' => $token,
				'token_type' => 'bearer',
				'expires_in' => auth('api')->factory()->getTTL() * 60
			];
		}
	}

	/**
	 * @title 会员注册
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function userRegister($request){
		$request->validate([
			'username' => 'required|unique:member,username',
			'password' => 'required',
			'mobile' => 'nullable|unique:member,mobile',
		], [
			'username.unique' => '用户名已存在！',
			'mobile.unique' => '手机号已存在！',
			'username.required' => '用户名不能为空！',
			'password.required' => '密码不能为空！'
		]);

		$member = Member::where('username', '=', $request->input('username'))->first();
		if(!$member){
			$data = [
				'username' => $request->input('username'),
				'password' => $request->input('password'),
				'nickname' => $request->input('nickname', ''),
				'avatar' => $request->input('avatar', ''),
				'mobile' => $request->input('mobile', 0),
			];

			$member = Member::updateOrCreate($data);
		}
		Registered::dispatch($member, $request->input('openid', ''), $request->input('type', ''));
		$token = auth('api')->login($member);
		return [
			'access_token' => $token,
			'token_type' => 'bearer',
			'expires_in' => auth('api')->factory()->getTTL() * 60
		];
	}

	/**
	 * @title 会员退出
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function userLogout($request){
		auth('api')->logout();
	}
}
