初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace app\storeapi\controller;
class Addon extends BaseStoreApi
{
public function __construct()
{
$this->site_id = request()->siteid();
}
/**
* 插件是否存在
*/
public function addonIsExit()
{
$addon = array_filter(array_map(function($item) {
if (addon_is_exit($item, $this->site_id)) return $item;
}, [ 'store', 'stock', 'scale', 'weighgoods', 'cardservice' ]));
$addon = array_values($addon);
return $this->response($this->success($addon));
}
}

View File

@@ -0,0 +1,339 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace app\storeapi\controller;
use addon\cashier\model\Menu;
use app\exception\ApiException;
use app\model\shop\Shop;
use app\model\system\Api;
use app\model\system\Site;
use app\model\system\User as UserModel;
use think\facade\Cache;
use think\Response;
class BaseStoreApi
{
public $lang;
public $params;
protected $user_info;
protected $uid;
protected $site_id;
protected $store_id;
protected $shop_info;
public $app_type;
protected $app_module = 'store';
protected $api_config;
protected $addon = '';
protected $store_list;
protected $menu_array;
public function __construct()
{
if ($_SERVER[ 'REQUEST_METHOD' ] == 'OPTIONS') {
exit;
}
$this->addon = request()->addon() ? request()->addon() : '';
//获取参数
$this->params = input();
$this->getApiConfig();
$this->site_id = request()->siteid();
// 验证token
$token = $this->checkToken();
if ($token[ 'code' ] != 0) throw new ApiException($token['code'], $token['message']);
if (empty($this->user_info[ 'user_group_list' ])) throw new ApiException(-1, lang('NO_PERMISSION'));
$store_list = array_column($this->user_info[ 'user_group_list' ], null, 'store_id');
if (isset($this->params[ 'store_id' ]) && !empty($this->params[ 'store_id' ])) {
$this->store_id = $this->params[ 'store_id' ];
} else {
$this->store_id = $this->user_info[ 'user_group_list' ][ 0 ][ 'store_id' ];
}
if (!isset($store_list[ $this->store_id ])) exit($this->response($this->error([], 'NO_PERMISSION')));
$this->store_list = $store_list;
$this->menu_array = $this->store_list[ $this->store_id ][ 'menu_array' ] ?? '';
//判断权限
if (!$this->checkAuth()) {
throw new ApiException(-1, lang('NO_PERMISSION'));
}
}
/**
* 获取api配置
*/
protected function getApiConfig()
{
$api_model = new Api();
$config_result = $api_model->getApiConfig();
$this->api_config = $config_result[ "data" ];
}
/**
* 检测token(使用私钥检测)
*/
protected function checkToken() : array
{
if (empty($this->params[ 'token' ])) {
return $this->error('', 'TOKEN_NOT_EXIST');
}
if ($this->api_config[ 'is_use' ] && isset($this->api_config[ 'value' ][ 'private_key' ])
&& !empty($this->api_config[ 'value' ][ 'private_key' ])) {
$decrypt = decrypt($this->params[ 'token' ], $this->api_config[ 'value' ][ 'private_key' ]);
} else {
$decrypt = decrypt($this->params[ 'token' ]);
}
if (empty($decrypt)) {
return $this->error('', 'TOKEN_ERROR');
}
$data = json_decode($decrypt, true);
if (empty($data)) {
return $this->error('', 'TOKEN_ERROR');
}
if (!empty($data[ 'expire_time' ]) && $data[ 'expire_time' ] > time()) {
return $this->error('', 'TOKEN_EXPIRE');
}
$this->user_info = $data[ 'user_info' ];
$this->app_module = $this->user_info[ 'app_module' ];
$this->uid = $data[ 'user_info' ][ 'uid' ];
$this->getShopInfo();
return success(0, '', $data);
}
/**
* 检测权限
* @return bool
*/
protected function checkAuth()
{
if ($this->user_info[ 'is_admin' ]) return true;
$url = implode('/', array_filter([ request()->addon(), request()->module(), request()->controller(), request()->action() ]));
$name = ( new Menu() )->getMenuValue([ [ 'url', '=', $url ], [ 'type', '=', 'api' ] ], 'name')[ 'data' ];
if (empty($name)) return true;
$menu_array = $this->store_list[ $this->store_id ][ 'menu_array' ] ?? '';
if (empty($menu_array)) return true;
if (!in_array($name, explode(',', $menu_array))) return false;
return true;
}
/**
* 创建token
* @param $user_info
* @param int $expire_time 有效时间 0为永久 单位s
* @return string
*/
protected function createToken($user_info, $expire_time = 0)
{
$data = [
'user_info' => $user_info,
'expire_time' => empty($expire_time) ? 0 : time() + $expire_time
];
if ($this->api_config[ 'is_use' ] && isset($this->api_config[ 'value' ][ 'private_key' ])
&& !empty($this->api_config[ 'value' ][ 'private_key' ])) {
$token = encrypt(json_encode($data), $this->api_config[ 'value' ][ 'private_key' ]);
} else {
$token = encrypt(json_encode($data));
}
return $token;
}
public function getShopInfo()
{
//获取店铺信息
$condition = array (
[ "site_id", "=", $this->site_id ]
);
$shop_info_result = ( new Shop() )->getShopInfo($condition);
$site_info = ( new Site() )->getSiteInfo($condition);
$this->shop_info = array_merge($shop_info_result[ 'data' ], $site_info[ 'data' ]);
}
public function getUserInfo($uid = null)
{
$condition = array (
['uid', '=', $uid ?? $this->uid],
['site_id', '=', $this->site_id],
['app_module', '=', 'shop'],
);
$user_model = new UserModel();
$user_info = $user_model->getUserInfo($condition, '*')['data'];
return $user_info;
}
/**
* 返回数据
* @param $data
* @return false|string
*/
public function response($data)
{
$data[ 'timestamp' ] = time();
return Response::create($data, 'json', 200);
}
/**
* 操作成功返回值函数
* @param string $data
* @param string $code_var
* @return array
*/
public function success($data = '', $code_var = 'SUCCESS')
{
$lang_array = $this->getLang();
$code_array = $this->getCode();
$lang_var = $lang_array[$code_var] ?? $code_var;
$code_var = $code_array[$code_var] ?? $code_array['SUCCESS'];
return success($code_var, $lang_var, $data);
}
/**
* 操作失败返回值函数
* @param string $data
* @param string $code_var
* @return array
*/
public function error($data = '', $code_var = 'ERROR')
{
$lang_array = $this->getLang();
$code_array = $this->getCode();
$lang_var = $lang_array[$code_var] ?? $code_var;
$code_var = $code_array[$code_var] ?? $code_array['ERROR'];
return error($code_var, $lang_var, $data);
}
/**
* 获取语言包数组
* @return array|mixed
*/
private function getLang()
{
$default_lang = config("lang.default_lang");
$addon = request()->addon();
$addon = $addon ?? '';
$cache_common = Cache::get("lang_app/storeapi/lang/" . $default_lang);
if (!empty($addon)) {
$addon_cache_common = Cache::get("lang_app/storeapi/lang/" . $addon . '_' . $default_lang);
if (!empty($addon_cache_common)) {
$cache_common = array_merge($cache_common, $addon_cache_common);
}
}
if (empty($cache_common)) {
$cache_common = include 'app/storeapi/lang/' . $default_lang . '.php';
Cache::tag("lang")->set("lang_app/storeapi/lang/" . $default_lang, $cache_common);
if (!empty($addon)) {
try {
$addon_cache_common = include 'addon/' . $addon . '/storeapi/lang/' . $default_lang . '.php';
if (!empty($addon_cache_common)) {
$cache_common = array_merge($cache_common, $addon_cache_common);
Cache::tag("lang")->set(
"lang_app/storeapi/lang/" . $addon . '_' . $default_lang,
$addon_cache_common
);
}
} catch (\Exception $e) {
}
}
}
$lang_path = $this->lang ?? '';
if (!empty($lang_path)) {
$cache_path = Cache::get("lang_" . $lang_path . "/" . $default_lang);
if (empty($cache_path)) {
$cache_path = include $lang_path . "/" . $default_lang . '.php';
Cache::tag("lang")->set("lang_" . $lang_path . "/" . $default_lang, $cache_path);
}
$lang = array_merge($cache_common, $cache_path);
} else {
$lang = $cache_common;
}
return $lang;
}
/**
* 获取code编码
* @return array|mixed
*/
private function getCode()
{
$addon = request()->addon();
$addon = $addon ?? '';
$cache_common = Cache::get("lang_code_app/storeapi/lang");
if (!empty($addon)) {
$addon_cache_common = Cache::get("lang_code_app/storeapi/lang/" . $addon);
if (!empty($addon_cache_common)) {
$cache_common = array_merge($cache_common, $addon_cache_common);
}
}
if (empty($cache_common)) {
$cache_common = include 'app/storeapi/lang/code.php';
Cache::tag("lang_code")->set("lang_code_app/storeapi/lang", $cache_common);
if (!empty($addon)) {
try {
$addon_cache_common = include 'addon/' . $addon . '/storeapi/lang/code.php';
if (!empty($addon_cache_common)) {
Cache::tag("lang_code")->set("lang_code_app/storeapi/lang/" . $addon, $addon_cache_common);
$cache_common = array_merge($cache_common, $addon_cache_common);
}
} catch (\Exception $e) {
}
}
}
$lang_path = $this->lang ?? '';
if (!empty($lang_path)) {
$cache_path = Cache::get("lang_code_" . $lang_path);
if (empty($cache_path)) {
$cache_path = include $lang_path . '/code.php';
Cache::tag("lang")->set("lang_code_" . $lang_path, $cache_path);
}
$lang = array_merge($cache_common, $cache_path);
} else {
$lang = $cache_common;
}
return $lang;
}
/**
* 添加日志
* @param string $action_name
* @param array $data
*/
protected function addLog($action_name, $data = [])
{
$user = new UserModel();
$user->addUserLog($this->uid, $this->user_info[ 'username' ], $this->site_id, $action_name, $data);
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace app\storeapi\controller;
use think\captcha\facade\Captcha as ThinkCaptcha;
use think\facade\Cache;
class Captcha extends BaseStoreApi
{
public function __construct()
{
$this->params = input();
}
/**
* 验证码
*/
public function captcha()
{
if (isset($this->params[ 'captcha_id' ]) && !empty($this->params[ 'captcha_id' ])) {
Cache::delete($this->params[ 'captcha_id' ]);
}
$captcha_data = ThinkCaptcha::create(null, true);
$captcha_id = md5(uniqid(null, true));
// 验证码10分钟有效
Cache::set($captcha_id, $captcha_data[ 'code' ], 600);
return $this->response($this->success([ 'id' => $captcha_id, 'img' => $captcha_data[ 'img' ] ]));
}
/**
* 检测验证码
* @param boolean $snapchat 阅后即焚
*/
public function checkCaptcha($snapchat = true) : array
{
if (!isset($this->params[ 'captcha_id' ]) || empty($this->params[ 'captcha_id' ])) {
return $this->error('', 'REQUEST_CAPTCHA_ID');
}
if (!isset($this->params[ 'captcha_code' ]) || empty($this->params[ 'captcha_code' ])) {
return $this->error('', 'REQUEST_CAPTCHA_CODE');
}
if ($snapchat) $captcha_data = Cache::pull($this->params[ 'captcha_id' ]);
else $captcha_data = Cache::get($this->params[ 'captcha_id' ]);
if (empty($captcha_data)) return $this->error('', 'CAPTCHA_FAILURE');
if ($this->params[ 'captcha_code' ] != $captcha_data) return $this->error('', 'CAPTCHA_ERROR');
return $this->success();
}
}

53
app/storeapi/lang/code.php Executable file
View File

@@ -0,0 +1,53 @@
<?php
return [
'SUCCESS' => 0,
'ERROR' => -1,
'FAIL' => -10001,
'SAVE_SUCCESS' => 10002,
'SAVE_FAIL' => -10002,
'REQUEST_SUCCESS' => 10003,
'REQUEST_FAIL' => -10003,
'DELETE_SUCCESS' => 10004,
'DELETE_FAIL' => -10004,
'UNKNOW_ERROR' => -10005,
'PARAMETER_ERROR' => -10006,
'REQUEST_SITE_ID' => -10007,
'REQUEST_APP_MODULE' => -10008,
'TOKEN_NOT_EXIST' => -1,
'TOKEN_ERROR' => -10009,
'TOKEN_EXPIRE' => -10010,
'ADDON_NOT_EXIST' => -10011,
'NO_PERMISSION' => -10012,
'CAPTCHA_FAILURE' => -1,
'CAPTCHA_ERROR' => -1,
'REQUEST_COUPON_TYPE_ID' => -1,
'REQUEST_CAPTCHA_ID' => -1,
'REQUEST_CAPTCHA_CODE' => -1,
'REQUEST_SKU_ID' => -1,
'REQUEST_NUM' => -1,
'REQUEST_CART_ID' => -1,
'REQUEST_CATEGORY_ID' => -1,
'REQUEST_ID' => -1,
'REQUEST_ORDER_ID' => -1,
'REQUEST_GOODS_EVALUATE' => -1,
'REQUEST_ORDER_STATUS' => -1,
'REQUEST_DIY_ID_NAME' => -1,
'REQUEST_TOPIC_ID' => -1,
'REQUEST_SECKILL_ID' => -1,
'REQUEST_KEYWORD' => -1,
'REQUEST_GOODS_ID' => -1,
'REQUEST_PINTUAN_ID' => -1,
'REQUEST_EMAIL' => -1,
'REQUEST_MOBILE' => -1,
'REQUEST_GROUPBUY_ID' => -1,
'REQUEST_RECHARGE_ID' => -1,
'REQUEST_BL_ID' => -1,
'REQUEST_NAME' => -1,
'REQUEST_STORE_ID' => -1,
'REQUEST_REAL_NAME' => -1,
'REQUEST_WITHDRAW_TYPE' => -1,
'REQUEST_BRANCH_BANK_NAME' => -1,
'REQUEST_BRANCH_BANK_ACCOUNT' => -1,
];

17
app/storeapi/lang/en-us.php Executable file
View File

@@ -0,0 +1,17 @@
<?php
return [
'SUCCESS' => 'success',
'ERROR' => 'error',
'FAIL' => 'fail',
'SAVE_SUCCESS' => 'save success',
'SAVE_FAIL' => 'save fail',
'REQUEST_SUCCESS' => 'request success',
'REQUEST_FAIL' => 'request error',
'DELETE_SUCCESS' => 'delete success',
'DELETE_FAIL' => 'delete fail',
'UNKNOW_ERROR' => 'unknow error',
'PARAMETER_ERROR' => 'parameter error',
'REQUEST_SITE_ID' => 'request site id',
'REQUEST_APP_MODULE' => 'request app module'
];

52
app/storeapi/lang/zh-cn.php Executable file
View File

@@ -0,0 +1,52 @@
<?php
return [
'SUCCESS' => '操作成功',
'ERROR' => '操作失败',
'SAVE_SUCCESS' => '保存成功',
'SAVE_FAIL' => '保存失败',
'REQUEST_SUCCESS' => '请求成功',
'REQUEST_FAIL' => '请求失败',
'DELETE_SUCCESS' => '删除成功',
'DELETE_FAIL' => '删除失败',
'UNKNOW_ERROR' => '未知错误',
'PARAMETER_ERROR' => '参数错误',
'REQUEST_SITE_ID' => '缺少必须参数站点id',
'REQUEST_APP_MODULE' => '缺少必须参数应用模块',
'TOKEN_NOT_EXIST' => 'token不存在',
'TOKEN_ERROR' => 'token错误',
'TOKEN_EXPIRE' => 'token已过期',
'CAPTCHA_FAILURE' => '验证码已失效',
'CAPTCHA_ERROR' => '验证码不正确',
'REQUEST_COUPON_TYPE_ID' => '缺少参数coupon_type_id',
'REQUEST_CAPTCHA_ID' => '缺少参数captcha_id',
'REQUEST_CAPTCHA_CODE' => '缺少参数captcha_code',
'REQUEST_SKU_ID' => '缺少参数sku_id',
'REQUEST_NUM' => '缺少参数num',
'REQUEST_CART_ID' => '缺少参数cart_id',
'REQUEST_CATEGORY_ID' => '缺少参数category_id',
'REQUEST_ID' => '缺少参数id',
'REQUEST_ORDER_ID' => '缺少参数order_id',
'REQUEST_GOODS_EVALUATE' => '缺少参数goods_evaluate',
'REQUEST_ORDER_STATUS' => '缺少参数order_status',
'REQUEST_DIY_ID_NAME' => '缺少参数id/name',
'REQUEST_TOPIC_ID' => '缺少参数topic_id',
'REQUEST_SECKILL_ID' => '缺少参数seckill_id',
'REQUEST_KEYWORD' => '缺少参数keyword',
'REQUEST_GOODS_ID' => '缺少参数goods_id',
'REQUEST_PINTUAN_ID' => '缺少参数pintuan_id',
'REQUEST_EMAIL' => '缺少参数email',
'REQUEST_MOBILE' => '缺少参数mobile',
'REQUEST_GROUPBUY_ID' => '缺少参数groupbuy_id',
'REQUEST_RECHARGE_ID' => '缺少参数recharge_id',
'REQUEST_BL_ID' => '缺少参数bl_id',
'REQUEST_NAME' => '缺少参数name',
'REQUEST_STORE_ID' => '缺少参数store_id',
'REQUEST_REAL_NAME' => '缺少参数real_name',
'REQUEST_WITHDRAW_TYPE' => '缺少参数withdraw_type',
'REQUEST_BRANCH_BANK_NAME' => '缺少参数branch_bank_name',
'REQUEST_BRANCH_BANK_ACCOUNT' => '缺少参数bank_account',
'ADDON_NOT_EXIST' => '商家手机管理端插件不存在',
'NO_PERMISSION' => '权限不足'
];