添加后台代理代码

This commit is contained in:
2026-03-15 01:27:05 +08:00
parent 11f9ac4dc1
commit ea08c9366a
5254 changed files with 721042 additions and 0 deletions

View File

@@ -0,0 +1,457 @@
<?php
use phprs\util\Verify;
use phprs\util\exceptions\Forbidden;
use phprs\util\Logger;
use phprs\util\exceptions\NotFound;
use phprs\ezsql\Sql;
use phprs\util\exceptions\BadRequest;
/// 接口返回用的信息类
class returnObject
{
public $error; /// 返回值: 0成功; 非0失败;
public $error_code; /// 错误号
public $msg; /// 错误信息
public $data; /// 返回的数据
public function returnObject($error = 0, $error_code = 0, $msg = null, $data = null)
{
$this->error = $error;
$this->error_code = $error_code;
$this->msg = $msg;
$this->data = $data;
}
public function to_array()
{
return (array)$this;
}
public function to_string()
{
return json_encode($this, JSON_UNESCAPED_UNICODE);
}
public function from_array($array)
{
foreach ($array as $key => $value)
{
if (property_exists($this, $key))
{
$this->$key = $value;
}
}
return true;
}
public function from_string($string)
{
return $this->from_array((array)json_decode($string));
}
}
/**
* 错误码
* 获取优惠券12000--12050
* 更新用户积分13000-13050
* 友乐牛牛用户登录13100-13150
* 基类
* @path("/apiBase")
*/
class apiBase
{
public static $domain = "sdk.tscce.cn";
//public static $domain = 'api.tscce.cn';
public $appid; // 开发者应用ID
public $devkey; // 开发者Key
public $businessid; // 商家ID
public $sid; // 开发者SID
public $scode; // 开发者SCODE
public $market_key; // 门店Key
public $appInfo; // 开发者应用信息
public $devInfo; // 开发者信息
public $marketInfo; // 商家信息
public $userInfo; // 全局用户信息
public $businessInfo;
public function __construct()
{
}
protected function getLocaleUrl($always_http = false)
{
$is_https =
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
(isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https');
$request_scheme = $is_https ? 'https://' : 'http://';
$hostname = $_SERVER['SERVER_NAME'];
$hostport = (($is_https && '443' == $_SERVER['SERVER_PORT']) || (!$is_https && '80' == $_SERVER['SERVER_PORT'])) ? '' : ':' . intval($_SERVER['SERVER_PORT']);
if ($always_http)
return 'http://' . $hostname . $hostport;
else
return $request_scheme . $hostname . $hostport;
}
protected function getFullUrl($relatively_url, $always_http = false)
{
if (mb_strstr($relatively_url, '/', false, USEDCHARSET) == $relatively_url)
return $this->getLocaleUrl($always_http) . $relatively_url;
else
return $this->getLocaleUrl($always_http) . '/' . $relatively_url;
}
public function init($appid = '', $devkey = '', $sid = '', $scode = '', $market_key = '')
{
$this->appid = $appid;
$this->devkey = $devkey;
$this->sid = $sid;
$this->scode = $scode;
$this->market_key = $market_key;
}
public function verifyMarketApi($devkey = '')
{
$this->devkey = $devkey;
if (empty($this->devkey))
{
return new returnObject(1, 10002, '未传入devkey参数');
}
$devList = Sql::select('syweb_admin.*')
->from('syweb_admin')
->where('syweb_admin.type=2 and syweb_admin.admin_key=? and syweb_admin.status=1', $this->devkey)
->get($this->db, null);
if (empty($devList) || count($devList) <= 0)
{
return new returnObject(1, 10003, 'devkey无效');
}
$this->devInfo = $devList[0];
return new returnObject(0);
}
/*******************************
* name: verify
* note: 验证参数是否有效
*******************************/
public function verify()
{
if (empty($this->appid))
return new returnObject(1, 10001, '未传入appid参数');
if (empty($this->devkey))
return new returnObject(1, 10002, '未传入devkey参数');
$devList = Sql::select('a.*')
->from('syweb_admin a')
->where('a.type=2 and a.admin_key=? and a.status=1', $this->devkey)
->get($this->db, null);
if (empty($devList) || count($devList) <= 0)
return new returnObject(1, 10003, 'devkey无效');
$this->devInfo = $devList[0];
/*
$appBaseList = Sql::select('a.*')
->from('syweb_app_base a')
->where('a.ref_key=? and a.dev_key=? and a.status=10', $this->appid, $this->devInfo['admin_key'])
->get($this->db, null);
if (empty($appBaseList) || count($appBaseList) <= 0)
{
return new returnObject(1, 10004, '指定的应用不存在或未被审核');
}
$appBaseKey = $appBaseList[0]["app_key"];
$appList = Sql::select('a.*')
->from('syweb_app a')
->where('a.ref_key=? and a.dev_key=? and a.status=10', $appBaseKey, $this->devInfo['admin_key'])
->get($this->db, null);
if (empty($appList) || count($appList) <= 0)
{
return new returnObject(1, 10004, '指定的应用不存在或未被审核');
}
$this->appInfo = $appList[0];
*/
$appList = Sql::select('b.*')
->from('syweb_app_base a, syweb_app b')
->where('a.app_key = b.ref_key and a.status = b.status and a.dev_key = b.dev_key and a.ref_key = ? and a.dev_key = ? and a.status = 10', $this->appid, $this->devInfo['admin_key'])
->get($this->db, null);
if (empty($appList) || count($appList) <= 0)
return new returnObject(1, 10004, '指定的应用不存在或未被审核');
$this->appInfo = $appList[0];
if (empty($this->sid))
return new returnObject(1, 10005, '请传入sid');
if (empty($this->scode))
return new returnObject(1, 10006, '请传入scode');
// 当前时间戳通过sid和scode获取用户信息必须保障sid未过期
$nowTime = time();
// 根据sid 查询用户信息
$userList = Sql::select('a.*')
->from('syweb_users a')
->where('a.sid=? and a.scode=? and a.sid_expire_time>?', $this->sid, $this->scode, $nowTime)
->get($this->db, null);
if (!empty($userList) && count($userList) > 0)
{
$this->userInfo = $userList[0];
// 延长SID过期时间 begin
$updateData = array();
$sid_expire_time = time() + (2 * 24 * 60 * 60);
$updateData['sid_expire_time'] = $sid_expire_time;
$pdo = $this->db;
$pdo->beginTransaction();
Sql::update('syweb_users')->setArgs($updateData)->where('id=?', $this->userInfo['id'])->exec($pdo);
$pdo->commit();
// 延长SID过期时间 end
// 判断SID对应的门店Key和传入的门店Key是否一致
if (!empty($market_key))
{
if ($this->userInfo['market_key'] != $market_key)
return new returnObject(1, 10011, '指定的SID和market_key不符');
}
$marketList = Sql::select('a.*')
->from('syweb_market a')
->where('a.market_key=?', $this->userInfo['market_key'])
->get($this->db, null);
if (empty($marketList) || count($marketList) <= 0)
return new returnObject(1, 10007, '指定的门店Key不存在或未被审核');
$this->market_key = $this->userInfo['market_key'];
$this->marketInfo = $marketList[0];
if (!empty($this->marketInfo) && !empty($this->marketInfo['templatemsg']))
$this->marketInfo['templatemsg'] = iunserializer($this->marketInfo['templatemsg']);
switch ($this->userInfo['auth_type'])
{
case AUTHTYPE_WECHAT:
{
$weixin_user_list = Sql::select('syweb_users_weixin.*')
->from('syweb_users_weixin')
->where('syweb_users_weixin.uid=?', $this->userInfo["id"])
->get($this->db, null);
if (!empty($weixin_user_list) && count($weixin_user_list) > 0)
{
$this->userInfo['weixin'] = $weixin_user_list[0];
return new returnObject(0);
}
else
return new returnObject(1, 10009, '未找到指定的微信用户信息');
break;
}
case AUTHTYPE_QQ:
{
$qq_user_list = Sql::select('syweb_users_qq.*')
->from('syweb_users_qq')
->where('syweb_users_qq.uid=?', $this->userInfo["id"])
->get($this->db, null);
if (!empty($qq_user_list) && count($qq_user_list) > 0)
{
$this->userInfo['qq'] = $qq_user_list[0];
return new returnObject(0);
}
else
return new returnObject(1, 10010, '未找到指定的QQ用户信息');
break;
}
case AUTHTYPE_JKX:
{
$jkx_user_list = Sql::select('a.*')
->from('syweb_users_jkx a')
->where('a.uid=?', $this->userInfo["id"])
->get($this->db, null);
if (!empty($jkx_user_list) && count($jkx_user_list) > 0)
{
$this->userInfo['jkx'] = $jkx_user_list[0];
return new returnObject(0);
}
else
return new returnObject(1, 10011, '未找到指定的聚开心用户信息');
break;
}
case AUTHTYPE_NIUNIUGAME:
{
$app_user_list = Sql::select('a.*')
->from('syweb_users_ylnn a')
->where('a.uid=?', $this->userInfo["id"])
->get($this->db, null);
if (!empty($app_user_list) && count($app_user_list) > 0)
{
$this->userInfo['app'] = $app_user_list[0];
return new returnObject(0);
}
else
return new returnObject(1, 10011, '未找到指定的应用认证用户信息');
break;
}
default:
{
return new returnObject(1, 10012, '未知的用户认证方式' . $this->userInfo['auth_type']);
break;
}
}
}
else
{
return new returnObject(1, 10008, '用户未登录或登录信息过期');
}
}
public function ToUrlParams($paramers)
{
$buff = "";
foreach ($paramers as $k => $v)
{
if ($k != "sign" && $v != "" && !is_array($v))
{
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/**
* @param string $market_key
* @return array|null
*/
public function verify_admin($market_key)
{
if (empty($this->appid))
return new returnObject(1, 10001, '未传入appid参数');
if (empty($this->devkey))
return new returnObject(1, 10002, '未传入devkey参数');
if (empty($market_key))
return new returnObject(1, 10011, '未传入market_key参数');
$devList = Sql::select('a.*')
->from('syweb_admin a')
->where('a.type=2 and a.admin_key=? and a.status=1', $this->devkey)
->get($this->db, null);
if (empty($devList) || count($devList) <= 0)
return new returnObject(1, 10003, 'devkey无效');
$this->devInfo = $devList[0];
/*
$appBaseList = Sql::select('a.*')
->from('syweb_app_base a')
->where('a.ref_key=? and a.dev_key=? and a.status=10', $this->appid, $this->devInfo['admin_key'])
->get($this->db, null);
if (empty($appBaseList) || count($appBaseList) <= 0)
{
return new returnObject(1, 10004, '指定的应用不存在或未被审核');
}
$appBaseKey = $appBaseList[0]["app_key"];
$appList = Sql::select('a.*')
->from('syweb_app a')
->where('a.ref_key=? and a.dev_key=? and a.status=10', $appBaseKey, $this->devInfo['admin_key'])
->get($this->db, null);
if (empty($appList) || count($appList) <= 0)
{
return new returnObject(1, 10004, '指定的应用不存在或未被审核');
}
$this->appInfo = $appList[0];
*/
$appList = Sql::select('b.*')
->from('syweb_app_base a, syweb_app b')
->where('a.app_key = b.ref_key and a.status = b.status and a.dev_key = b.dev_key and a.ref_key = ? and a.dev_key = ? and a.status = 10', $this->appid, $this->devInfo['admin_key'])
->get($this->db, null);
if (empty($appList) || count($appList) <= 0)
return new returnObject(1, 10004, '指定的应用不存在或未被审核');
$this->appInfo = $appList[0];
$marketList = Sql::select('a.*')
->from('syweb_market a')
->where('a.market_key=?', $market_key)
->get($this->db, null);
if (empty($marketList) || count($marketList) <= 0)
return new returnObject(1, 10007, '指定的门店Key不存在或未被审核');
$this->market_key = $market_key;
$this->marketInfo = $marketList[0];
if (!empty($this->marketInfo) && !empty($this->marketInfo['templatemsg']))
$this->marketInfo['templatemsg'] = iunserializer($this->marketInfo['templatemsg']);
return new returnObject(0);
}
/**
* @功能 带参数跳转到指定地址
* @param string $forwardUrl
* @param mixed $paramers
**/
public function forwardUrl($forwardUrl, $paramers)
{
$paramerStr = "";
if (!empty($paramers) && count($paramers) > 0)
{
foreach ($paramers as $key => $value)
{
if (empty($paramerStr))
{
$paramerStr = $key . "=" . $value;
}
else
{
$paramerStr .= "&" . $key . "=" . $value;
}
}
}
if (!empty($paramerStr))
{
if (strstr($forwardUrl, '?'))
{
if (strstr($forwardUrl, '&'))
{
$forwardUrl .= '&' . $paramerStr;
}
else
{
$forwardUrl .= $paramerStr;
}
}
else
{
$forwardUrl .= '?' . $paramerStr;
}
}
header('Location: ' . $forwardUrl);
exit();
}
/** @inject("ioc_factory") */
private $factory;
/**
* @property({"default":"@db"})
* @var PDO
*/
public $db;
}