<?php namespace Service\Src;

use Dever;

class Base
{
    public $key = 'jmbuybuybuy';

	public function __construct()
    {
        
        $p = Dever::input('p');
        if ($p) {
            $this->config = Dever::json_decode(Dever::decode($p, $this->key));
        }
        # 第三方账户
        $this->account = $this->getConfig('account', 1);
        # 小刊分类
        $this->cate = $this->getConfig('cate', 1);
        # 渠道
        $this->source = $this->getConfig('source', 1);
        # 类型 1为直接购买 2为购买兑换码
        $this->type = $this->getConfig('type', 1);
        # 销售商
        $this->seller = $this->getConfig('seller', 1);

        $this->signature = Dever::input('signature');
        $this->source_type = Dever::input('source_type');
        $this->location('list', array(), 'buy');
        $this->user = Dever::load('passport/user')->data();
        # 用户信息
        if (!$this->user && $this->signature) {
            $uid = Dever::load('passport/applet')->check();
            $this->user = Dever::db('passport/user')->one($uid);
        } elseif ($this->source_type) {
            $this->user = array();
            $this->user['id'] = -1;
            $this->user['username'] = '未登录用户';
            $this->user['mobile'] = '';
        }
    }

    # 创建url
    public function createUrl()
    {
        # 第三方账户
        $this->account = Dever::input('account', 1);
        # 分类
        $this->cate = Dever::input('cate', 1);
        # 渠道
        $this->source = Dever::input('source', 1);
        # 类型 1为直接购买 2为购买兑换码
        $this->type = Dever::input('type', 1);
        # 销售商
        $this->seller = Dever::input('seller', 1);
        
        $url = $this->url('home');

        return $url;
    }

    private function getConfig($key, $value)
    {
        if (isset($this->config[$key]) && $this->config[$key]) {
            return $this->config[$key];
        }
        return $value;
    }

    # 获取基本配置
    public function config()
    {
        $cate = Dever::db('journal/cate')->one($this->cate);

        $cate['login_title'] = '登录后才可以使用' . $cate['name'] . '服务';

        $cate['bglogo'] = 'background-image:url('.$cate['logo'].');';

        $url = urlencode($this->url('list_code'));
        $cate['login'] = 'location.href=\'' . Dever::url('request.auth?account='.$this->account.'&source='.$this->source.'&refer=' . $url, 'oauth') . '\'';

        $info = Dever::db('journal/info')->getOne($cate['journal_id']);

        if ($info && $info['pic_gd']) {
            $cate['cover'] = $info['pic_gd'];
        }

        $cate['list_link'] = $this->url('list_code');
        $cate['my_link'] = $this->url('my_code');
        return $cate;
    }

    # 生成url
    protected function url($uri, $param = array(), $project = 'service')
    {
        if ($uri == 'list' && $this->type == 2) {
            $uri = 'list_code';
        }
    	$send['cate'] = $this->cate;
    	$send['source'] = $this->source;
        $send['type'] = $this->type;
        $send['seller'] = $this->seller;
        $param['p'] = Dever::encode(Dever::json_encode($send), $this->key);
        if ($this->signature) {
            $param['signature'] = $this->signature;
        }
        if ($this->source_type) {
            $param['source_type'] = $this->source_type;
        }
    	$param = http_build_query($param);
    	$url = Dever::url($uri . '?' . $param, $project);

    	return $url;
    }

    protected function location($uri, $param = array(), $project = 'service')
    {
    	return Dever::location($this->url($uri, $param, $project));
    }

    protected function createOrder($prefix = 'JC')
    {
        $order = Dever::order($prefix);

        //$order = Dever::rand(2, 2) . Dever::rand(6, 0);

        $info = Dever::db('journal/order')->one(array('order_id' => $order));

        if ($info) {
            return $this->createOrder($prefix);
        }

        return $order;
    }
}