<?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>
// +----------------------------------------------------------------------
use think\Db;
// 应用公共文件

function makeSalt(){
    $str =rand(100000,999999);
    return $str;
}

/**
 * @param $file
 * @return string
 */
function uplaod_avatar($file){
    // 移动到框架应用根目录/public/uploads/ 目录下
    $info = $file->validate(['size'=>15678,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/avatar');
    if($info){
          return $info->getSaveName();
        }else{
          return "";
    }
}

/**
 * 日志记录
 * @param $log
 * @param $id
 */
function write_log($log,$userinfo,$module="",$action="",$role=0){

    $data=[
        "action_id"=>isset($userinfo['id'])? $userinfo['id']:"",
        "msg"=>$log,
        "addtime"=>date("y-m-d H:i:s"),
        "action_name"=>isset($userinfo['nickname'])? $userinfo['nickname']:"sys",
        "module"=>$module,
        "action"=>$action,
        "username"=>isset($userinfo['username'])? $userinfo['username']:"sys",
        "role"=>$role==0?(isset($userinfo['role_id'])&&$userinfo['role_id']!=0?$userinfo['role_id']:0):3,
    ];
    Db::name("system_log")->insert($data);
}

/**
 * @param $code
 * @param $msg
 */
function error_show($code,$msg=""){
    $data = [
        "code"=>$code,
        "msg"=>$msg
    ];

    echo  json_encode($data);
die();
}

/**
 * @param $code
 * @param string $msg
 * @param array $list
 */
function app_show($code,$msg="",$list=[]){
    $data = [
        "code"=>$code,
        "msg"=>$msg,
        "data"=>$list
    ];
    echo  json_encode($data);
die();
}
function makeNo($str){

    $date=date("mdHis");
    $year = date("Y")-2000;
    $msec=randomkeys(4);
    return $str.$msec.$year.$date;
}
function randomkeys($length) {
    $returnStr='';
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ

    for($i = 0; $i < $length; $i ++) {
        $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
    }
    return $returnStr;
}
