<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

class Journal extends Core
{
    public function getCate()
    {
        $id = Dever::input('system', 1);

        $cate = array();
        if ($id > 0) {
            $cate = Dever::db('journal/cate')->one($id);
            $this->data['cate'] = $cate;
        }

        $cateList = Dever::db('journal/cate')->getAll();
        $this->data['cateList'] = $cateList;

        return $cate;
    }

    # 小刊首页
    public function home()
    {
        # 精选推荐
        $this->jingxuan(-1, 5);

        $cate = $this->getCate();

        # 小刊列表
        $where = array();
        if (isset($cate['id'])) {
            $where['cate_id'] = $cate['id'];
        }
        if (checkVersion()->journal_buy == 2) {
            # 隐藏支付的小刊
            //$where['buy'] = 2;
        }
        $data = Dever::db('journal/info')->getAll($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->info($v);
            }
            $this->data['journal'] = $data;
        } else {
            $this->data['journal'] = array();
        }

        return $this->data;
    }

    # 根据分类获取小刊列表
    public function getList()
    {
        $key = 'journal_list_' . Dever::input('system');
        //$data = Dever::cache($key);
        $data = array();

        if ($data) {
            return $data;
        }

        $cate = $this->getCate();

        $time = time();
        $day = (7 * 86400);

        # 小刊列表
        $where = array();
        if (isset($cate['id'])) {
            $where['cate_id'] = $cate['id'];
        }
        if ($this->app) {
            $where['cate_id_1'] = 1;
            $where['time'] = $time - $day;

            # 获取最新的小刊
            $this->data['push'] = Dever::db('journal/info')->getNew($where);

        } else {
            if (isset($cate['journal_id'])) {
                $this->data['push'] = Dever::db('journal/info')->getOne($cate['journal_id']);
            } else {
                # 获取最新的小刊
                $this->data['push'] = Dever::db('journal/info')->getNew();
            }
        }
        

        $this->data['push'] = $this->ding($this->data['push']);

        
        $data = Dever::db('journal/info')->getAllByPage($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->info($v);
            }
            $this->data['journal'] = $data;
        } else {
            $this->data['journal'] = array();
        }

        //Dever::cache($key, $this->data, 900);

        return $this->data;
    }

    # 根据分类获取我已订阅的小刊列表
    public function getMyList()
    {
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }

        $cate = $this->getCate();

        if (!$cate) {
            Dever::alert('错误的信息');
        }

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

        $this->data['push'] = $this->ding($this->data['push']);

        # 获取我的信息
        $this->data['user'] = Dever::load('passport/api')->info($this->data['uid']);
        
        # 我的小刊列表
        $where = array();
        $where['uid'] = $this->data['uid'];
        $where['type'] = 4;
        $where['source'] = '1,2';
        $where['cate_id'] = $cate['id'];

        $key = 'my_subscribe_' . $where['uid'] . '_' . $cate['id'];
        //$data = Dever::cache($key);
        $data = array();
        if (!$data) {
            $where['status'] = 2;
            $data = Dever::db('act/subscribe')->getMyAll($where);
            if ($data) {
                //Dever::cache($key, $data, 900);
            }
        }
        
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->info($v);
            }
            $this->data['journal'] = $data;
        } else {
            $this->data['journal'] = array();
        }

        return $this->data;
    }

    public function ding($data)
    {
        $data = Dever::load('journal/api')->ding($data);

        return $data;
    }

    # 获取详细信息
    private function info($data, $subscribe = true)
    {
        if ($data['pdate'] > 0) {
            $data['pdate_time'] = date('Y-m-d H:i:s', $data['pdate']);
        }

        $data = $this->ding($data);

        if ($data['pic_view_bg']) {
            $data['pic_view_bg'] = explode(',', $data['pic_view_bg']);
        } else {
            $data['pic_view_bg'] = array();
        }

        $data['buy'] = intval($data['buy']);

        # 获取订阅列表
        if ($subscribe) {
            $data['subscribe'] = array();
            //$data['subscribe'] = Dever::load('act/lib/subscribe')->getList($data['id'], 4);

            if (!$data['subscribe']) {
                $data['subscribe'] = array();
            }
        }

        return $data;
    }

    # 精选推荐更多
    public function jingxuan($page = true, $num = 4)
    {
        if (!$page) {
            $page = true;
        }
        if ($page === -1) {
            $page = false;
        }
        if (!$num) {
            $num = 4;
        }
        $this->data['push_jingxuan'] = Dever::load('push/lib/data')->get('jingxuan', '小刊精选推荐', 4, $page, $num);

        $count = count($this->data['push_jingxuan']);

        
        if ($num == 5) {
            if ($count < 4 || !$this->data['push_jingxuan']) {
                $this->data['push_jingxuan'] = array();
            }

            $this->data['push_jingxuan_more'] = 2;
            # 是否有更多按钮
            if ($count > 4 && isset($this->data['push_jingxuan'][4])) {
                unset($this->data['push_jingxuan'][4]);
                $this->data['push_jingxuan_more'] = 1;
            }
        } else {
            $page = Dever::pageInfo();
            if ($page['status'] != 1) {
                Dever::$global['page']['current']['next_page'] = 1;
            }
        }

        return $this->data;
    }

    # 小刊查看详情
    public function view()
    {
        $id = $this->check();
        $this->data['info'] = $this->info($this->data['info'], false);
        $this->button_old($this->data['info']);

        if ($this->data['uid'] > 0) {
            //Dever::score($this->data['uid'], 'view_journal', '浏览小刊首页');
        }

        return $this->data;
    }

    # 按钮设置
    private function button_old($info)
    {
        $this->data['button'] = array();
        $this->setButton(-1);

        $uid = $this->data['uid'];

        # 查看是否订阅
        $subscribe = Dever::load('act/lib/subscribe')->get($info['id'], $uid, 4);
        $this->data['info']['subscribe'] = 2;

        # 查看是否有活动
        $active = Dever::db('journal/active')->one(array('id' => $info['id']));
        $this->data['info']['active'] = 2;
        if ($active && $active['status'] == 1) {
            $this->data['info']['active'] = 1;
        }

        if (isset($this->data['info']['share_content'])) {
            $source = Dever::db('source/info')->one(1);
            $this->data['info']['share_link'] = $source['code'];
        }
        
        $time = time();
        if ($active && $time >= $active['start'] && $time < $active['end']) {
            $this->data['info']['active'] = 1;
        }

        if ($this->data['info']['active'] == 1) {
            $this->setButton('share', 1);
        } else {
            $this->setButton('share', -1);
        }

        if ($info['buy'] == 2) {
            # 免费的
            $this->setButton('read', 1, '开始阅读');
            $this->setButton('code', -1);
            $this->setButton('share', -1);
        } elseif ($subscribe == 1) {
            $this->data['info']['subscribe'] = 1;
            # 已订阅
            $this->setButton('read', 1, '开始阅读');
            $this->setButton('code', 2, '再次购买');

            if (checkVersion()->journal_buy == 2) {
                $this->setButton('code', 1, '兑换码阅读');
                $this->setButton('share', -1);
            }
        } elseif ($info['buy'] == 1 || $info['buy'] == 3) {
            # 需要购买
            $this->setButton('read', 2, '购买阅读');
            $this->setButton('code', 1, '兑换码阅读');

            if (checkVersion()->journal_buy == 2) {
                $this->setButton('read', -1);
                $this->setButton('share', -1);
            }
        }

        $applet_type = Dever::input('applet_type');
        if ($applet_type == 2 && isset($this->data['config']['ios_pay']) && $this->data['config']['ios_pay'] == 1 && $info['buy'] == 1) {
            # 关闭支付
            if ($subscribe == 1) {
                $this->data['info']['subscribe'] = 1;
                $this->setButton('read', 1, '开始阅读');
            } else {
                $this->setButton('read', -1);
            }
            
            $this->setButton('code', 1, '兑换码阅读');
            //$this->setButton('share', -1);
        }

        # 会员随便看
        $this->data['info']['vip'] = 2;
        $vip = Dever::load('vip/lib/member')->get($uid, $info['cate_id']);
        if ($vip > 0) {
            $this->data['info']['vip'] = 1;
            $this->setButton('read', 1, '开始阅读');
        }
    }

    # 小刊查看详情 新版本
    public function view_v1()
    {
        $id = $this->check();
        $this->data['info'] = $this->info($this->data['info'], false);
        $this->button($this->data['info']);

        if ($this->data['uid'] > 0) {
            //Dever::score($this->data['uid'], 'view_journal', '浏览小刊首页');
        }

        return $this->data;
    }

    # 按钮设置
    private function button($info)
    {
        $this->data['button'] = array();
        $this->setButton();

        $uid = $this->data['uid'];

        # 查看是否订阅
        $subscribe = Dever::load('act/lib/subscribe')->get($info['id'], $uid, 4);
        $this->data['info']['subscribe'] = 2;

        # 查看是否有活动
        $active = Dever::db('journal/active')->one(array('id' => $info['id']));
        $this->data['info']['active'] = 2;
        if ($active && $active['status'] == 1) {
            $this->data['info']['active'] = 1;
        }

        if (isset($this->data['info']['share_content'])) {
            $source = Dever::db('source/info')->one(1);
            $this->data['info']['share_link'] = $source['code'];
        }
        
        $time = time();
        if ($active && $time >= $active['start'] && $time < $active['end']) {
            $this->data['info']['active'] = 1;
        }

        if ($this->data['info']['active'] == 1) {
            $this->setButton('share', 1);
        } else {
            $this->setButton('share', -1);
        }

        if ($info['buy'] == 2) {
            # 免费的
            $this->setButton('read', 1);
            $this->setButton('buy', -1);
            $this->setButton('code', -1);
            $this->setButton('share', -1);
        } elseif ($subscribe == 1) {
            $this->data['info']['subscribe'] = 1;
            # 已订阅
            $this->setButton('read', 1);
            $this->setButton('buy', 1, '再次购买');
            $this->setButton('code', 1);

            /*
            $subscribeInfo = Dever::load('act/lib/subscribe')->getInfo($info['id'], $uid, 4);
            if (isset($subscribeInfo['source']) && $subscribeInfo['source'] == 2) {
                $this->setButton('code', 1, '兑换码阅读');
            }
            */

            if (checkVersion()->journal_buy == 2) {
                $this->setButton('code', 1);
                $this->setButton('share', -1);
            }
        } elseif ($info['buy'] == 1 || $info['buy'] == 3) {
            # 需要购买
            $this->setButton('read', -1);
            $this->setButton('buy', 1);
            $this->setButton('code', 1);

            if (checkVersion()->journal_buy == 2) {
                $this->setButton('buy', -1);
                $this->setButton('share', -1);
            }
        }

        $applet_type = Dever::input('applet_type');

        $this->data['info']['vip_state'] = 1;
        $this->data['info']['code_state'] = 1;
        $version_code = Dever::input('version_code');
        if ($version_code) {
            $source_type = Dever::input('source_type');
            $version_where['source_type'] = $source_type;
            $version_where['name'] = $version_code;
            $version = Dever::db('main/version')->getOne($version_where);
            if ($version && $version['vip'] > 0) {
               $this->data['info']['vip_state'] = $version['vip'];
               $this->data['info']['code_state'] = $version['button'];
               if ($this->source_type == 'ios' && $this->data['info']['code_state'] == 2) {
                    $this->setButton('read', 1);
               }
            }
        }

        if ($applet_type == 2 && isset($this->data['config']['ios_pay']) && $this->data['config']['ios_pay'] == 1 && $info['buy'] == 1) {
            # 关闭支付
            if ($subscribe == 1) {
                $this->data['info']['subscribe'] = 1;
                $this->setButton('read', 1);
            } else {
                $this->setButton('read', -1);
            }
            
            $this->setButton('buy', -1);
            $this->setButton('code', 1);
            //$this->setButton('share', -1);
        }

        # 会员随便看
        $this->data['info']['vip'] = 2;
        $vip = Dever::load('vip/lib/member')->get($uid, $info['cate_id']);
        if ($vip > 0) {
            $this->data['info']['subscribe'] = 1;
            $this->data['info']['vip'] = 1;
            $this->setButton('read', 1);
        }

        # 是否在外站订阅过
        $user = Dever::db('passport/user')->one($uid);
        if ($user && $user['mobile']) {
            $mobileWhere['mobile'] = $user['mobile'];
            $mobileWhere['product_id'] = $info['id'];
            $mobile = Dever::db('journal/mobile')->one($mobileWhere);
            if ($mobile) {
                $this->data['info']['subscribe'] = 1;
                $this->setButton('read', 1);
            }
        }
    }

    private function setButton($key = false, $status = false, $name = false)
    {
        # 按钮状态，-1为不显示
        if (!$this->data['button']) {
            $this->data['button'] = array
            (
                # 第一个按钮
                'read' => array('status' => -1, 'name' => '开始阅读'),
                # 第二个按钮
                'buy' => array('status' => -1, 'name' => '购买阅读'),
                # 第三个按钮
                'code' => array('status' => -1, 'name' => '兑换阅读'),
                # 第四个按钮 免费阅读赢取大礼包 改为
                'share' => array('status' => 1, 'name' => '积分打榜赢奖品'),
            );

            if ($key == -1) {
                unset($this->data['button']['buy']);
            }
        }

        if ($key && isset($this->data['button'][$key])) {
            if ($status) {
                $this->data['button'][$key]['status'] = $status;
            }
            if ($name) {
                $this->data['button'][$key]['name'] = $name;
            }
        }
    }

    # 购买阅读页
    public function buy()
    {
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        $id = $this->check();

        # 获取购买说明
        $buy = Dever::db('journal/buy')->one(array('id' => $id));

        $this->data['buy_desc'] = '';
        if ($buy) {
            $this->data['buy_desc'] = $buy['buy_content'];
        }

        $this->data['buy_applet_notice'] = '在此购买单本可直接阅读电子刊，购买多本，直接累计积分，不会生成兑换码。';
        if ($buy && isset($buy['buy_applet_notice']) && $buy['buy_applet_notice']) {
            $this->data['buy_applet_notice'] = $buy['buy_applet_notice'];
        }

        if ($this->data['info']['buy'] == 2) {
            $this->data['buy_list'] = array();
            return $this->data;
        }
        # 获取购买列表
        $this->data['buy_list'] = Dever::db('journal/buy_num')->getAll(array('info_id' => $id));

        if ($this->data['info']['cash'] <= 0) {
            $this->data['info']['cash'] = 100000;
        }
        if ($this->data['buy_list']) {
            foreach ($this->data['buy_list'] as $k => $v) {
                if (!$v['name']) {
                    $this->data['buy_list'][$k]['name'] = '购买' . $v['num'] . '本';
                }

                $price = $v['num'] * $this->data['info']['cash'];
                if (!$v['price']) {
                    $this->data['buy_list'][$k]['price'] = $price;
                }

                $this->data['buy_list'][$k]['cash'] = $price;
            }
        }

        return $this->data;
    }

    # 小刊邀请活动页
    public function active()
    {
        $id = $this->check();

        # 获取购买说明
        $buy = Dever::db('journal/buy')->one(array('id' => $id));

        # 获取活动说明
        $this->data['active'] = Dever::db('journal/active')->one(array('id' => $id));


        if (!$this->data['active']) {
            $this->data['active'] = (object) array();
            return $this->data;
        }

        $time = time();
        if ($this->data['active'] && $time >= $this->data['active']['start'] && $time < $this->data['active']['end']) {
            $this->data['active']['status'] = 1;
        } else {
            $this->data['active']['status'] = 2;
        }

        if ($time < $this->data['active']['start']) {
            $this->data['active']['status'] = 3;
        }

        $this->data['active']['buy_title'] = '';
        $this->data['active']['buy_desc'] = '';
        if ($buy) {
            $this->data['active']['buy_title'] = $buy['buy_title'];
            $this->data['active']['buy_desc'] = $buy['buy_desc'];
        }

        # 获取我邀请的好友
        if ($this->data['uid']) {
            $this->data['invite'] = Dever::load('act/lib/invite')->getList($this->data['uid'], $id);
        }

        # 是否显示购买按钮 1显示 2不显示
        $this->data['buy'] = 1;
        if (checkVersion()->journal_buy == 2) {
            $this->data['buy'] = 2;
        }

        return $this->data;
    }

    # 积分排行榜
    public function score()
    {
        $id = $this->check();

        $this->data['score'] = Dever::load('act/lib/score')->getList($id);

        $this->data['my'] = array();
        if ($this->data['uid'] && $this->data['score']) {
            foreach ($this->data['score'] as $k => $v) {
                if ($v['uid'] == $this->data['uid']) {
                    $this->data['my'] = $v;
                    $this->data['my']['pai'] = $k + 1;
                }
            }
        }

        if (!$this->data['my'] && $this->data['uid'] > 0) {
            $my = Dever::load('act/lib/score')->getList($id, 4, $this->data['uid']);
            if ($my) {
                $this->data['my'] = $my[0];
                $this->data['my']['pai'] = '50+';
            }
        }

        if (!$this->data['my']) {
            $user = Dever::load('passport/api')->info($this->data['uid']);
            $this->data['my'] = array();
            $this->data['my']['uid'] = $this->data['uid'];
            $this->data['my']['username'] = $user['username'];
            $this->data['my']['avatar'] = $user['avatar'];
            $this->data['my']['mobile'] = $user['mobile'];
            $this->data['my']['score'] = '0';
            $this->data['my']['pai'] = '未上榜';
        }

        # 获取活动说明
        $this->data['active'] = Dever::db('journal/active')->one(array('id' => $id));


        if (!$this->data['active']) {
            $this->data['active'] = (object) array();
            return $this->data;
        }

        $time = time();
        if ($this->data['active'] && $time >= $this->data['active']['start'] && $time < $this->data['active']['end']) {
            $this->data['active']['status'] = 1;
        } else {
            $this->data['active']['status'] = 2;
        }

        if ($time < $this->data['active']['start']) {
            $this->data['active']['status'] = 3;
        }

        return $this->data;
    }

    # 邀请好友排行榜
    public function invite()
    {
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }

        $id = $this->check();

        # 获取活动说明
        $this->data['active'] = Dever::db('journal/active')->one(array('id' => $id));

        $this->data['invite'] = Dever::load('act/lib/invite')->getList($this->data['uid'], $id);

        return $this->data;
    }


    # 小刊阅读页
    public function read()
    {
        //return Dever::load('main/journal_v1')->read();
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        # 验证是否可以阅读
        $id = $this->check();
        $this->data['info'] = $this->info($this->data['info'], false);

        # 是否转码
        //$this->data['info']['bgmusic'] = convert($this->data['info']['bgmusic'], 'mp3', 'journal/info', $this->data['info']['id'], 'bgmusic');

        # 会员随便看
        $vip = Dever::load('vip/lib/member')->get($this->data['uid'], $this->data['info']['cate_id']);

        if ($vip <= 0) {
            # 是否在外站订阅过
            $user = Dever::db('passport/user')->one($this->data['uid']);
            if ($user && $user['mobile']) {
                $mobileWhere['mobile'] = $user['mobile'];
                $mobileWhere['product_id'] = $id;
                $mobile = Dever::db('journal/mobile')->one($mobileWhere);
                if ($mobile) {
                    $vip = 1;
                }
            }
        }

        if ($this->app) {
            # 会员状态
            $this->data['info']['vip_state'] = 1;
            $this->data['info']['code_state'] = 1;
            $version_code = Dever::input('version_code');
            if ($version_code) {
                $source_type = Dever::input('source_type');
                $version_where['source_type'] = $source_type;
                $version_where['name'] = $version_code;
                $version = Dever::db('main/version')->getOne($version_where);
                if ($version && $version['vip'] > 0) {
                   $this->data['info']['vip_state'] = $version['vip'];
                   $this->data['info']['code_state'] = $version['button'];
                   if ($this->source_type == 'ios' && $this->data['info']['code_state'] == 2) {
                        $vip = 1;
                   }
                }
            }
        }
        
        $submit = false;
        if ($vip > 0) {
            $subscribe = 1;
        } else {
            $subscribe = Dever::load('act/lib/subscribe')->get($id, $this->data['uid'], 4);

            if ($this->data['info']['buy'] == 1 && $subscribe == 2) {
                Dever::alert('未订阅');
            }

            if ($this->data['info']['buy'] == 2) {
                $submit = true;
                //Dever::load('act/lib/subscribe')->submit($this->data['uid'], $id, 4);
            }
        }

        if ($this->data['uid'] > 0) {
            //Dever::score($this->data['uid'], 'read_journal', '阅读小刊');
        }

        # 获取最新10条内容
        $where = array();
        $where['info_id'] = $id;
        $data = Dever::db('journal/content')->getAll($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->readContent($v);
            }
            $this->data['content'] = $data;
        } else {
            $this->data['content'] = array();
        }

        # 浏览量+1
        //Dever::db('journal/info')->addView($id);

        # 传给下一步操作的参数
        $this->data['end']['id'] = $id;
        $this->data['end']['uid'] = $this->data['uid'];
        $this->data['end']['submit'] = $submit;

        return $this->data;
    }

    public function readContent($info)
    {
        $name = $info['name'];
        $focus = $info['focus'];
        $author_id = $info['author_id'];
        $content = $info['content'];
        unset($info['name']);
        unset($info['focus']);
        unset($info['author_id']);
        unset($info['content']);

        if (($info['type'] == 1 || $info['type'] == 7 || $info['type'] == 8) && $info['text']) {
            $info['text'] = Dever::array_decode($info['text']);
            if ($info['text']) {
                foreach ($info['text'] as $k => $v) {
                    if (isset($info['text'][$k]['bgcolor_type']) && $info['text'][$k]['bgcolor_type'] == array(2)) {
                        $info['text'][$k]['bgcolor'] = '';
                    }
                }
            }
            
        } else {
            unset($info['text']);
        }

        if ($info['type'] == 10 && $info['palaces']) {
            $info['palaces'] = Dever::array_decode($info['palaces']);
        } else {
            unset($info['palaces']);
        }

        if ($info['type'] == 21 && $info['talk']) {
            $info['talk'] = Dever::array_decode($info['talk']);
            foreach ($info['talk'] as $k => $v) {
                $info['talk'][$k]['talk_type'] = implode(',', $v['talk_type']);
                $info['talk'][$k]['talk_location'] = implode(',', $v['talk_location']);
            }
        } else {
            unset($info['talk']);
        }

        if ($info['type'] == 3) {
            $info['info'] = array();
            $info['info']['name'] = $name;
            $info['info']['focus'] = $focus;
            $info['info']['author_id'] = $author_id;
            $info['info']['content'] = $content;
            $info['info'] = Dever::load('content/lib/article')->getContent($info['info'], $this->data['uid']);
        } elseif ($info['type'] == 4 || $info['type'] == 5) {
            $info['video'] = convert($info['video'], 'mp4', 'journal/content', $info['id'], 'video');
            $info = Dever::load('video/lib/vod')->avinfo($info, 'video', 'journal/content');
            if ($info['type'] == 5) {
                $info['name'] = $name;
            }
        } elseif ($info['type'] == 6) {
            $info['name'] = $name;
            //音频先不转码
            //$info['music'] = convert($info['music'], 'mp3', 'journal/content', $info['id'], 'music');
        } elseif ($info['type'] == 11) {
            $info['info'] = Dever::load('content/lib/article')->get($info['article_id']);
        } elseif ($info['type'] == 12) {
            $info['info'] = Dever::load('video/lib/vod')->get($info['vod_id']);
        } elseif ($info['type'] == 13) {
            $info['info'] = Dever::load('video/lib/live')->get($info['live_id']);

            if ($this->data['uid'] > 0) {
                $info['info']['note'] = Dever::load('act/lib/note')->get($this->data['uid'], $info['info']['id'], 3);
                
            } else {
                $info['info']['note'] = 2;
            }
        }

        if (isset($info['info']['author_id']) && $info['info']['author_id']) {
            $info['info']['author'] = Dever::db('content/author')->one($info['info']['author_id']);
        }

        if (isset($info['pic']) && $info['pic']) {
            $info['pic'] = str_replace('http://', 'https://', $info['pic']);
        }

        return $info;
    }

    private function check()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('小刊已下架');
        }

        $status = false;
        if (strstr($id, 'preview_')) {
            $id = str_replace('preview_', '', $id);
            $status = true;
        }

        $where['id'] = $id;
        $this->data['info'] = Dever::db('journal/info')->one($where);

        if (!$this->data['info']) {
            Dever::alert('小刊已下架');
        }

        if ($status) {
            $this->data['info']['status'] = '2';
        }

        return $id;
    }

    # 购买
    public function buy_action()
    {
        return Dever::load('act/order')->pay();
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        $source_uid = $this->getSourceUid(false);
        $buy_id = Dever::input('buy_id');
        $journal_id = Dever::input('journal_id');
        $cate = $this->getCate();

        if (!$buy_id) {
            Dever::alert('请传入正确的支付ID');
        }
        if (!$journal_id) {
            Dever::alert('请传入正确的小刊ID');
        }

        $info = Dever::db('journal/info')->one($journal_id);
        $buy = Dever::db('journal/buy_num')->one($buy_id);

        if (!$buy || !$info) {
            Dever::alert('请传入正确的小刊ID');
        }

        if (!$buy['name']) {
            $buy['name'] = '购买' . $buy['num'] . '本';
        }

        if ($info && isset($info['cate_id']) && $info['cate_id'] > 0) {
            $cate = Dever::db('journal/cate')->one($info['cate_id']);
            $this->data['system'] = $info['cate_id'];
        }

        $user = Dever::db('passport/user')->one($this->data['uid']);
        $wechat = Dever::db('passport/wechat')->one(array('uid' => $this->data['uid'], 'type' => 1, 'system_id' => $this->data['system']));

        $source_type = Dever::input('source_type');

        if ($source_type == 'ios') {
            $method = 'apple';
            $order_data['system'] = 3;
            $account_id = 3;
            # 使用苹果内购支付
            $receipt = Dever::input('receipt');
            if (!$receipt) {
                Dever::alert('苹果内购支付失败，没有receipt参数');
            }
        } elseif ($source_type == 'android') {
            $method = 'app';
            $order_data['system'] = 4;
            $account_id = 3;
        } else {
            $method = 'applet';
            $order_data['system'] = 1;
            $account_id = 1;
            # 根据cate_id走不同的支付id，暂时不需要
            if ($cate && $cate['id'] == 2) {
                $account_id = 4;
            } elseif ($cate && $cate['id'] == 3) {
                $account_id = 5;
            }
            //$account_id = $cate['pay_id'];
        }

        $order_id = $this->createOrder('JP');
        $order_data['cate_id'] = $cate['id'];
        $order_data['order_id'] = $order_id;
        $order_data['product_id'] = $journal_id;
        $order_data['buy_id'] = $buy_id;
        $order_data['uid'] = $this->data['uid'];
        if ($source_uid > 0) {
            $order_data['source_uid'] = $source_uid;
        }
        
        $order_data['type'] = 1;
        $order_data['status'] = 1;
        $order_data['name'] = $info['name'] . '-' . $buy['name'];
        //$order_data['num'] = Dever::input('num', 1);
        $order_data['buy_num'] = $buy['num'];
        if (isset($buy['price']) && $buy['price'] && $buy['price'] > 0) {
            $order_data['cash'] = $buy['price'];
        } else {
            $order_data['cash'] = $info['cash'] * $order_data['num'];
        }

        $source = Dever::input('source');
        $system = Dever::input('system', 1);
        if (!$source) {
            $source = $system;
        }
        if ($source) {
            $order_data['source'] = $source;
        }
        $seller = Dever::input('seller');
        if ($seller) {
            $order_data['seller_id'] = $seller;
        }

        $id = Dever::db('act/order')->insert($order_data);

        if (!$id) {
            Dever::alert('支付失败');
        }

        //$param参数
        $param = array
        (
            'account_id' => $account_id,
            'project_id' => 1,
            'uid' => $this->data['uid'],
            'username' => $user['username'],
            'name' => $order_data['name'],
            'cash' => $order_data['cash'],
            //'cash' => '0.01',
            'openid' => $wechat['openid'],
            'product_id' => $journal_id . '-' . $buy_id,
            'order_id' => $order_id,
        );

        if ($method == 'apple') {
            $param['other'] = $receipt;
        }

        $this->data['pay'] = Dever::load('pay/api.' . $method, $param);

        if ($order_data['system'] == 1) {
            if (isset($this->data['pay']['prepay_id'])) {
                Dever::load('act/lib/form')->submit($this->data['uid'], $this->data['pay']['prepay_id'], 2, $this->data['system']);
            } else {
                Dever::alert('支付失败');
            }
        }

        return $this->data;
    }

    # 根据code检索是否存在
    public function checkCode()
    {
        return true;
        return Dever::load('act/order')->checkCode();
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        $code = trim(Dever::input('code'));

        if ($code) {

            $info = Dever::db('code/info')->one(array('code' => $code));

            if (!$info) {
                Dever::alert('兑换码不存在');
            }

            if ($info['type'] == 3) {
                Dever::alert('兑换码已被使用');
            }

            if ($info['type'] == 4) {
                Dever::alert('兑换码已作废');
            }

            if ($info['type'] == 2 && $this->data['uid'] != $info['uid']) {
                //Dever::alert('兑换码已被领取');
            }

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

            if ($order) {
                Dever::alert('兑换码已被使用');
            }

            if ($info['product_id']) {
                $product = Dever::db('journal/info')->one($info['product_id']);
                $this->data['product'] = $product;

                return $this->data;
            }

        } else {
            Dever::alert('请输入正确的兑换码');
        }

        return true;
    }

    # 兑换
    public function code()
    {
        return Dever::load('act/order')->code();
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        $journal_id = Dever::input('journal_id', false, '> 0', '请传入正确的小刊id');

        $code = Dever::input('code');

        $source_uid = $this->getSourceUid(false);

        if ($code) {

            $code = trim($code);

            $code = str_replace("\n", '', $code);

            $product = Dever::db('journal/info')->one($journal_id);

            $info = Dever::db('code/info')->one(array('code' => $code));

            if (!$info) {
                Dever::alert('兑换码不存在');
            }

            if ($info['type'] == 3) {
                Dever::alert('兑换码已被使用');
            }

            if ($info['state'] != 1) {
                Dever::alert('兑换码已作废');
            }

            if ($info['type'] == 4) {
                Dever::alert('兑换码已作废');
            }

            if ($info['type'] == 2 && $this->data['uid'] != $info['uid']) {
                //Dever::alert('兑换码已被领取');
            }

            if ($info['product_id'] > 0 && $info['product_id'] != $journal_id) {
                Dever::alert('请正确使用小刊兑换码');
            }

            if (isset($info['order_id']) && $info['order_id'] > 0) {
                $order_info = Dever::db('journal/order')->one(array('order_id' => $info['order_id']));
                if (!$order_info) {
                    Dever::alert('您要使用的兑换码还未支付');
                }

                if ($order_info && $order_info['status'] != 2) {
                    Dever::alert('您要使用的兑换码还未支付');
                }
            }

            $order = Dever::db('journal/order')->one(array('product_id' => $journal_id, 'code' => $code, 'type' => 2, 'uid' => $this->data['uid']));

            if ($order) {
                Dever::alert('您已经使用过该兑换码');
            }

            Dever::db('code/info')->update(array('where_id' => $info['id'], 'type' => 3, 'uid' => $this->data['uid'], 'ldate' => time()));

            $order_id = $this->createOrder('JC');

            $order_data['order_id'] = $order_id;
            $order_data['product_id'] = $journal_id;
            $order_data['cate_id'] = $product['cate_id'];
            $order_data['uid'] = $this->data['uid'];
            $order_data['code'] = $code;
            if ($source_uid > 0) {
                $order_data['source_uid'] = $source_uid;
            }
            
            $order_data['type'] = 2;
            if (!$info['create_uid'] || $info['create_uid'] <= 0) {
                # 通用兑换码
                $order_data['type'] = 4;
                $order_data['buy_num'] = $info['product_num'];
            }
            
            $order_data['status'] = 2;
            $order_data['name'] = $product['name'];

            $id = Dever::db('journal/order')->insert($order_data);

            # 订阅
            Dever::load('act/lib/subscribe')->submit($this->data['uid'], $journal_id, 2);

            $score = false;
            $num = false;
            if (isset($info['score']) && $info['score'] > 0) {
                $score = $info['score'];
            } elseif (isset($info['product_num']) && $info['product_num'] > 0) {
                if ($product && $product['dh_score'] > 0) {
                    $score = $info['product_num'] * $product['dh_score'];
                } elseif ($product) {
                    $num = $info['product_num'];
                }
            }

            if (isset($info['score']) && $info['score'] < 0) {
                $score = -1;
            }

            if ($score >= 0) {
                # 增加积分
                Dever::score($this->data['uid'], 'dh_journal', '兑换小刊', 'act/lib/score.submit?method=code&type=4&id=' . $journal_id, $score, $num);
            }

        } else {
            Dever::alert('请输入正确的兑换码');
        }

        return array('order_id' => $order_id);
    }

    private 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;
    }
}