feat(sign): 完善签到功能送优惠卷功能
This commit is contained in:
@@ -2,19 +2,22 @@
|
||||
|
||||
namespace addons\shopro\controller\zy;
|
||||
|
||||
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
use app\admin\model\zy\Tags;
|
||||
use think\exception\PDOException;
|
||||
use app\admin\model\zy\sign\Record;
|
||||
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\zy\circle\Circle;
|
||||
use app\admin\model\zy\link\Complaint;
|
||||
use think\exception\ValidateException;
|
||||
use addons\shopro\library\activity\traits\GiveGift;
|
||||
|
||||
class Sys extends Base
|
||||
{
|
||||
use GiveGift;
|
||||
|
||||
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);
|
||||
$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()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ trait CouponSend
|
||||
foreach ($coupons as $coupon) {
|
||||
try {
|
||||
$userCoupon = $this->send($user, $coupon);
|
||||
$success[] = $coupon->id;
|
||||
$success[] = $coupon->name;
|
||||
} catch (HttpResponseException $e) {
|
||||
$data = $e->getResponse()->getData();
|
||||
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
||||
|
||||
Reference in New Issue
Block a user