<?php
/**
 *
 * User: anyluck
 * Date: 2020/6/3
 * Time: 9:46
 */

namespace app\web\controller;


use app\common\controller\Upfile;
use app\common\model\PhoneCode;

use app\common\model\System;
use app\common\model\User;
use app\web\model\Mail;
use app\web\model\Send;
use app\web\model\Team;
use app\web\model\Tripartite;
use Ramsey\Uuid\Uuid;

class Login
{
    // todo 登录注册

    // 登录
    public function in_login()
    {
        $phone = input("phone");
        if (!$phone) json_result(400, "请输入手机号码或者邮箱账号");
        $pwd = input("pwd");
        if (!$pwd) json_result(400, "请输入登录密码");
        $where["phone|mail"] = $phone;
        $user = User::where($where)->find();
        if (!$user) {
            json_result(400, "账号不存在");
        }
        if (!password_verify($pwd, $user->pwd)) {
            json_result(400, "密码错误");
        }
        if ($user->status == 1) {// 禁止登录
            if ($user->status_time > time()) {
                json_result(400, "该账号已经封号");
            } else {// 解除禁止登陸
                User::update(["status" => 0, "status_time" => ""], ["id" => $user->id]);
            }
        }
        // 给账号加活跃度
        $update["update_time"]=time();
        User::update($update,["id"=>$user->id]);
        json_result(200, "登录成功", $user);
    }


    // 获取验证码
    public function code()
    {
        $phone = input("phone");
        if (!$phone) json_result(400, "请输入手机号或者邮箱");
        $type = input("type") ?: 0;

        // 查询是否重复发送
        $where["phone"] = $phone;
        $where["add_time"] = ["<", time() - (1 * 60)];
        $phone_code = PhoneCode::where($where)->find();
        if ($phone_code) {
            json_result(400, "验证码已发送请稍后再发送");
        }
        $res = Tripartite::send_code($phone, $type);
        json_result(200, "验证码发送成功");

    }

    // 注册
    public function register()
    {
        $phone = input("phone");
        if (!$phone) json_result(400, "请输入手机号或者邮箱");
        $code = input("code");
        if (!$code) json_result(400, "请输入验证码");
        $name = input("name");
        if (!$name) json_result(400, "请输入账户呢称");
        $type = input("type") ?: 0;
        $pwd = input("pwd");
        if (!$pwd) json_result(400, "请输入密码");
        $pid = input("pid");

        // 查看账号是存在
        $where["phone|mail"] = $phone;
        $user = User::where($where)->find();
        if ($user) {
            json_result(400, "账号已注册,请前去登录");
        }

        // 验证码
        $phone_code = Tripartite::push_code($phone);
        if ($phone_code != $code) {
            json_result(400, "验证码错误");
        }

        if ($pid) {
            $wheres["code"] = $pid;
            $pid_user = User::where($wheres)->find();
            if (!$pid_user) {
                json_result(400, "上级账号不存在");
            }
            $add["pid"] = $pid_user->id;
//            Team::all_tem($pid_user->id);
        }
        $add["code"] = uniqid();
        if ($type == 0) {
            $add["phone"] = $phone;
        } else {
            $add["mail"] = $phone;
        }

        $add["name"] = $name;
        $add["uuid"] = uniqid();// 收款ID
        $uuid = Uuid::uuid1();
        $add["token"] = $uuid->getHex();;
        $add["add_time"] = time();
        $add["pwd"] = password_hash($pwd, PASSWORD_DEFAULT);;

//        dump($add);die();
        User::create($add);
        json_result(200, "注册成功");

    }

    // 忘记密码 第一部
    public function forget_password()
    {
        $phone = input("phone");
        if (!$phone) json_result(400, "请输入手机号码或者邮箱");
        $type = input("type") ?: 0;
        $code = input("code");
        if (!$code) json_result(400, "请输入验证码");
        $name = input("name");
        // 查看账号是存在
        $where["phone|mail"] = $phone;
        $user = User::where($where)->find();
        if (!$user) {
            json_result(400, "账号不存在");
        }
        // 验证码
        $phone_code = Tripartite::push_code($phone, $type);
        if ($phone_code != $code) {
            json_result(400, "验证码错误");
        }
        json_result(200, "验证码成功");
    }

    // 修改密码--第二步
    public function update_password()
    {
        $pwd = input("pwd");
        if (!$pwd) json_result(400, "请输入密码");
        $phone = input("phone");
        if (!$phone) json_result(400, "请输入手机号码或者邮箱");
        // 查看账号是存在
        $where["phone|mail"] = $phone;
        $user = User::where($where)->find();
        if (!$user) {
            json_result(400, "账号不存在");
        }
        $add["pwd"] = password_hash($pwd, PASSWORD_DEFAULT);;
        User::where(["id" => $user->id])->update($add);
        json_result(200, "设置密码成功");
    }

    // 上传图片
    public function up_image()
    {
        $upfile = new Upfile();
        $ret = $upfile->Uploads();
        json_result(200, "", $ret["url"]);
    }

    // 会员协议
    public function Membership()
    {
        $where["name"]="Membership";
        $data=System::where($where)->field("value")->find();
        json_result(200,"",$data->value?:"");

    }

    // 测试接口
    public function test()
    {
//        $eaml="www.794230322@outlook.com";
//        $eaml="3398530177@qq.com";
//        $data["user_email"]=$eaml;
//        $data["name"]=$eaml;
//        $data["content"]="【BTS】您的验证码是123456";
//       Mail::sendEmail($data);

    }

}