<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\common\model\bbfxshop;

use think\Db;
use think\Cache;

class Attachment
{
    public static function upload($params){ 
        global $_W;
        
        if(in_array($params['imagetype'],['pem','txt','crt','xlsx','xlx','docx','doc','ppt','csv','key'])){
            return __json(0,"该类型文件不可上传");
        }

        if(empty($_W['base'])){
            $set = \app\common\model\bbfxshop\Common::getConfig(true);
            $config = $set['data']['aliyun_oss'];
        }
        else{
            $config = $_W['base']['data']['aliyun_oss'];
        }

        if($config['status'] != 1){
            return __json(0,"未开启附件存储");
        }

        if($config['type'] == 1){
            $result = \app\common\model\bbfxshop\QiniuOss::upload($params['url']);
        }
        else{
            $result = \app\common\model\bbfxshop\AliyunOss::upload($params['url']);
        }
        if($result['code'] == 1){
            // self::initCache();
        }
        return $result;
    }

    public static function initCache(){
        $attachment_list = Db::name("attachment")->where(['oss_domain'=>['neq','']])->field("url,oss_domain")->select();
        if(!empty($attachment_list)){
            $attachment_list = array_skey($attachment_list,"url");
            Cache::set("attachment_list",$attachment_list);
        }
    }
}