<?php namespace Place\Api;
use Dever;
use Place;
use Place\Lib\Main;
class Data extends Main
{
    # 首页
    public function home()
    {
        if (Place::$info['type'] == 1) {
            # 公开
            $data['entry'] = Place::$resource[0];
        } elseif (Place::$info['type'] == 2 && Place::$user) {
            # 注册
            $data['entry'] = Place::$resource[0];
        } elseif (Place::$info['type'] > 2 && Place::$user && Place::$user['entry_type'] > 2) {
            $data['entry'] = Place::$resource[0];
        } else {
            # 需要注册、购买、输入密码等等
            if (Place::$info['type'] == 2) {
                $data['button'] = array
                (
                    'name' => '注册',
                    'type' => 2,
                );
            } elseif (Place::$info['type'] == 3) {
                $data['button'] = array
                (
                    'name' => '邀请码',
                    'type' => 3,
                );
            } elseif (Place::$info['type'] == 4) {
                $data['button'] = array
                (
                    'name' => '密码',
                    'type' => 4,
                );
            } elseif (Place::$info['type'] == 5) {
                $data['button'] = array
                (
                    'name' => '购买',
                    'type' => 5,
                );
            }
        }

        # 最新内容
        //$data['content'] = Dever::load('info', 'content')->getList(false);
        return Place::result($data);
    }

    # 验证码登录或者注册
    public function login_commit(){}
    public function login()
    {
        $account = Dever::input('account', 'is_string', '账户信息');
        $env = Dever::input('env', 'is_numeric', '环境信息', 3);
        $mobile = Dever::input('mobile', 'is_numeric', '手机号');
        $pwd = Dever::input('pwd');
        $invite = Dever::input('invite');
        if (!$pwd) {
            $code = Dever::input('code', 'is_numeric', '验证码');
            #Dever::load('template', 'msg')->check('reg_code', $mobile, $code);
        }
        $where['mobile'] = $mobile;
        $member = Dever::db('member', 'place')->find($where);
        if ($member) {
            if ($pwd && Dever::load('common', 'manage')->hash($pwd, $member['salt']) != $member['password']) {
                Dever::error('登录失败，密码无效');
            }
            # 已注册，登录
            $id = $member['id'];
        } else {
            if ($pwd) {
                Dever::error('登录失败，账户无效');
            }
            if ($invite) {
                $parent_uid = Dever::load('code', 'invite')->getUid($invite);
                if (!$parent_uid) {
                    Dever::error('邀请码不存在');
                }
            }
            $data = $where;
            $data['name'] = \Dever\Helper\Str::hide($mobile);
            $data['vip_id'] = -1;
            $data['agent_id'] = -1;
            $id = Dever::db('member', 'place')->insert($data);
            if ($invite) {
                $parent = Dever::db('member', 'place')->find($parent_uid);
                if ($parent && $parent['agent_id'] && $parent['agent_id'] > 0) {
                    Dever::load('relation', 'invite')->set($parent_uid, $id);
                    Dever::db('member', 'place')->update($id, array('parent_uid' => $parent_uid));
                }
            }
        }
        $result['t'] = \Dever\Helper\Secure::login($id);
        # 获取openid
        $result = Dever::load('util', 'api')->openid($account, $env, $id, $result);
        if (isset($result['openid'])) {
            unset($result['openid']);
        }
        return $result;
    }

    # 发送短信验证码
    public function sms()
    {
        $mobile = Dever::input('mobile', 'is_numeric', '手机号');
        $data = Dever::load('template', 'msg')->send('reg_code', array('mobile' => $mobile));
        return $data;
    }
}