2024-09-29 15:43:18 +08:00

100 lines
2.4 KiB
PHP

<?php
namespace app\merchant\controller\api;
use think\admin\Controller;
use think\admin\service\InterfaceService;
/**
* 商户接口授权基础控制器
* Class Auth
* @package app\merchant\controller\api
*/
class Auth extends Controller
{
/**
* 当前请求数据
* @var array
*/
protected $data;
/**
* 接口ID
* @var string
*/
protected $appid = '';
/**
* 接口密钥
* @var string
*/
protected $appkey = '';
/**
* 接口服务对象
* @var InterfaceService
*/
protected $service;
/**
* 接口商户数据
*/
protected $merchant;
/**
* 接口授权初始化
*/
protected function initialize()
{
$this->service = InterfaceService::instance();
$this->service->setAuth(input('appid'), '')->getInput(false);
// 从数据库中账号检查
$map = ['appid' => $this->service->getAppid()];
$this->merchant = $this->app->db->name('MerchantList')->where($map)->find();
// 商户验证
if (empty($this->merchant)) $this->error('接口账号不存在!');
if (empty($this->merchant['status'])) $this->error('接口账号已被禁用!');
if (!empty($this->merchant['is_deleted'])) $this->error('接口账号已被移除!');
if ($this->merchant['surplus'] < 1) $this->error('接口账号余额不足!');
// 接口初始化,接收正确的 appid + data 数据
$this->service->setAuth($this->merchant['appid'], $this->merchant['appkey'])->showCheck();
[$this->data, $this->appid] = [$this->service->getData(), $this->service->getAppid()];
}
/**
* 返回失败的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
public function error($info, $data = '{-null-}', $code = 0):void
{
$this->service->error($info, $data, $code);
}
/**
* 返回成功的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
public function success($info, $data = '{-null-}', $code = 1):void
{
$this->service->success($info, $data, $code);
}
/**
* 返回接口数据
* @param mixed $info
* @param array $data
* @param integer $code
*/
protected function response($info, array $data = [], $code = 1):void
{
$this->service->baseResponse($info, $data, $code);
}
}