<?php
namespace app\workerapi\logic;
use app\common\enum\LoginEnum;
use app\common\enum\notice\NoticeEnum;
use app\common\logic\BaseLogic;
use app\common\model\master_worker\MasterWorker;
use app\common\model\master_worker_register\MasterWorkerRegister;
use app\common\service\FileService;
use app\common\service\sms\SmsDriver;
 use think\facade\Config;
use app\workerapi\service\MasterWokerTokenService;

/**
 * @author 林海涛
 * @date ${DATA}
 */
class LoginLogic extends BaseLogic
{

    /**
     * @notes 确认手机号
     * @param $params
     * @return bool
     * @author 段誉
     * @date 2022/9/21 17:28
     */
    public static function confirmMobile(array $params)
    {
        try {
            // 验证码请求
            $sceneId = NoticeEnum::GCSSJHM_CAPTCHA;
            // 校验短信
            $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
            if (!$checkSmsCode) {
                throw new \Exception('验证码错误');
            }
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function register(array $params)
    {
        try {
            MasterWorkerRegister::create([
                'maintain_exp_type' => $params['maintain_exp_type'],
                'other_exp_type' => $params['other_exp_type'],
                'city' => $params['city'],
                'vehicle_type' => $params['vehicle_type'],
                'name' => $params['name'],
                'age' => $params['age'],
                'mobile' => $params['mobile'],
            ]);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function login($params)
    {
        try {
            // 账号/手机号 密码登录
            $where = ['account' => $params['account']];
            if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
                //手机验证码登录
                $where = ['mobile' => $params['account']];
            }

            $user = MasterWorker::where($where)->findOrEmpty();
            if ($user->isEmpty()) {
                throw new \Exception('用户不存在');
            }

            //更新登录信息
            $user->login_time = time();
            $user->login_ip = request()->ip();
            $user->save();

            //设置token
            $userInfo = MasterWokerTokenService::setToken($user->id, 1);

            //返回登录信息
            $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
            $avatar = FileService::getFileUrl($avatar);

            return [
                'nickname' => $userInfo['nickname'],
                'sn' => $userInfo['sn'],
                'mobile' => $userInfo['mobile'],
                'avatar' => $avatar,
                'token' => $userInfo['token'],
            ];
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function logout($userInfo)
    {
        //token不存在，不注销
        if (!isset($userInfo['token'])) {
            return false;
        }
        //设置token过期
        return MasterWokerTokenService::expireToken($userInfo['token']);
    }

}