<?php
namespace Passport\Src;

use Dever;
use Passport\Lib\Base;

class App extends Base
{
    /**
     * 更新用户信息
     *
     * @return mixed
     */
    public function update()
    {
        $uid = $this->check();
        $name = Dever::input('username');
        $avatar = Dever::input('avatar');
        $sex = Dever::input('gender');
        $city = Dever::input('city');
        $mobile = Dever::input('mobile');
        $province = Dever::input('province');
        $country = Dever::input('country');

        $info = Dever::load('passport/user-one', $uid);
        if ($info) {
            $update['set_username'] = $name;
            $update['set_avatar'] = $avatar;
            $update['set_sex'] = $sex;
            $update['set_city'] = $city;
            $update['set_province'] = $province;
            $update['set_country'] = $country;
            if ($country && $province && $city) {
            	$update['set_area'] = $country . ',' . $province . ',' . $city;
            }
            
            $update['where_id'] = $uid;
            Dever::load('passport/user-update', $update);
        } else {
            Dever::alert('无效的用户id,请重新登录');
        }

        $result = Dever::db('passport/user')->one($uid);
        if ($name) {
            $result['username'] = $name;
        }
        if ($avatar) {
            $result['avatar'] = $avatar;
        }
        if ($sex) {
            $result['sex'] = $sex;
        }
        if ($city) {
            $result['city'] = $city;
        }
        if ($province) {
            $result['province'] = $province;
        }
        if ($country) {
            $result['country'] = $country;
        }
        if ($country && $province && $city) {
            $result['area'] = $update['set_area'];
        }
        
        $result['uid'] = $uid;
        $result['signature'] = Dever::login($uid);

        return $result;
    }

    /**
     * 注册用户信息：app端已经拿到了openid了
     *
     * @return mixed
     */
    public function wechat_reg()
    {
        $user = array();
        $user['temp'] = 2;
        $user['username'] = Dever::input('username');
        $user['avatar'] = Dever::input('avatar');
        $user['sex'] = Dever::input('gender');
        $user['city'] = Dever::input('city');
        $user['province'] = Dever::input('province');
        $user['country'] = Dever::input('country');
        

        $data = array();
        $data['openid'] = Dever::input('openid');
        $data['unionid'] = Dever::input('unionid');
        $data['access_token'] = Dever::input('access_token');
        $data['refresh_token'] = Dever::input('refresh_token');
        $data['expires_in'] = Dever::input('expires_in');

        $account = Dever::input('account', 1);
        $system = Dever::input('system', 1);
        $source_type = Dever::input('source_type');
        $source = Dever::input('source');

        $result = $this->wechat($data, $user, $account, $system, $source_type, $source);

        if (isset($result['uid']) && $result['uid']) {
            # 记录app登录日志
            $appData = array('uid' => $result['uid'], 'source_type' => $source_type);
            $app = Dever::db('passport/app')->one($appData);
            if (!$app) {
                $appData['system_id'] = $system;
                Dever::db('passport/app')->insert($appData);
            }
        }

        return $result;
    }

    /**
     * 生成用户或者登录
     *
     * @return int
     */
    public function login()
    {
    	$mobile = Dever::input('mobile');
    	$code = Dever::input('mcode');
        $system = Dever::input('system', 1);
    	$source_type = Dever::input('source_type', 'ios');

        # 添加测试账号
        if ($mobile == '18710005124') {
            
        } else {
            $mobile = Dever::load('passport/reg')->checkMobileExists(false, -1);
        }
    	

        $uid = false;
        $info = Dever::load('passport/user-one', array('mobile' => $mobile));
        if (!$info) {
            $data['username'] = Dever::hide($mobile);
            $data['mobile'] = $mobile;
            $data['system'] = $system;
            $data['source_type'] = $source_type;
            $uid = $this->reg($source_type, $data);

            Dever::score($uid, 'mobile_reg', '手机号注册');
        } else {
            $uid = $this->combine($info['id'], $mobile, 'mobile');
        }

        # 记录app登录日志
        $appData = array('uid' => $uid, 'source_type' => $source_type);
        $app = Dever::db('passport/app')->one($appData);
        if (!$app) {
            $appData['system_id'] = $system;
        	Dever::db('passport/app')->insert($appData);
        }

        $result = Dever::db('passport/user')->one($uid);
        $result['uid'] = $uid;
        $result['signature'] = Dever::login($uid);
        return $result;
    }

    /**
     * uuid登录
     *
     * @return int
     */
    public function login_uuid()
    {
        # 这里后续要加密
        $uuid = Dever::input('uuid');
        $system = Dever::input('system', 1);
        $source_type = Dever::input('source_type', 'applet');

        $appData = array('uuid' => $uuid, 'source_type' => $source_type);
        $app = Dever::db('passport/app')->one($appData);
        if (!$app) {
            $user['add_username'] = '';
            $user['add_bind'] = 2;
            $user['add_temp'] = 1;
            $user['add_source_type'] = $source_type;
            $user['add_system_id'] = $system;
            $uid = Dever::load('passport/user-insert', $user);
            $this->createUsername($uid, '', true);

            $appData['uid'] = $uid;
            $appData['system_id'] = $system;
            Dever::db('passport/app')->insert($appData);
        } else {
            $uid = $app['uid'];
        }

        $result = Dever::db('passport/user')->one($uid);
        $result['uid'] = $uid;
        $result['signature'] = Dever::login($uid);
        return $result;
    }

    //获取用户信息
    public function user() {
        $uid = $this->check();
        $data=Dever::db('passport/user')->one($uid);
        if(!$data['pic']){
                $data['pic']=DEVER_APP_HOST . '../data/upload/pic.png';
        }
        return $data;
    }
}