<?php
namespace Base\Domain\Authorization\AuthorizationType;

use Base\Common\ToolUtil;
use Base\Domain\Authorization\AuthorizationLogin;
use Base\Domain\Authorization\AuthorizationLoginNew;
use Base\Model\Authorization as AuthorizationModel;
use PhalApi\Exception\BadRequestException;
use PhalApi\Tool;

//具体的业务处理类型
class WeChatBusiness extends AuthorizationLogin {
    //第三方登录类型 1微信
    public $authorizationType = 1;
    public $puserInfo = [];  //微信信息
    public $haveRegister = false; //是否已注册
    public $haveBind = false; //是否已经绑定了
    public $authorizationInfo = []; //authorizationInfo表信息
    public $state = [];  //第三方的state
    public $successJump = '/platform/home'; //登录成功后进行跳转页面

    //具体的业务处理
    public function businessHandle($weChatUserInfo){
        $this->puserInfo = $weChatUserInfo;
        $this->checkoutUserInfo(); //检测用户信息
        $this->notRegisterHandle();  //未注册过的处理进行第三方信息


        //进行统一的第三方登录处理
        //初始化注册的企业信息
        $this->makeRegisterInfo();
        $this->loginHandle();
    }

    //检测用户是否已注册
    protected function checkoutUserInfo(){
        $authorizationInfo =  (new AuthorizationModel())->getData(['authorization_type'=>$this->authorizationType,'openid'=>$this->puserInfo['openid']]);

        //未注册
        $this->haveRegister = false;

        //未绑定
        $this->haveBind = false;
        if (!empty($authorizationInfo['member_id'])){
            $this->haveRegister = true;
            $this->haveBind = true;
            $memberInfo =  \Base\Model\Member::model()->get($authorizationInfo['member_id']);
            if (empty($memberInfo)){
                throw new BadRequestException('该用户不存在，请检查参数');
            }

            $this->login_user_name = $memberInfo['username'];
        }
    }

    //生成注册的用户处理--主要准备pp_member表数据
    public function makeRegisterInfo(){
        $this->registerInfo = [
            'from_channel'=>'weChat',//member来源
            'avatar'=>!empty($this->puserInfo['headimgurl'])?$this->puserInfo['headimgurl']:'', //头像
            'nickname' => '企业管理员',  //昵称
            'sex'=>'',  //性别
            'email'=>'', //邮箱信息
            'mobile'=>empty($this->puserInfo['login_mobile'])?'':$this->puserInfo['login_mobile'], //手机
        ];

        //账号重复
        //查询账户是否重复，重复进行
        $username = $this->registerInfo['from_channel'].\PhalApi\Tool::createRandStr(6,$this->puserInfo['openid']).\PhalApi\Tool::createRandStr(3);
        $findUserInfo = \Base\Model\Member::model()->getData(['username' => $username]);
        if (!empty($findUserInfo)) {
            $username .= \PhalApi\Tool::createRandStr(6,$this->puserInfo['openid']); // 换个账号名+加后缀
        }

        $this->registerInfo['username'] = $username; //用户名
    }

    //入库数据准备pp_authorization表和pp_authorization_details表数据
    protected function pData()
    {
        //组装入库信息
        //第三方表信息准备
        $authorizationPdata = [
            'openid'=>$this->puserInfo['openid'],
            'unionid'=>$this->puserInfo['unionid'],
            'authorization_type'=>$this->authorizationType,
            'add_time'=>date('Y-m-d H:i:s')
        ];

        //第三方信息详情
        $authorization_details = [
            'nickname'=>$this->puserInfo['nickname'],
            'sex'=>$this->puserInfo['sex'],
            'headimgurl'=>$this->puserInfo['headimgurl'],
            'province'=>$this->puserInfo['province'],
            'city'=>$this->puserInfo['city'],
            'country'=>$this->puserInfo['country'],
            'privilege'=>json_encode($this->authorizationInfo['privilege']),
            'add_time'=>date('Y-m-d H:i:s'),
            'authorization_id'=>0,
        ];

        $pData = [
            'authorization'=>$authorizationPdata,
            'authorization_details'=>$authorization_details
        ];

        return $pData;
    }

    //未注册过写入pp_authorization表和pp_authorization_details表
    protected function notRegisterHandle(){
        $authorizationInfo =  (new AuthorizationModel())->getData(['authorization_type'=>$this->authorizationType,'openid'=>$this->puserInfo['openid']]);
        //如果第三方表有直接返回
        if (!empty($authorizationInfo)){
            $this->authorizationInfo = $authorizationInfo;
        }else{
            $pData =  $this->pData();
            $result = (new  AuthorizationModel())->addData($pData['authorization'],$pData['authorization_details']);
            $this->authorizationInfo = $result['authorization'];
        }
    }

    //注册后额外处理
    public function extraHandle()
    {

    }


}
