feat: 初始化雄戈商城服务端代码与Webhook自动部署配置

这个提交包含在:
wikiboss
2026-09-12 16:40:24 +08:00
当前提交 e90ec58e61
共有 6438 个文件被更改,包括 1136083 次插入 和 0 次删除

查看文件

@@ -0,0 +1,57 @@
<?php
namespace addons\shopro\job;
use addons\shopro\library\traits\ActivityCache;
use addons\shopro\model\Activity;
use think\queue\Job;
/**
* 订单自动操作
*/
class ActivityAutoOper extends BaseJob
{
use ActivityCache;
/**
* 活动自动删除
*/
public function autoClose(Job $job, $data)
{
try {
$activity = $data['activity'];
$activity = Activity::get($activity['id']);
// 一个活动会存在多个队列,要排重
if ($activity) {
// 如果活动还没被删除
// 规则
$rules = $activity['rules'];
// 当前配置应该自动结束的时间
$laterTime = $activity['endtime'];
if (isset($rules['activity_auto_close']) && $rules['activity_auto_close'] > 0) {
$laterTime += ($rules['activity_auto_close'] * 60);
}
// 如果当前时间大于 laterTime,可以执行删除
if (time() >= $laterTime) {
// 删除活动 【软删】
$activity->delete();
// 删除活动缓存
$this->delActivity($activity);
}
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoClose' . ':执行失败,错误信息:' . $e->getMessage());
}
}
}

查看文件

@@ -0,0 +1,57 @@
<?php
namespace addons\shopro\job;
use addons\shopro\library\traits\ActivityCache;
use addons\shopro\library\traits\Groupon;
use addons\shopro\model\Activity;
use addons\shopro\model\ActivityGroupon;
use think\queue\Job;
/**
* 订单自动操作
*/
class ActivityGrouponAutoOper extends BaseJob
{
use ActivityCache, Groupon;
/**
* 拼团判断,将团结束,
*/
public function expire(Job $job, $data){
try {
$activity = $data['activity'];
$activity_groupon_id = $data['activity_groupon_id'];
$activityGroupon = ActivityGroupon::where('id', $activity_groupon_id)->find();
// 活动正在进行中, 走这里的说明人数 都没满
if ($activityGroupon && $activityGroupon['status'] == 'ing') {
\think\Db::transaction(function () use ($activity, $activityGroupon) {
$rules = $activity['rules'];
// 是否虚拟成团
$is_fictitious = $rules['is_fictitious'] ?? 0;
// 最大虚拟人数 ,不填或者 "" 不限制人数,都允许虚拟成团, 0相当于不允许虚拟成团
$fictitious_num = (!isset($rules['fictitious_num']) || $rules['fictitious_num'] == '') ? 'no-limit' : $rules['fictitious_num'];
// 拼团剩余人数
$surplus_num = $activityGroupon['num'] - $activityGroupon['current_num'];
if ($is_fictitious && ($fictitious_num == 'no-limit' || $fictitious_num >= $surplus_num)) {
// 虚拟成团,如果虚拟用户不够,则自动解散
$this->finishFictitiousGroupon($activityGroupon);
} else {
// 解散退款
$this->invalidRefundGroupon($activityGroupon);
}
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-expire' . ':执行失败,错误信息:' . $e->getMessage());
}
}
}

查看文件

@@ -0,0 +1,66 @@
<?php
namespace addons\shopro\job;
use addons\shopro\model\Activity;
use addons\shopro\model\RobOrderModel;
use think\queue\Job;
/**
* 抢购自动分配名额
*/
class ActivityRobOrder extends BaseJob
{
/**
* 抢购自动分配名额
*/
public function autoRob(Job $job, $data)
{
try {
$activity = $data['activity'];
$activity = Activity::get($activity['id']);
if ($activity) {
// 获取预约列表
$orderUserId = RobOrderModel::where(['activity_id' => $activity->id, 'is_rob' => 2])
->field('user_id')
->select();
if (!empty($orderUserId)) {
// 已提前设置资格人数
$count = RobOrderModel::where(['activity_id' => $activity->id, 'is_rob' => 1])
->count();
// 剩余名额
$num = $activity->num - $count;
if (!empty($num)) {
$arr = [];
$orderUserId = array_column(collection($orderUserId)->toArray(), 'user_id');
if (count($orderUserId) <= $num) {
$arr = $orderUserId;
} else {
// 随机取值
$arr = randomSelectArr($orderUserId, $num);
}
// 分配资格
foreach ($arr as $userId) {
RobOrderModel::where(['user_id' => $userId, 'activity_id' => $activity->id])->update([
'is_rob' => 1
]);
}
}
}
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoClose' . ':执行失败,错误信息:' . $e->getMessage());
}
}
}

查看文件

@@ -0,0 +1,25 @@
<?php
namespace addons\shopro\job;
use addons\shopro\model\Order;
use addons\shopro\model\OrderAction;
use think\queue\Job;
/**
* BaseJob 基类
*/
class BaseJob
{
public function failed($data){
// 记录日志
\think\Db::name('shopro_failed_job')->insert([
'data' => json_encode($data),
'createtime' => time(),
'updatetime' => time()
]);
}
}

查看文件

@@ -0,0 +1,41 @@
<?php
namespace addons\shopro\job;
use think\queue\Job;
use think\Db;
/**
* 处理分销任务
*/
class CommissionJob extends BaseJob
{
/**
* 分销商升级
*
* @param Job $job 当前的任务对象
* @param int $userId 升级用户ID
*/
public function agentUpgradeJob(Job $job, $data)
{
// 1.统计用户表、分销商表信息 使用library的Agent类去更新
// 2.检查是否能成为分销商 如果刚好能成为分销商,走新增分销商流程 走library的成为分销商条件
// 3.检查是否能往上升级 走AgentLevel方法 按照权重排序倒序检查
// 4.递归往上继续走
try{
Db::transaction(function () use ($job, $data) {
$userId = is_array($data) ? ($data['userId'] ?? 0) : 0;
$agent = new \addons\shopro\library\commission\Agent($userId);
if(!$agent->user) {
\think\Log::error('queue-' . get_class() . ':分销商ID' . $userId . '不存在');
}else {
$agent->runUpgradeAgentPlan();
}
$job->delete();
});
}catch(\Exception $e) {
\think\Log::error('queue-' . get_class() . ':分销商升级执行失败,错误信息:' . $e->getMessage() . '|' . $e->getLine());
$job->delete();
}
}
}

查看文件

@@ -0,0 +1,40 @@
<?php
namespace addons\shopro\job;
use think\queue\Job;
/**
* 队列消息通知
*/
class Notification extends BaseJob
{
/**
* 发送通知
*/
public function send(Job $job, $data){
try {
// 这里获取到的 $notifiables 和 notification 两个都是数组,不是类,尴尬, 更可恨的 notification 只是 {"delay":0,"event":"changemobile"}
$notifiables = $data['notifiables'];
$notification = $data['notification'];
// 因为 notification 只有参数,需要把对应的类传过来,在这里重新初始化
$notification_name = $data['notification_name'];
// 重新实例化 notification 实例
if (class_exists($notification_name)) {
$notification = new $notification_name($notification['data']);
// 发送消息
\addons\shopro\library\notify\Notify::sendNow($notifiables, $notification);
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::error('queue-' . get_class()
. (isset($notification->event) ? ('-' . $notification->event) : '')
. ':执行失败,错误信息:' . $e->getMessage());
}
}
}

查看文件

@@ -0,0 +1,139 @@
<?php
namespace addons\shopro\job;
use addons\shopro\model\GoodsComment;
use addons\shopro\model\Order;
use addons\shopro\model\OrderAction;
use addons\shopro\model\OrderItem;
use addons\shopro\model\Config;
use think\queue\Job;
/**
* 订单自动操作
*/
class OrderAutoOper extends BaseJob
{
/**
* 订单自动关闭
*/
public function autoClose(Job $job, $data){
try {
$order = $data['order'];
// 重新查询订单
$order = Order::get($order['id']);
if ($order && $order['status'] == 0) {
\think\Db::transaction(function () use ($order, $data) {
// 订单关闭前
\think\Hook::listen('order_close_before', $data);
// 执行关闭
$order->status = Order::STATUS_INVALID;
$order->ext = json_encode($order->setExt($order, ['invalid_time' => time()])); // 取消时间
$order->save();
OrderAction::operAdd($order, null, null, 'system', '系统自动失效订单');
// 订单自动关闭之后 行为 返还用户优惠券,积分
$data = ['order' => $order];
\think\Hook::listen('order_close_after', $data);
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoClose' . ':执行失败,错误信息:' . $e->getMessage());
}
}
/**
* 订单自动确认
*/
public function autoConfirm(Job $job, $data) {
try {
$order = $data['order'];
$item = $data['item'];
// 重新查询订单
$order = Order::get($order['id']);
// 只要没有退款成功,所有队列正常执行
$item = OrderItem::where('id', $item['id'])
->where('dispatch_status', OrderItem::DISPATCH_STATUS_SENDED)
->where('refund_status', 'not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH])
->find();
if ($order && $item) {
\think\Db::transaction(function () use ($order, $item, $data) {
(new Order())->getedItem($order, $item, ['oper_type' => 'system']);
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoConfirm' . ':执行失败,错误信息:' . $e->getMessage());
}
}
public function autoComment(Job $job, $data) {
try {
$order = $data['order'];
$item = $data['item'];
// 重新查询订单
$order = Order::get($order['id']);
// 只要没有退款成功,所有队列正常执行
$item = OrderItem::where('id', $item['id'])
->where('dispatch_status', OrderItem::DISPATCH_STATUS_GETED)
->where('comment_status', OrderItem::COMMENT_STATUS_NO)
->where('refund_status', 'not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH])
->find();
if ($order && $item) {
\think\Db::transaction(function () use ($order, $item, $data) {
// 订单评价前
\think\Hook::listen('order_comment_before', $data);
// 获取自动好评内容
$config = Config::where('name', 'order')->cache(300)->find(); // 读取配置自动缓存 5 分钟
$config = isset($config) ? json_decode($config['value'], true) : [];
$comment_content = (isset($config['order_comment_content']) && $config['order_comment_content'])
? $config['order_comment_content'] : '用户默认好评'; // 单位天
GoodsComment::create([
'goods_id' => $item['goods_id'],
'order_id' => $order['id'],
'user_id' => $order['user_id'],
'level' => 5, // 自动好评
'content' => $comment_content,
'images' => '',
'status' => 'show'
]);
$item->comment_status = OrderItem::COMMENT_STATUS_OK; // 评价成功
$item->save();
OrderAction::operAdd($order, $item, null, 'system', '系统自动评价成功');
// 订单评价后
$data = ['order' => $order, 'item' => $item];
\think\Hook::listen('order_comment_after', $data);
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoComment' . $e->getMessage());
}
}
}

查看文件

@@ -0,0 +1,86 @@
<?php
namespace addons\shopro\job;
use addons\shopro\library\traits\ActivityCache;
use addons\shopro\library\traits\Groupon;
use addons\shopro\library\traits\StockSale;
use addons\shopro\model\Config;
use addons\shopro\model\GoodsComment;
use addons\shopro\model\Order;
use addons\shopro\model\OrderAction;
use addons\shopro\model\OrderItem;
use think\queue\Job;
/**
* 订单支付完成
*/
class OrderPayed extends BaseJob
{
use StockSale, ActivityCache, Groupon;
/**
* 订单支付完成
*/
public function payed(Job $job, $data)
{
try {
$order = $data['order'];
$user = $data['user'];
$order = Order::with('item')->where('id', $order['id'])->find();
// 数据库删订单的问题常见,这里被删的订单直接把队列移除
if ($order) {
\think\Db::transaction(function () use ($order, $user, $data) {
// 订单减库存
$this->realForwardStockSale($order);
// 判断,如果是拼团 真实加入团
if (strpos($order['activity_type'], 'groupon') !== false) {
$this->joinGroupon($order, $user);
}
// 处理发票审核
if ($order->invoice_status == 1) {
$invoice = \addons\shopro\model\OrderInvoice::get(['order_id' => $order->id]);
if ($invoice) {
$invoice->status = 0;
$invoice->save();
}
}
// 处理消费返积分 TODO: 待测试
$scoreConfig = Config::where('name', 'score')->find();
$scoreConfig = json_decode($scoreConfig['value'], true);
if (!empty($scoreConfig['consume_get_score']) && !empty($scoreConfig['consume_get_score_ratio'])) {
$scoreRatio = intval($scoreConfig['consume_get_score_ratio']);
$score = intval($scoreRatio * 0.01 * $order->total_fee);
if ($score > 0) {
\addons\shopro\model\User::score($score, $user['id'], 'consume_get_score', $order['id']);
}
}
// 触发订单支付完成事件, 如果这个订单刚好完成拼团,并且是自动发货订单,则这个订单的自动发货事件会比订单支付之后事件早
// $data = ['order' => $order];
// \think\Hook::listen('order_payed_after', $data);
// 检测有没有自动发货的商品,有就自动发货
$order->checkDispatchAndSend($order, ['user' => $user]);
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
$error = json_encode([
'a' => $e->getLine(),
'b' => $e->getFile(),
'c' => $e->getTrace(),
'd' => $e->getMessage()
], JSON_UNESCAPED_UNICODE);
\think\Log::error('queue-' . get_class() . '-payed' . ':执行失败,错误信息:' . $error);
}
}
}

查看文件

@@ -0,0 +1,42 @@
<?php
namespace addons\shopro\job;
use addons\shopro\model\TradeOrder;
use addons\shopro\model\Config;
use think\queue\Job;
/**
* 交易订单自动操作
*/
class TradeOrderAutoOper extends BaseJob
{
/**
* 订单自动关闭
*/
public function autoClose(Job $job, $data){
try {
$order = $data['order'];
// 重新查询订单
$order = TradeOrder::get($order['id']);
if ($order && $order['status'] == 0) {
\think\Db::transaction(function () use ($order, $data) {
// 执行关闭
$order->status = TradeOrder::STATUS_INVALID;
$order->ext = json_encode($order->setExt($order, ['invalid_time' => time()])); // 取消时间
$order->save();
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoClose' . ':执行失败,错误信息:' . $e->getMessage());
}
}
}

查看文件

@@ -0,0 +1,74 @@
<?php
namespace addons\shopro\job;
use think\queue\Job;
/**
* 微信任务
*/
class Wechat extends BaseJob
{
/**
* 异步分批创建新的队列任务
*/
public function createQueueByOpenIdsArray(Job $job, $openIdsArray)
{
try {
$count = count($openIdsArray);
if ($count > 0) {
$page = ceil($count / 100);
for ($i = 0; $i < $page; $i++) {
\think\Queue::push('\addons\shopro\job\Wechat@saveSubscribeUserInfo', array_slice($openIdsArray, $i * 100, 100), 'shopro');
}
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::error('queue-' . get_class() . '-createQueueByOpenIdsArray' . ':执行失败,错误信息:' . $e->getMessage());
}
}
/**
* 保存更新关注用户
*/
public function saveSubscribeUserInfo(Job $job, $openIdsArray)
{
try {
$wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
$result = $wechat->getSubscribeUserInfoByOpenId($openIdsArray);
if (isset($result['user_info_list'])) {
$userInfoList = $result['user_info_list'];
$insertData = [];
foreach ($userInfoList as $u) {
$wechatFans = \app\admin\model\shopro\wechat\Fans::get(['openid' => $u['openid']]);
if ($wechatFans) {
$wechatFans->save([
'nickname' => $u['nickname'],
'headimgurl' => $u['headimgurl'],
'sex' => $u['sex'],
'country' => $u['country'],
'province' => $u['province'],
'city' => $u['city'],
'subscribe' => 1,
'subscribe_time' => $u['subscribe_time']
]);
}else{
$insertData[] = $u;
}
}
if (count($insertData) > 0) {
$wechatFans = new \app\admin\model\shopro\wechat\Fans;
$wechatFans->allowField(true)->saveAll($insertData);
}
}
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::error('queue-' . get_class() . '-saveSubscribeUserInfo' . ':执行失败,错误信息2:' . $e->getMessage() . '|' . json_encode($insertData, JSON_UNESCAPED_UNICODE));
}
}
}