Files
fast/addons/shopro/controller/zy/Sys.php

157 lines
5.8 KiB
PHP

<?php
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()
{
$params = $this->request->param();
$model = new Tags();
if (isset($params['type'])) {
$model->where('type', $params['type']);
}
if (isset($params['group'])) {
$model->where('group', $params['group']);
}
$res = $model->select();
$this->success('Success', $res);
}
// 举报投诉
public function complain()
{
$params = $this->request->param();
if ($params['type'] == 3) { //用户举报
$target = User::get($params['target_id']);
} elseif ($params['type'] == 2) { //影圈举报
$target = Circle::get($params['target_id']);
} elseif ($params['type'] == 1) {
$target = ['id' => 0];
$params['target_id'] = 0;
} else {
$this->error('type 类型错误');
}
if (empty($target)) {
$this->error('举报对象不存在');
}
if (empty($params['content'])) {
$this->error('举报内容不能为空');
}
$user = auth_user();
$res = Complaint::get(['user_id' => $user['id'], 'target_id' => $params['target_id'], 'type' => $params['type'], 'status' => 0]);
if ($res) {
$this->error('您已举报过该对象');
}
$params['status'] = 0;
$params['user_id'] = $user['id'];
$params['username'] = $params['username'] ?? $user['username'];
Db::startTrans();
try {
$result = (new Complaint)->allowField(true)->save($params);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result === false) {
$this->error('操作失败');
}
$this->success('Success');
}
public function visitor()
{
$params = $this->request->param();
$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()]);
}
public function task()
{
$record = Record::where('user_id', $this->auth->id)->where('date', date('Y-m-d'))->find();
$query = SignSet::field('name,begin_date,end_date,intro')->where('status', 1);
$res = $query->paginate($params['pageSize'] ?? 10);
$list = $res->items();
foreach ($list as &$val) {
$val['progress'] = empty($record) ? '今日未完成' : '今日已完成';
}
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
}
}