<?php
/**
 * 用户中心登陆管理Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/11/04
 * Time: 09:00
 */

namespace JinDouYun\Controller\UserCenter;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Cache\SmsVerification;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\UserCenter\MUserCenterLogin;
use Mall\Framework\Db\Db;

class UserCenterLogin extends BaseController
{
    private $objMUserCenterLogin;
    private $SmsVerification;

    public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken=false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
        $this->objMUserCenterLogin = new MUserCenterLogin();
        $this->SmsVerification = new SmsVerification();
    }

    /**
     *  用户中心登陆
     */
    public function login()
    {
    
        //接收参数
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $userData = [
            'mobile' => isset($params['mobile']) ? trim($params['mobile']) :  '',
            'password' => isset($params['password']) ? $params['password'] : '',
        ];
        //校验参数
        foreach($userData as $key => $value){
            if(empty($value) && $value !== 0){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        //source判断登录来源 1:后台管理员,员工 2:供应商 3:商户
        $userData['source'] = isset($params['source']) ? $params['source'] : 1;

        $result = $this->objMUserCenterLogin->login($userData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 选择企业后生成AUTHORIZATION
     */
    public function createToken() {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $userInfo = [
            'id' => isset($params['userCenterId']) ? $params['userCenterId'] : '',
        ];
        foreach($userInfo as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMUserCenterLogin->createToken($userInfo);
        parent::sendOutput($result);

    }



}