feat(sign): 完善签到功能送优惠卷功能

This commit is contained in:
2025-06-17 15:11:19 +08:00
parent 5227396484
commit 335c6a6c68
5 changed files with 71 additions and 2 deletions

View File

@@ -37,3 +37,7 @@ ADD COLUMN `status` tinyint NOT NULL DEFAULT 0 COMMENT '状态0禁用1启
DROP TABLE IF EXISTS `zy_sign`; DROP TABLE IF EXISTS `zy_sign`;
-- 已执行 2025-06-16 -- 已执行 2025-06-16
ALTER TABLE `zy_sign_record`
ADD COLUMN `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '获得礼物备注' AFTER `last`;

View File

@@ -2,19 +2,22 @@
namespace addons\shopro\controller\zy; namespace addons\shopro\controller\zy;
use think\Db; use think\Db;
use think\Exception; use think\Exception;
use app\admin\model\zy\Tags; use app\admin\model\zy\Tags;
use think\exception\PDOException; use think\exception\PDOException;
use app\admin\model\zy\sign\Record;
use app\admin\model\zy\link\Visitor; use app\admin\model\zy\link\Visitor;
use app\admin\model\zy\sign\SignSet;
use app\admin\model\shopro\user\User; use app\admin\model\shopro\user\User;
use app\admin\model\zy\circle\Circle; use app\admin\model\zy\circle\Circle;
use app\admin\model\zy\link\Complaint; use app\admin\model\zy\link\Complaint;
use think\exception\ValidateException; use think\exception\ValidateException;
use addons\shopro\library\activity\traits\GiveGift;
class Sys extends Base class Sys extends Base
{ {
use GiveGift;
public function index() public function index()
{ {
@@ -79,4 +82,64 @@ class Sys extends Base
$res = Visitor::where('type', $params['type'])->where('obj_id', $params['obj_id'])->paginate($params['pageSize'] ?? 10); $res = Visitor::where('type', $params['type'])->where('obj_id', $params['obj_id'])->paginate($params['pageSize'] ?? 10);
$this->success('Success', ['list' => $res->items(), 'count' => $res->total()]); $this->success('Success', ['list' => $res->items(), 'count' => $res->total()]);
} }
public function signin()
{
$signSet = SignSet::where('status', 1)->select();
if (empty($signSet)) {
$this->error('没有签到活动');
}
Db::startTrans();
try {
$user = auth_user();
$newRecord = new Record;
$newRecord->user_id = $user->id;
$newRecord->date = date('Y-m-d');
$newRecord->last = 1;
$record = Record::where('user_id', $user->id)->order('id', 'desc')->find();
if (!empty($record)) {
if ($newRecord->date == $record->date) {
$this->error('您今天已签到,请勿重复签到');
}
if ($record['date'] == date('Y-m-d', strtotime('-1 day'))) {
$newRecord->last = $record->last + 1; //连续签到
}
}
$newRecord->remark = '';
foreach ($signSet as $set) {
if ($newRecord->last >= $set['last']) { // 达成连续签到天数
if ($set['chance1'] > 0 && rand(0, 100) <= $set['chance1']) {
$res1 = $this->giveCoupons($user, $set['coupon1_id']);
if ($res1['errors']) $this->error('优惠券赠送失败,请联系管理员', $res1);
$newRecord->remark .= '获得【' . implode(',', $res1['success']) . '】;';
}
if ($set['chance2'] > 0 && mt_rand(0, 100) <= $set['chance2']) {
$res2 = $this->giveCoupons($user, $set['coupon2_id']);
if ($res2['errors']) $this->error('优惠券赠送失败,请联系管理员', $res2);
$newRecord->remark .= '获得【' . implode(',', $res2['success']) . '】;';
}
}
}
$newRecord->save();
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('Success', $newRecord);
}
public function signList()
{
$params = $this->request->param();
$query = Record::where('user_id', $this->auth->id)->order('id', 'desc');
if (!empty($params['date_begin'])) {
$query->where('date', '>=', $params['date_begin']);
}
if (!empty($params['date_end'])) {
$query->where('date', '<=', $params['date_end']);
}
$res = $query->paginate($params['pageSize'] ?? 10);
$this->success('Success', ['list' => $res->items(), 'count' => $res->total()]);
}
} }

View File

@@ -70,7 +70,7 @@ trait CouponSend
foreach ($coupons as $coupon) { foreach ($coupons as $coupon) {
try { try {
$userCoupon = $this->send($user, $coupon); $userCoupon = $this->send($user, $coupon);
$success[] = $coupon->id; $success[] = $coupon->name;
} catch (HttpResponseException $e) { } catch (HttpResponseException $e) {
$data = $e->getResponse()->getData(); $data = $e->getResponse()->getData();
$message = $data ? ($data['msg'] ?? '') : $e->getMessage(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage();

View File

@@ -4,6 +4,7 @@ return [
'User_id' => '用户id', 'User_id' => '用户id',
'Date' => '签到日期', 'Date' => '签到日期',
'Last' => '已持续天数', 'Last' => '已持续天数',
'Remark' => '获得礼物',
'Create_time' => '创建时间', 'Create_time' => '创建时间',
'Update_time' => '修改时间', 'Update_time' => '修改时间',
'User.username' => '用户名' 'User.username' => '用户名'

View File

@@ -30,6 +30,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{field: 'user.username', title: __('User.username'), operate: 'LIKE'}, {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
{field: 'date', title: __('Date')}, {field: 'date', title: __('Date')},
{field: 'last', title: __('Last')}, {field: 'last', title: __('Last')},
{field: 'remark', title: __('Remark')},
{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false}, {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false}, {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate} {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}