// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\wxchat\controller\member; use addon\wxchat\model\WxchatTask; use addon\wxchat\model\WxchatTaskRecord; use think\facade\Db; use think\facade\Request; use ywxapp\controller\FrontendBase; use ywxapp\service\WalletService; /** * 任务赚钱体系:每日任务 / 邀请好友 / 完善资料 -> 领取金币 */ class Task extends FrontendBase { /** * 任务列表(附带我的进度) */ public function lists() { $uid = $this->auth->uid; $tasks = WxchatTask::where('status', 1) ->order('sort', 'asc') ->select(); $records = WxchatTaskRecord::where('uid', $uid)->column('*', 'task_id'); $list = []; foreach ($tasks as $t) { $rec = $records[$t->id] ?? null; $list[] = [ 'task_id' => $t->id, 'title' => $t->title, 'type' => $t->type, 'desc' => $t->desc, 'reward_coins' => $t->reward_coins, 'target' => $t->target, 'progress' => $rec ? (int) $rec['progress'] : 0, 'status' => $rec ? (int) $rec['status'] : 0, // 0进行中 1待领 2已领 ]; } $this->success(['list' => $list, 'coins' => WalletService::getCoins($uid)]); } /** * 进度上报(由业务事件调用:发布动态/完善资料/邀请成功等) */ public function progress() { $uid = $this->auth->uid; $taskId = (int) Request::post('task_id', 0); $step = max(1, (int) Request::post('step', 1)); $task = WxchatTask::where('id', $taskId)->where('status', 1)->find(); if (! $task) { $this->error('任务不存在'); } $rec = WxchatTaskRecord::where('uid', $uid)->where('task_id', $taskId)->find(); if (! $rec) { $rec = new WxchatTaskRecord(); $rec->uid = $uid; $rec->task_id = $taskId; $rec->progress = 0; $rec->status = 0; $rec->reward_coins = 0; $rec->create_at = time(); } if ($rec->status == 2) { $this->error('任务已领取'); } $rec->progress = min($task->target, $rec->progress + $step); if ($rec->progress >= $task->target) { $rec->status = 1; // 完成待领 } $rec->update_at = time(); $rec->save(); $this->success(['status' => $rec->status, 'progress' => $rec->progress], '进度已更新'); } /** * 领取奖励(发放金币) */ public function claim() { $uid = $this->auth->uid; $taskId = (int) Request::post('task_id', 0); $task = WxchatTask::where('id', $taskId)->find(); if (! $task) { $this->error('任务不存在'); } $rec = WxchatTaskRecord::where('uid', $uid)->where('task_id', $taskId)->find(); if (! $rec || $rec->status == 0) { $this->error('任务未完成'); } if ($rec->status == 2) { $this->error('奖励已领取'); } $reward = $task->reward_coins; WalletService::incCoins($uid, $reward, '完成任务:' . $task->title); $rec->status = 2; $rec->reward_coins = $reward; $rec->update_at = time(); $rec->save(); $this->success(['coins' => WalletService::getCoins($uid), 'reward' => $reward], '领取成功'); } /** * 邀请好友(模拟:记录一次有效邀请并推进邀请任务) */ public function invite() { $uid = $this->auth->uid; $task = WxchatTask::where('type', 'invite')->where('status', 1)->order('id')->find(); if (! $task) { $this->success([], '暂无邀请任务'); return; } // 上报一次邀请进度(真实环境应在被邀请人注册成功后触发) $rec = WxchatTaskRecord::where('uid', $uid)->where('task_id', $task->id)->find(); if (! $rec) { $rec = new WxchatTaskRecord(); $rec->uid = $uid; $rec->task_id = $task->id; $rec->progress = 0; $rec->status = 0; $rec->reward_coins = 0; $rec->create_at = time(); } if ($rec->status == 2) { $this->success([], '邀请任务已完成'); return; } $rec->progress = min($task->target, $rec->progress + 1); if ($rec->progress >= $task->target) { $rec->status = 1; } $rec->update_at = time(); $rec->save(); $this->success(['progress' => $rec->progress, 'status' => $rec->status], '邀请成功'); } }