<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 统一返回json
 * @param array $data 业务数据
 * @param int $code 错误码，0:成功，非0：失败
 * @param string $msg 错误信息
 */
/**
 * 生成带code的数组
 * @param array  $sendData             返回的数据
 * @param int    $errCode              错误码
 * @param string $errMsg               附加详细错误信息

 * @return array
 */
function code($sendData = [], $errCode = 0, $errMsg = '')
{
    header("Access-Control-Allow-Origin:*");
    header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
    header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
    $_errCode = $errCode;
    $_errMsg = $errMsg;

    if (isset($_errMsg)) {
        $_errMsg = $_errMsg;
//        if (!empty($errMsg)) {
//            $_errMsg .= empty($_errMsg) ? $errMsg : ':'.$errMsg;
//        }
    } else {
        // 不存在的code,设置为未知错误
        $_errCode = 10000;
        $_errMsg = config("err_code.10000");
        $_errMsg = $_errMsg;
    }
    // 组装返回数据
    $return = ['err_code' => $_errCode, 'err_msg' => $_errMsg];
    foreach ($sendData as $key => $value) {
        $return["data"] = $value;
    }
    return $return;

}
function randomkeys($length)
{
    $key = 'bp';
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyz   
               ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
    for($i=0;$i<$length;$i++)
    {
        $key .= $pattern{mt_rand(0,35)};    //生成php随机数
    }
    return $key;
}
function create_unique()
{
    $uniqid = uniqid();
    $uniqid = str_replace('.', '', $uniqid);
    $unString = base_convert($uniqid, 16, 36);
    // 补足17位
    return str_pad($unString, 17, rand(1,9999999));
}
