275 lines
8.0 KiB
PHP
Executable File
275 lines
8.0 KiB
PHP
Executable File
<?php
|
||
declare ( strict_types = 1 );
|
||
|
||
namespace app;
|
||
|
||
use app\admin\service\UpdateService;
|
||
use app\BaseController;
|
||
use think\App;
|
||
use think\exception\HttpResponseException;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Db;
|
||
use think\facade\Log;
|
||
use think\Validate;
|
||
use think\Response;
|
||
use think\facade\Lang;
|
||
|
||
|
||
/**
|
||
* 控制器基础类
|
||
*/
|
||
abstract class AdminRest extends BaseController
|
||
{
|
||
//app名称
|
||
public $_app = null;
|
||
//控制器名称
|
||
public $_controller = null;
|
||
//执行方法名称
|
||
public $_action = null;
|
||
//method
|
||
public $_method = 'GET';
|
||
//query参数
|
||
public $_param = [];
|
||
//body参数
|
||
public $_input = [];
|
||
//头部
|
||
public $_header = [];
|
||
//头部token
|
||
public $_token = null;
|
||
//获取配置信息
|
||
protected $_config = [];
|
||
//语言信息
|
||
public $_lang = 'zh-cn';
|
||
//角色
|
||
public $_role = 'guest';
|
||
//host信息
|
||
public $_host = null;
|
||
//访问ip信息
|
||
public $_ip = null;
|
||
//用户信息
|
||
public $_user = null;
|
||
//唯一app标示
|
||
public $_uniacid = '2';
|
||
//定义检查中间件
|
||
// protected $middleware = ['app\middleware\AppInit', 'app\middleware\CheckInput' ,'think\middleware\LoadLangPack' ,'app\middleware\AutoStatic','app\middleware\StaticHour', 'app\middleware\GetAuthConfig'];
|
||
//判断是否是微擎
|
||
public $_is_weiqin = false;
|
||
/**
|
||
* 小程序版本
|
||
* 0 => 无限开版 其他 = 几开版
|
||
* @var array
|
||
*/
|
||
protected $card_auth_version = 0;
|
||
|
||
/**
|
||
* 可开通名片数量
|
||
* 0 => 无限开版 其他 = 名片数量
|
||
* @var array
|
||
*/
|
||
protected $card_auth_card = 0;
|
||
|
||
public function __construct ( App $app )
|
||
{
|
||
|
||
|
||
parent::__construct( $app );
|
||
|
||
//获取method
|
||
$this->_method = $this->request->method( true );
|
||
|
||
$this->_is_weiqin = longbingIsWeiqin();
|
||
//获取app名称
|
||
$this->_app = $app->http->getName();
|
||
//获取controller
|
||
$this->_controller = $this->request->controller();
|
||
//获取action名称
|
||
$this->_action = $this->request->action();
|
||
//获取param
|
||
$this->_param = $this->request->param();
|
||
//获取body参数
|
||
$this->_input = json_decode( $this->request->getInput(), true );
|
||
//获取头部信息
|
||
$this->_header = $this->request->header();
|
||
//获取请求host
|
||
$this->_host = $this->_header[ 'host' ];
|
||
//获取访问ip
|
||
$this->_ip = $_SERVER[ 'REMOTE_ADDR' ];
|
||
|
||
if ( $this->_is_weiqin ) {
|
||
|
||
global $_GPC, $_W;
|
||
|
||
$this->_uniacid = $_W[ 'uniacid' ];
|
||
|
||
if (empty($_W['user']) || empty($_W[ 'uniacid' ])) {
|
||
|
||
echo json_encode(['code' => 401, 'error' => '请登录管理系统!']);
|
||
exit;
|
||
}
|
||
|
||
}else{
|
||
|
||
//获取token 通过header获取token,如果不存在,则从param中获取。
|
||
if ( !isset( $this->_header[ 'token' ] ) || empty($this->_header[ 'token' ]))
|
||
{
|
||
if(!isset( $this->_param[ 'token' ] ) || empty($this->_param[ 'token' ]))
|
||
{
|
||
//返回数数据
|
||
echo json_encode(['code' => 401, 'error' => '请重新登录!']);
|
||
exit;
|
||
}else{
|
||
$this->_header[ 'token' ] = $this->_param[ 'token' ];
|
||
}
|
||
}
|
||
|
||
//获取token
|
||
$this->_token = $this->_header[ 'token' ] ;
|
||
//语言
|
||
if ( isset( $this->_header[ 'lang' ] ) ) $this->_lang = $this->_header[ 'lang' ];
|
||
//获取用户信息
|
||
$this->_user = getUserForToken( $this->_token );
|
||
|
||
if ($this->_user == null) {
|
||
|
||
echo json_encode(['code' => 401, 'error' => '请登录系统!']);
|
||
exit;
|
||
}
|
||
|
||
$this->_uniacid = !empty( $this->_user ) && isset( $this->_user[ 'uniacid' ] ) ? $this->_user[ 'uniacid' ] : 2;
|
||
}
|
||
|
||
landNotice($this->_uniacid);
|
||
|
||
|
||
|
||
}
|
||
|
||
//返回请求成功的数据
|
||
public function success ( $data, $code = 200 )
|
||
{
|
||
$result[ 'data' ] = $data;
|
||
$result[ 'code' ] = $code;
|
||
$result[ 'sign' ] = null;
|
||
//复杂的签名
|
||
// if(isset($this->_user['keys'])){
|
||
// $result['sign'] = rsa2CreateSign($this->_user['keys'] ,json_encode($data));
|
||
// }
|
||
//简单的签名
|
||
if ( !empty( $this->_token ) ) $result[ 'sign' ] = createSimpleSign( $this->_token, is_string( $data ) ? $data : json_encode( $data ) );
|
||
return $this->response( $result, 'json', $code );
|
||
}
|
||
|
||
//返回错误数据
|
||
public function error ( $msg, $code = 400 )
|
||
{
|
||
$result[ 'error' ] = Lang::get($msg);
|
||
$result[ 'code' ] = $code;
|
||
return $this->response( $result, 'json', 200 );
|
||
}
|
||
|
||
/**
|
||
* 输出返回数据
|
||
* @access protected
|
||
* @param mixed $data 要返回的数据
|
||
* @param String $type 返回类型 JSON XML
|
||
* @param integer $code HTTP状态码
|
||
* @return Response
|
||
*/
|
||
protected function response ( $data, $type = 'json', $code = 200 )
|
||
{
|
||
return Response::create( $data, $type )->code( $code );
|
||
}
|
||
|
||
/**
|
||
* REST 调用
|
||
* @access public
|
||
* @param string $method 方法名
|
||
* @return mixed
|
||
* @throws \Exception
|
||
*/
|
||
public function _empty ( $method )
|
||
{
|
||
if ( method_exists( $this, $method . '_' . $this->method . '_' . $this->type ) ) {
|
||
// RESTFul方法支持
|
||
$fun = $method . '_' . $this->method . '_' . $this->type;
|
||
}
|
||
elseif ( $this->method == $this->restDefaultMethod && method_exists( $this, $method . '_' . $this->type ) ) {
|
||
$fun = $method . '_' . $this->type;
|
||
}
|
||
elseif ( $this->type == $this->restDefaultType && method_exists( $this, $method . '_' . $this->method ) ) {
|
||
$fun = $method . '_' . $this->method;
|
||
}
|
||
if ( isset( $fun ) ) {
|
||
return App::invokeMethod( [
|
||
$this,
|
||
$fun
|
||
]
|
||
);
|
||
}
|
||
else {
|
||
// 抛出异常
|
||
throw new \Exception( 'error action :' . $method );
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
*
|
||
* 获取支付信息
|
||
*/
|
||
public function payConfig (){
|
||
$uniacid_id = !empty($uniacid)?$uniacid:$this->_uniacid;
|
||
|
||
$pay = Db::name('lbfarm_pay_config')->where(['uniacid'=>$uniacid_id])->find();
|
||
|
||
$config = Db::name( 'lbfarm_config')->where(['uniacid' => $uniacid_id])->find();
|
||
|
||
if(empty($pay[ 'mch_id' ])||empty($pay[ 'pay_key' ])){
|
||
$this->errorMsg('未配置支付信息');
|
||
}
|
||
$setting[ 'payment' ][ 'merchant_id' ] = $pay[ 'mch_id' ];
|
||
$setting[ 'payment' ][ 'key' ] = $pay[ 'pay_key' ];
|
||
$setting[ 'payment' ][ 'cert_path' ] = $pay[ 'cert_path' ];
|
||
$setting[ 'payment' ][ 'key_path' ] = $pay[ 'key_path' ];
|
||
$setting[ 'app_id' ] = $config['appid'];
|
||
$setting[ 'secret' ] = $config['appsecret'];
|
||
Log::write($setting);
|
||
return $setting;
|
||
}
|
||
|
||
|
||
/**
|
||
* User: chenniang
|
||
* Date: 2019-09-12 20:37
|
||
* @param string $msg
|
||
* @return void
|
||
* descption:直接抛出异常
|
||
*/
|
||
protected function errorMsg($msg = '',$code = 400){
|
||
$msg = Lang::get($msg);
|
||
$this->results($msg,$code);
|
||
}
|
||
|
||
/**
|
||
* 返回封装后的 API 数据到客户端
|
||
* @access protected
|
||
* @param mixed $msg 提示信息
|
||
* @param mixed $data 要返回的数据
|
||
* @param int $code 错误码,默认为0
|
||
* @param string $type 输出类型,支持json/xml/jsonp
|
||
* @param array $header 发送的 Header 信息
|
||
* @return void
|
||
* @throws HttpResponseException
|
||
*/
|
||
protected function results($msg, $code, array $header = [])
|
||
{
|
||
$result = [
|
||
'error' => $msg,
|
||
'code' => $code,
|
||
];
|
||
$response = Response::create($result, 'json', 200)->header($header);
|
||
throw new HttpResponseException($response);
|
||
}
|
||
}
|