基础扩展

This commit is contained in:
ASUS 2025-09-16 18:18:20 +08:00
parent 96fc75c2b4
commit dfd284f255
10 changed files with 472 additions and 27 deletions

View File

@ -3,30 +3,13 @@
namespace app\controller;
use support\Request;
use support\think\Db;
use support\think\Model;
class IndexController
{
public function index(Request $request)
{
return <<<EOF
<style>
* {
padding: 0;
margin: 0;
}
iframe {
border: none;
overflow: scroll;
}
</style>
<iframe
src="https://www.workerman.net/wellcome"
width="100%"
height="100%"
allow="*"
sandbox="allow-scripts allow-same-origin"
></iframe>
EOF;
return '哈啰';
}
public function view(Request $request)

View File

@ -2,3 +2,24 @@
/**
* Here is your custom functions.
*/
if (!function_exists('p')) {
/**
* 将数组 key 的命名方式转换为小写驼峰
* @param array $array 被转换的数组
* @param array $keys 要转换的 key默认所有
* @return array
*/
function p(...$p)
{
if(count($p) > 1) {
foreach ($p as $k => $v) {
print_r($v);
print_r('---');
}
}else {
print_r($p[0]);
}
die;
}
}

View File

@ -0,0 +1,258 @@
<?php
namespace app\model;
// 消息推送
use support\Log;
use support\think\Db;
class MessagePushTask
{
public static function send_msg()
{
// 查询任务
$event_list = Db::name('tb_push_event')->where(['status' => 0])->select();
if($event_list) {
foreach ($event_list as $k => $event) {
$user_list = self::getPullInfoUser($event);
}
}
}
/**
* 找到可以接收订阅消息的用户
* @param $type类型 1按次数发送 2自定义 3按标签
* @return array
*/
public static function getPullInfoUser($event): array
{
$user_arr = [];
switch ($event['type']) {
case 1;
$user_arr = self::getPullInfoUserToNumber($event);
break;
case 2;
$user_arr = self::getPullInfoUserToCustom($event);
break;
case 3;
$user_arr = self::getPullInfoUserToLabel($event);
break;
}
return $user_arr;
}
// 按照可接收次数获取用户信息
public static function getPullInfoUserToNumber($event)
{
$user_num = [];
if($event['can_send_times'] > 0) {
$user_num = Db::name('tb_user_push_event_num')->alias('upen')->where(['num', '>=', $event['can_send_times']])
->join('tb_user_info u', 'upen.user_id = u.id')
->field('upen.user_id as user_id, u.wechat_ac_open_id as open_id')
->column('open_id', 'user_id');
}
return $user_num;
}
// 按照自定义获取用户信息
public static function getPullInfoUserToCustom($event)
{
$data_arr = [];
// 按照性别
if(isset($event['gender']) && is_string($event['gender'])) {
$gender_arr = explode(',', $event['gender']);
foreach ($gender_arr as $k => $gender) {
// 未知
if($gender == 0) {
$data_arr[] = Db::name('tb_user_info')->whereNotIn('sex', [0,1])
->field('user_id as user_id, wechat_ac_open_id as open_id')
->column('open_id', 'user_id');
}
// 男
if($gender == 1) {
$data_arr[] = Db::name('tb_user_info')->where('sex', 1)
->field('user_id as user_id, wechat_ac_open_id as open_id')
->column('open_id', 'user_id');
}
// 女
if($gender == 2) {
$data_arr[] = Db::name('tb_user_info')->where('sex', 0)
->field('user_id as user_id, wechat_ac_open_id as open_id')
->column('open_id', 'user_id');
}
}
}
// 按照下单次数
if(isset($event['order_history']) && is_string($event['order_history'])) {
$order_history_arr = explode(',', $event['order_history']);
foreach ($order_history_arr as $k => $order_history) {
// 从未下单
if($order_history == 0) {
$data_arr[] = Db::name('tb_user_info')
->alias('u') // 用户表别名u
->leftJoin('tb_order_info o', 'u.id = o.user_id')
->where('o.user_id', 'null')
->field('u.id as user_id, u.wechat_ac_open_id as open_id')
->distinct(true)
->column('open_id', 'user_id');
}
// 下过1单
if($order_history == 1) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) = 1')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
// 下过2-5单
if($order_history == 2) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) BETWEEN 2 AND 5')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
// 下过5单起
if($order_history == 3) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) >= 5')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
}
}
// 按照下单时间 no
if(isset($event['order_times']) && is_string($event['order_times'])) {
$order_times_arr = explode(',', $event['order_times']);
foreach ($order_times_arr as $k => $order_times) {
// 今天下过
if($order_times == 0) {
$data_arr[] = Db::name('tb_user_info')
->alias('u') // 用户表别名u
->innerJoin('tb_order_info o', 'u.id = o.user_id')
->where('o.create_time', '>=', date('Y-m-d 00:00:00'))
->where('o.create_time', '<=', date('Y-m-d 23:59:59'))
->field('u.id as user_id, u.wechat_ac_open_id as open_id')
->distinct(true)
->column('open_id', 'user_id');
}
// 昨天下过
if($order_times == 1) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) = 1')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
// 2周内没下过
if($order_times == 2) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) BETWEEN 2 AND 5')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
// 半个月-1个月没下过
if($order_times == 3) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) >= 5')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
// 1个月以上没下过
if($order_times == 4) {
$data_arr[] = Db::name('tb_user_info')
->alias('u')
// 关联订单表,使用内连接确保只包含有订单的用户
->innerJoin('tb_order_info o', 'u.id = o.user_id')
// 按用户ID分组
->group('u.id')
// 筛选出订单数量为1的用户
->having('COUNT(o.id) >= 5')
// 选择需要的用户字段,同时查询订单数量(可选)
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
->column('open_id', 'user_id');
}
}
}
return $data_arr;
}
// 按照标签获取用户信息
public static function getPullInfoUserToLabel($event)
{
return [];
}
// 给用户送送优惠券
public static function sendUserCoupon(int | array $user_id, $coupon_id, $number = 1)
{
try {
Db::startTrans();
if(is_int($user_id)) {
}elseif (is_array($user_id)) {
}
Db::commit();
return true;
}catch (\Exception $e) {
Db::rollback();
Log::info('用户送券异常-->' . $e->getMessage());
return false;
}
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace app\model;
use support\Log;
use support\think\Db;
// 自定义推送任务计算
class PushTaskCustom
{
/**
* 性别
*/
public static function gender($arr = []):array
{
return [];
}
/**
* 下单
*/
public static function orderHistory($arr = []):array
{
return [];
}
/**
* 下单时间
*/
public static function orderTimes($arr = []):array
{
return [];
}
/**
* 会员
*/
public static function vip($arr = []):array
{
return [];
}
/**
* 手机
*/
public static function telPhone($arr = []):array
{
return [];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace app\model;
use support\Log;
use support\think\Db;
// 更新用户可推送次数
class SaveUserPushNumber
{
public static function savePushNumber()
{
$user_event_push_num_list = Db::name('tb_user_push_event_num')->select()->toArray();
if($user_event_push_num_list) {
foreach ($user_event_push_num_list as $k => $user_event_push_num) {
Db::name('tb_user_push_event_num')->where(['user_id' => $user_event_push_num['user_id']])->update([
'num' => 4,
'update_time' => date('Y-m-d H:i:s'),
]);
}
}
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace app\process;
use support\Log;
use Workerman\Crontab\Crontab;
// 消息推送
class MessagePushTask
{
public function onWorkerStart()
{
// 每月1号 营销接收次数更新
new Crontab('0 0 1 * *', function(){
Log::info('营销接收次数更新开始执行---->');
\app\model\SaveUserPushNumber::savePushNumber();
Log::info('营销接收次数更新执行结束---->');
});
// 每分钟执行 营销推送
new Crontab('1 * * * * *', function(){
Log::info('营销推送开始执行---->');
\app\model\MessagePushTask::send_msg();
Log::info('营销推送执行结束---->');
});
}
}

View File

@ -30,7 +30,8 @@
"webman/think-orm": "^2.1",
"webman/redis": "^2.1",
"illuminate/events": "^11.46",
"webman/redis-queue": "^1.3"
"webman/redis-queue": "^1.3",
"workerman/crontab": "^1.0"
},
"suggest": {
"ext-event": "For better performance. "

58
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b15b44132f3c3dc4460aa6a5d7710233",
"content-hash": "2f950d877ad756c4aca10580de6402e6",
"packages": [
{
"name": "carbonphp/carbon-doctrine-types",
@ -2249,6 +2249,62 @@
},
"time": "2025-02-17T03:34:21+00:00"
},
{
"name": "workerman/crontab",
"version": "v1.0.7",
"source": {
"type": "git",
"url": "https://github.com/walkor/crontab.git",
"reference": "74f51ca8204e8eb628e57bc0e640561d570da2cb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/walkor/crontab/zipball/74f51ca8204e8eb628e57bc0e640561d570da2cb",
"reference": "74f51ca8204e8eb628e57bc0e640561d570da2cb",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": ">=7.0",
"workerman/workerman": ">=4.0.20"
},
"type": "library",
"autoload": {
"psr-4": {
"Workerman\\Crontab\\": "./src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "walkor",
"email": "walkor@workerman.net",
"homepage": "http://www.workerman.net",
"role": "Developer"
}
],
"description": "A crontab written in PHP based on workerman",
"homepage": "http://www.workerman.net",
"keywords": [
"crontab"
],
"support": {
"email": "walkor@workerman.net",
"forum": "http://wenda.workerman.net/",
"issues": "https://github.com/walkor/workerman/issues",
"source": "https://github.com/walkor/crontab",
"wiki": "http://doc.workerman.net/"
},
"time": "2025-01-15T07:20:50+00:00"
},
{
"name": "workerman/redis",
"version": "v2.0.4",

View File

@ -12,6 +12,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use app\process\MessagePushTask;
use support\Log;
use support\Request;
use app\process\Http;
@ -26,8 +27,8 @@ return [
'user' => '',
'group' => '',
'reusePort' => false,
// 'eventLoop' => '',
'eventLoop' => \Workerman\Events\Swoole::class,
'eventLoop' => '',
// 'eventLoop' => \Workerman\Events\Swoole::class,
'context' => [],
'constructor' => [
'requestClass' => Request::class,
@ -59,5 +60,8 @@ return [
'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
]
]
],
'MessagePushTask' => [
'handler' => MessagePushTask::class,
]
];

View File

@ -7,13 +7,13 @@ return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
'hostname' => '192.168.1.31',
// 数据库名
'database' => 'test',
'database' => 'czg_cashier',
// 数据库用户名
'username' => 'root',
// 数据库密码
'password' => '123456',
'password' => 'Chaozg123.',
// 数据库连接端口
'hostport' => '3306',
// 数据库连接参数