<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/27
 * Time: 11:14
 */

namespace Addons\Upload;


use Services\Service;

class Helper extends Service
{
    /**
     * 生成唯一编码
     * @return string
     */
    public function randomCode(){
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return  $orderSn;
    }

    /**
     * 图片类型判断
     * @param string $type
     * @return string
     */
    public function upTypeFilter($type='img'){
        return in_array($type,['img','file']) ? $type : 'img';
    }
    public function upload_file($field, $path) {
        if (!key_exists($field, $_FILES)) {
            return false;
        }
        $ext = explode('.', $_FILES[$field]["name"]);
        $ext = $ext[count($ext) - 1];
        try {
            mkdir($path);
        } catch (\Exception $e) {
        }
        $path = $path . '/' . md5_file($_FILES[$field]['tmp_name']) . '.' . $ext;
        $saveImg = move_uploaded_file($_FILES[$field]['tmp_name'], $path);
        return $saveImg ? $path : $saveImg;
    }
}