<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/27
 * Time: 下午5:49
 */

require_once(BASE_ROOT_PATH . '/helper/login/ilogin.php');
require_once(BASE_ROOT_PATH . '/helper/login/unionid_log.php');
require_once(BASE_ROOT_PATH . '/helper/login/mobile_log.php');
require_once(BASE_ROOT_PATH . '/helper/login/openid_log.php');
require_once(BASE_ROOT_PATH . '/helper/login/open_userinfo_log.php');
require_once(BASE_ROOT_PATH . '/helper/login/pub_userinfo_log.php');
require_once(BASE_ROOT_PATH . '/helper/login/memberid_log.php');

require_once(BASE_ROOT_PATH . '/helper/account_helper.php');
require_once(BASE_ROOT_PATH . '/helper/push_helper.php');

class login_helper
{
    //微信授权之后，并且验证码通过后的处理
    public static function onBinded($mobile,$code,$relay_id=0,$fUseOpenid = true)
    {
        $logger = new login\mobile_log($mobile);
        if($logger->ismember() == true)
        {
            $user_info = self::user_info();
            if($user_info != false) {
                $logger->bind($user_info,$fUseOpenid);
                unset($_SESSION['wx_author']);
            }
            return $logger->login();
        }
        else
        {
            $passwd = $code . substr($mobile, -4);
            $user_info = self::user_info();
            if($logger->register($passwd,$user_info,$relay_id) != false)
            {
                account_helper::onRegister($logger->memberid(),$relay_id);
                //todo 判断是否是App，如果不是，应该发短信提醒
                unset($_SESSION['wx_author']);
                return $logger->login();
            }
        }

        return false;
    }

    private static function user_info()
    {
        if(array_key_exists('wx_author',$_SESSION)) {
            if (array_key_exists('user_info', $_SESSION['wx_author'])) {
                $user_info = $_SESSION['wx_author']['user_info'];
                return $user_info;
            }
        }
        return false;
    }
}