helper.php 2.5 KB
Newer Older
zazaname's avatar
zazaname committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php

use \Illuminate\Support\Facades;

/**
 * @dsec 返回成功的json数组
 * @param string $msg
 * @return mixed
 */
function success($msg = 'success', $data = [])
{
    return [
        'message' => $msg,
        'data' => $data,
        'statusCode' => 200,
    ];
}

/**
 * @dsec 返回成功的json数组
 * @param string $msg
 * @return mixed
 */
function error($msg = 'error', $data = [])
{
    return [
        'message' => $msg,
        'data' => $data,
        'statusCode' => 300,
    ];
}

/**
 * @dsec 返回成功的json数组
 * @param string $msg
 * @return mixed
 */
function json($data = [])
{
zazaname's avatar
zazaname committed
40
    return response()->json($data)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
zazaname's avatar
zazaname committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

/**
 * 钉钉打印日志
 * @param $message
 * @return array|bool|string
 */
function dingdingLog($message)
{
    if (is_array($message)) {
        $message = "该参数是数组:" . var_export($message, true);
    }
    $data = array('msgtype' => 'text', 'text' => array('content' => '[ ' . date("Y-m-d H:i:s") . ' ] - 日志:' . $message));
    $post_string = json_encode($data);
    $ch = curl_init();
    $access_token = 'b74a7e686873baaed443c4d4bf6c68e3f58debc79324c4d3ea14549dc43586f2';
    curl_setopt($ch, CURLOPT_URL, 'https://oapi.dingtalk.com/robot/send?access_token=' . $access_token);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function p($obj)
{
    if (is_string($obj)) {
        echo $obj;
    } else if (is_array($obj)) {
        echo '<pre>';
        var_export($obj);
    } else {
        var_dump($obj);
    }
    exit();
}

/**
 * 获取最后一条sql语句    待测试
 * @param string $connection
 * @return mixed
 */
function get_sql($connection = 'mysql')
{
    //\DB::connection($connection)->enableQueryLog(); // 开启查询日志
    $sql = Facades\DB::connection($connection)->getQueryLog();
    $query = end($sql);

    $tmp = str_replace('?', '"'.'%s'.'"', $query['query']);
    $tmp = vsprintf($tmp, $query['bindings']);
    $tmp = str_replace("\\","",$tmp);

    $query['sql'] = $tmp;
    return $query;
}