Commit ab3d1d88 authored by hujun's avatar hujun

edit

parent 822e5666
<?php <?php
namespace app\api\extend; namespace app\api\extend;
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: zw * User: zw
* Date: 2017/12/4 * Date: 2017/12/4
* Time: 9:35 * Time: 9:35
* 基类 * 基类
*/ */
use think\Controller; use think\Controller;
use think\Request; use think\Request;
use think\Response; use think\Response;
class Basic extends Controller{ class Basic extends Controller{
/** /**
* 访问请求对象 * 访问请求对象
* @var Request * @var Request
*/ */
public $request; public $request;
/** /**
* 当前访问身份 * 当前访问身份
* @var string * @var string
*/ */
public $token; public $token;
/** /**
* 基础接口SDK * 基础接口SDK
* @param Request|null $request * @param Request|null $request
*/ */
public function __construct(Request $request = null) public function __construct(Request $request = null)
{ {
// CORS 跨域 Options 检测响应 // CORS 跨域 Options 检测响应
$this->corsOptionsHandler(); $this->corsOptionsHandler();
// 输入对象 // 输入对象
$this->request = is_null($request) ? Request::instance() : $request; $this->request = is_null($request) ? Request::instance() : $request;
} }
/** /**
* 输出返回数据 * 输出返回数据
* @param string $msg 提示消息内容 * @param string $msg 提示消息内容
* @param string $code 业务状态码 * @param string $code 业务状态码
* @param mixed $data 要返回的数据 * @param mixed $data 要返回的数据
* @param string $type 返回类型 JSON XML * @param string $type 返回类型 JSON XML
* @return Response * @return Response
*/ */
public function response($msg, $code = 'SUCCESS', $data = [], $type = 'json') public function response($msg, $code = 'SUCCESS', $data = [], $type = 'json')
{ {
$result = ['msg' => $msg, 'code' => $code, 'data' => $data, 'type' => strtolower($type)]; $result = ['msg' => $msg, 'code' => $code, 'data' => $data, 'type' => strtolower($type)];
return Response::create($result, $type); return Response::create($result, $type);
} }
/** /**
* 一维数据数组生成数据树 * 一维数据数组生成数据树
* @param array $list 数据列表 * @param array $list 数据列表
* @param string $id 父ID Key * @param string $id 父ID Key
* @param string $pid ID Key * @param string $pid ID Key
* @param string $son 定义子数据Key * @param string $son 定义子数据Key
* @return array * @return array
*/ */
public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub') public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub')
{ {
list($tree, $map) = [[], []]; list($tree, $map) = [[], []];
foreach ($list as $item) { foreach ($list as $item) {
$map[$item[$id]] = $item; $map[$item[$id]] = $item;
} }
foreach ($list as $item) { foreach ($list as $item) {
if (isset($item[$pid]) && isset($map[$item[$pid]])) { if (isset($item[$pid]) && isset($map[$item[$pid]])) {
$map[$item[$pid]][$son][] = &$map[$item[$id]]; $map[$item[$pid]][$son][] = &$map[$item[$id]];
} else { } else {
$tree[] = &$map[$item[$id]]; $tree[] = &$map[$item[$id]];
} }
} }
unset($map); unset($map);
return $tree; return $tree;
} }
/** /**
* 一维数据数组生成数据树 * 一维数据数组生成数据树
* @param array $list 数据列表 * @param array $list 数据列表
* @param string $id ID Key * @param string $id ID Key
* @param string $pid 父ID Key * @param string $pid 父ID Key
* @param string $path * @param string $path
* @param string $ppath * @param string $ppath
* @return array * @return array
*/ */
public static function arr2table(array $list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '') public static function arr2table(array $list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '')
{ {
$tree = []; $tree = [];
foreach (self::arr2tree($list, $id, $pid) as $attr) { foreach (self::arr2tree($list, $id, $pid) as $attr) {
$attr[$path] = "{$ppath}-{$attr[$id]}"; $attr[$path] = "{$ppath}-{$attr[$id]}";
$attr['sub'] = isset($attr['sub']) ? $attr['sub'] : []; $attr['sub'] = isset($attr['sub']) ? $attr['sub'] : [];
$attr['spl'] = str_repeat("&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;", substr_count($ppath, '-')); $attr['spl'] = str_repeat("&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;", substr_count($ppath, '-'));
$sub = $attr['sub']; $sub = $attr['sub'];
unset($attr['sub']); unset($attr['sub']);
$tree[] = $attr; $tree[] = $attr;
if (!empty($sub)) { if (!empty($sub)) {
$tree = array_merge($tree, (array)self::arr2table($sub, $id, $pid, $path, $attr[$path])); $tree = array_merge($tree, (array)self::arr2table($sub, $id, $pid, $path, $attr[$path]));
} }
} }
return $tree; return $tree;
} }
/** /**
* 获取数据树子ID * 获取数据树子ID
* @param array $list 数据列表 * @param array $list 数据列表
* @param int $id 起始ID * @param int $id 起始ID
* @param string $key 子Key * @param string $key 子Key
* @param string $pkey 父Key * @param string $pkey 父Key
* @return array * @return array
*/ */
public static function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid') public static function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid')
{ {
$ids = [intval($id)]; $ids = [intval($id)];
foreach ($list as $vo) { foreach ($list as $vo) {
if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) === intval($id)) { if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) === intval($id)) {
$ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey)); $ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey));
} }
} }
return $ids; return $ids;
} }
/** /**
* Cors Options 授权处理 * Cors Options 授权处理
*/ */
public static function corsOptionsHandler() public static function corsOptionsHandler()
{ {
if (request()->isOptions()) { if (request()->isOptions()) {
header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token'); header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
header('Access-Control-Allow-Credentials:true'); header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods:GET,POST,OPTIONS'); header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
header('Access-Control-Max-Age:1728000'); header('Access-Control-Max-Age:1728000');
header('Content-Type:text/plain charset=UTF-8'); header('Content-Type:text/plain charset=UTF-8');
header('Content-Length: 0', true); header('Content-Length: 0', true);
header('status: 204'); header('status: 204');
header('HTTP/1.0 204 No Content'); header('HTTP/1.0 204 No Content');
exit; exit;
} }
} }
/** /**
* Cors Request Header信息 * Cors Request Header信息
* @return array * @return array
*/ */
public static function corsRequestHander() public static function corsRequestHander()
{ {
return [ return [
'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => true, 'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS', 'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS',
'Access-Defined-X-Support' => 'service@cuci.cc', 'Access-Defined-X-Support' => 'service@cuci.cc',
'Access-Defined-X-Servers' => 'Guangzhou Cuci Technology Co. Ltd', 'Access-Defined-X-Servers' => 'Guangzhou Cuci Technology Co. Ltd',
]; ];
} }
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment