Files
2026-03-15 01:27:05 +08:00

1184 lines
31 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Authorization, Content-Length, X-Requested-With");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: text/html; charset=utf-8");
/**
* 登录相关接口
*/
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;
require_once 'apiBase.php';
/**
*
* 登录管理
* @path("/cloud")
*/
class Cloud extends apiBase
{
public $activityInfo; // 当前活动信息
public $tableInfo; // 当前操作的表格信息
public $fieldList; // 当前表格中的字段列表
public function tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name)
{
$result = array();
// 验证公共参数是否合法
parent::init($appid, $devkey, $sid, $scode, $market_key);
$verify_result = parent::verify();
// 判断公共验证是否存在错误
if (is_error_api($verify_result))
{
return $verify_result;
}
if (empty($activity_key))
{
$result["error"] = '1';
$result["error_code"] = 13000;
$result["msg"] = "请传入对应的活动Key";
return $result;
}
$activityList = Sql::select('a.*')
->from('syweb_business_activity a')
->where('a.activity_key=?', $activity_key)
->get($this->db, null);
if (empty($activityList) || count($activityList) <= 0)
{
$result["error"] = '1';
$result["error_code"] = 13001;
$result["msg"] = "指定的活动不存在或已经下架";
return $result;
}
$this->activityInfo = $activityList[0];
$activityMarketList = Sql::select('a.*')
->from('syweb_business_activity_market a')
->where('a.activity_key=? and a.market_key=?', $activity_key, $market_key)
->get($this->db, null);
if (empty($activityMarketList) || count($activityMarketList) <= 0)
{
$result["error"] = '1';
$result["error_code"] = 13002;
$result["msg"] = "当前门店未参与该活动!";
return $result;
}
if (empty($table_name))
{
$result["error"] = '1';
$result["error_code"] = 13003;
$result["msg"] = "请传入对应的表格名称";
return $result;
}
$tableList = Sql::select('a.*')
->from('syweb_table a')
->where('a.table_alias_name=? and a.app_key=?', $table_name, $this->appInfo["app_key"])
->get($this->db, null);
if (empty($tableList) || count($tableList) <= 0)
{
$result["error"] = '1';
$result["error_code"] = 13004;
$result["msg"] = "指定的表格不存在或已经被删除";
return $result;
}
$this->tableInfo = $tableList[0];
// 指定表格中的字段列表
$this->fieldList = Sql::select('a.*')
->from('syweb_table_fields a')
->where('a.table_key=?', $this->tableInfo["table_key"])
->get($this->db, null);
if (empty($this->fieldList) || count($this->fieldList) <= 0)
{
$result["error"] = '1';
$result["error_code"] = 13005;
$result["msg"] = "指定表格不存在任何字段,无法修改数据";
return $result;
}
}
/**
* 数据存储接口(错误代码13000-13050)
* @route({"POST","/add"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"market_key","$._POST.market_key"}) market_key
* @param({"activity_key","$._POST.activity_key"}) activity_key
* @param({"table_name","$._POST.table_name"}) table_name
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postAddData($appid = '', $devkey = '', $sid = '', $scode = '', $market_key = '', $activity_key = '', $table_name = '')
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
$insertData = array();
$tableDataKey = time() . "" . random(4, true);
$insertData['table_data_key'] = $tableDataKey;
$insertData['market_key'] = $this->marketInfo["market_key"]; // 门店Key
$insertData['table_key'] = $this->tableInfo['table_key'];// 存储表格Key
$insertData['app_key'] = $this->appInfo['app_key'];// 应用Key
// 活动Key
$insertData['activity_key'] = $this->activityInfo['activity_key'];
$insertData['is_edit'] = 1;
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
// 数值类型字段
if (is_numeric($P[$fName]))
{
$insertData[$fName] = $P[$fName];
}
break;
case 2:
// 字符类型字段
if (isset($P[$fName]))
{
$insertData[$fName] = $P[$fName];
}
break;
case 3:
// 字符类型字段
if (isset($P[$fName]))
{
$insertData[$fName] = $P[$fName];
}
break;
case 4:
// 字符类型字段
if (isset($P[$fName]))
{
$insertData[$fName] = $P[$fName];
}
break;
case 5:
// 字符类型字段
if (isset($P[$fName]))
{
$insertData[$fName] = $P[$fName];
}
break;
case 6:
// 数值类型字段
if (is_numeric($P[$fName]))
{
$insertData[$fName] = $P[$fName];
}
break;
default:
break;
}
}
$insertData['createtime'] = TIMESTAMP;
$pdo = $this->db;
$pdo->beginTransaction();
Sql::insertInto('syweb_' . $this->appInfo['identity'] . '_table_data')->values($insertData)->exec($pdo);
$pdo->commit();
if (!empty($tableDataKey))
{
$result["error"] = '0';
$result["table_data_key"] = $tableDataKey;
$result["msg"] = "数据存储成功";
return $result;
}
else
{
$result["error"] = '1';
$result["error_code"] = 13010;
$result["msg"] = "数据存储失败";
return $result;
}
}
/**
* 数据修改接口(13051-13100)
* @route({"POST","/edit"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"activity_key","$._POST.activity_key"}) activity_key
* @param({"market_key","$._POST.market_key"}) market_key
* @param({"table_name","$._POST.table_name"}) table_name
* @param({"where_data_key","$._POST.where_data_key"}) where_data_key
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postEditData(
$appid = '',
$devkey = '',
$sid = '',
$scode = '',
$market_key = '',
$activity_key = '',
$table_name = '',
$where_data_key = ''
)
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
// 修改限定条件 begin
$conditionStr = " 1=1 ";
$fieldsCount = 0;
$updateSql = Sql::update('syweb_' . $this->appInfo['identity'] . '_table_data');
if (!empty($where_data_key))
{
$conditionStr .= " and table_data_key='" . $where_data_key . "'";
}
// 限制应用
$conditionStr .= " and app_key='" . $this->appInfo['app_key'] . "'";
// 限制表格
$conditionStr .= " and table_key='" . $this->tableInfo["table_key"] . "'";
// 限制门店
$conditionStr .= " and market_key='" . $this->marketInfo["market_key"] . "'";
// 限制活动
$conditionStr .= " and activity_key='" . $this->activityInfo["activity_key"] . "'";
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
// 数值类型字段
if (is_numeric($P[$fName]))
{
$updateSql->set($fName, $P[$fName]);
$fieldsCount = $fieldsCount + 1;
}
if (is_numeric($P["plus_" . $fName]))
{
$updateSql->set($fName, Sql::native($fName . '+' . $P["plus_" . $fName]));
$fieldsCount = $fieldsCount + 1;
}
if (is_numeric($P["sub_" . $fName]))
{
$updateSql->set($fName, Sql::native($fName . '-' . $P["sub_" . $fName]));
$fieldsCount = $fieldsCount + 1;
}
if (is_numeric($P["where_" . $fName]))
{
$conditionStr .= " and `" . $fName . "`= '" . $P["where_" . $fName] . "'";
}
break;
case 2:
// 字符类型字段
if (isset($P[$fName]))
{
$updateSql->set($fName, $P[$fName]);
$fieldsCount = $fieldsCount + 1;
}
if (isset($P["plus_" . $fName]))
{
$updateSql->set($fName, Sql::native("concat(" . $fName . ",'" . $P["plus_" . $fName] . "')"));
$fieldsCount = $fieldsCount + 1;
}
if (isset($P["where_" . $fName]))
{
$conditionStr .= " and `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
default:
/// 其他数据类型默认按字符串处理
if (isset($P[$fName]))
{
$updateSql->set($fName, $P[$fName]);
$fieldsCount = $fieldsCount + 1;
}
if (isset($P["plus_" . $fName]))
{
$updateSql->set($fName, Sql::native("concat(" . $fName . ",'" . $P["plus_" . $fName] . "')"));
$fieldsCount = $fieldsCount + 1;
}
if (isset($P["where_" . $fName]))
{
$conditionStr .= " and `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
}
}
if (!empty($conditionStr))
{
$updateSql->where($conditionStr);
}
// 修改限定条件 end
$pdo = $this->db;
$pdo->beginTransaction();
if ($fieldsCount > 0)
{
$updateCount = $updateSql->exec($pdo);
}
$pdo->commit();
$result["error"] = '0';
$result["msg"] = "数据修改成功。";
return $result;
}
/**
* 根据条件查询存储数据
* @route({"POST","/"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"market_key","$._POST.market_key"}) 门店Key
* @param({"activity_key","$._POST.activity_key"}) 活动Key
* @param({"table_name","$._POST.table_name"}) 表格别名
* @param({"where_data_key","$._POST.where_data_key"}) where_data_key
* @param({"page","$._POST.page"}) page
* @param({"psize","$._POST.psize"}) psize
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postQueryData($appid = '', $devkey = '', $sid = '', $scode = '',
$market_key = '', $activity_key = '', $table_name = '', $where_data_key = '', $page = 1, $psize = ''
)
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
$fields = ""; // 要查询的字段列表
$orderfield = ''; // 排序字符
$fieldsCount = 0;
// 组装公共查询条件 begin
$condition = array();
$conditionStr = " 1=1 ";
if (!empty($where_data_key))
{
$conditionStr .= " and table_data_key='" . $where_data_key . "'";
}
// 限制应用
$conditionStr .= " and app_key='" . $this->appInfo['app_key'] . "'";
// 限制表格
$conditionStr .= " and table_key='" . $this->tableInfo["table_key"] . "'";
// 限制门店
$conditionStr .= " and market_key='" . $this->marketInfo["market_key"] . "'";
// 限制活动
$conditionStr .= " and activity_key='" . $this->activityInfo["activity_key"] . "'";
// 组装公共查询条件 end
$pdo = $this->db;
$pdo->beginTransaction();
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
if (empty($fields))
{
$fields = " " . $fName . " ";
}
else
{
$fields .= " ," . $fName . " ";
}
if (!empty($P["order_" . $fName]))
{
if (empty($orderfield))
{
$orderfield = ' `' . $fName . '` ' . $P["order_" . $fName];
}
else
{
$orderfield .= ', `' . $fName . '` ' . $P["order_" . $fName];
}
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
if (is_numeric($P["where_" . $fName]))
{
if (empty($conditionStr))
{
$conditionStr = $fName . "= " . $P["where_" . $fName];
}
else
{
$conditionStr .= "and " . $fName . "= " . $P["where_" . $fName];
}
}
break;
case 2:
if (isset($P["where_" . $fName]))
{
$conditionStr .= " AND `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
default:
break;
}
}
if (empty($orderfield))
{
$orderfield = ' `id` asc';
}
if (empty($fields))
{
$fields = " `id`,table_data_key,createtime ";
}
else
{
$fields .= " ,`id`,table_data_key,createtime ";
}
// 分页 begin
$pindex = max(1, intval($page));
if ($psize)
{
$psize = intval($psize);
}
else
{
$psize = 20;
}
// 分页 end
$total = Sql::select(' COUNT(id) as num ')
->from('syweb_' . $this->appInfo['identity'] . '_table_data')
->where($conditionStr)
->get($this->db, null);
$list = array();
if (!empty($total) && !empty($total[0]["num"]))
{
if (empty($fields))
{
$fields = " * ";
}
$sql = Sql::select($fields)->from('syweb_' . ucfirst(strtolower($this->appInfo['identity'])) . '_table_data');
$sql = $sql->where($conditionStr);
if ($orderfield)
{
$sql = $sql->orderBy($orderfield);
}
$sql = $sql->limit(($pindex - 1) * $psize, $psize);
$list = $sql->get($this->db, null);
}
$pdo->commit();
if ($list)
{
foreach ($list as $key => &$row)
{
/*foreach ($row as $columnKey => &$column) {
if ( $columnKey=="t1" || $columnKey=="t2" ||
$columnKey=="t3" || $columnKey=="t4" || $columnKey=="t5" ||
$columnKey=="t6" || $columnKey=="t7" || $columnKey=="t8" ||
$columnKey=="t9" || $columnKey=="id" || $columnKey=="business_id" || $columnKey=="table_id" || $columnKey=="app_id") {
if( is_numeric($column) ){
$column = (int)$column;
}
}
if ( $columnKey=="s1" || $columnKey=="s2" ||
$columnKey=="s3" || $columnKey=="s4" || $columnKey=="s5" ||
$columnKey=="s6" || $columnKey=="s7" || $columnKey=="s8" ||
$columnKey=="s9" ) {
if( !empty($column) ){
$column = TranslateParameter($column);
}
}
}*/
}
$result["error"] = '0';
$result["msg"] = '数据获取成功。';
$result["data"] = $list;
$result["pager"]["total"] = (int)$total[0]["num"];// 总记录数
$result["pager"]["pindex"] = $pindex;// 当前页索引
$result["pager"]["psize"] = $psize;// 每页记录条数
return $result;
}
else
{
$list = array();
$result["error"] = '0';
$result["msg"] = '未查询到任何数据记录。';
$result["data"] = $list;
$result["pager"]["total"] = (int)$total[0]["num"];// 总记录数
$result["pager"]["pindex"] = $pindex;// 当前页索引
$result["pager"]["psize"] = $psize;// 每页记录条数
return $result;
}
}
/**
* 根据条件查询存储数据
* @route({"POST","/all"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"market_key","$._POST.market_key"}) 门店Key(只用来校验, 不用作数据过滤条件)
* @param({"activity_key","$._POST.activity_key"}) 活动Key(只用来校验, 不用作数据过滤条件)
* @param({"table_name","$._POST.table_name"}) 表格别名
* @param({"where_data_key","$._POST.where_data_key"}) where_data_key
* @param({"page","$._POST.page"}) page
* @param({"psize","$._POST.psize"}) psize
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postQueryData_2($appid = '', $devkey = '', $sid = '', $scode = '',
$market_key = '', $activity_key = '', $table_name = '', $where_data_key = '', $page = 1, $psize = ''
)
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
$fields = ""; // 要查询的字段列表
$orderfield = ''; // 排序字符
$fieldsCount = 0;
// 组装公共查询条件 begin
$condition = array();
$conditionStr = " 1=1 ";
if (!empty($where_data_key))
{
$conditionStr .= " and table_data_key='" . $where_data_key . "'";
}
// 限制应用
$conditionStr .= " and app_key='" . $this->appInfo['app_key'] . "'";
// 限制表格
$conditionStr .= " and table_key='" . $this->tableInfo["table_key"] . "'";
// 限制门店
//$conditionStr .= " and market_key='".$this->marketInfo["market_key"]."'";
// 限制活动
//$conditionStr .= " and activity_key='".$this->activityInfo["activity_key"]."'";
// 组装公共查询条件 end
$pdo = $this->db;
$pdo->beginTransaction();
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
if (empty($fields))
{
$fields = " " . $fName . " ";
}
else
{
$fields .= " ," . $fName . " ";
}
if (!empty($P["order_" . $fName]))
{
if (empty($orderfield))
{
$orderfield = ' `' . $fName . '` ' . $P["order_" . $fName];
}
else
{
$orderfield .= ', `' . $fName . '` ' . $P["order_" . $fName];
}
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
if (is_numeric($P["where_" . $fName]))
{
if (empty($conditionStr))
{
$conditionStr = $fName . "= " . $P["where_" . $fName];
}
else
{
$conditionStr .= "and " . $fName . "= " . $P["where_" . $fName];
}
}
break;
case 2:
if (isset($P["where_" . $fName]))
{
$conditionStr .= " AND `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
default:
break;
}
}
if (empty($orderfield))
{
$orderfield = ' `id` asc';
}
if (empty($fields))
{
$fields = " `id`,table_data_key,createtime ";
}
else
{
$fields .= " ,`id`,table_data_key,createtime ";
}
// 分页 begin
$pindex = max(1, intval($page));
if ($psize)
{
$psize = intval($psize);
}
else
{
$psize = 20;
}
// 分页 end
$total = Sql::select(' COUNT(id) as num ')
->from('syweb_' . $this->appInfo['identity'] . '_table_data')
->where($conditionStr)
->get($this->db, null);
$list = array();
if (!empty($total) && !empty($total[0]["num"]))
{
if (empty($fields))
{
$fields = " * ";
}
$sql = Sql::select($fields)->from('syweb_' . ucfirst(strtolower($this->appInfo['identity'])) . '_table_data');
$sql = $sql->where($conditionStr);
if ($orderfield)
{
$sql = $sql->orderBy($orderfield);
}
$sql = $sql->limit(($pindex - 1) * $psize, $psize);
$list = $sql->get($this->db, null);
}
$pdo->commit();
if ($list)
{
foreach ($list as $key => &$row)
{
/*foreach ($row as $columnKey => &$column) {
if ( $columnKey=="t1" || $columnKey=="t2" ||
$columnKey=="t3" || $columnKey=="t4" || $columnKey=="t5" ||
$columnKey=="t6" || $columnKey=="t7" || $columnKey=="t8" ||
$columnKey=="t9" || $columnKey=="id" || $columnKey=="business_id" || $columnKey=="table_id" || $columnKey=="app_id") {
if( is_numeric($column) ){
$column = (int)$column;
}
}
if ( $columnKey=="s1" || $columnKey=="s2" ||
$columnKey=="s3" || $columnKey=="s4" || $columnKey=="s5" ||
$columnKey=="s6" || $columnKey=="s7" || $columnKey=="s8" ||
$columnKey=="s9" ) {
if( !empty($column) ){
$column = TranslateParameter($column);
}
}
}*/
}
$result["error"] = '0';
$result["msg"] = '数据获取成功。';
$result["data"] = $list;
$result["pager"]["total"] = (int)$total[0]["num"];// 总记录数
$result["pager"]["pindex"] = $pindex;// 当前页索引
$result["pager"]["psize"] = $psize;// 每页记录条数
return $result;
}
else
{
$list = array();
$result["error"] = '0';
$result["msg"] = '未查询到任何数据记录。';
$result["data"] = $list;
$result["pager"]["total"] = (int)$total[0]["num"];// 总记录数
$result["pager"]["pindex"] = $pindex;// 当前页索引
$result["pager"]["psize"] = $psize;// 每页记录条数
return $result;
}
}
/**
* 根据条件删除存储数据
* @route({"POST","/delete"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"market_key","$._POST.market_key"}) market_key
* @param({"activity_key","$._POST.activity_key"}) activity_key
* @param({"table_name","$._POST.table_name"}) table_name
* @param({"where_data_key","$._POST.where_data_key"}) where_data_key
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postDelData($appid = '', $devkey = '', $sid = '', $scode = '',
$market_key = '', $activity_key = '', $table_name = '', $where_data_key = '')
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
$fieldsCount = 0;
$updateSql = Sql::update('syweb_' . ucfirst(strtolower($this->appInfo['identity'])) . '_table_data');
$condition = array();
$conditionStr = " 1=1 ";
if (!empty($where_data_key))
{
$conditionStr .= " and table_data_key='" . $where_data_key . "'";
}
// 限制应用
$conditionStr .= " and app_key='" . $this->appInfo['app_key'] . "'";
// 限制表格
$conditionStr .= " and table_key='" . $this->tableInfo["table_key"] . "'";
// 限制门店
$conditionStr .= " and market_key='" . $this->marketInfo["market_key"] . "'";
// 限制活动
$conditionStr .= " and activity_key='" . $this->activityInfo["activity_key"] . "'";
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
if (is_numeric($P["where_" . $fName]))
{
$conditionStr .= " and `" . $fName . "`= '" . $P["where_" . $fName] . "'";
}
break;
case 2:
if (isset($P["where_" . $fName]))
{
$conditionStr .= " and `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
default:
break;
}
}
$pdo = $this->db;
$pdo->beginTransaction();
$delResult = Sql::deleteFrom('syweb_' . ucfirst(strtolower($this->appInfo['identity'])) . '_table_data')->where($conditionStr)->exec($this->db);
$this->db->commit();
$result["error"] = '0';
$result["msg"] = "数据删除成功。";
return $result;
}
/**
* 根据条件查询存储数据总数
* @route({"POST","/count"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"market_key","$._POST.market_key"}) market_key
* @param({"activity_key","$._POST.activity_key"}) activity_key
* @param({"table_name","$._POST.table_name"}) table_name
* @param({"where_data_key","$._POST.where_data_key"}) where_data_key
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postQueryCountData($appid = '', $devkey = '', $sid = '', $scode = '',
$market_key = '', $activity_key = '', $table_name = '', $where_data_key = '')
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
// 查询条件 begin
$conditionStr = "";
$conditionStr = " 1=1 ";
if (!empty($where_data_key))
{
$conditionStr .= " and table_data_key='" . $where_data_key . "'";
}
// 限制应用
$conditionStr .= " and app_key='" . $this->appInfo['app_key'] . "'";
// 限制表格
$conditionStr .= " and table_key='" . $this->tableInfo["table_key"] . "'";
// 限制门店
$conditionStr .= " and market_key='" . $this->marketInfo["market_key"] . "'";
// 限制活动
$conditionStr .= " and activity_key='" . $this->activityInfo["activity_key"] . "'";
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
if (is_numeric($P["where_" . $fName]))
{
if (empty($conditionStr))
{
$conditionStr = $fName . "= " . $P["where_" . $fName];
}
else
{
$conditionStr .= "and " . $fName . "= " . $P["where_" . $fName];
}
}
break;
case 2:
if (isset($P["where_" . $fName]))
{
$conditionStr .= " AND `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
default:
break;
}
}
// 查询条件 end
$total = Sql::select(' COUNT(id) as countnum ')
->from('syweb_' . ucfirst(strtolower($this->appInfo['identity'])) . '_table_data')
->where($conditionStr)
->get($this->db, null);
if (!empty($total) && !empty($total[0]["countnum"]))
{
$result["error"] = '0';
$result["msg"] = '数据获取成功。';
$result["countnum"] = (int)$total[0]["countnum"];// 总数量
return $result;
}
else
{
$result["error"] = '0';
$result["msg"] = '未查询到任何数据记录。';
$result["countnum"] = 0;
return $result;
}
}
/**
* 根据条件查询存储数据
* @route({"POST","/countdata"})
* @param({"appid","$._POST.appid"}) 应用appid
* @param({"devkey","$._POST.devkey"}) 开发者key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端随机码
* @param({"market_key","$._POST.market_key"}) market_key
* @param({"activity_key","$._POST.activity_key"}) activity_key
* @param({"table_name","$._POST.table_name"}) table_name
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
* @return("body")
*/
public function postSingleQueryData($appid = '', $devkey = '', $sid = '', $scode = '',
$market_key = '', $activity_key = '', $table_name = ''
)
{
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->tableVerify($appid, $devkey, $sid, $scode, $market_key, $activity_key, $table_name);
if (is_error_api($verify_result))
{
return $verify_result;
}
$hasFields = array();
$fields = "";
// 查询条件 begin
$conditionStr = " 1=1 ";
if (!empty($where_data_key))
{
$conditionStr .= " and table_data_key='" . $where_data_key . "'";
}
// 限制应用
$conditionStr .= " and app_key='" . $this->appInfo['app_key'] . "'";
// 限制表格
$conditionStr .= " and table_key='" . $this->tableInfo["table_key"] . "'";
// 限制门店
$conditionStr .= " and market_key='" . $this->marketInfo["market_key"] . "'";
// 限制活动
$conditionStr .= " and activity_key='" . $this->activityInfo["activity_key"] . "'";
foreach ($this->fieldList as $fieldItem)
{
$fName = $fieldItem["field_name"];
if (empty($fieldItem))
{
continue;
}
// 根据字段类型做判断
switch ($fieldItem["field_type"])
{
case 1:
if (isset($P["min_" . $fName]))
{
if (empty($fields))
{
$fields = " min(" . $fName . ") as " . $P["min_" . $fName];
}
else
{
$fields .= " ,min(" . $fName . ") as " . $P["min_" . $fName];
}
}
if (isset($P["max_" . $fName]))
{
if (empty($fields))
{
$fields = " max(" . $fName . ") as " . $P["max_" . $fName];
}
else
{
$fields .= " ,max(" . $fName . ") as " . $P["max_" . $fName];
}
}
if (isset($P["sum_" . $fName]))
{
if (empty($fields))
{
$fields = " sum(" . $fName . ") as " . $P["sum_" . $fName];
}
else
{
$fields .= " ,sum(" . $fName . ") as " . $P["sum_" . $fName];
}
}
if (isset($P["count_" . $fName]))
{
if (empty($fields))
{
$fields = " count(" . $fName . ") as " . $P["count_" . $fName];
}
else
{
$fields .= " ,count(" . $fName . ") as " . $P["count_" . $fName];
}
}
if (is_numeric($P["where_" . $fName]))
{
if (empty($conditionStr))
{
$conditionStr = $fName . "= " . $P["where_" . $fName];
}
else
{
$conditionStr .= "and " . $fName . "= " . $P["where_" . $fName];
}
}
break;
case 2:
if (isset($P["count_" . $fName]))
{
if (empty($fields))
{
$fields = " count(" . $fName . ") as " . $P["count_" . $fName];
}
else
{
$fields .= " ,count(" . $fName . ") as " . $P["count_" . $fName];
}
}
if (isset($P["where_" . $fName]))
{
$conditionStr .= " AND `" . $fName . "` = '" . $P["where_" . $fName] . "'";
}
break;
default:
break;
}
}
$pdo = $this->db;
if (empty($fields))
{
$result["error"] = '1';
$result["error_code"] = 13010; // 未找到指定的QQ用户信息
$result["msg"] = "请输入需要查询的数据对象";
return $result;
}
$list = Sql::select($fields)
->from('syweb_' . ucfirst(strtolower($this->appInfo['identity'])) . '_table_data')
->where($conditionStr)
->get($this->db, null);
if (!empty($list) && count($list) > 0)
{
foreach ($list as $key => &$row)
{
foreach ($row as $columnKey => &$column)
{
$column = (int)$column;
}
}
$result["error"] = '0';
$result["msg"] = '数据获取成功。';
$result["data"] = $list[0];
return $result;
}
else
{
$list = array();
$result["error"] = '0';
$result["msg"] = '未查询到任何数据记录。';
$result["data"] = $list;
return $result;
}
}
/** @inject("ioc_factory") */
private $factory;
/**
* @property({"default":"@db"})
* @var PDO
*/
public $db;
// 此处删除了代码
}