jh-admin/addon/supply/api/controller/Api.php

101 lines
3.8 KiB
PHP

<?php
namespace addon\supply\api\controller;
class Api extends ApiBase
{
/***
* 请求入口
*/
protected $Format = 'JSON';
/**
* index
* @author Sunday <153788231@qq.com>
* 接口统一入口
* unit_type 设备类型 可传 ios android 不传作为网页处理
*/
public function index()
{
try {
if ($this->CheckSign()) {
//获取非JSON格式信息
$method = input('method', '');
//判断非JSON数据是否存在
if (empty($method)) {
//检测是否JSON数据
if (input('biz_content')) {
//转化JSON数据为数组
$tabal = $this->getJson();
if (!empty($tabal) && is_array($tabal)) {
$data = array();
foreach ($tabal as $key => $val) {
if (!empty($val['method'])) {
$method = $val['method'];
//判断接口是否存正常
request()->withPost($val);
[$Controller, $Action] = $this->getMethod($method);
$data[$key] = app($Controller, $val)->$Action();
} else {
$data[$key] = array('msg' => "接口地址不正确或请求方式不正确", 'code' => 0, 'data' => $val);
}
}
return $this->ajaxSuccess($data);
} else {
$data = array();
return $this->ajaxError($data, -1, '数据不能为空或不是标准的JSON表达式或方法不存在');
}
} else {
$data = array();
return $this->ajaxError($data, -1, '不是标准的JSON表达式或方法不存在');
}
} else {
//请求验证
if (!empty($method)) {
request()->withPost(input());
[$Controller, $Action] = $this->getMethod($method);
$request = app($Controller, [$this->AppInfo])->$Action();
if (isset($request['code']) && $request['code'] < 0) {
return $this->ajaxError($request, -1, $request['message']);
} else {
return $this->ajaxSuccess($request);
}
} else {
return $this->ajaxError([], -1, '方法不存在');
}
}
} else {
return $this->ajaxError($this->error);
}
} catch (\Exception $e) {
return $this->ajaxError($e->getMessage());
}
}
public function getMethod($method)
{
$path = explode('.', $method);
$current = current($path);
$action = end($path);
$system_array = ['admin', 'shop', 'install', 'cron', 'api', 'pay', 'public', 'app', 'index', 'shopapi', 'storeapi'];
$addon = in_array($current, $system_array) ? 'app' : 'addon';
unset($path[count($path) - 1]);
$path[count($path) - 1] = ucfirst(end($path));
return [
$addon . '\\' . implode('\\', $path),
$action
];
}
/**
* 检查是否为JSON格式
*/
private function getJson()
{
$requests = input('biz_content') ? input('biz_content') : '';
$get = !is_null(json_decode($requests, true)) ? json_decode($requests, true) : $requests;
return $get;
}
}