<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2019/12/5
 * Time: 17:44
 */

namespace app\api\controller;


use app\api\controller\base\Base;
use app\api\controller\base\Permissions;
use app\common\model\User;
use app\common\service\WebService;

class Common extends Base
{
    //登入
    public function login()
    {
        $return_url = $this->request->param('return_url', '');
        session('return_url', $return_url, 'login');

        $callback = urlencode(url('/api/common/loginNotify', '', false, true));
        $state = md5('appointment' . time());
        session('state', $state, 'login');

        $url = "https://www.jucai.gov.cn/api/auth/wechat_auth?url=$callback&state=$state";
        $this->redirect($url);
    }

    public function loginLw()
    {
        $user = User::get(9);
        //登入成功 ,返回 前端
        return (Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
        $return_url = 'https://localhost:8080/';
        $this->redirect($return_url . '?jwt=' . Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
    }

    public function loginNotify()
    {
        $mystate = session('state', '', 'login');
        $state = $this->request->param('state');
        if (!$mystate || !$state || $mystate != $state) {
            $this->json_error('登入失败，请重新登入');
        }

        (new WebService())->record('登入信息');

        $post = $this->request->param();
        $validate = new \think\Validate([
            ['openid', 'max:50'],
            ['unionid', 'max:50'],
//            ['nickname|昵称', 'max:50'],
//            ['headimgurl|头像', 'max:255'],
//            ['sex|性别', 'in:0,1,2'],
//            ['country|国家', 'max:50'],
//            ['province|省份', 'max:50'],
//            ['city|城市', 'max:50'],
        ]);
        if (!$validate->check($post)) {
            $this->json_error('提交失败：' . $validate->getError());
        }

        $unionid = $this->request->param('unionid');
        $passport = $unionid;
        $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $unionid]);
        if (!$user) {
            $openid = $this->request->param('openid');
            $passport = $openid;
            $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $openid, 'unionid' => $unionid]);
            if (!$user) {
                $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $openid]);
            }
        }
        if (!$passport) {
            $this->json_error('openid 不能为空');
        }
        if (!$user) {
            //注册
            $user = new User();
            $data = [
                'openid' => $post['openid']??'',
                'unionid' => $post['unionid']??'',
                'passport' => $passport,
                'nickname' => limitStrLen('nickname', $post['nickname']??'', 50),
                'user_type' => User::TYPE_WECHAT,
                'user_cate' => User::CATE_USER,
                'head_pic' => limitStrLen('headimgurl', $post['headimgurl']??'', 255),
                'status' => User::STATUS_PASS,
                'ip' => $this->request->ip(),
                'sex' => $post['sex']??0,
                'country' => $post['country']??'',
                'province' => $post['province']??'',
                'city' => $post['city']??'',
                "login_time" => time(),
                "create_time" => time()
            ];
            if (false == $user->allowField(true)->save($data)) {
                $this->json_error('添加失败');
            }
        } else {
            $data = [
                "login_time" => time(),
                'openid' => $post['openid']??'',
                'unionid' => $post['unionid']??'',
                'head_pic' => limitStrLen('headimgurl', $post['headimgurl']??'', 255),
            ];
            $user->allowField(true)->save($data);
        }

        //登入成功 ,返回 前端
        $return_url = session('return_url', '', 'login');
        $this->redirect($return_url . '?jwt=' . Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
    }
}
