<?php namespace Place\Api;
use Dever;
use Place;
use Place\Lib\Main;
class Down extends Main
{
    protected $login = true;
    protected $entry = true;
    
    # 下载某个附件
    public function act()
    {
        $g = \Dever\Helper\Secure::decode(Dever::input('g'));
        if (!$g) {
            Dever::error('无权限');
        }
        $info = Dever::load('value', 'content')->getInfo(Place::$user, $g);
        if (isset($info['value']) && $info['value']) {
            # 记录下载次数
            $data['uid'] = Place::$uid;
            $data['info_id'] = $info['id'];
            $data['value_id'] = $info['value']['id'];
            Dever::db('down', 'content')->insert($data);
            header('location:' . $info['value']['file']);
        }
        Dever::error('无权限');
    }

    # 我的下载
    public function getUserList()
    {
        $where['uid'] = Place::$uid;
        # 每页10条
        $set['num'] = 10;
        $data = Dever::db('down', 'content')->select($where, $set);
        $result = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $info = Dever::db('info', 'content')->find($v['info_id']);
                if ($info['status'] == 1) {
                    $value = Dever::db('value', 'content')->find($v['info_value_id']);
                    if ($value) {
                        $info['down_id'] = $v['id'];
                        $info['cdate'] = $v['cdate'];
                        $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
                        $info['value_name'] = $value['name'];
                        # 这里是否要再次下载？先不用吧
                        $result[] = $info;
                    }
                }
            }
        }
        return $result;
    }
}