<?php
// 应用公共文件

function ajax_return($code = 0, $msg = '', $data = [])
{
    $res      = ['code' => $code, 'msg' => $msg, 'data' => $data];
    $response = \think\Response::create($res, 'json');
    throw new \think\exception\HttpResponseException($response);
}

function url(string $url = '', array $vars = [], $suffix = true, $domain = true)
{
    return \think\facade\Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
}

function rand_str($len = 6)
{
    $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $res = '';
    for ($i = 0; $i < $len; $i++) {
        $key = rand(0, strlen($str));
        $res .= $str[$key];
    }
    return $res;
}

function array_get($array, $key, $default = null)
{
    return isset($array[$key]) ? $array[$key] : $default;
}

/**
 * 公共数据导出实现功能
 * @param string $expTitle
 * @param array $expCellName [['a','A']]
 * @param array $expTableData [['a'=>1]]
 * @param array $textValue ['a']
 */
function export_exl($expTitle, $expCellName, $expTableData, $textValue=[]) {
    $fileName    = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
    $cellNum     = count($expCellName);
    $dataNum     = count($expTableData);
    $objPHPExcel = new \PHPExcel();
    $cellName    = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
    for ($i = 0; $i < $cellNum; $i++) {
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
    }
// Miscellaneous glyphs, UTF-8
    for ($i = 0; $i < $dataNum; $i++) {
        for ($j = 0; $j < $cellNum; $j++) {
            if (in_array($expCellName[$j][0],$textValue)) {
                $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
            } else {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
    }
    ob_end_clean();
    header('pragma:public');
    header('Content-type:application/vnd.ms-excel');
    header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;
}



/**
 * @param string $file
 * @param array $cell
 * @param int $crop
 * @param int $sheet
 * @return array
 */
function import_exl($file = '', $cell = [], $crop = 0, $sheet = 0)
{
    $file = iconv("utf-8", "gb2312", $file);   //转码
    if (empty($file) OR !file_exists($file)) {
        die('file not exists!');
    }

    $objRead = new PHPExcel_Reader_Excel2007();   //建立reader对象
    if (!$objRead->canRead($file)) {
        $objRead = new PHPExcel_Reader_Excel5();
        if (!$objRead->canRead($file)) {
            die('No Excel!');
        }
    }

    $cellName  = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
    $obj       = $objRead->load($file);  //建立excel对象
    $currSheet = $obj->getSheet($sheet);   //获取指定的sheet表
    $columnH   = $currSheet->getHighestColumn();   //取得最大的列号
    $columnCnt = array_search($columnH, $cellName);
    $rowCnt    = $currSheet->getHighestRow();   //获取总行数

    $data = [];
    for ($_row = 1; $_row <= $rowCnt; $_row++) {  //读取内容
        if ($_row > $crop) {
            for ($_column = 0; $_column <= $columnCnt; $_column++) {
                $cellId    = $cellName[$_column] . $_row;
                $cellValue = $currSheet->getCell($cellId)->getValue();
                //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue();  #获取公式计算的值
                if ($cellValue instanceof PHPExcel_RichText) {   //富文本转换字符串
                    $cellValue = $cellValue->__toString();
                } else {
                    $cellValue = (string)$cellValue;
                }
                if (!empty($cell[$_column])) {
                    $data[$_row][$cell[$_column]] = $cellValue;
                } else {
                    $data[$_row][] = $cellValue;
                }
            }
        }
    }

    return array_values($data);

}