140 lines
3.9 KiB
PHP
140 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace addon\supply\api\controller;
|
|
|
|
use app\Controller;
|
|
use think\facade\Db;
|
|
|
|
class ApiBase extends Controller
|
|
{
|
|
//客户端类型
|
|
protected $client_type_array = array('android', 'mobile', 'wap', 'wechat', 'ios', 'windows');
|
|
//列表默认分页数
|
|
protected $page = 5;
|
|
// 输出参数
|
|
protected $options = [
|
|
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
|
];
|
|
protected $alias = '';
|
|
protected $action = '';
|
|
protected $error = '';
|
|
protected $appSecret = '';
|
|
protected $AppInfo = '';
|
|
|
|
public function initialize()
|
|
{
|
|
$this->appSecret = input('app_secret');
|
|
}
|
|
|
|
public function ajaxSuccess($result = array(), $code = 0, $message = '成功', $format = 'JSON')
|
|
{
|
|
$data['result'] = $result;
|
|
$data['code'] = $code;
|
|
$data['message'] = $message;
|
|
return $this->ajaxReturn($data, $format);
|
|
}
|
|
|
|
public function ajaxError($result, $resultCode = -1, $message = '错误')
|
|
{
|
|
$data['result'] = $result;
|
|
$data['code'] = $resultCode;
|
|
$data['message'] = $message;
|
|
return $this->ajaxReturn($data);
|
|
}
|
|
|
|
/**
|
|
* Ajax方式返回数据到客户端
|
|
* @access protected
|
|
* @param mixed $data
|
|
* 要返回的数据
|
|
* @param String $type
|
|
* AJAX返回数据格式
|
|
* @return void
|
|
*/
|
|
protected function ajaxReturn($data, $type = '')
|
|
{
|
|
if (empty ($type)) $type = 'JSON';
|
|
|
|
switch (strtoupper($type)) {
|
|
case 'JSON' :
|
|
return json($data)->getcontent();;
|
|
break;
|
|
case 'XML' :
|
|
return xml($data);
|
|
break;
|
|
case 'JSONP' :
|
|
return jsonp($data);
|
|
break;
|
|
default :
|
|
// 用于扩展其他返回格式数据
|
|
event('ajax_return', $data);
|
|
}
|
|
}
|
|
|
|
|
|
/***
|
|
* 签名
|
|
* @return void
|
|
*/
|
|
public function CheckSign()
|
|
{
|
|
|
|
$app_id = input('app_id');
|
|
$app_secret = input('app_secret');
|
|
$timestamp = input('timestamp', time());
|
|
$sign = input('signature');
|
|
if (!request()->isPost()) {
|
|
$this->error = '请求方式不正确请使用POST方式';
|
|
return false;
|
|
}
|
|
// if (empty($app_id) || empty($app_secret)) {
|
|
// $this->error = 'app_id或app_secret不能为空';
|
|
// return false;
|
|
// }
|
|
// if (empty($timestamp)) {
|
|
// $this->error = '请求时间不能为空';
|
|
// return false;
|
|
// } else if (time() - $timestamp > 600) {
|
|
// $this->error = '请求时间不合法';
|
|
// return false;
|
|
// }
|
|
if (empty($sign)) {
|
|
$this->error = '签名不能为空';
|
|
return false;
|
|
} else {
|
|
$data = input();
|
|
$getSign = $this->getSign($data);
|
|
// if ($getSign != $sign) {
|
|
// $this->error = '签名不正确';
|
|
// return false;
|
|
// }
|
|
$where = ['app_id' => $app_id, 'app_secret' => $app_secret];
|
|
$AppInfo = DB::name('supply_app')->where($where)->cache(json_encode($where), 3600)->find();
|
|
if (empty($AppInfo) || $AppInfo['status'] != 1) {
|
|
$this->error = 'app_id或app_secret不正确';
|
|
return false;
|
|
} else {
|
|
$this->AppInfo = $AppInfo;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/***
|
|
* 获取签名
|
|
* @param $path
|
|
* @param $postData
|
|
* @return string
|
|
*/
|
|
public function getSign($data)
|
|
{
|
|
unset($data['signature']);
|
|
ksort($data);
|
|
$content = http_build_query($data);
|
|
$sign = hash_hmac("sha1", $content, $this->appSecret, true);
|
|
$signHexWithLowcase = bin2hex($sign);
|
|
$signHexUppercase = strtoupper($signHexWithLowcase);
|
|
return $signHexUppercase;
|
|
}
|
|
}
|