<?php

namespace app\love\controller;

use app\love\model\ActiveApplyModel;
use app\love\model\ActiveModel;
use app\love\model\UserMatingModel;
use app\love\model\UserModel;
use cmf\controller\HomeBaseController;
use think\facade\Session;

class LoginController extends HomeBaseController
{
    /**
     * 登录
     */
    public function index()
    {
        $userId = cmf_get_current_user_id();

        $active_id = $this->request->param('active_id', 0);
        if (!empty($active_id)) {
            Session::set('active_id', $active_id);
        }

        if (!empty($userId)) {
            $user = UserModel::get($userId);
            if (!empty($user)) {
                if ($user['user_status'] == 0) {
                    $this->redirect(cmf_url('love/register/ban'));
                }
                //完善资料
                if ($user['is_complete'] == 2) {
                    if (empty($user['idcard'])) {
                        $this->redirect(cmf_url('love/register/idcard'));
                    } else {
                        $this->redirect(cmf_url('love/register/intro'));
                    }
                }

                if ($active_id) {
                    $this->redirect(url('love/active/detail') . "?id={$active_id}");
                } else {
                    $this->redirect(url('/'));
                }
            }
        }

        $this->redirect('https://www.jucai.gov.cn/api/auth/wechat_auth?url=' . urlencode(url('love/login/wechatBack', '', true, true)));
    }

    /**
     * 微信回调
     */
    public function wechatBack()
    {
        $param   = $this->request->param();
        $open_id = $param['openid'];

        //登录
        $user = UserModel::get(['open_id' => $open_id]);
        if (empty($user)) {
            $user = UserModel::create([
                'user_type'   => 2,
                'open_id'     => $open_id,
//                'username'    => $param['nickname'],
//                'avatar'      => $param['headimgurl'],
                'create_time' => time(),
            ]);
            UserMatingModel::create(['user_id' => $user['id']]);
            //活动
            $active_id = Session::pull('active_id');
            if (!empty($active_id)) {
                $active = ActiveModel::get($active_id);
                if (!empty($active)) {
                    ActiveApplyModel::create([
                        'active_id'    => $active_id,
                        'user_id'      => $user['id'],
                        'create_time'  => time(),
                        'check_status' => 2,
                        'check_time'   => time(),
                    ]);
                }
            }
        }

        session('user', $user->toArray());
        $data = [
            'last_login_time' => time(),
            'last_login_ip'   => get_client_ip(0, true),
        ];
        UserModel::update($data, ['id' => $user['id']]);
        $token = cmf_generate_user_token($user["id"], 'mobile');
        if (!empty($token)) {
            session('token', $token);
        }

        $domain = Session::get('domain');
        $this->redirect($domain ?: '/');
    }

    public function login1()
    {
        $user = UserModel::get(3);
        session('user', $user->toArray());

        $token = cmf_generate_user_token(3, 'mobile');
        if (!empty($token)) {
            session('token', $token);
        }

        return 'OK';
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        session("user", null);//只有前台用户退出
        return redirect($this->request->root() . "/");
    }
}
