You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
6.3 KiB
212 lines
6.3 KiB
<?php
|
|
|
|
namespace app\service;
|
|
|
|
use app\model\Admin;
|
|
use app\model\Cert;
|
|
use Firebase\JWT\JWT;
|
|
use Firebase\JWT\Key;
|
|
use log\LogOpe;
|
|
use think\exception\HttpResponseException;
|
|
use think\facade\Log;
|
|
use think\facade\Request;
|
|
|
|
class BaseService
|
|
{
|
|
|
|
protected $admin = null;
|
|
protected $admin_id = null;
|
|
protected $token = null;
|
|
protected $log = null;
|
|
protected $orderlog = null;
|
|
protected $goodslog = null;
|
|
|
|
protected $account_id;
|
|
protected $account_name;
|
|
|
|
public function __construct(){
|
|
|
|
$this->log = new LogOpe(get_class($this));
|
|
$this->orderlog = new LogOpe("order/".date("YmdH") . "Order");
|
|
$this->goodslog = new LogOpe("goods/".date("YmdH") . "Goods");
|
|
|
|
// 获取token
|
|
if(isset($_SERVER['HTTP_TOKEN'])){
|
|
$this->token = $_SERVER['HTTP_TOKEN'];
|
|
$this->checkToken();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 请求校验(抛异常)
|
|
* @param $class
|
|
* @param string $scene
|
|
* @param string|array|null $method 指定参数类型
|
|
* @param bool $limit 限制请求类型
|
|
* @throws \Exception
|
|
*/
|
|
public function checkVaild($class, string $scene = '', string $method = null) {
|
|
$method = strtolower($method);
|
|
switch ($method){
|
|
case "get":
|
|
if (!Request::isGet()){
|
|
throw new \think\Exception('请求类型错误');
|
|
}
|
|
$data = Request::get();
|
|
break;
|
|
case "post":
|
|
if (!Request::isPost()){
|
|
throw new \think\Exception('请求类型错误');
|
|
}
|
|
$data = Request::post();
|
|
break;
|
|
case 'put':
|
|
if (!Request::isPut()){
|
|
throw new \think\Exception('请求类型错误');
|
|
}
|
|
$data = Request::put();
|
|
break;
|
|
case 'patch':
|
|
if (!Request::isPatch()){
|
|
throw new \think\Exception('请求类型错误');
|
|
}
|
|
$data = Request::patch();
|
|
break;
|
|
case 'delete':
|
|
if (!Request::isDelete()){
|
|
throw new \think\Exception('请求类型错误');
|
|
}
|
|
$data = Request::delete();
|
|
break;
|
|
default:
|
|
if (is_array($method)){
|
|
$data = $method;
|
|
}else{
|
|
$data = Request::param();
|
|
}
|
|
}
|
|
if ($scene) {
|
|
$result = validate($class)->scene($scene)->check($data);
|
|
} else {
|
|
$result = validate($class)->check($data);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 构造列表搜索 withSearch 转换函数
|
|
* @param $param array 需要搜索的字段
|
|
* @param $where array where条件,匹配中存在search中字段则排除原数组字段,使用whthSearch搜索
|
|
* @param string $wherePrefix
|
|
* @return array[] withSearch条件参数[0] [1]
|
|
*/
|
|
public function buildSearch(array $param,array &$where, string $wherePrefix = '') {
|
|
$search = [0 => [], 1 => []];
|
|
foreach ($param as $v){
|
|
if ($v == ''){
|
|
continue;
|
|
}
|
|
$search[0][] = $v;
|
|
if (isset($where[$v])){
|
|
if ($where[$v] != ''){
|
|
$search[1][$v] = $where[$v];
|
|
}
|
|
unset($where[$v]);
|
|
}
|
|
}
|
|
$where = $this->buildWhere($where, true, $wherePrefix);
|
|
return $search;
|
|
}
|
|
|
|
|
|
/**
|
|
* 重组where数据,同时兼容2种查询方法插入
|
|
*
|
|
* @param $where
|
|
* @param bool $filter
|
|
* @param string $prefix
|
|
* @return array
|
|
*/
|
|
public function buildWhere($where, $filter = true, $prefix = ''){
|
|
//为 where 拼接前缀
|
|
if ($prefix != ''){
|
|
foreach ($where as $k => $v){
|
|
if (is_numeric($k)){
|
|
if (strpos($where[$k][0],'.') === false) {
|
|
$where[$k][0] = $prefix . $where[$k][0];
|
|
}
|
|
} elseif (strpos($k,'.') === false) {
|
|
$where[$prefix . $k] = $v;
|
|
unset($where[$k]);
|
|
}
|
|
}
|
|
}
|
|
// 删除where内容为空的数据
|
|
if ($filter == true){
|
|
$where = array_filter($where,
|
|
function($val) {//等于0不能过滤
|
|
if ($val === 0 || $val === '0' || $val != false) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
// 拼接重置where为多维数组,支持2种方式组合where
|
|
foreach ($where as $k => $v) {
|
|
if (is_numeric($k)) {
|
|
continue;
|
|
} else if (is_array($v)) {
|
|
array_unshift($where[$k], $k);
|
|
} else {
|
|
$where[$k] = [$k, '=', $v];
|
|
}
|
|
}
|
|
return $where = array_values($where);
|
|
}
|
|
|
|
|
|
/**
|
|
* TOKEN校验
|
|
*/
|
|
protected function checkToken() {
|
|
$cert = Cert::where('status', 0)->where('type', "entrust")->find();
|
|
if(!$cert){
|
|
throw new \think\Exception('缺少证书', 400);
|
|
}
|
|
$key = $cert->token;
|
|
$jwt = new JWT();
|
|
$data = $jwt::decode($this->token, new Key($key, 'HS512'));
|
|
if(!empty($data)){
|
|
$data = json_decode( json_encode($data), true);
|
|
$this->admin_id = Admin::where('account_id', $data['accountId'])->value('id');
|
|
$this->account_id = $data['accountId'];
|
|
$this->account_name = $data['accountName'];
|
|
}
|
|
}
|
|
|
|
|
|
public function createSign($type){
|
|
$cert = Cert::where('status', 0)->where('type', $type)->find();
|
|
if(!$cert){
|
|
throw new \think\Exception('缺少证书', 400);
|
|
}
|
|
$priv_key = file_get_contents($cert->private_key);
|
|
$exp = 5 * 60 * 1000;
|
|
$payload = [
|
|
'iss' => $cert->token,
|
|
'exp' => time() + $exp,
|
|
'iat' => time(),
|
|
];
|
|
if($type == "user_real"){
|
|
$payload['aud'] = "BD84DD42A7234B05B0C5D11616132AC4";
|
|
}
|
|
$sign = JWT::encode($payload, $priv_key, 'RS256');
|
|
return $sign;
|
|
}
|
|
|
|
|
|
}
|