<?php

namespace App\Api\Controllers;

use App\Models\Article;
use App\Models\User;
use Illuminate\Http\Request;
use Jason\Api;
use Sms;

class AuthController extends Controller
{

    public function license()
    {
        return $this->success([
            'agreement' => Article::where('id', 7)->value('content'),
            'privacy'   => Article::where('id', 7)->value('content'),
        ]);
    }

    /**
     * Notes   : 干事登录，type必须大于1的
     *
     * @Date   : 2022/4/30 15:49
     * @Author : <Jason.C>
     * @param  Request  $request
     * @return mixed
     */
    function code2(Request $request)
    {
        $username = $request->username;

        $user = User::where('mobile', $username)->where('type', '>=', 1)->first();
        if (!$user) {
            return $this->failed('对不起，您无权登录');
        } else {
            Sms::send($username);

            return $this->success('发送成功');
        }
    }

    public function code(Request $request)
    {
        $username = $request->username;

        $user = User::where('mobile', $username)->where('type', 0)->first();
        if (!$user) {
            return $this->failed('对不起，您无权登录');
        } else {
            Sms::send($username);

            return $this->success('发送成功');
        }
    }

    public function loginByCode(Request $request)
    {
        $username = $request->username;
        $code     = $request->verify;

        if (Sms::check($username, $code)) {
            $user = User::where('mobile', $username)->first();

            $token = Api::login($user);

            return $this->success([
                'access_token' => 'Bearer ' . $token,
            ]);
        } else {
            return $this->failed('验证码不正确');
        }

    }

}
