123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /**
- * Author: luzheng.liu
- * Time: 2020/8/26 16:08
- */
- namespace app\common\until;
- use think\Db;
- use think\facade\Request;
- use think\Response;
- use think\Validate;
- class Until {
- public static $userId = 0;
- public static $isAdmin = false;
- public static $startTime = 0;
- public static $endTime = 0;
- /**
- * @param array $data
- * @param int $code
- * @param string $message
- */
- public static function output(array $data = [], string $message = 'success', int $code = Enum::SUCCESS_CODE) {
- $data = self::convertUnderlineArray($data);
- $re = [
- 'code' => $code,
- 'message' => $message,
- 'data' => $data
- ];
- self::addLog($re);
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode($re);
- die;
- }
- /**
- * @param string $showMsg
- * @param string $systemErrorMsg
- * @param array $data
- * @param int $code
- */
- public static function outputSystemError(string $showMsg = '', string $systemErrorMsg = '', $data = [], $code = Enum::THROW_ERR_CODE) {
- $output = ['code' => $code, 'msg' => $showMsg, 'systemErrorMsg' => $systemErrorMsg, 'data' => $data];
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode($output);
- die;
- }
- public static function modelToArray($data): array {
- if (empty($data)) {
- return [];
- }
- return json_decode(json_encode($data), true);
- }
- public static function check(array $rule, array $data) {
- $validate = new Validate();
- if (!$validate->check($data, $rule)) {
- self::output([], (string)$validate->getError(), Enum::THROW_ERR_CODE);
- }
- }
- public static function getInput(): array {
- self::$startTime = time();
- $input = file_get_contents("php://input");
- $data = json_decode($input, true);
- if (empty($data)) {
- return input();
- }
- return json_decode($input, true);
- }
- /**
- * 公共下划线转驼峰(新)
- * 由于convertUnderlineArray会默认把字段值的类型全部转成string,再转回来以后,会造成类型的丢失.
- * 所以这个新的转换方法,默认不转换.需要的时候才会去转换. 这样就不会有来回转换造成类型丢失的问题了.
- * @param array $arr
- * @param bool $ucFirst 首字母大写
- * @param bool $strictMode 严格模式: 严格区分int和字符串,默认为严格模式
- * @return array
- */
- public static function convertUnderlineArray($arr = [], $ucFirst = false, $strictMode = true): array {
- if (empty($arr)) {
- return [];
- }
- $newArray = [];
- foreach ($arr as $key => $value) {
- $str = ucwords(str_replace('_', ' ', $key));
- $str = str_replace(' ', '', lcfirst($str));
- $str = $ucFirst ? ucfirst($str) : $str;
- //如果是数组重构这个数组用递归的方法
- if (is_array($value)) {
- $newArray[$str] = self::convertUnderlineArray($value, $ucFirst, $strictMode);
- continue;
- }
- // 非严格模式下全部转成字符串
- if ($strictMode === false) {
- $newArray[$str] = (string)$value;
- continue;
- }
- $newArray[$str] = $value ?? '';
- }
- return $newArray;
- }
- public static function returnSquarePoint($lng, $lat, $distance = 99) {
- $dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
- $dlng = rad2deg($dlng);
- $dlat = $distance / 6371;
- $dlat = rad2deg($dlat);
- return [
- 'left-top' => ['lat' => $lat + $dlat, 'lng' => $lng - $dlng],
- 'right-top' => ['lat' => $lat + $dlat, 'lng' => $lng + $dlng],
- 'left-bottom' => ['lat' => $lat - $dlat, 'lng' => $lng - $dlng],
- 'right-bottom' => ['lat' => $lat - $dlat, 'lng' => $lng + $dlng]
- ];
- }
- /**
- * 生成订单编号
- * @return string
- */
- public static function createSn(): string {
- return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid('', true), 7, 13), 1))), 0, 12);
- }
- public static function addLog($re) {
- try {
- Db::table('log')->insert([
- // 'admin_id' => self::$adminId,
- 'url' => Request::server('REQUEST_SCHEME').'://'.Request::host().Request::url(),
- 'token' => Request::header('token') ?? '',
- 'input' => file_get_contents("php://input") ?: json_encode(Request::input()),
- 'output' => json_encode($re),
- 'time' => time() - self::$startTime
- ]);
- } catch (\Exception $e) {
- throw new \RuntimeException($e->getMessage());
- }
- }
- public static function msectime() {
- list($msec, $sec) = explode(' ', microtime());
- return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- }
- }
|