auth->id; $this->success('', [ 'assessments' => AssessmentService::lists('relation', $userId), 'cards' => ContentService::lists($userId, ['type' => 'card', 'limit' => 3])['list'], 'conflicts' => ContentService::lists($userId, ['type' => 'conflict', 'limit' => 3])['list'], 'courses' => ['list' => $this->coursesData()], 'tasks' => $this->tasks(), 'memories' => $this->memoryList(5), 'stats' => $this->stats($userId), ]); } /** * 关系测评列表 / 详情 / 提交 */ public function assessments() { $this->success('', ['list' => AssessmentService::lists('relation', $this->auth->id)]); } public function assessmentDetail() { $data = AssessmentService::detail($this->request->param('id', 0)); if (!$data) { $this->error('量表不存在'); } $this->success('', $data); } public function assessmentSubmit() { $answers = $this->request->post('answers', ''); $answers = is_array($answers) ? $answers : json_decode(htmlspecialchars_decode((string)$answers, ENT_QUOTES), true); if (!is_array($answers)) { $this->error('请完成全部题目'); } $result = AssessmentService::submit($this->auth->id, (int)$this->request->post('id', 0), $answers); if (isset($result['error'])) { $this->error($result['error']); } $this->success('测评完成', $result); } /** * 沟通引导卡 / 冲突工具 */ public function cards() { $this->success('', ContentService::lists($this->auth->id, ['type' => 'card', 'limit' => 30])); } public function conflicts() { $this->success('', ContentService::lists($this->auth->id, ['type' => 'conflict', 'limit' => 30])); } /** * 关系课程列表 + 购买 */ public function courses() { $this->success('', [ 'list' => $this->coursesData(), 'total' => count($this->coursesData()), ]); } protected function coursesData() { $data = ContentService::lists($this->auth->id, ['type' => 'course', 'part' => 'relation', 'limit' => 30]); return $data['list']; } public function courseBuy() { $userId = $this->auth->id; $contentId = (int)$this->request->post('id', 0); $course = Db::name('content')->where('id', $contentId)->where('type', 'course')->find(); if (!$course) { $this->error('课程不存在'); } if (ContentService::hasPurchased($userId, $contentId)) { $this->success('已购买', ['purchased' => true]); } $orderNo = 'HC' . date('YmdHis') . Random::numeric(4); $id = Db::name('course_order')->insertGetId([ 'order_no' => $orderNo, 'user_id' => $userId, 'content_id' => $contentId, 'amount' => 0, 'pay_status' => 'unpaid', 'createtime' => time(), ]); $this->success('订单已创建', [ 'order_id' => (int)$id, 'order_no' => $orderNo, 'amount' => (float)0, 'pay_hint' => '课程支付通道待接入,可联系客服开通', 'purchased' => false, ]); } /** * 双人任务 */ public function taskList() { $this->success('', ['list' => $this->tasks()]); } public function taskSave() { $userId = $this->auth->id; $id = (int)$this->request->post('id', 0); $title = mb_substr(trim((string)$this->request->post('title', '')), 0, 80); if (!$title) { $this->error('请填写任务内容'); } $data = [ 'user_id' => $userId, 'scope' => 'relation', 'title' => $title, 'category' => $this->request->post('category', 'daily'), 'share_partner' => 1, 'updatetime' => time(), ]; if ($id) { $row = Db::name('task')->where('id', $id)->where('user_id', $userId)->find(); if (!$row) { $this->error('任务不存在'); } Db::name('task')->where('id', $id)->update($data); } else { $data['createtime'] = time(); $id = Db::name('task')->insertGetId($data); } $this->success('已保存', ['id' => (int)$id, 'list' => $this->tasks()]); } public function taskDone() { $userId = $this->auth->id; $id = (int)$this->request->post('id', 0); $row = Db::name('task')->where('id', $id)->where('user_id', $userId)->where('scope', 'relation')->find(); if (!$row) { $this->error('任务不存在'); } $done = $row['done'] ? 0 : 1; Db::name('task')->where('id', $id)->update([ 'done' => $done, 'done_time' => $done ? time() : null, 'updatetime' => time(), ]); // 完成双人任务,双人共同记录进「爱的记忆」 if ($done) { Db::name('memory')->insert([ 'user_id' => $userId, 'bind_id' => 0, 'title' => '一起完成:' . $row['title'], 'memory_date' => date('Y-m-d'), 'type' => 'daily', 'content' => '', 'remind' => 0, 'createtime' => time(), ]); } $this->success($done ? '已完成' : '已取消', ['done' => $done, 'list' => $this->tasks()]); } /** * 纪念与回顾 */ public function memories() { $this->success('', ['list' => $this->memoryList(50)]); } public function memorySave() { $userId = $this->auth->id; $title = mb_substr(trim((string)$this->request->post('title', '')), 0, 60); $date = (string)$this->request->post('memory_date', date('Y-m-d')); if (!$title) { $this->error('请填写纪念日名称'); } if (!strtotime($date)) { $this->error('日期格式不正确'); } $id = Db::name('memory')->insertGetId([ 'user_id' => $userId, 'title' => $title, 'memory_date' => $date, 'type' => $this->request->post('type', 'anniversary'), 'content' => mb_substr((string)$this->request->post('content', ''), 0, 500), 'remind' => (int)$this->request->post('remind', 1), 'createtime' => time(), ]); $this->success('已记录', ['id' => (int)$id, 'list' => $this->memoryList(50)]); } public function memoryRemove() { $id = (int)$this->request->post('id', 0); $row = Db::name('memory')->where('id', $id)->where('user_id', $this->auth->id)->find(); if (!$row) { $this->error('记录不存在'); } Db::name('memory')->where('id', $id)->update(['deletetime' => time()]); $this->success('已删除', ['list' => $this->memoryList(50)]); } // ---------------- 内部方法 ---------------- protected function tasks() { $rows = Db::name('task') ->where('user_id', $this->auth->id) ->where('scope', 'relation') ->whereNull('deletetime') ->order('done asc, id desc') ->select(); return array_map(function ($row) { return [ 'id' => (int)$row['id'], 'title' => $row['title'], 'category' => $row['category'], 'done' => (int)$row['done'], ]; }, $rows); } protected function memoryList($limit) { $rows = Db::name('memory') ->where('user_id', $this->auth->id) ->whereNull('deletetime') ->order('memory_date', 'desc') ->limit($limit) ->select(); return array_map(function ($row) { $days = (int)floor((strtotime(date('Y-m-d')) - strtotime($row['memory_date'])) / 86400); $nextDays = null; if ($row['type'] === 'anniversary') { $nextTs = strtotime(date('Y') . '-' . date('m-d', strtotime($row['memory_date']))); if ($nextTs < strtotime(date('Y-m-d'))) { $nextTs = strtotime('+1 year', $nextTs); } $nextDays = (int)ceil(($nextTs - strtotime(date('Y-m-d'))) / 86400); } return [ 'id' => (int)$row['id'], 'title' => $row['title'], 'memory_date' => $row['memory_date'], 'type' => $row['type'], 'content' => $row['content'], 'days_since' => $days, 'days_to_next' => $nextDays, ]; }, $rows); } protected function stats($userId) { return [ 'task_total' => Db::name('task')->where('user_id', $userId)->where('scope', 'relation')->whereNull('deletetime')->count(), 'task_done' => Db::name('task')->where('user_id', $userId)->where('scope', 'relation')->where('done', 1)->whereNull('deletetime')->count(), 'memory_total' => Db::name('memory')->where('user_id', $userId)->whereNull('deletetime')->count(), 'test_count' => Db::name('assessment_record') ->where('user_id', $userId) ->whereIn('assessment_id', function ($query) { $query->name('assessment')->where('scope', 'relation')->field('id'); }) ->count(), ]; } }