Merge branch 'develop' of https://gitee.com/Tyssen/yi-shi into develop

# Conflicts:
#	Server/.env
#	Server/application/common/model/CompanyAccountModel.php
#	Server/application/common/model/DeviceModel.php
#	Server/application/common/model/WechatAccountModel.php
This commit is contained in:
Ghost
2025-03-18 14:58:50 +08:00
1810 changed files with 8495 additions and 6592 deletions

1
Server/.gitignore vendored
View File

@@ -1,4 +1,5 @@
.idea
.env
.vscode
.DS_Store
composer.lock

0
Server/.htaccess Executable file → Normal file
View File

0
Server/CHANGELOG.md Executable file → Normal file
View File

0
Server/LICENSE.txt Executable file → Normal file
View File

0
Server/README.md Executable file → Normal file
View File

0
Server/application/.htaccess Executable file → Normal file
View File

View File

View File

@@ -1,157 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\CollectProductModel;
use app\common\model\ProductGroupModel;
use think\db\Query;
class CollectProductController extends BaseLoginController {
/**
* 获取列表
*
* @return \think\response\Json
*/
public function index() {
$parentId = intval($this->request->param('parent_id'));
$type = trim($this->request->param('type'));
$video = trim($this->request->param('video'));
$repeat = trim($this->request->param('repeat'));
$groupId = intval($this->request->param('group_id'));
$status = trim($this->request->param('status'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = CollectProductModel::where(1);
if (!empty($parentId)) {
$query->where('parent_id', $parentId);
} else {
$query->where('parent_id', 0);
}
if (isset(CollectProductModel::typeAssoc()[$type])) {
$query->where('type', $type);
}
if (isset(CollectProductModel::videoAssoc()[$video])) {
$query->where('video', $video);
}
if (isset(CollectProductModel::repeatAssoc()[$repeat])) {
$query->where('repeat', $repeat);
}
if (isset(CollectProductModel::statusAssoc()[$status])) {
$query->where('status', $status);
}
if (!empty($groupId)) {
$query->where('group_id', $groupId);
}
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('src_url', '%' . $keywords . '%', 'OR');
$q->whereLike('title', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$list[] = array_merge($model->toArray(), [
'product_num' => $model->productNum(),
'status_name' => CollectProductModel::statusAssoc()[$model->status],
'video_name' => CollectProductModel::videoAssoc()[$model->video],
'repeat_name' => CollectProductModel::repeatAssoc()[$model->repeat],
'platform_name' => CollectProductModel::platformAssoc()[$model->platform],
'mark_up_rate' => floatval($model->mark_up_rate),
'mark_up_val' => floatval($model->mark_up_val),
'start_time' => $model->start_time > 0 ? date('Y-m-d H:i:s', $model->start_time) : '',
'stop_time' => $model->stop_time > 0 ? date('Y-m-d H:i:s', $model->stop_time) : '',
'group_name' => isset(ProductGroupModel::assoc()[$model->group_id])
? ProductGroupModel::assoc()[$model->group_id]
: '',
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'statuses' => $this->assocToList(CollectProductModel::statusAssoc()),
'platforms' => $this->assocToList(CollectProductModel::platformAssoc()),
'videos' => $this->assocToList(CollectProductModel::videoAssoc()),
'repeats' => $this->assocToList(CollectProductModel::repeatAssoc()),
'groups' => $this->assocToList(ProductGroupModel::assoc()),
]);
}
/**
* 保存
*
* @return \think\response\Json
*/
public function save() {
//$id = intval($this->request->param('id'));
$type = trim($this->request->param('type'));
$srcUrl = trim($this->request->param('src_url'));
$video = intval($this->request->param('video'));
$repeat = intval($this->request->param('repeat'));
$markUpRate = intval($this->request->param('mark_up_rate'));
$markUpVal = intval($this->request->param('mark_up_val'));
$groupId = intval($this->request->param('group_id'));
if (empty($srcUrl)
OR !isset(CollectProductModel::typeAssoc()[$type])
OR !isset(CollectProductModel::videoAssoc()[$video])
OR !isset(CollectProductModel::repeatAssoc()[$repeat])) {
return $this->jsonFail('参数错误');
}
if (!empty($id)) {
$model = CollectProductModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
} else {
$model = new CollectProductModel();
}
$platform = '';
if ($type === CollectProductModel::TYPE_PRODUCT
AND preg_match('#^https\:\/\/www\.goofish\.com\/item#', $srcUrl)) {
$platform = CollectProductModel::PLATFORM_XIANYU;
} elseif ($type === CollectProductModel::TYPE_SHOP
AND preg_match('#^https\:\/\/www\.goofish\.com\/personal#', $srcUrl)) {
$platform = CollectProductModel::PLATFORM_XIANYU;
} else {
return $this->jsonFail('采集链接错误');
}
$model = new CollectProductModel();
$model->type = $type;
$model->src_url = $srcUrl;
$model->platform = $platform;
$model->target = CollectProductModel::TARGET_PRODUCT;
$model->video = $video;
$model->repeat = $repeat;
$model->mark_up_rate = $markUpRate;
$model->mark_up_val = $markUpVal;
$model->group_id = $groupId;
$model->user_id = $this->userModel->id;
$model->save();
return $this->jsonSucc();
}
}

View File

@@ -1,92 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\DeviceModel;
use think\db\Query;
class DeviceController extends BaseLoginController {
/**
* 获取列表
*
* @return \think\response\Json
*/
public function index() {
$isOnline = intval($this->request->param('is_online'));
$status = trim($this->request->param('status'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$onlines = [
1 => '在线',
2 => '离线',
];
$query = DeviceModel::where(1);
if (isset(DeviceModel::statusAssoc()[$status])) {
$query->where('status', $status);
}
if ($isOnline == 1) {
$query->where('is_online', DeviceModel::IS_ONLINE_YES);
$query->where('active_time', '>=', time() - DeviceModel::ACTIVE_TIME);
} elseif ($isOnline == 2) {
$query->where(function (Query $q) {
$q->whereOr('is_online', DeviceModel::IS_ONLINE_NO);
$q->whereOr('active_time', '<', time() - DeviceModel::ACTIVE_TIME);
});
}
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('number', '%' . $keywords . '%', 'OR');
$q->whereLike('ip', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$list[] = array_merge($model->toArray(), [
'is_online' => $model->isOnline() ? 1 : 2,
'is_online_name' => $onlines[$model->isOnline() ? 1 : 2],
'status_name' => DeviceModel::statusAssoc()[$model->status],
'active_time' => date('Y-m-d H:i:s', $model->active_time),
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'statuses' => $this->assocToList(DeviceModel::statusAssoc()),
'onlines' => $this->assocToList($onlines),
]);
}
/**
* 获取关联数组
*
* @return \think\response\Json
*/
public function assoc() {
return $this->jsonSucc($this->assocToList(DeviceModel::assoc()));
}
}

View File

@@ -1,151 +0,0 @@
<?php
namespace app\backend\controller;
use think\db\Query;
class DeviceStatController extends BaseLoginController {
/**
* 获取列表
*
* @return \think\response\Json
*/
public function index() {
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = DeviceStatModel::where(1);
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('days', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
if ($pageNo <= 1) {
$num = DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->count();
$succNum = DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->where('status', DeviceQqModel::STATUS_SUCC)
->count();
$rewardTotal = floatval(DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->where('status', DeviceQqModel::STATUS_SUCC)
->sum('reward'));
$rewardPrice = 0;
if ($succNum > 0) {
$rewardPrice = round($rewardTotal / $succNum, 2);
}
$finishTime = intval(DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->where('finish_time', '>', 0)
->avg('finish_time'));
$createTime = intval(DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->where('finish_time', '>', 0)
->avg('create_time'));
$succFinishTime = intval(DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->where('status', DeviceQqModel::STATUS_SUCC)
->where('finish_time', '>', 0)
->avg('finish_time'));
$succCreateTime = intval(DeviceQqModel::where(1)
->where('create_time', '>=', 1724860800)
->where('status', DeviceQqModel::STATUS_SUCC)
->where('finish_time', '>', 0)
->avg('create_time'));
$succRate = $num > 0 ? round($succNum / $num * 100, 2) : 0;
$failNum = $num - $succNum;
$failRate = $num > 0 ? round($failNum / $num * 100, 2) : 0;
$succFailRate = 0;
if ($num > $succNum) {
$succFailRate = $succNum / ($num - $succNum);
$succFailRate = round($succFailRate * 100, 2);
}
$failInfo = [];
foreach (DeviceQqModel::where(1)
->field('remark, COUNT(id) AS num')
->where('create_time', '>=', 1724860800)
->whereNotIn('status', [DeviceQqModel::STATUS_REG, DeviceQqModel::STATUS_SUCC])
->group('remark')
->select() as $infoModel) {
if ($infoModel->remark) {
$failInfo[] = [
'title' => $infoModel->remark,
'num' => $infoModel->num,
'rate' => $num > 0 ? round($infoModel->num / $num * 100, 2) : 0,
];
}
}
$list[] = [
'days' => '合计',
'num' => $num,
'succ_num' => $succNum,
'reward_price' => $rewardPrice,
'reward_total' => $rewardTotal,
'avg_time' => $finishTime > $createTime ? $finishTime - $createTime : 0,
'avg_succ_time' => $succFinishTime > $succCreateTime ? $succFinishTime - $succCreateTime : 0,
'succ_fail_rate' => $succFailRate,
'succ_rate' => $succRate,
'fail_num' => $failNum,
'fail_rate' => $failRate,
'fail_info' => $failInfo,
];
}
foreach ($query->select() as $model) {
$succRate = 0;
$failNum = $model->num > $model->succ_num ? $model->num - $model->succ_num : 0;
$failRate = 0;
$succFailRate = 0;
if ($model->num > 0) {
$succRate = $model->succ_num / $model->num;
$succRate = round($succRate * 100, 2);
$failRate = $failNum / $model->num;
$failRate = round($failRate * 100, 2);
}
if ($model->num > $model->succ_num) {
$succFailRate = $model->succ_num / ($model->num - $model->succ_num);
$succFailRate = round($succFailRate * 100, 2);
}
$list[] = array_merge($model->toArray(), [
'reward_price' => floatval($model->reward_price),
'reward_total' => floatval($model->reward_total),
'succ_fail_rate' => $succFailRate,
'fail_num' => $failNum,
'succ_rate' => $succRate,
'fail_rate' => $failRate,
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
]);
}
}

View File

@@ -1,32 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\TaskModel;
class MessageReplyController extends BaseLoginController {
/**
* 关闭
*
* @return \think\response\Json
*/
public function close() {
$devices = $this->request->param('devices');
if (empty($devices) OR !is_array($devices)) {
return $this->jsonFail('参数错误');
}
TaskModel::where(1)
->whereIn('device_id', $devices)
->where('platform', TaskModel::PLATFORM_XIANYU)
->whereIn('status', [TaskModel::STATUS_AWAIT, TaskModel::STATUS_ALLOC])
->where('is_deleted', TaskModel::IS_DELETED_NO)
->update([
'is_deleted' => TaskModel::IS_DELETED_YES,
'update_time' => time(),
]);
return $this->jsonSucc();
}
}

View File

@@ -1,59 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\ProductContentPoolModel;
class ProductContentPoolController extends BaseLoginController {
/**
* 获取关联数组
*
* @return \think\response\Json
*/
public function assoc() {
return $this->jsonSucc(ProductContentPoolModel::assoc());
}
/**
* 保存
*
* @return \think\response\Json
* @throws \Exception
*/
public function save() {
$contents = $this->request->param('contents');
if (!is_array($contents)) {
return $this->jsonFail('参数错误');
}
for ($i = 1; $i <= 6; $i ++) {
if (isset($contents[$i])) {
$content = trim($contents[$i]);
$content = trim($content, '-');
$content = trim($content);
if (!empty($content)) {
$model = ProductContentPoolModel::where(1)
->where('number', $i)
->find();
if (empty($model)) {
$model = new ProductContentPoolModel();
}
$model->number = $i;
$model->content = trim($content);
$model->save();
} else {
ProductContentPoolModel::where(1)
->where('number', $i)
->delete();
}
} else {
ProductContentPoolModel::where(1)
->where('number', $i)
->delete();
}
}
return $this->jsonSucc();
}
}

View File

@@ -1,361 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\ProductGroupModel;
use app\common\model\ProductModel;
use think\db\Query;
class ProductController extends BaseLoginController {
/**
* 获取列表
*
* @return \think\response\Json
*/
public function index() {
$groupId = trim($this->request->param('group_id'));
$isUsed = trim($this->request->param('is_used'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = ProductModel::where(1);
if (strlen($groupId) > 0) {
$query->where('group_id', $groupId);
}
if (isset(ProductModel::isUsedAssoc()[$isUsed])) {
$query->where('is_used', $isUsed);
}
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('title', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$images = [];
foreach ($model->images as $image) {
$images[] = array_merge($image, [
'url' => $this->absoluteUrl($image['path']),
]);
}
$list[] = array_merge($model->toArray(), [
'images' => $images,
'is_used_name' => ProductModel::isUsedAssoc()[$model->is_used],
'group_name' => isset(ProductGroupModel::assoc()[$model->group_id])
? ProductGroupModel::assoc()[$model->group_id]
: '',
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'groups' => $this->assocToList(ProductGroupModel::assoc()),
'isUseds' => $this->assocToList(ProductModel::isUsedAssoc()),
]);
}
/**
* 批量修改主题
*
* @return \think\response\Json
*/
public function theme() {
$checked = $this->request->param('checked');
$themes = $this->request->param('themes');
if (empty($checked)
OR !is_array($checked)) {
return $this->jsonFail('参数错误');
}
if (empty($themes) OR !is_array($themes)) {
$themes = [];
}
ProductModel::where(1)
->whereIn('id', $checked)
->update([
'themes' => json_encode($themes, JSON_UNESCAPED_UNICODE),
'update_time' => time(),
]);
return $this->jsonSucc();
}
/**
* 批量修改标签
*
* @return \think\response\Json
*/
public function label() {
$checked = $this->request->param('checked');
$labels = $this->request->param('labels');
if (empty($checked)
OR !is_array($checked)) {
return $this->jsonFail('参数错误');
}
if (empty($labels) OR !is_array($labels)) {
$labels = [];
}
ProductModel::where(1)
->whereIn('id', $checked)
->update([
'labels' => json_encode($labels, JSON_UNESCAPED_UNICODE),
'update_time' => time(),
]);
return $this->jsonSucc();
}
/**
* 批量修改标题
*
* @return \think\response\Json
*/
public function title() {
$checked = $this->request->param('checked');
$type = intval($this->request->param('type'));
$title = trim($this->request->param('title'));
if (empty($checked)
OR !is_array($checked)
OR !in_array($type, [0, 1])
OR empty($title)) {
return $this->jsonFail('参数错误');
}
foreach (ProductModel::where(1)
->whereIn('id', $checked)
->select() as $model) {
if ($type == 0) {
$model->title = $model->title . $title;
} else {
$model->title = $title . $model->title;
}
$model->save();
}
return $this->jsonSucc();
}
/**
* 批量修改描述
*
* @return \think\response\Json
*/
public function content() {
$checked = $this->request->param('checked');
$type = intval($this->request->param('type'));
$content = trim($this->request->param('content'));
if (empty($checked)
OR !is_array($checked)
OR !in_array($type, [0, 1])
OR empty($content)) {
return $this->jsonFail('参数错误');
}
foreach (ProductModel::where(1)
->whereIn('id', $checked)
->select() as $model) {
if ($type == 0) {
$model->content = $model->content . "\n" . $content;
} else {
$model->content = $content . "\n" . $model->content;
}
$model->save();
}
return $this->jsonSucc();
}
/**
* 批量修改库存
*
* @return \think\response\Json
*/
public function stock() {
$checked = $this->request->param('checked');
$stock = intval($this->request->param('stock'));
if (empty($checked)
OR !is_array($checked)) {
return $this->jsonFail('参数错误');
}
if ($stock <= 0) {
return $this->jsonFail('库存不可小于1');
}
ProductModel::where(1)
->whereIn('id', $checked)
->update([
'stock' => $stock,
'update_time' => time(),
]);
return $this->jsonSucc();
}
/**
* 批量修改价格
*
* @return \think\response\Json
*/
public function price() {
$checked = $this->request->param('checked');
$type = intval($this->request->param('type'));
$rate = floatval($this->request->param('rate'));
$val = floatval($this->request->param('val'));
$price = floatval($this->request->param('price'));
if (empty($checked)
OR !is_array($checked)
OR !in_array($type, [0, 1, 2])) {
return $this->jsonFail('参数错误');
}
if ($type == 0 OR $type == 1) {
if ($rate <= 0 AND $val <= 0) {
return $this->jsonFail('请输入比例或数值');
}
} elseif ($type == 2) {
if ($price <= 0) {
return $this->jsonFail('请输入金额');
}
}
foreach (ProductModel::where(1)
->whereIn('id', $checked)
->select() as $model) {
if ($type == 0) {
if ($rate > 0) {
$model->price -= $model->price * $rate / 100;
} else {
$model->price -= $val;
}
} elseif ($type == 1) {
if ($rate > 0) {
$model->price += $model->price * $rate / 100;
} else {
$model->price += $val;
}
} elseif ($type == 2) {
$model->price = $price;
}
$model->price = round($model->price, 2);
if ($model->price > 0) {
$model->save();
}
}
return $this->jsonSucc();
}
/**
* 保存
*
* @return \think\response\Json
*/
public function save() {
$id = intval($this->request->param('id'));
$groupId = intval($this->request->param('group_id'));
$title = trim($this->request->param('title'));
$content = trim($this->request->param('content'));
$cb = $this->request->param('cb');
$images = $this->request->param('images');
$labels = $this->request->param('labels');
$themes = $this->request->param('themes');
$opts = $this->request->param('opts');
$address = trim($this->request->param('address'));
$price = floatval($this->request->param('price'));
$stock = intval($this->request->param('stock'));
$shippingFee = floatval($this->request->param('shipping_fee'));
$video = trim($this->request->param('video'));
if (empty($title)
OR $groupId < 0
OR empty($content)
OR !is_array($cb)
OR !is_array($images)
OR empty($images)
OR !is_array($labels)
OR !is_array($themes)
OR !is_array($opts)
OR $price < 0
OR $stock < 0
OR $shippingFee < 0) {
return $this->jsonFail('参数错误');
}
foreach ($images as $i => $image) {
unset($images[$i]['url']);
}
if (!empty($id)) {
$model = ProductModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
} else {
$model = new ProductModel();
}
$model->group_id = $groupId;
$model->title = $title;
$model->content = $content;
$model->cb = $cb;
$model->video = $video;
$model->images = $images;
$model->labels = $labels;
$model->themes = $themes;
$model->address = $address;
$model->price = $price;
$model->stock = $stock;
$model->shipping_fee = $shippingFee;
$model->opts = $opts;
$model->save();
return $this->jsonSucc();
}
/**
* 删除
*
* @return \think\response\Json
* @throws \Exception
*/
public function delete() {
$id = intval($this->request->param('id'));
if (empty($id)) {
return $this->jsonFail('参数错误');
}
$model = ProductModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
$model->delete();
return $this->jsonSucc();
}
}

View File

@@ -1,118 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\CollectProductModel;
use app\common\model\ProductGroupModel;
use app\common\model\ProductModel;
use think\db\Query;
class ProductGroupController extends BaseLoginController {
/**
* 获取列表
*
* @return \think\response\Json
*/
public function index() {
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = ProductGroupModel::where(1);
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('name', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$list[] = array_merge($model->toArray(), [
'product_num' => $model->productNum(),
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
]);
}
/**
* 保存
*
* @return \think\response\Json
*/
public function save() {
$id = intval($this->request->param('id'));
$name = trim($this->request->param('name'));
if (empty($name)) {
return $this->jsonFail('参数错误');
}
if (!empty($id)) {
$model = ProductGroupModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
} else {
$model = new ProductGroupModel();
}
$model->name = $name;
$model->save();
return $this->jsonSucc();
}
/**
* 删除
*
* @return \think\response\Json
* @throws \Exception
*/
public function delete() {
$id = intval($this->request->param('id'));
if (empty($id)) {
return $this->jsonFail('参数错误');
}
$model = ProductGroupModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
CollectProductModel::where(1)
->where('group_id', $model->id)
->update([
'group_id' => 0,
]);
ProductModel::where(1)
->where('group_id', $model->id)
->update([
'group_id' => 0,
]);
$model->delete();
return $this->jsonSucc();
}
}

View File

@@ -1,63 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\ProductModel;
use app\common\model\ProductUseModel;
use app\common\model\TaskModel;
use app\common\task\ProductReleaseTask;
class ProductReleaseController extends BaseLoginController {
/**
* 保存
*
* @return \think\response\Json
*/
public function save() {
$devices = $this->request->param('devices');
$params = ProductReleaseTask::params($this->request);
if (empty($devices)
OR !is_array($devices)
OR is_null($params)) {
return $this->jsonFail('参数错误');
}
foreach ($devices as $deviceId) {
$pdts = [];
for ($i = 0; $i < $params['release_num']; $i ++) {
$product = array_shift($params['products']);
if (!empty($product)) {
$pdts[] = $product;
}
}
if (!empty($pdts)) {
$model = new TaskModel();
$model->device_id = $deviceId;
$model->platform = TaskModel::PLATFORM_XIANYU;
$model->type = TaskModel::TYPE_PRODUCT_RELEASE;
$model->params = array_merge($params, ['products' => $pdts]);
$model->run_type = TaskModel::RUN_TYPE_ONCE;
$model->run_time = '';
if ($model->save()) {
foreach ($pdts as $pdt) {
$useModel = new ProductUseModel();
$useModel->device_id = $deviceId;
$useModel->release_id = $model->id;
$useModel->product_id = $pdt['id'];
$useModel->use_type = $model->platform;
$useModel->save();
ProductModel::where(1)
->where('id', $pdt['id'])
->update([
'is_used' => ProductModel::IS_USED_YES,
]);
}
}
}
}
return $this->jsonSucc();
}
}

View File

@@ -1,795 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\CloneInfo;
use app\common\model\MemberModel;
use app\common\model\MemberMoneyModel;
use app\common\model\MemberQrcodeModel;
use app\common\model\MemberWithdrawModel;
use app\common\model\StatisticsModel;
use app\common\Utils;
class StatController extends BaseLoginController {
protected $chartColumns = [
'register' => '用户注册',
'qr_number' => '扫码企点号',
'qr_member' => '扫码人数',
'qrcode' => '取码次数',
'qrcode_succ' => '取码成功',
'clone_succ' => '克隆成功',
'clone_error' => '克隆错误',
'clone_timeout' => '克隆超时',
'xinyue_select' => '选择克隆心悦数',
'xinyue_clone' => '克隆成功心悦数',
'reward_clone' => '克隆奖励笔数',
'reward_invite1' => '一级奖励笔数',
'reward_invite2' => '二级奖励笔数',
'money_clone' => '克隆奖励金额',
'money_invite1' => '一级奖励金额',
'money_invite2' => '二级奖励金额',
'withdraw' => '申请提现笔数',
'withdraw_check' => '检测通过笔数',
'withdraw_money' => '申请提现金额',
'withdraw_money_check' => '检测通过金额',
'withdraw_succ' => '提现成功笔数',
'withdraw_succ_money' => '提现成功金额',
'withdraw_fail' => '提现失败笔数',
'withdraw_fail_money' => '提现失败金额',
];
public function dayChartView() {
$day = trim($this->request->param('day'));
$columns = trim($this->request->param('columns'));
$columns = explode('|', $columns);
if (empty($day) OR empty($columns)) {
exit('Page not found.');
}
$legend = [];
$xAxis = [];
$series = [];
for ($i = 0; $i <= 23; $i ++) {
$xAxis[] = $i . '时';
}
foreach ($columns as $i => $column) {
if (isset($this->chartColumns[$column])) {
$legend[] = $this->chartColumns[$column];
$series[$column] = [
'name' => $this->chartColumns[$column],
'type' => 'line',
'stack' => 'Total',
'data' => [],
];
} else {
unset($columns[$i]);
}
}
$list = $this->getDayData($day);
$rows = [];
foreach ($list as $row) {
$rows[$row['hour']] = $row;
}
foreach ($xAxis as $axi) {
if (isset($rows[$axi])) {
foreach ($columns as $column) {
$series[$column]['data'][] = $rows[$axi][$column];
}
} else {
foreach ($columns as $column) {
$series[$column]['data'][] = 0;
}
}
}
return $this->fetch('/chart-day', [
'legend' => $legend,
'xAxis' => $xAxis,
'series' => array_values($series),
]);
}
public function monthChartView() {
$month = trim($this->request->param('month'));
$columns = trim($this->request->param('columns'));
$columns = explode('|', $columns);
if (empty($month) OR empty($columns)) {
exit('Page not found.');
}
$time = strtotime($month . '01');
if ($time === FALSE) {
exit('Page not found.');
}
$days = date('t', $time);
$legend = [];
$xAxis = [];
$series = [];
for ($i = 1; $i <= $days; $i ++) {
$xAxis[] = $i . '日';
}
foreach ($columns as $i => $column) {
if (isset($this->chartColumns[$column])) {
$legend[] = $this->chartColumns[$column];
$series[$column] = [
'name' => $this->chartColumns[$column],
'type' => 'line',
'stack' => 'Total',
'data' => [],
];
} else {
unset($columns[$i]);
}
}
$list = $this->getMonthData($month);
$rows = [];
foreach ($list as $row) {
$row['day'] = str_replace(['年', '月'], '-', $row['day']);
$row['day'] = str_replace(['日'], '', $row['day']);
$row['day'] = intval(date('d', strtotime($row['day']))) . '日';
$rows[$row['day']] = $row;
}
foreach ($xAxis as $axi) {
if (isset($rows[$axi])) {
foreach ($columns as $column) {
$series[$column]['data'][] = $rows[$axi][$column];
}
} else {
foreach ($columns as $column) {
$series[$column]['data'][] = 0;
}
}
}
return $this->fetch('/chart-day', [
'legend' => $legend,
'xAxis' => $xAxis,
'series' => array_values($series),
]);
}
/**
* 日统计图表
*
* @return \think\response\Json
*/
public function dayChart() {
$day = trim($this->request->param('day'));
$columns = $this->request->param('columns');
if (empty($day)) {
$day = date('Y-m-d');
}
if (strtotime($day) === FALSE) {
return $this->jsonFail('日期错误');
}
if (empty($columns)) {
$columns = ['xinyue_select', 'xinyue_clone', 'money_clone'];
}
return $this->jsonSucc([
'day' => $day,
'columns' => $columns,
'chartColumns' => $this->assocToList($this->chartColumns),
'url' => $this->absoluteUrl('/backend/stat/dayChartView?token=' . $this->token . '&day=' . $day . '&columns=' . implode('|', $columns)),
]);
}
/**
* 月统计图表
*
* @return \think\response\Json
*/
public function monthChart() {
$month = trim($this->request->param('month'));
$columns = $this->request->param('columns');
if (empty($month)) {
$month = date('Ym');
}
if (empty($columns)) {
$columns = ['xinyue_select', 'xinyue_clone', 'money_clone'];
}
return $this->jsonSucc([
'month' => $month,
'months' => $this->assocToList($this->getMonths()),
'columns' => $columns,
'chartColumns' => $this->assocToList($this->chartColumns),
'url' => $this->absoluteUrl('/backend/stat/monthChartView?token=' . $this->token . '&month=' . $month . '&columns=' . implode('|', $columns)),
]);
}
/**
* 日统计列表
*
* @return \think\response\Json
*/
public function dayIndex() {
$day = trim($this->request->param('day'));
if (empty($day)) {
$day = date('Y-m-d');
}
if (strtotime($day) === FALSE) {
return $this->jsonFail('日期错误');
}
return $this->jsonSucc([
'list' => $this->getDayData($day),
'day' => $day,
]);
}
/**
* 月统计
*
* @return \think\response\Json
*/
public function monthIndex() {
$month = trim($this->request->param('month'));
if (empty($month)) {
$month = date('Ym');
}
return $this->jsonSucc([
'list' => $this->getMonthData($month),
'month' => $month,
'months' => $this->assocToList($this->getMonths()),
]);
}
/**
* 获取统计信息
*
* @return \think\response\Json
*/
public function get() {
$weekTime = time() - 7 * 24 * 3600;
$monthTime = time() - 30 * 24 * 3600;
$memberTotal = MemberModel::where(1)
->count();
$memberToday = MemberModel::where(1)
->where('create_time', '>=', strtotime(date('Y-m-d')))
->count();
$memberYesterday = MemberModel::where(1)
->where('create_time', '>=', strtotime(date('Y-m-d')) - 24 * 3600)
->where('create_time', '<', strtotime(date('Y-m-d')))
->count();
$memberWeek = MemberModel::where(1)
->where('create_time', '>=', $weekTime)
->count();
$memberMonth = MemberModel::where(1)
->where('create_time', '>=', $monthTime)
->count();
/*$xinyueTotal = MemberQrcodeModel::where(1)
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->sum('select_xinyue_count');
$xinyueToday = MemberQrcodeModel::where(1)
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->where('create_time', '>=', strtotime(date('Y-m-d')))
->sum('select_xinyue_count');
$xinyueYesterday = MemberQrcodeModel::where(1)
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->where('create_time', '>=', strtotime(date('Y-m-d')) - 24 * 3600)
->where('create_time', '<', strtotime(date('Y-m-d')))
->sum('select_xinyue_count');
$xinyueWeek = MemberQrcodeModel::where(1)
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->where('create_time', '>=', $weekTime)
->sum('select_xinyue_count');
$xinyueMonth = MemberQrcodeModel::where(1)
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->where('create_time', '>=', $monthTime)
->sum('select_xinyue_count');*/
$xinyueTotal = StatisticsModel::where(1)
->sum('xinyue_select');
$xinyueToday = StatisticsModel::where(1)
->where('day', date('Ymd'))
->sum('xinyue_select');
$xinyueYesterday = StatisticsModel::where(1)
->where('day', date('Ymd', time() - 24 * 3600))
->sum('xinyue_select');
$xinyueWeek = StatisticsModel::where(1)
->where('day', '>=', date('Ymd', time() - 7 * 24 * 3600))
->sum('xinyue_select');
$xinyueMonth = StatisticsModel::where(1)
->where('day', '>=', date('Ymd', time() - 30 * 24 * 3600))
->sum('xinyue_select');
$moneyTotal = MemberMoneyModel::where(1)
->where('money', '>', 0)
->where('status', MemberMoneyModel::STATUS_SUCC)
->sum('money');
$moneyToday = MemberMoneyModel::where(1)
->where('money', '>', 0)
->where('status', MemberMoneyModel::STATUS_SUCC)
->where('create_time', '>=', strtotime(date('Y-m-d')))
->sum('money');
$moneyYesterday = MemberMoneyModel::where(1)
->where('money', '>', 0)
->where('status', MemberMoneyModel::STATUS_SUCC)
->where('create_time', '>=', strtotime(date('Y-m-d')) - 24 * 3600)
->where('create_time', '<', strtotime(date('Y-m-d')))
->sum('money');
$moneyWeek = MemberMoneyModel::where(1)
->where('money', '>', 0)
->where('status', MemberMoneyModel::STATUS_SUCC)
->where('create_time', '>=', $weekTime)
->sum('money');
$moneyMonth = MemberMoneyModel::where(1)
->where('money', '>', 0)
->where('status', MemberMoneyModel::STATUS_SUCC)
->where('create_time', '>=', $monthTime)
->sum('money');
$withdrawTotal = MemberMoneyModel::where(1)
->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberMoneyModel::STATUS_SUCC, MemberMoneyModel::STATUS_AWAIT])
->sum('money');
$withdrawToday = MemberMoneyModel::where(1)
->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberMoneyModel::STATUS_SUCC, MemberMoneyModel::STATUS_AWAIT])
->where('create_time', '>=', strtotime(date('Y-m-d')))
->sum('money');
$withdrawYesterday = MemberMoneyModel::where(1)
->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberMoneyModel::STATUS_SUCC, MemberMoneyModel::STATUS_AWAIT])
->where('create_time', '>=', strtotime(date('Y-m-d')) - 24 * 3600)
->where('create_time', '<', strtotime(date('Y-m-d')))
->sum('money');
$withdrawWeek = MemberMoneyModel::where(1)
->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberMoneyModel::STATUS_SUCC, MemberMoneyModel::STATUS_AWAIT])
->where('create_time', '>=', $weekTime)
->sum('money');
$withdrawMonth = MemberMoneyModel::where(1)
->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberMoneyModel::STATUS_SUCC, MemberMoneyModel::STATUS_AWAIT])
->where('create_time', '>=', $monthTime)
->sum('money');
$remitTotal = MemberWithdrawModel::where(1)
//->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberWithdrawModel::STATUS_SUCC, MemberWithdrawModel::STATUS_AUTO_SUCC])
->sum('money');
$remitToday = MemberWithdrawModel::where(1)
//->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberWithdrawModel::STATUS_SUCC, MemberWithdrawModel::STATUS_AUTO_SUCC])
->where('verify_time', '>=', strtotime(date('Y-m-d')))
->sum('money');
$remitYesterday = MemberWithdrawModel::where(1)
//->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberWithdrawModel::STATUS_SUCC, MemberWithdrawModel::STATUS_AUTO_SUCC])
->where('verify_time', '>=', strtotime(date('Y-m-d')) - 24 * 3600)
->where('verify_time', '<', strtotime(date('Y-m-d')))
->sum('money');
$remitWeek = MemberWithdrawModel::where(1)
//->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberWithdrawModel::STATUS_SUCC, MemberWithdrawModel::STATUS_AUTO_SUCC])
->where('verify_time', '>=', $weekTime)
->sum('money');
$remitMonth = MemberWithdrawModel::where(1)
//->whereIn('type', MemberMoneyModel::withdrawTypes())
->whereIn('status', [MemberWithdrawModel::STATUS_SUCC, MemberWithdrawModel::STATUS_AUTO_SUCC])
->where('verify_time', '>=', $monthTime)
->sum('money');
return $this->jsonSucc([
'member_total' => $memberTotal,
'member_today' => $memberToday,
'member_yesterday' => $memberYesterday,
'member_week' => $memberWeek,
'member_month' => $memberMonth,
'xinyue_total' => $xinyueTotal,
'xinyue_today' => $xinyueToday,
'xinyue_yesterday' => $xinyueYesterday,
'xinyue_week' => $xinyueWeek,
'xinyue_month' => $xinyueMonth,
'money_total' => $moneyTotal,
'money_today' => $moneyToday,
'money_yesterday' => $moneyYesterday,
'money_week' => $moneyWeek,
'money_month' => $moneyMonth,
'withdraw' => round($moneyTotal + $withdrawTotal, 2),
'withdraw_total' => abs($withdrawTotal),
'withdraw_today' => abs($withdrawToday),
'withdraw_yesterday' => abs($withdrawYesterday),
'withdraw_week' => abs($withdrawWeek),
'withdraw_month' => abs($withdrawMonth),
'remit_total' => abs($remitTotal),
'remit_today' => abs($remitToday),
'remit_yesterday' => abs($remitYesterday),
'remit_week' => abs($remitWeek),
'remit_month' => abs($remitMonth),
'clone_total' => 'Loading...',
'clone_today' => '-',
'clone_today_nr' => '-',
'clone_today_r' => '-',
'clone_yesterday' => '-',
'clone_week' => '-',
'clone_month' => '-',
'clone_today_pay' => '-',
'clone_today_unpay' => '-',
]);
}
/**
* 获取克隆统计信息
*
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getClone() {
$cloneTotal = StatisticsModel::where(1)
->sum('xinyue_clone');
$cloneToday = StatisticsModel::where(1)
->where('day', date('Ymd'))
->sum('xinyue_clone');
$cloneTodayNr = StatisticsModel::where(1)
->where('day', date('Ymd'))
->sum('xinyue_clone_nr');
$cloneYesterday = StatisticsModel::where(1)
->where('day', date('Ymd', time() - 24 * 3600))
->sum('xinyue_clone');
$cloneWeek = StatisticsModel::where(1)
->where('day', '>=', date('Ymd', time() - 7 * 24 * 3600))
->sum('xinyue_clone');
$cloneMonth = StatisticsModel::where(1)
->where('day', '>=', date('Ymd', time() - 30 * 24 * 3600))
->sum('xinyue_clone');
$cloneTodayUnpay = CloneInfo::getCloneSuccUnpayByTime(strtotime(date('Ymd')), time());
$cloneTodayPay = CloneInfo::getCloneSuccPayByTime(strtotime(date('Ymd')), time());
return $this->jsonSucc([
'clone_total' => $cloneTotal,
'clone_today' => $cloneToday,
'clone_today_nr' => $cloneTodayNr,
'clone_today_r' => $cloneToday - $cloneTodayNr,
'clone_yesterday' => $cloneYesterday,
'clone_week' => $cloneWeek,
'clone_month' => $cloneMonth,
'clone_today_pay' => $cloneTodayPay,
'clone_today_unpay' => $cloneTodayUnpay,
]);
}
/**
* 获取日统计数据
*
* @param $day
* @return array
*/
protected function getDayData($day) {
$list = [];
foreach (StatisticsModel::where(1)
->where('day', date('Ymd', strtotime($day)))
->order('hour', 'DESC')
->select() as $model) {
$xinyueClone = $model->xinyue_clone;
$moneyTotal = $model->money_clone
+ $model->money_invite1
+ $model->money_invite2
+ $model->money_jm
+ $model->money_point;
$scan1CloneCount = $model->scan1_clone_count;
$scan2CloneCount = $model->scan2_clone_count;
$scan3CloneCount = $model->scan3_clone_count;
$list[] = array_merge($model->toArray(), [
'day' => date('Y年m月d日', strtotime($model->day)),
'hour' => $model->hour . '时',
'reward_price' => $xinyueClone > 0 ? round($moneyTotal / $xinyueClone, 2) : 0,
'scan1_price' => $scan1CloneCount > 0 ? round($model->scan1_money / $scan1CloneCount, 2) : 0,
'scan2_price' => $scan2CloneCount > 0 ? round($model->scan2_money / $scan2CloneCount, 2) : 0,
'scan3_price' => $scan3CloneCount > 0 ? round($model->scan3_money / $scan3CloneCount, 2) : 0,
]);
}
return $list;
}
/**
* 获取月份
*
* @return array
*/
protected function getMonths() {
$begin = 20240301;
$months = [];
$nowTime = time();
while ($nowTime >= strtotime($begin)) {
if (!isset($months[date('Ym', $nowTime)])) {
$months[date('Ym', $nowTime)] = date('Y年m月', $nowTime);
}
$nowTime -= 24 * 3600;
}
return $months;
}
/**
* 获取月统计数据
*
* @param $month
* @return array|\think\response\Json
*/
protected function getMonthData($month) {
$time = strtotime($month . '01');
if ($time === FALSE) {
return $this->jsonFail('月份错误');
}
$today = date('Ymd');
$days = [];
for ($i = date('t', $time); $i >= 1; $i --) {
$day = date('Ymd', strtotime(date('Y', $time) . '-' . date('m', $time) . '-' . $i));
if ($day <= $today) {
$days[] = $day;
}
}
$list = [];
foreach ($days as $day) {
$row = [
'day' => date('Y年m月d日', strtotime($day)),
'register' => 0,
'qr_number' => 0,
'qr_member' => 0,
'qrcode' => 0,
'qrcode_succ' => 0,
'clone_succ' => 0,
'clone_error' => 0,
'clone_timeout' => 0,
'xinyue_select' => 0,
'xinyue_clone' => 0,
'reward_clone' => 0,
'reward_invite1' => 0,
'reward_invite2' => 0,
'money_clone' => 0,
'money_invite1' => 0,
'money_invite2' => 0,
'money_jm' => 0,
'money_point' => 0,
'withdraw' => 0,
'withdraw_check' => 0,
'withdraw_money' => 0,
'withdraw_money_check' => 0,
'withdraw_succ' => 0,
'withdraw_succ_money' => 0,
'withdraw_fail' => 0,
'withdraw_fail_money' => 0,
'scan1_num' => 0,
'scan2_num' => 0,
'scan3_num' => 0,
'scan1_xinyue_count' => 0,
'scan2_xinyue_count' => 0,
'scan3_xinyue_count' => 0,
'scan1_clone_count' => 0,
'scan2_clone_count' => 0,
'scan3_clone_count' => 0,
'scan1_money' => 0,
'scan2_money' => 0,
'scan3_money' => 0,
'scan1_price' => 0,
'scan2_price' => 0,
'scan3_price' => 0,
'first_settlement_count' => 0,
'repeat_settlement_count' => 0,
'first_reward_money' => 0,
'repeat_reward_money' => 0,
'first_clone_succ' => 0,
'repeat_clone_succ' => 0,
'first_price' => 0,
'repeat_price' => 0,
];
foreach (StatisticsModel::where(1)
->where('day', $day)
->select() as $model) {
$row['register'] += $model->register;
$row['qr_number'] += $model->qr_number;
$row['qr_member'] += $model->qr_member;
$row['qrcode'] += $model->qrcode;
$row['qrcode_succ'] += $model->qrcode_succ;
$row['clone_succ'] += $model->clone_succ;
$row['clone_error'] += $model->clone_error;
$row['clone_timeout'] += $model->clone_timeout;
$row['xinyue_select'] += $model->xinyue_select;
$row['xinyue_clone'] += $model->xinyue_clone;
$row['reward_clone'] += $model->reward_clone;
$row['reward_invite1'] += $model->reward_invite1;
$row['reward_invite2'] += $model->reward_invite2;
$row['money_clone'] += $model->money_clone;
$row['money_invite1'] += $model->money_invite1;
$row['money_invite2'] += $model->money_invite2;
$row['money_jm'] += $model->money_jm;
$row['money_point'] += $model->money_point;
$row['withdraw'] += $model->withdraw;
$row['withdraw_check'] += $model->withdraw_check;
$row['withdraw_money'] += $model->withdraw_money;
$row['withdraw_money_check'] += $model->withdraw_money_check;
$row['withdraw_succ'] += $model->withdraw_succ;
$row['withdraw_succ_money'] += $model->withdraw_succ_money;
$row['withdraw_fail'] += $model->withdraw_fail;
$row['withdraw_fail_money'] += $model->withdraw_fail_money;
$row['first_settlement_count'] += $model->first_settlement_count;
$row['repeat_settlement_count'] += $model->repeat_settlement_count;
$row['first_reward_money'] += $model->first_reward_money;
$row['repeat_reward_money'] += $model->repeat_reward_money;
$row['first_clone_succ'] += $model->first_clone_succ;
$row['repeat_clone_succ'] += $model->repeat_clone_succ;
/*$row['scan1_num'] += $model->scan1_num;
$row['scan2_num'] += $model->scan2_num;
$row['scan3_num'] += $model->scan3_num;
$row['scan1_xinyue_count'] += $model->scan1_xinyue_count;
$row['scan2_xinyue_count'] += $model->scan2_xinyue_count;
$row['scan3_xinyue_count'] += $model->scan3_xinyue_count;
$row['scan1_clone_count'] += $model->scan1_clone_count;
$row['scan2_clone_count'] += $model->scan2_clone_count;
$row['scan3_clone_count'] += $model->scan3_clone_count;
$row['scan1_money'] += $model->scan1_money;
$row['scan2_money'] += $model->scan2_money;
$row['scan3_money'] += $model->scan3_money;*/
}
$xinyueClone = $row['xinyue_clone'];
$moneyTotal = $row['money_clone']
+ $row['money_invite1']
+ $row['money_invite2']
+ $row['money_jm']
+ $row['money_point'];
//$scan1CloneCount = $row['scan1_clone_count'];
//$scan2CloneCount = $row['scan2_clone_count'];
//$scan3CloneCount = $row['scan3_clone_count'];
$row['reward_price'] = $xinyueClone > 0 ? round($moneyTotal / $xinyueClone, 2) : 0;
//$row['scan1_price'] = $scan1CloneCount > 0 ? round($row['scan1_money'] / $scan1CloneCount, 2) : 0;
//$row['scan2_price'] = $scan2CloneCount > 0 ? round($row['scan2_money'] / $scan2CloneCount, 2) : 0;
//$row['scan3_price'] = $scan3CloneCount > 0 ? round($row['scan3_money'] / $scan3CloneCount, 2) : 0;
$row['first_reward_money'] = round($row['first_reward_money'], 2);
$row['repeat_reward_money'] = round($row['repeat_reward_money'], 2);
if ($row['first_clone_succ'] > 0) {
$row['first_price'] = round($row['first_reward_money'] / $row['first_clone_succ'], 2);
}
if ($row['repeat_clone_succ'] > 0) {
$row['repeat_price'] = round($row['repeat_reward_money'] / $row['repeat_clone_succ'], 2);
}
$list[] = $row;
}
return $list;
}
public function scan() {
$time = time();// - 24 * 3600;
$ids0 = MemberQrcodeModel::where(1)
->field('id')
->where('create_time', '>=', strtotime(date('Ymd', $time)))
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->where('settlement_count1', '>', 0)
->select()
->column('id');
$ids1 = MemberQrcodeModel::where(1)
->field('id')
->where('create_time', '>=', strtotime(date('Ymd', $time)))
->where('status', MemberQrcodeModel::STATUS_SUCCESS)
->where('settlement_count2', '>', 0)
->select()
->column('id');
$money0 = 0;
$price0 = 0;
$xinyue0 = 0;
$cloneSucc0 = 0;
$cloneFailRate0 = 0;
if (!empty($ids0)) {
$money0 = MemberQrcodeModel::where(1)
->field('reward_price * reward_count AS reward_money0')
->whereIn('id', $ids0)
->select()
->reduce(function ($money, $item) {
return $money + $item['reward_money0'];
});
$xinyue0 = MemberQrcodeModel::where(1)
->whereIn('id', $ids0)
->sum('settlement_count1');
$qrIds0 = MemberQrcodeModel::where(1)
->field('qr_id')
->whereIn('id', $ids0)
->where('qr_id', '>', 0)
->select()
->column('qr_id');
if (!empty($qrIds0)) {
$cloneSucc0 = CloneInfo::getCloneSuccCount1ByQrIds($qrIds0);
}
if ($xinyue0 > 0) {
$cloneFailRate0 = round(($xinyue0 - $cloneSucc0) / $xinyue0, 4) * 100;
}
}
$money1 = 0;
$price1 = 0;
$xinyue1 = 0;
$cloneSucc1 = 0;
$cloneFailRate1 = 0;
if (!empty($ids1)) {
$money1 = MemberQrcodeModel::where(1)
->field('reward_price2 * reward_count2 AS reward_money0')
->whereIn('id', $ids1)
->select()
->reduce(function ($money, $item) {
return $money + $item['reward_money0'];
});
$xinyue1 = MemberQrcodeModel::where(1)
->whereIn('id', $ids1)
->sum('settlement_count2');
if ($cloneSucc1 > 0) {
$price1 = floatval(round($money1 / $cloneSucc1, 2));
}
$qrIds1 = MemberQrcodeModel::where(1)
->field('qr_id')
->whereIn('id', $ids1)
->where('qr_id', '>', 0)
->select()
->column('qr_id');
if (!empty($qrIds1)) {
$cloneSucc1 = CloneInfo::getCloneSuccCount2ByQrIds($qrIds1);
}
if ($xinyue1 > 0) {
$cloneFailRate1 = round(($xinyue1 - $cloneSucc1) / $xinyue1, 4) * 100;
}
}
$total = floatval(MemberMoneyModel::where(1)
->where('create_time', '>=', strtotime(date('Ymd', $time)))
->whereIn('type', MemberMoneyModel::rewardTypes())
->sum('money'));
Utils::allocNumber($total, $money0, $money1);
if ($cloneSucc0 > 0) {
$price0 = floatval(round($money0 / $cloneSucc0, 2));
}
if ($cloneSucc1 > 0) {
$price1 = floatval(round($money1 / $cloneSucc1, 2));
}
return $this->jsonSucc([
'scan0' => [
'num' => count($ids0),
'money' => floatval(round($money0, 2)),
'price' => $price0,
'xinyue' => $xinyue0,
'clone_succ' => $cloneSucc0,
'rate_clone_fail' => $cloneFailRate0,
],
'scan1' => [
'num' => count($ids1),
'money' => floatval(round($money1, 2)),
'price' => $price1,
'xinyue' => $xinyue1,
'clone_succ' => $cloneSucc1,
'rate_clone_fail' => $cloneFailRate1,
],
]);
}
}

View File

@@ -1,228 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\DeviceModel;
use app\common\model\LogModel;
use app\common\model\TaskDetailModel;
use app\common\model\TaskModel;
class TaskController extends BaseLoginController {
/**
* 获取列表
*
* @return \think\response\Json
*/
public function index() {
$deviceId = intval($this->request->param('device_id'));
$type = trim($this->request->param('type'));
$runType = trim($this->request->param('run_type'));
$status = trim($this->request->param('status'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = TaskModel::where(1)
->alias('t1')
->field('t1.*')
->leftJoin(
DeviceModel::where(1)->getTable() . ' t2',
't2.id = t1.device_id')
->where('t1.is_deleted', TaskModel::IS_DELETED_NO);
if (!empty($deviceId)) {
$query->where('t1.device_id', $deviceId);
}
if (isset(TaskModel::typeAssoc()[$type])) {
$query->where('t1.type', $type);
}
if (isset(TaskModel::statusAssoc()[$status])) {
$query->where('t1.status', $status);
}
if (isset(TaskModel::runTypeAssoc()[$runType])) {
$query->where('t1.run_type', $runType);
}
if (!empty($keywords)) {
$query->whereLike('t2.number', '%' . $keywords . '%');
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('t1.id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$device = $model->device();
$list[] = array_merge($model->toArray(), [
'device_number' => $device ? $device->number : '',
'device_name' => $device ? $device->name : '',
'device_online' => ($device AND $device->isOnline()) ? '在线' : '离线',
'type_name' => TaskModel::typeAssoc()[$model->type],
'run_type_name' => TaskModel::runTypeAssoc()[$model->run_type],
'status_name' => TaskModel::statusAssoc()[$model->status],
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'platforms' => $this->assocToList(TaskModel::platformAssoc()),
'types' => $this->assocToList(TaskModel::typeAssoc()),
'runTypes' => $this->assocToList(TaskModel::runTypeAssoc()),
'statuses' => $this->assocToList(TaskModel::statusAssoc()),
]);
}
/**
* 日志
*
* @return \think\response\Json
*/
public function log() {
$id = intval($this->request->param('id'));
if (empty($id)) {
return $this->jsonFail('参数错误');
}
$detail = TaskDetailModel::where(1)
->where('task_id', $id)
->order('id', 'DESC')
->find();
if (empty($detail)) {
return $this->jsonSucc([]);
}
$list = [];
foreach (LogModel::where(1)
->where('task_id', $detail->id)
->order('id', 'ASC')
->select() as $model) {
$list[] = [
'id' => $model->id,
'type' => $model->type,
'message' => $model->message,
'create_time' => $model->create_time,
];
}
return $this->jsonSucc($list);
}
/**
* 保存
*
* @return \think\response\Json
*/
public function save() {
$devices = $this->request->param('devices');
$platform = trim($this->request->param('platform'));
$type = trim($this->request->param('type'));
$runType = trim($this->request->param('run_type'));
$runTime = trim($this->request->param('run_time'));
if (empty($devices)
OR !is_array($devices)
OR !isset(TaskModel::platformAssoc()[$platform])
OR !isset(TaskModel::typeAssoc()[$type])
OR !isset(TaskModel::runTypeAssoc()[$runType])) {
return $this->jsonFail('参数错误');
}
$timeTypes = [TaskModel::RUN_TYPE_TIMER, TaskModel::RUN_TYPE_DAILY];
if (in_array($runType, $timeTypes)
AND empty($runTime)) {
return $this->jsonFail('参数错误');
}
$params = call_user_func_array(TaskModel::taskClasses()[$type] . '::params', [$this->request]);
if (is_null($params)) {
return $this->jsonFail('参数错误');
}
foreach ($devices as $deviceId) {
$model = new TaskModel();
$model->platform = $platform;
$model->type = $type;
$model->device_id = $deviceId;
$model->params = $params;
$model->run_type = $runType;
$model->run_time = in_array($runType, $timeTypes) ? $runTime : '';
$model->save();
}
return $this->jsonSucc();
}
/**
* 删除
*
* @return \think\response\Json
*/
public function delete() {
$id = intval($this->request->param('id'));
if (empty($id)) {
return $this->jsonFail('参数错误');
}
$model = TaskModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
TaskDetailModel::where(1)
->where('task_id', $model->id)
->update([
'is_deleted' => TaskModel::IS_DELETED_YES,
]);
$model->is_deleted = TaskModel::IS_DELETED_YES;
$model->save();
return $this->jsonSucc();
}
/**
* 批量删除
*
* @return \think\response\Json
*/
public function batchDelete() {
$ids = $this->request->param('ids');
if (empty($ids) OR !is_array($ids)) {
return $this->jsonFail('参数错误');
}
TaskDetailModel::where(1)
->whereIn('task_id', $ids)
->update([
'is_deleted' => TaskModel::IS_DELETED_YES,
]);
TaskModel::where(1)
->whereIn('id', $ids)
->update([
'is_deleted' => TaskModel::IS_DELETED_YES,
]);
return $this->jsonSucc();
}
/**
* 获取执行方式
*
* @return \think\response\Json
*/
public function runTypeAssoc() {
return $this->jsonSucc($this->assocToList(TaskModel::runTypeAssoc()));
}
}

View File

@@ -1,54 +0,0 @@
<?php
namespace app\backend\controller;
class UploadController extends BaseLoginController {
// 上传图片
public function index() {
if (!empty($_FILES)
AND !empty($_FILES['file'])
AND is_uploaded_file($_FILES['file']['tmp_name'])) {
$ext = strtolower(trim(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)));
$dir = ROOT_PATH . DS . 'public' . DS;
$path = 'upload/' . $ext . '/' . date('Y-m-d') . '/' . time() . '-' . uniqid() . '.' . $ext;
if (is_dir(dirname($dir . $path)) OR @mkdir(dirname($dir . $path), 0777, TRUE)) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $dir . $path)) {
return json([
'name' => $_FILES['file']['name'],
'path' => $path,
'url' => $this->absoluteUrl($path),
], 200);
}
}
}
if (!empty($_FILES)) {
return json([
'url' => '',
], 500);
}
}
public function editor() {
if (!empty($_FILES)
AND !empty($_FILES['upload'])
AND is_uploaded_file($_FILES['upload']['tmp_name'])) {
$ext = strtolower(trim(pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION)));
$dir = ROOT_PATH . DS . 'public' . DS;
$path = 'upload/' . $ext . '/' . date('Y-m-d') . '/' . time() . '-' . uniqid() . '.' . $ext;
if (is_dir(dirname($dir . $path)) OR @mkdir(dirname($dir . $path), 0777, TRUE)) {
if (move_uploaded_file($_FILES['upload']['tmp_name'], $dir . $path)) {
return json([
'uploaded' => true,
'url' => $this->absoluteUrl($path),
]);
}
}
}
return json([
'uploaded' => false,
'url' => '',
]);
}
}

View File

@@ -1,114 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\UserModel;
use app\common\model\UserTokenModel;
class UserController extends BaseController {
/**
* 登录
*
* @return \think\response\Json
*/
public function login() {
$username = trim($this->request->param('username'));
$password = trim($this->request->param('password'));
if (empty($username)
OR empty($password)) {
return $this->jsonFail('参数错误');
}
$user = UserModel::get([
'username' => $username,
'password' => md5($password),
]);
if (empty($user)) {
return $this->jsonFail('账号/密码错误');
}
if ($user->status != UserModel::STATUS_ACTIVE) {
return $this->jsonFail('账号不可用');
}
$tokenModel = new UserTokenModel();
$tokenModel->token = md5(time() . uniqid());
$tokenModel->user_id = $user->id;
if ($tokenModel->save()) {
$user->login_time = time();
$user->login_count += 1;
$user->login_ip = $this->request->ip();
if ($user->save()) {
return $this->jsonSucc([
'logged' => TRUE,
'token' => $tokenModel->token,
'token_expired' => 365 * 24 * 3600,
'user' => $this->userJson($user),
]);
}
}
return $this->jsonFail('登录失败');
}
/**
* 获取当前登录用户
*
* @return \think\response\Json
*/
public function get() {
if (!empty($this->userModel)) {
return $this->jsonSucc([
'logged' => TRUE,
'user' => $this->userJson($this->userModel),
]);
} else {
return $this->jsonSucc([
'logged' => FALSE,
'user' => NULL,
]);
}
}
/**
* 设置密码
*
* @return \think\response\Json
*/
public function password() {
if (empty($this->userModel)) {
return $this->jsonFail('未登录');
}
$oldPassword = trim($this->request->param('oldPassword'));
$newPassword = trim($this->request->param('newPassword'));
if (empty($oldPassword)
OR empty($newPassword)
OR strlen($newPassword) < 6
OR strlen($newPassword) > 16) {
return $this->jsonFail('参数错误');
}
if (md5($oldPassword) != $this->userModel->password) {
return $this->jsonFail('原密码输入错误');
}
$this->userModel->password = md5($newPassword);
$this->userModel->save();
return $this->jsonSucc([]);
}
/**
* 登出
*
* @return \think\response\Json
*/
public function logout() {
if (!empty($this->tokenModel)) {
$this->tokenModel->delete();
}
return $this->jsonSucc([]);
}
}

View File

@@ -1,85 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\XianyuModel;
use think\db\Query;
class XianyuController extends BaseLoginController {
/**
* 闲鱼列表
*
* @return \think\response\Json
*/
public function index() {
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = XianyuModel::where(1);
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('username', '%' . $keywords . '%', 'OR');
$q->whereLike('nickname', '%' . $keywords . '%', 'OR');
$q->whereLike('remark', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$list[] = array_merge($model->toArray(), [
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
]);
}
public function update() {
}
/**
* 备注
*
* @return \think\response\Json
*/
public function remark() {
$id = intval($this->request->param('id'));
$remark = trim($this->request->param('remark'));
if (empty($id)) {
return $this->jsonFail('参数错误');
}
$model = XianyuModel::get($id);
if (empty($model)) {
return $this->jsonFail('对象未找到');
}
$model->remark = $remark;
$model->save();
return $this->jsonSucc();
}
}

View File

@@ -1,73 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\DeviceModel;
use app\common\model\XianyuProductModel;
use app\common\model\XianyuModel;
use think\db\Query;
class XianyuProductController extends BaseLoginController {
/**
* 列表
*
* @return \think\response\Json
*/
public function index() {
$username = trim($this->request->param('username'));
$onSale = trim($this->request->param('on_sale'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = XianyuProductModel::where(1);
if (!empty($username)) {
$query->where('username', $username);
}
if (isset(XianyuProductModel::onSaleAssoc()[$onSale])) {
$query->where('on_sale', $onSale);
}
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('title', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$device = DeviceModel::get($model->device_id);
$xianyu = XianyuModel::get(['username' => $model->username]);
$list[] = array_merge($model->toArray(), [
'device_number' => $device ? $device->number : '',
'on_sale_name' => XianyuProductModel::onSaleAssoc()[$model->on_sale],
'nickname' => $xianyu ? $xianyu->nickname : '',
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'xianyus' => $this->assocToList(XianyuModel::assoc()),
'onSales' => $this->assocToList(XianyuProductModel::onSaleAssoc()),
]);
}
}

View File

@@ -1,66 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\DeviceModel;
use app\common\model\XianyuModel;
use app\common\model\XianyuShopModel;
use think\db\Query;
class XianyuShopController extends BaseLoginController {
/**
* 列表
*
* @return \think\response\Json
*/
public function index() {
$username = trim($this->request->param('username'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = XianyuShopModel::where(1);
if (!empty($username)) {
$query->where('username', $username);
}
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
//$q->whereLike('title', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$device = DeviceModel::get($model->device_id);
$xianyu = XianyuModel::get(['username' => $model->username]);
$list[] = array_merge($model->toArray(), [
'device_number' => $device ? $device->number : '',
'nickname' => $xianyu ? $xianyu->nickname : '',
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'xianyus' => $this->assocToList(XianyuModel::assoc()),
]);
}
}

View File

@@ -1,73 +0,0 @@
<?php
namespace app\backend\controller;
use app\common\model\DeviceModel;
use app\common\model\XianyuModel;
use app\common\model\XianyuShopProductModel;
use think\db\Query;
class XianyuShopProductController extends BaseLoginController {
/**
* 列表
*
* @return \think\response\Json
*/
public function index() {
$username = trim($this->request->param('username'));
$pay = trim($this->request->param('pay'));
$keywords = trim($this->request->param('keywords'));
$pageNo = intval($this->request->param('page'));
$pageSize = intval($this->request->param('pageSize'));
if ($pageNo <= 0) {
$pageNo = 1;
}
if ($pageSize <= 0) {
$pageSize = 30;
}
$query = XianyuShopProductModel::where(1);
if (!empty($username)) {
$query->where('username', $username);
}
if (isset(XianyuShopProductModel::payAssoc()[$pay])) {
$query->where('pay', $pay);
}
if (!empty($keywords)) {
$query->where(function (Query $q) use ($keywords) {
$q->whereLike('title', '%' . $keywords . '%', 'OR');
});
}
$totalCount = $query->count();
$pageCount = $totalCount > 0 ? ceil($totalCount / $pageSize) : 1;
if ($pageNo > $pageCount) {
$pageNo = $pageCount;
}
$query->order('id', 'DESC');
$query->limit(($pageNo - 1) * $pageSize, $pageSize);
$list = [];
foreach ($query->select() as $model) {
$device = DeviceModel::get($model->device_id);
$xianyu = XianyuModel::get(['username' => $model->username]);
$list[] = array_merge($model->toArray(), [
'device_number' => $device ? $device->number : '',
'pay_name' => XianyuShopProductModel::payAssoc()[$model->pay],
'nickname' => $xianyu ? $xianyu->nickname : '',
]);
}
return $this->jsonSucc([
'list' => $list,
'page' => $pageNo,
'pageCount' => $pageCount,
'totalCount' => $totalCount,
'xianyus' => $this->assocToList(XianyuModel::assoc()),
'pays' => $this->assocToList(XianyuShopProductModel::payAssoc()),
]);
}
}

View File

@@ -1,54 +0,0 @@
<?php
namespace app\backend\model;
class TimeRangeModel {
/**
* 获取年
*
* @return array
*/
static public function getYears() {
$years = [];
$timeS = strtotime(date('Y') . '-01-01');
$timeE = strtotime((date('Y', $timeS) + 1) . '-01-01') - 1;
for ($i = 0; $i > -3; $i --) {
$years[] = [
'key' => $i,
'label' => date('Y 年', $timeS),
'timeS' => $timeS,
'timeE' => $timeE,
];
$timeE = $timeS - 1;
$timeS = strtotime(date('Y', $timeE) . '-01-01');
}
return $years;
}
/**
* 获取月份
*
* @return array
*/
static public function getMonths() {
$months = [];
$timeS = strtotime(date('Y-m') . '-01');
$timeE = $timeS + date('t', $timeS) * 24 * 3600 - 1;
for ($i = 0; $i > -24; $i --) {
$months[] = [
'key' => $i,
'label' => date('Y 年 m 月', $timeS) . ' (' . date('Y.m.d', $timeS) . '-' . date('Y.m.d', $timeE) . ')',
'timeS' => $timeS,
'timeE' => $timeE,
];
$timeS = strtotime(date('Y-m', $timeS - 1) . '-01');
$timeE = $timeS + date('t', $timeS) * 24 * 3600 - 1;
}
return $months;
}
}

0
Server/application/command.php Executable file → Normal file
View File

View File

@@ -0,0 +1,50 @@
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\facade\Config;
class InitDatabase extends Command
{
protected function configure()
{
$this->setName('init:database')
->setDescription('初始化数据库,创建必要的表结构');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('开始初始化数据库...');
try {
// 读取SQL文件
$sqlFile = app()->getAppPath() . 'common/database/tk_users.sql';
if (!file_exists($sqlFile)) {
$output->error('SQL文件不存在: ' . $sqlFile);
return;
}
$sql = file_get_contents($sqlFile);
// 分割SQL语句
$sqlArr = explode(';', $sql);
// 执行SQL语句
foreach ($sqlArr as $statement) {
$statement = trim($statement);
if ($statement) {
Db::execute($statement);
$output->writeln('执行SQL: ' . mb_substr($statement, 0, 100) . '...');
}
}
$output->info('数据库初始化完成!');
} catch (\Exception $e) {
$output->error('数据库初始化失败: ' . $e->getMessage());
}
}
}

0
Server/application/common.php Executable file → Normal file
View File

0
Server/application/common/AliyunSMS.php Executable file → Normal file
View File

0
Server/application/common/command/BaseCommand.php Executable file → Normal file
View File

0
Server/application/common/command/TestCommand.php Executable file → Normal file
View File

View File

@@ -0,0 +1,16 @@
<?php
// common模块路由配置
use think\facade\Route;
// 定义RESTful风格的API路由 - 认证相关
Route::group('v1/auth', function () {
// 无需认证的接口
Route::post('login', 'app\\common\\controller\\Auth@login'); // 账号密码登录
Route::post('mobile-login', 'app\\common\\controller\\Auth@mobileLogin'); // 手机号验证码登录
Route::post('code', 'app\\common\\controller\\Auth@sendCode'); // 发送验证码
// 需要JWT认证的接口
Route::get('info', 'app\\common\\controller\\Auth@info')->middleware(['jwt']); // 获取用户信息
Route::post('refresh', 'app\\common\\controller\\Auth@refresh')->middleware(['jwt']); // 刷新令牌
});

View File

@@ -0,0 +1,168 @@
<?php
namespace app\common\controller;
use app\common\helper\ResponseHelper;
use app\common\service\AuthService;
use think\Controller;
use think\facade\Request;
/**
* 认证控制器
* 处理用户登录和身份验证
*/
class Auth extends Controller
{
/**
* 允许跨域请求的域名
* @var string
*/
protected $allowOrigin = '*';
/**
* 认证服务实例
* @var AuthService
*/
protected $authService;
/**
* 初始化
* 设置跨域相关响应头
*/
public function initialize()
{
parent::initialize();
// 允许跨域访问
header('Access-Control-Allow-Origin: ' . $this->allowOrigin);
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
// 预检请求直接返回200
if (Request::method(true) == 'OPTIONS') {
exit();
}
// 初始化认证服务
$this->authService = new AuthService();
}
/**
* 用户登录
* @return \think\response\Json
*/
public function login()
{
// 获取登录参数
$params = Request::only(['username', 'password', 'is_encrypted']);
// 参数验证
$validate = validate('common/Auth');
if (!$validate->scene('login')->check($params)) {
return ResponseHelper::error($validate->getError());
}
try {
// 判断密码是否已加密
$isEncrypted = isset($params['is_encrypted']) && $params['is_encrypted'] === true;
// 调用登录服务
$result = $this->authService->login(
$params['username'],
$params['password'],
Request::ip(),
$isEncrypted
);
return ResponseHelper::success($result, '登录成功');
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage());
}
}
/**
* 手机号验证码登录
* @return \think\response\Json
*/
public function mobileLogin()
{
// 获取登录参数
$params = Request::only(['mobile', 'code', 'is_encrypted']);
// 参数验证
$validate = validate('common/Auth');
if (!$validate->scene('mobile_login')->check($params)) {
return ResponseHelper::error($validate->getError());
}
try {
// 判断验证码是否已加密
$isEncrypted = isset($params['is_encrypted']) && $params['is_encrypted'] === true;
// 调用手机号登录服务
$result = $this->authService->mobileLogin(
$params['mobile'],
$params['code'],
Request::ip(),
$isEncrypted
);
return ResponseHelper::success($result, '登录成功');
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage());
}
}
/**
* 发送验证码
* @return \think\response\Json
*/
public function sendCode()
{
// 获取参数
$params = Request::only(['mobile', 'type']);
// 参数验证
$validate = validate('common/Auth');
if (!$validate->scene('send_code')->check($params)) {
return ResponseHelper::error($validate->getError());
}
try {
// 调用发送验证码服务
$result = $this->authService->sendLoginCode(
$params['mobile'],
$params['type']
);
return ResponseHelper::success($result, '验证码发送成功');
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage());
}
}
/**
* 获取用户信息
* @return \think\response\Json
*/
public function info()
{
try {
$result = $this->authService->getUserInfo(request()->userInfo);
return ResponseHelper::success($result);
} catch (\Exception $e) {
return ResponseHelper::unauthorized($e->getMessage());
}
}
/**
* 刷新令牌
* @return \think\response\Json
*/
public function refresh()
{
try {
$result = $this->authService->refreshToken(request()->userInfo);
return ResponseHelper::success($result, '刷新成功');
} catch (\Exception $e) {
return ResponseHelper::unauthorized($e->getMessage());
}
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace app\common\helper;
class ResponseHelper
{
/**
* 成功响应
* @param mixed $data 响应数据
* @param string $msg 响应消息
* @param int $code 响应代码
* @return \think\response\Json
*/
public static function success($data = null, $msg = '操作成功', $code = 200)
{
return json([
'code' => $code,
'msg' => $msg,
'data' => $data
]);
}
/**
* 错误响应
* @param string $msg 错误消息
* @param int $code 错误代码
* @param mixed $data 错误数据
* @return \think\response\Json
*/
public static function error($msg = '操作失败', $code = 400, $data = null)
{
return json([
'code' => $code,
'msg' => $msg,
'data' => $data
]);
}
/**
* 未授权响应
* @param string $msg 错误消息
* @return \think\response\Json
*/
public static function unauthorized($msg = '未授权访问')
{
return self::error($msg, 401);
}
/**
* 禁止访问响应
* @param string $msg 错误消息
* @return \think\response\Json
*/
public static function forbidden($msg = '禁止访问')
{
return self::error($msg, 403);
}
}

View File

@@ -1,11 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class DeviceGroupModel extends Model {
}

View File

@@ -1,37 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class ProductGroupModel extends Model {
/**
* 获取关联数组
*
* @return array
*/
static public function assoc() {
$assoc = NULL;
if (is_null($assoc)) {
$assoc = [];
foreach (static::where(1)
->order('id', 'DESC')
->select() as $model) {
$assoc[$model->getAttr('id')] = $model->getAttr('name');
}
}
return $assoc;
}
/**
* 商品数量
*
* @return ProductModel
*/
public function productNum() {
return ProductModel::where(1)
->where('group_id', $this->getAttr('id'))
->count();
}
}

View File

@@ -1,26 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class ProductModel extends Model {
const IS_USED_NO = 0;
const IS_USED_YES = 10;
protected $json = ['cb', 'images', 'labels', 'themes', 'opts'];
protected $jsonAssoc = TRUE;
/**
* 获取是否使用
*
* @return string[]
*/
static public function isUsedAssoc() {
return [
static::IS_USED_NO => '未使用',
static::IS_USED_YES => '已使用',
];
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class TaskDetailModel extends Model {
const STATUS_AWAIT = 0;
const STATUS_RUNNING = 10;
const STATUS_SUCC = 20;
const STATUS_FAIL = 99;
protected $json = ['params', 'info'];
protected $jsonAssoc = TRUE;
}

View File

@@ -1,159 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class TaskModel extends Model {
const PLATFORM_XIANYU = 'XIANYU';
const TYPE_PRODUCT_RELEASE = 'PRODUCT_RELEASE';
const TYPE_PRODUCT_POLISH = 'PRODUCT_POLISH';
const TYPE_PRODUCT_ON = 'PRODUCT_ON';
const TYPE_PRODUCT_OFF = 'PRODUCT_OFF';
const TYPE_COIN_SIGN = 'COIN_SIGN';
const TYPE_COIN_DEDUCT = 'COIN_DEDUCT';
const TYPE_PRICE_CUT = 'PRICE_CUT';
const TYPE_COMMENT_REMOVE = 'COMMENT_REMOVE';
const TYPE_PRODUCT_RE_RELEASE = 'PRODUCT_RE_RELEASE';
const TYPE_RAISE_XY = 'RAISE_XY';
const TYPE_RAISE_XY_TZ = 'RAISE_XY_TZ';
const TYPE_RAISE_XY_UNITY = 'RAISE_XY_UNITY';
const TYPE_SYNC_USER = 'SYNC_USER';
const TYPE_SYNC_SHOP = 'SYNC_SHOP';
const TYPE_UPDATE_USER = 'UPDATE_USER';
const TYPE_PRODUCT_WELFARE = 'PRODUCT_WELFARE';
const TYPE_MESSAGE_REPLY = 'MESSAGE_REPLY';
const RUN_TYPE_ONCE = 'ONCE';
const RUN_TYPE_TIMER = 'TIMER';
const RUN_TYPE_DAILY = 'DAILY';
const STATUS_AWAIT = 0;
const STATUS_ALLOC = 10;
const STATUS_COMPLETE = 20;
const IS_DELETED_NO = 0;
const IS_DELETED_YES = 10;
protected $json = ['params'];
protected $jsonAssoc = TRUE;
/**
* 获取类型
*
* @return string[]
*/
static public function typeAssoc() {
return [
static::TYPE_PRODUCT_RELEASE => '[闲鱼]发布商品',
static::TYPE_PRODUCT_POLISH => '[闲鱼]擦亮商品',
static::TYPE_PRODUCT_ON => '[闲鱼]上架商品',
static::TYPE_PRODUCT_OFF => '[闲鱼]下架商品',
static::TYPE_COIN_SIGN => '[闲鱼]签到鱼币',
static::TYPE_COIN_DEDUCT => '[闲鱼]鱼币抵扣',
static::TYPE_PRICE_CUT => '[闲鱼]一键降价',
static::TYPE_COMMENT_REMOVE => '[闲鱼]删除留言',
static::TYPE_PRODUCT_RE_RELEASE => '[闲鱼]编辑重复',
static::TYPE_RAISE_XY => '[闲鱼]养号',
static::TYPE_RAISE_XY_UNITY => '[闲鱼]互助养号',
static::TYPE_RAISE_XY_TZ => '[闲鱼]会玩养号',
static::TYPE_SYNC_USER => '[闲鱼]采集账号信息',
static::TYPE_SYNC_SHOP => '[闲鱼]采集店铺信息',
static::TYPE_UPDATE_USER => '[闲鱼]修改账号信息',
static::TYPE_PRODUCT_WELFARE => '[闲鱼]公益宝贝',
static::TYPE_MESSAGE_REPLY => '[闲鱼]消息回复',
];
}
/**
* 获取状态
*
* @return string[]
*/
static public function statusAssoc() {
return [
static::STATUS_AWAIT => '加入队列',
static::STATUS_ALLOC => '准备运行',
static::STATUS_COMPLETE => '运行成功',
];
}
/**
* 获取执行方式
*
* @return string[]
*/
static public function runTypeAssoc() {
return [
static::RUN_TYPE_ONCE => '立刻执行',
static::RUN_TYPE_TIMER => '定时执行',
static::RUN_TYPE_DAILY => '每天执行',
];
}
/**
* 平台
*
* @return string[]
*/
static public function platformAssoc() {
return [
static::PLATFORM_XIANYU => '闲鱼',
];
}
/**
* 任务类
*
* @return string[]
*/
static public function taskClasses() {
return [
static::TYPE_PRODUCT_RELEASE => '\app\common\task\ProductReleaseTask',
static::TYPE_PRODUCT_POLISH => '\app\common\task\ProductPolishTask',
static::TYPE_PRODUCT_ON => '\app\common\task\ProductOnTask',
static::TYPE_PRODUCT_OFF => '\app\common\task\ProductOffTask',
static::TYPE_COIN_SIGN => '\app\common\task\CoinSignTask',
static::TYPE_COIN_DEDUCT => '\app\common\task\CoinDeductTask',
static::TYPE_PRICE_CUT => '\app\common\task\PriceCutTask',
static::TYPE_COMMENT_REMOVE => '\app\common\task\CommentRemoveTask',
static::TYPE_PRODUCT_RE_RELEASE => '\app\common\task\ProductReReleaseTask',
static::TYPE_RAISE_XY => '\app\common\task\RaiseXyTask',
static::TYPE_RAISE_XY_UNITY => '\app\common\task\RaiseXyUnityTask',
static::TYPE_RAISE_XY_TZ => '\app\common\task\RaiseXyTzTask',
static::TYPE_SYNC_USER => '\app\common\task\SyncUserTask',
static::TYPE_SYNC_SHOP => '\app\common\task\SyncShopTask',
static::TYPE_UPDATE_USER => '\app\common\task\UpdateUserTask',
static::TYPE_PRODUCT_WELFARE => '\app\common\task\ProductWelfareTask',
static::TYPE_MESSAGE_REPLY => '\app\common\task\MessageReplyTask',
];
}
/**
* 分配到详情
*
* @param TaskModel $model
* @return bool
*/
static public function toDetail(TaskModel $model) {
$detail = new TaskDetailModel();
$detail->setAttr('task_id', $model->getAttr('id'));
$detail->setAttr('device_id', $model->getAttr('device_id'));
$detail->setAttr('platform', $model->getAttr('platform'));
$detail->setAttr('type', $model->getAttr('type'));
$detail->setAttr('params', $model->getAttr('params'));
$detail->setAttr('info', new \stdClass());
return $detail->save();
}
/**
* 获取设备
*
* @return DeviceModel
*/
public function device() {
return DeviceModel::get($this->getAttr('device_id'));
}
}

View File

@@ -0,0 +1,163 @@
<?php
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
class User extends Model
{
use SoftDelete;
/**
* 数据表名
* @var string
*/
protected $table = 'tk_users';
/**
* 主键
* @var string
*/
protected $pk = 'id';
/**
* 自动写入时间戳
* @var bool
*/
protected $autoWriteTimestamp = true;
/**
* 创建时间字段
* @var string
*/
protected $createTime = 'create_at';
/**
* 更新时间字段
* @var string
*/
protected $updateTime = 'update_at';
/**
* 软删除字段
* @var string
*/
protected $deleteTime = 'delete_at';
/**
* 隐藏属性
* @var array
*/
protected $hidden = ['password', 'delete_at'];
/**
* 获取管理员用户信息
* @param string $username 用户名
* @param string $password 密码(可能是加密后的)
* @param bool $isEncrypted 密码是否已加密
* @return array|null
*/
public static function getAdminUser($username, $password, $isEncrypted = false)
{
// 查询用户
$user = self::where('username', $username)->find();
if (!$user) {
// 记录日志
\think\facade\Log::info('用户不存在', ['username' => $username]);
return null;
}
// 记录密码验证信息
\think\facade\Log::info('密码验证', [
'username' => $username,
'input_password' => $password,
'stored_hash' => $user->password,
'is_encrypted' => $isEncrypted,
'password_info' => password_get_info($user->password)
]);
// 验证密码
$isValid = false;
if ($isEncrypted) {
// 前端已加密,直接比较哈希值
// 注意:这里需要确保前端和后端使用相同的加密算法和盐值
$storedHash = self::getStoredHash($user->password);
$isValid = hash_equals($storedHash, $password);
\think\facade\Log::info('加密密码验证', [
'username' => $username,
'stored_hash' => $storedHash,
'input_hash' => $password,
'is_valid' => $isValid
]);
} else {
// 未加密使用password_verify验证
$isValid = password_verify($password, $user->password);
}
\think\facade\Log::info('密码验证结果', [
'username' => $username,
'is_valid' => $isValid,
'is_encrypted' => $isEncrypted
]);
if (!$isValid) {
return null;
}
return [
'id' => $user->id,
'username' => $user->username,
'name' => $user->username, // 暂时使用username作为name
'role' => 'admin', // 暂时固定为admin角色
'permissions' => ['*'], // 暂时拥有所有权限
];
}
/**
* 获取存储的哈希值
* 用于前端加密密码的验证
* @param string $bcryptHash 数据库中存储的bcrypt哈希值
* @return string 用于前端验证的哈希值
*/
protected static function getStoredHash($bcryptHash)
{
// 这里需要实现与前端相同的加密算法
// 例如如果前端使用SHA256加盐这里需要提取原始密码并进行相同的处理
// 注意:这只是一个示例,实际实现可能需要根据您的具体需求调整
// 假设我们能够从bcrypt哈希中提取原始密码实际上这是不可能的这里只是示例
// 在实际应用中,您需要在用户注册或修改密码时同时存储前端加密的哈希值
$originalPassword = '123456'; // 这里应该是从数据库中获取的原始密码
$salt = 'yishi_salt_2024'; // 与前端相同的盐值
// 使用与前端相同的算法
return hash('sha256', $originalPassword . $salt);
}
/**
* 通过手机号获取用户信息
* @param string $mobile 手机号
* @return array|null
*/
public static function getUserByMobile($mobile)
{
// 查询用户
$user = self::where('mobile', $mobile)->find();
if (!$user) {
return null;
}
return [
'id' => $user->id,
'username' => $user->username,
'name' => $user->username, // 暂时使用username作为name
'mobile' => $user->mobile,
'role' => 'user', // 暂时固定为user角色
'permissions' => ['user'], // 暂时拥有用户权限
];
}
}

View File

@@ -1,39 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class UserModel extends Model {
const STATUS_ACTIVE = 0; // 正常
const STATUS_DISABLE = 99; // 禁用
/**
* 获取状态
*
* @return string[]
*/
public static function statusAssoc() {
return [
static::STATUS_ACTIVE => '正常',
static::STATUS_DISABLE => '禁用',
];
}
/**
* 只读
*
* @var array
*/
protected $readonly = ['username'];
/**
* JSON 字段
*
* @var array
*/
protected $json = ['roles'];
protected $jsonAssoc = TRUE;
}

View File

@@ -1,9 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class UserTokenModel extends Model {
}

View File

@@ -1,11 +0,0 @@
<?php
namespace app\common\model;
use think\Model;
class WechatFriendModel extends Model {
}

View File

@@ -0,0 +1,157 @@
<?php
namespace app\common\service;
use app\common\model\User;
use app\common\util\JwtUtil;
use think\facade\Log;
class AuthService
{
/**
* 令牌有效期(秒)
*/
const TOKEN_EXPIRE = 7200;
/**
* 短信服务实例
* @var SmsService
*/
protected $smsService;
/**
* 构造函数
*/
public function __construct()
{
$this->smsService = new SmsService();
}
/**
* 用户登录
* @param string $username 用户名
* @param string $password 密码(可能是加密后的)
* @param string $ip 登录IP
* @param bool $isEncrypted 密码是否已加密
* @return array
* @throws \Exception
*/
public function login($username, $password, $ip, $isEncrypted = false)
{
// 获取用户信息
$user = User::getAdminUser($username, $password, $isEncrypted);
if (empty($user)) {
// 记录登录失败
Log::info('登录失败', ['username' => $username, 'ip' => $ip, 'is_encrypted' => $isEncrypted]);
throw new \Exception('用户名或密码错误');
}
// 生成JWT令牌
$token = JwtUtil::createToken($user, self::TOKEN_EXPIRE);
$expireTime = time() + self::TOKEN_EXPIRE;
// 记录登录成功
Log::info('登录成功', ['username' => $username, 'ip' => $ip]);
return [
'token' => $token,
'token_expired' => $expireTime,
'member' => $user
];
}
/**
* 手机号验证码登录
* @param string $mobile 手机号
* @param string $code 验证码(可能是加密后的)
* @param string $ip 登录IP
* @param bool $isEncrypted 验证码是否已加密
* @return array
* @throws \Exception
*/
public function mobileLogin($mobile, $code, $ip, $isEncrypted = false)
{
// 验证验证码
if (!$this->smsService->verifyCode($mobile, $code, 'login', $isEncrypted)) {
Log::info('验证码验证失败', ['mobile' => $mobile, 'ip' => $ip, 'is_encrypted' => $isEncrypted]);
throw new \Exception('验证码错误或已过期');
}
// 获取用户信息
$user = User::getUserByMobile($mobile);
if (empty($user)) {
Log::info('用户不存在', ['mobile' => $mobile, 'ip' => $ip]);
throw new \Exception('用户不存在');
}
// 生成JWT令牌
$token = JwtUtil::createToken($user, self::TOKEN_EXPIRE);
$expireTime = time() + self::TOKEN_EXPIRE;
// 记录登录成功
Log::info('手机号登录成功', ['mobile' => $mobile, 'ip' => $ip]);
return [
'token' => $token,
'token_expired' => $expireTime,
'member' => $user
];
}
/**
* 发送登录验证码
* @param string $mobile 手机号
* @param string $type 验证码类型
* @return array
* @throws \Exception
*/
public function sendLoginCode($mobile, $type)
{
return $this->smsService->sendCode($mobile, $type);
}
/**
* 获取用户信息
* @param array $userInfo JWT中的用户信息
* @return array
* @throws \Exception
*/
public function getUserInfo($userInfo)
{
if (empty($userInfo)) {
throw new \Exception('获取用户信息失败');
}
// 移除不需要返回的字段
unset($userInfo['exp']);
unset($userInfo['iat']);
return $userInfo;
}
/**
* 刷新令牌
* @param array $userInfo JWT中的用户信息
* @return array
* @throws \Exception
*/
public function refreshToken($userInfo)
{
if (empty($userInfo)) {
throw new \Exception('刷新令牌失败');
}
// 移除过期时间信息
unset($userInfo['exp']);
unset($userInfo['iat']);
// 生成新令牌
$token = JwtUtil::createToken($userInfo, self::TOKEN_EXPIRE);
$expireTime = time() + self::TOKEN_EXPIRE;
return [
'token' => $token,
'token_expired' => $expireTime
];
}
}

View File

@@ -0,0 +1,202 @@
<?php
namespace app\common\service;
use think\facade\Cache;
use think\facade\Log;
/**
* 短信服务类
*/
class SmsService
{
/**
* 验证码有效期(秒)
*/
const CODE_EXPIRE = 300;
/**
* 验证码长度
*/
const CODE_LENGTH = 4;
/**
* 发送验证码
* @param string $mobile 手机号
* @param string $type 验证码类型 (login, register)
* @return array
* @throws \Exception
*/
public function sendCode($mobile, $type)
{
// 检查发送频率限制
$this->checkSendLimit($mobile, $type);
// 生成验证码
$code = $this->generateCode();
// 缓存验证码
$this->saveCode($mobile, $code, $type);
// 发送验证码(实际项目中对接短信平台)
$this->doSend($mobile, $code, $type);
// 记录日志
Log::info('发送验证码', [
'mobile' => $mobile,
'type' => $type,
'code' => $code
]);
return [
'mobile' => $mobile,
'expire' => self::CODE_EXPIRE,
// 测试环境返回验证码,生产环境不应返回
'code' => $code
];
}
/**
* 验证验证码
* @param string $mobile 手机号
* @param string $code 验证码(可能是加密后的)
* @param string $type 验证码类型
* @param bool $isEncrypted 验证码是否已加密
* @return bool
*/
public function verifyCode($mobile, $code, $type, $isEncrypted = false)
{
$cacheKey = $this->getCodeCacheKey($mobile, $type);
$cacheCode = Cache::get($cacheKey);
if (!$cacheCode) {
Log::info('验证码不存在或已过期', [
'mobile' => $mobile,
'type' => $type
]);
return false;
}
// 验证码是否匹配
$isValid = false;
if ($isEncrypted) {
// 前端已加密,需要对缓存中的验证码进行相同的加密处理
$encryptedCacheCode = $this->encryptCode($cacheCode);
$isValid = hash_equals($encryptedCacheCode, $code);
// 记录日志
Log::info('加密验证码验证', [
'mobile' => $mobile,
'cache_code' => $cacheCode,
'encrypted_cache_code' => $encryptedCacheCode,
'input_code' => $code,
'is_valid' => $isValid
]);
} else {
// 未加密,直接比较
$isValid = ($cacheCode === $code);
// 记录日志
Log::info('明文验证码验证', [
'mobile' => $mobile,
'cache_code' => $cacheCode,
'input_code' => $code,
'is_valid' => $isValid
]);
}
// 验证成功后删除缓存
if ($isValid) {
Cache::rm($cacheKey);
}
return $isValid;
}
/**
* 检查发送频率限制
* @param string $mobile 手机号
* @param string $type 验证码类型
* @throws \Exception
*/
protected function checkSendLimit($mobile, $type)
{
$cacheKey = $this->getCodeCacheKey($mobile, $type);
// 检查是否存在未过期的验证码
if (Cache::has($cacheKey)) {
throw new \Exception('验证码已发送,请稍后再试');
}
// 检查当日发送次数限制
$limitKey = "sms_limit:{$mobile}:" . date('Ymd');
$sendCount = Cache::get($limitKey, 0);
if ($sendCount >= 10) {
throw new \Exception('今日发送次数已达上限');
}
// 更新发送次数
Cache::set($limitKey, $sendCount + 1, 86400);
}
/**
* 生成随机验证码
* @return string
*/
protected function generateCode()
{
// 生成4位数字验证码
return sprintf("%0" . self::CODE_LENGTH . "d", mt_rand(0, pow(10, self::CODE_LENGTH) - 1));
}
/**
* 保存验证码到缓存
* @param string $mobile 手机号
* @param string $code 验证码
* @param string $type 验证码类型
*/
protected function saveCode($mobile, $code, $type)
{
$cacheKey = $this->getCodeCacheKey($mobile, $type);
Cache::set($cacheKey, $code, self::CODE_EXPIRE);
}
/**
* 执行发送验证码
* @param string $mobile 手机号
* @param string $code 验证码
* @param string $type 验证码类型
* @return bool
*/
protected function doSend($mobile, $code, $type)
{
// 实际项目中对接短信平台API
// 这里仅做模拟,返回成功
return true;
}
/**
* 获取验证码缓存键名
* @param string $mobile 手机号
* @param string $type 验证码类型
* @return string
*/
protected function getCodeCacheKey($mobile, $type)
{
return "sms_code:{$mobile}:{$type}";
}
/**
* 加密验证码
* 使用与前端相同的加密算法
* @param string $code 原始验证码
* @return string 加密后的验证码
*/
protected function encryptCode($code)
{
// 使用与前端相同的加密算法
$salt = 'yishi_salt_2024'; // 与前端相同的盐值
return hash('sha256', $code . $salt);
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace app\common\util;
use think\facade\Config;
use think\facade\Request;
/**
* JWT工具类
* 用于生成和验证JWT令牌
*/
class JwtUtil
{
/**
* 密钥
* @var string
*/
protected static $secret = 'YiShi@2023#JWT';
/**
* 头部
* @var array
*/
protected static $header = [
'alg' => 'HS256', // 加密算法
'typ' => 'JWT' // 类型
];
/**
* 创建JWT令牌
* @param array $payload 载荷信息
* @param int $expire 过期时间(秒)默认2小时
* @return string
*/
public static function createToken($payload, $expire = 7200)
{
$header = self::base64UrlEncode(json_encode(self::$header, JSON_UNESCAPED_UNICODE));
// 附加过期时间
$payload['exp'] = time() + $expire;
$payload['iat'] = time(); // 签发时间
$payload = self::base64UrlEncode(json_encode($payload, JSON_UNESCAPED_UNICODE));
$signature = self::signature($header . '.' . $payload, self::$secret);
return $header . '.' . $payload . '.' . $signature;
}
/**
* 验证令牌
* @param string $token 令牌
* @return array|bool 验证通过返回载荷信息失败返回false
*/
public static function verifyToken($token)
{
if (empty($token)) {
return false;
}
$tokenArray = explode('.', $token);
if (count($tokenArray) != 3) {
return false;
}
list($header, $payload, $signature) = $tokenArray;
// 验证签名
if (self::signature($header . '.' . $payload, self::$secret) !== $signature) {
return false;
}
// 解码载荷
$payload = json_decode(self::base64UrlDecode($payload), true);
// 验证是否过期
if (isset($payload['exp']) && $payload['exp'] < time()) {
return false;
}
return $payload;
}
/**
* 生成签名
* @param string $input 输入
* @param string $key 密钥
* @return string
*/
private static function signature($input, $key)
{
return self::base64UrlEncode(hash_hmac('sha256', $input, $key, true));
}
/**
* URL安全的Base64编码
* @param string $input
* @return string
*/
private static function base64UrlEncode($input)
{
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($input));
}
/**
* URL安全的Base64解码
* @param string $input
* @return string
*/
private static function base64UrlDecode($input)
{
$remainder = strlen($input) % 4;
if ($remainder) {
$input .= str_repeat('=', 4 - $remainder);
}
return base64_decode(str_replace(['-', '_'], ['+', '/'], $input));
}
/**
* 从请求头中获取Token
* @return string|null
*/
public static function getRequestToken()
{
$authorization = Request::header('Authorization');
if (!$authorization) {
return null;
}
// 检查Bearer前缀
if (strpos($authorization, 'Bearer ') !== 0) {
return null;
}
return substr($authorization, 7);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace app\common\validate;
use think\Validate;
/**
* 认证相关验证器
*/
class Auth extends Validate
{
/**
* 验证规则
* @var array
*/
protected $rule = [
'username' => 'require|length:3,20',
'password' => 'require|length:6,64',
'mobile' => 'require|mobile',
'code' => 'require|length:4,6',
'is_encrypted' => 'boolean',
'type' => 'require|in:login,register',
];
/**
* 错误信息
* @var array
*/
protected $message = [
'username.require' => '用户名不能为空',
'username.length' => '用户名长度必须在3-20个字符之间',
'password.require' => '密码不能为空',
'password.length' => '密码长度必须在6-64个字符之间',
'mobile.require' => '手机号不能为空',
'mobile.mobile' => '手机号格式不正确',
'code.require' => '验证码不能为空',
'code.length' => '验证码长度必须在4-6个字符之间',
'is_encrypted.boolean' => '加密标志必须为布尔值',
'type.require' => '验证码类型不能为空',
'type.in' => '验证码类型不正确',
];
/**
* 验证场景
* @var array
*/
protected $scene = [
'login' => ['username', 'password', 'is_encrypted'],
'mobile_login' => ['mobile', 'code', 'is_encrypted'],
'refresh' => [],
'send_code' => ['mobile', 'type'],
];
}

View File

@@ -0,0 +1,19 @@
<?php
// +----------------------------------------------------------------------
// | 设备管理模块路由配置
// +----------------------------------------------------------------------
use think\facade\Route;
// 定义RESTful风格的API路由 - 设备管理相关
Route::group('v1/devices', function () {
// 设备列表和查询
Route::get('', 'app\\devices\\controller\\Device@index'); // 获取设备列表
Route::get('count', 'app\\devices\\controller\\Device@count'); // 获取设备总数
Route::get(':id', 'app\\devices\\controller\\Device@read'); // 获取设备详情
// 设备管理
Route::post('', 'app\\devices\\controller\\Device@save'); // 添加设备
Route::put('refresh', 'app\\devices\\controller\\Device@refresh'); // 刷新设备状态
Route::delete(':id', 'app\\devices\\controller\\Device@delete'); // 删除设备
})->middleware(['jwt']);

View File

@@ -0,0 +1,272 @@
<?php
namespace app\devices\controller;
use think\Controller;
use app\devices\model\Device as DeviceModel;
use think\facade\Request;
use app\common\util\JwtUtil;
/**
* 设备管理控制器
*/
class Device extends Controller
{
/**
* 用户信息
* @var object
*/
protected $user;
/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
// 设置时区
date_default_timezone_set('Asia/Shanghai');
}
/**
* 获取设备总数
* @return \think\response\Json
*/
public function count()
{
try {
// 获取查询条件
$where = [];
// 租户ID
$tenantId = Request::param('tenant_id');
if (is_numeric($tenantId)) {
$where['tenantId'] = $tenantId;
}
// 设备在线状态
$alive = Request::param('alive');
if (is_numeric($alive)) {
$where['alive'] = $alive;
}
// 获取设备总数
$count = DeviceModel::getDeviceCount($where);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'count' => $count
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
/**
* 获取设备列表
* @return \think\response\Json
*/
public function index()
{
try {
// 获取查询条件
$where = [];
// 设备IMEI
$imei = Request::param('imei');
if (!empty($imei)) {
$where['imei'] = ['like', "%{$imei}%"];
}
// 设备备注
$memo = Request::param('memo');
if (!empty($memo)) {
$where['memo'] = ['like', "%{$memo}%"];
}
// 设备在线状态
$alive = Request::param('alive');
if (is_numeric($alive)) {
$where['alive'] = $alive;
}
// 获取分页参数
$page = (int)Request::param('page', 1);
$limit = (int)Request::param('limit', 10);
// 获取排序参数
$sort = Request::param('sort', 'id');
$order = Request::param('order', 'desc');
// 获取设备列表
$list = DeviceModel::getDeviceList($where, "{$sort} {$order}", $page, $limit);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'total' => $list->total(),
'list' => $list->items()
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
/**
* 获取设备详情
* @return \think\response\Json
*/
public function read()
{
try {
// 获取设备ID
$id = Request::param('id/d');
if (empty($id)) {
return json([
'code' => 400,
'msg' => '参数错误'
]);
}
// 获取设备详情
$info = DeviceModel::getDeviceInfo($id);
if (empty($info)) {
return json([
'code' => 404,
'msg' => '设备不存在'
]);
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $info
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
/**
* 刷新设备
* @return \think\response\Json
*/
public function refresh()
{
try {
return json([
'code' => 200,
'msg' => '刷新成功',
'data' => []
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
/**
* 添加设备
* @return \think\response\Json
*/
public function save()
{
try {
// 获取设备数据
$data = Request::post();
// 验证IMEI是否为空
if (empty($data['imei'])) {
return json([
'code' => 400,
'msg' => '设备IMEI不能为空'
]);
}
// 验证IMEI是否已存在
$exists = DeviceModel::where('imei', $data['imei'])->where('isDeleted', 0)->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '设备IMEI已存在'
]);
}
// 添加设备
$id = DeviceModel::addDevice($data);
return json([
'code' => 200,
'msg' => '添加成功',
'data' => [
'id' => $id
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '添加失败:' . $e->getMessage()
]);
}
}
/**
* 删除设备
* @return \think\response\Json
*/
public function delete()
{
try {
// 获取设备ID
$id = Request::param('id/d');
if (empty($id)) {
return json([
'code' => 400,
'msg' => '参数错误'
]);
}
// 验证设备是否存在
$exists = DeviceModel::where('id', $id)->where('isDeleted', 0)->find();
if (!$exists) {
return json([
'code' => 404,
'msg' => '设备不存在'
]);
}
// 删除设备
$result = DeviceModel::deleteDevice($id);
return json([
'code' => 200,
'msg' => '删除成功',
'data' => [
'result' => $result
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
}

View File

@@ -0,0 +1,147 @@
<?php
namespace app\devices\model;
use think\Model;
use think\Db;
/**
* 设备模型类
*/
class Device extends Model
{
// 设置表名
protected $name = 'device';
// 设置主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
protected $deleteTime = 'deleteTime';
// 定义字段类型
protected $type = [
'id' => 'integer',
'createTime' => 'integer',
'updateTime' => 'integer',
'deleteTime' => 'integer',
'alive' => 'integer',
'isDeleted' => 'integer',
'tenantId' => 'integer',
'groupId' => 'integer'
];
/**
* 获取设备总数
* @param array $where 查询条件
* @return int 设备总数
*/
public static function getDeviceCount($where = [])
{
// 默认只统计未删除的设备
if (!isset($where['isDeleted'])) {
$where['isDeleted'] = 0;
}
return self::where($where)->count();
}
/**
* 获取设备列表
* @param array $where 查询条件
* @param string $order 排序方式
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getDeviceList($where = [], $order = 'id desc', $page = 1, $limit = 10)
{
// 默认只查询未删除的设备
if (!isset($where['isDeleted'])) {
$where['isDeleted'] = 0;
}
return self::where($where)
->order($order)
->paginate($limit, false, ['page' => $page]);
}
/**
* 获取设备详情
* @param int $id 设备ID
* @return array|null 设备信息
*/
public static function getDeviceInfo($id)
{
return self::where('id', $id)
->where('isDeleted', 0)
->find();
}
/**
* 添加设备
* @param array $data 设备数据
* @return int 新增设备ID
*/
public static function addDevice($data)
{
$device = new self();
$device->allowField(true)->save($data);
return $device->id;
}
/**
* 更新设备
* @param int $id 设备ID
* @param array $data 设备数据
* @return bool 更新结果
*/
public static function updateDevice($id, $data)
{
return self::where('id', $id)
->where('isDeleted', 0)
->update($data);
}
/**
* 删除设备(软删除)
* @param int $id 设备ID
* @return bool 删除结果
*/
public static function deleteDevice($id)
{
return self::where('id', $id)
->update([
'isDeleted' => 1,
'deleteTime' => date('Y-m-d H:i:s', time())
]);
}
/**
* 按设备品牌统计数量
* @return array 统计结果
*/
public static function countByBrand()
{
return self::where('isDeleted', 0)
->group('brand')
->field('brand, count(*) as count')
->select();
}
/**
* 按设备在线状态统计数量
* @return array 统计结果
*/
public static function countByStatus()
{
return self::where('isDeleted', 0)
->group('alive')
->field('alive, count(*) as count')
->select();
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace app\http\middleware;
use app\common\util\JwtUtil;
use think\facade\Log;
/**
* JWT认证中间件
*/
class JwtAuth
{
/**
* 处理请求
* @param \think\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
// 获取Token
$token = JwtUtil::getRequestToken();
// 验证Token
if (!$token) {
return json([
'code' => 401,
'msg' => '未授权访问,缺少有效的身份凭证',
'data' => null
])->header(['Content-Type' => 'application/json; charset=utf-8']);
}
$payload = JwtUtil::verifyToken($token);
if (!$payload) {
return json([
'code' => 401,
'msg' => '授权已过期或无效',
'data' => null
])->header(['Content-Type' => 'application/json; charset=utf-8']);
}
// 将用户信息附加到请求中
$request->userInfo = $payload;
// 写入日志
Log::info('JWT认证通过', ['user_id' => $payload['id'] ?? 0, 'username' => $payload['username'] ?? '']);
return $next($request);
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace app\http\middleware;
/**
* JWT中间件别名
* 解决类不存在: app\http\middleware\jwt的问题
*/
class jwt extends JwtAuth
{
// 继承JwtAuth的所有功能
}

0
Server/application/provider.php Executable file → Normal file
View File

0
Server/application/tags.php Executable file → Normal file
View File

0
Server/build.php Executable file → Normal file
View File

0
Server/composer.json Executable file → Normal file
View File

10
Server/config/app.php Executable file → Normal file
View File

@@ -45,7 +45,7 @@ return [
// 默认语言
'default_lang' => 'zh-cn',
// 应用类库后缀
'class_suffix' => true,
'class_suffix' => false,
// 控制器类后缀
'controller_suffix' => false,
@@ -54,9 +54,9 @@ return [
// +----------------------------------------------------------------------
// 默认模块名
'default_module' => 'frontend',
'default_module' => 'index',
// 禁止访问模块
'deny_module_list' => ['common'],
'deny_module_list' => [],
// 默认控制器名
'default_controller' => 'Index',
// 默认操作名
@@ -89,9 +89,9 @@ return [
// IP代理获取标识
'http_agent_ip' => 'X-REAL-IP',
// URL伪静态后缀
'url_html_suffix' => '',
'url_html_suffix' => 'html',
// URL普通方式参数 用于自动生成
'url_common_param' => true,
'url_common_param' => false,
// URL参数方式 0 按名称成对解析 1 按顺序解析
'url_param_type' => 0,
// 是否开启路由延迟解析

0
Server/config/cache.php Executable file → Normal file
View File

0
Server/config/console.php Executable file → Normal file
View File

0
Server/config/cookie.php Executable file → Normal file
View File

4
Server/config/database.php Executable file → Normal file
View File

@@ -28,11 +28,11 @@ return [
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
'charset' => env('database.charset', 'utf8mb4'),
// 数据库表前缀
'prefix' => Env::get('database.prefix', 'tk_'),
// 数据库调试模式
'debug' => true,
'debug' => env('database.debug', true),
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效

0
Server/config/log.php Executable file → Normal file
View File

8
Server/config/middleware.php Executable file → Normal file
View File

@@ -15,4 +15,12 @@
return [
// 默认中间件命名空间
'default_namespace' => 'app\\http\\middleware\\',
// 别名或分组
'alias' => [
'jwt' => 'JwtAuth',
],
// 优先级设置,此数组中的中间件会按照数组中的顺序优先执行
'priority' => [],
];

0
Server/config/session.php Executable file → Normal file
View File

0
Server/config/template.php Executable file → Normal file
View File

0
Server/config/trace.php Executable file → Normal file
View File

0
Server/extend/.gitignore vendored Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/.coveralls.yml Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/.gitignore vendored Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/.travis.yml Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/CHANGELOG.md Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/LICENSE.md Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/README-CN.md Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/README.md Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/autoload.php Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/build-phar.sh Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/composer.json Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/example.jpg Executable file → Normal file
View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

0
Server/extend/aliyun-oss-php-sdk-2.4.1/index.php Executable file → Normal file
View File

0
Server/extend/aliyun-oss-php-sdk-2.4.1/phpunit.xml Executable file → Normal file
View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

Some files were not shown because too many files have changed in this diff Show More