瀏覽代碼

复制伴声的task

lizhen_gitee 3 年之前
父節點
當前提交
c22b6abc1c
共有 2 個文件被更改,包括 189 次插入0 次删除
  1. 172 0
      application/api/controller/Task.php
  2. 17 0
      application/common/model/Task.php

+ 172 - 0
application/api/controller/Task.php

@@ -0,0 +1,172 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+
+/**
+ * 任务接口
+ */
+class Task extends Api
+{
+    protected $noNeedLogin = [''];
+    protected $noNeedRight = '*';
+
+    public function _initialize()
+    {
+        parent::_initialize();
+        $this->taskModel = new \app\common\model\Task();
+        $this->tasklogModel = new \app\common\model\TaskLog();
+    }
+
+    /**
+     * 获取任务列表
+     */
+    public function getTaskList() {
+        $type_id = $this->request->request("type_id"); // 任务类型:1=新手任务,2=每日任务,3=成长任务
+        $plat = $this->request->request("plat",1); // 平台:1=安卓,2=ios,3=小程序
+        $page = $this->request->request('page',1); // 分页
+        $pageNum = $this->request->request('pageNum',10); // 分页
+        // 分页搜索构建
+        $pageStart = ($page-1)*$pageNum;
+        $urlArr = ["1"=>"android_url","2"=>"ios_url","3"=>"mini_url"];
+        $jump_url = $urlArr[$plat];
+
+        if($type_id == 2) {
+
+            // 获取基本信息
+            $where = [];
+            $type_id && $where["type_id"] = $type_id;
+            $where["is_show"] = 1;
+            $taskList = $this->taskModel
+                ->field("id,type_id,image,name,exp,number,".$jump_url." as jump_url")
+                ->where($where)
+                ->limit($pageStart,$pageNum)
+                ->select();
+
+            $today = strtotime(date("Y-m-d 00:00:00"));
+
+            $where = [];
+            $where["user_id"] = $this->auth->id;
+            $where["createtime"] = ["gt",$today];
+            $finishInfo = \app\common\model\TaskLog::where($where)->select();
+            $finishIds = [];
+            if($finishInfo) foreach($finishInfo as $k => $v) {
+                $finishIds[$v["task_id"]] = $v;
+            }
+
+            if($taskList) {
+                foreach($taskList as $k => $v) {
+                    $finishInfo = isset($finishIds[$v["id"]])?$finishIds[$v["id"]]:[];
+                    if($finishInfo) {
+                        $taskList[$k]["pace"] = $finishInfo->pace;
+                        $taskList[$k]["is_finish"] = $finishInfo->is_finish;
+                        $taskList[$k]["finish_number"] = $finishInfo->finish_number;
+                        $taskList[$k]["is_reward"] = $finishInfo->is_reward;
+                        if($finishInfo->is_finish != 1 && $finishInfo->pace != 0) {
+                            $taskList[$k]["pace_txt"] = $finishInfo->finish_number."/".$v["number"];
+                        }
+                    } else {
+                        $taskList[$k]["pace"] = 0;
+                        $taskList[$k]["is_finish"] = 0;
+                        $taskList[$k]["finish_number"] = 0;
+                        $taskList[$k]["is_reward"] = 0;
+                        $taskList[$k]["pace_txt"] = "0/".$v["number"];
+                    }
+                }
+            }
+        } else {
+            // 获取基本信息
+            $where = [];
+            $type_id && $where["a.type_id"] = $type_id;
+            $where["a.is_show"] = 1;
+            $taskList = $this->taskModel->alias("a")
+                ->field("a.id,a.type_id,a.image,a.name,a.exp,a.number,a.".$jump_url." as jump_url,a.url_type,l.pace,l.is_reward,l.is_finish,l.finish_number")
+                ->join("hx_task_log l", "l.task_id = a.id and l.user_id = ".$this->auth->id, "left")
+                ->where($where)
+                ->limit($pageStart,$pageNum)
+                ->order("is_finish","asc")
+                ->select();
+
+            if($taskList) {
+                foreach($taskList as $k => $v) {
+                    $v["pace"] || $taskList[$k]["pace"] = 0;
+                    $v["is_finish"] || $taskList[$k]["is_finish"] = 0;
+                    $v["finish_number"] || $taskList[$k]["finish_number"] = 0;
+                    $v["is_reward"] || $taskList[$k]["is_reward"] = 0;
+                    if($v["is_finish"] != 1 && $v["pace"] != 0) {
+                        $taskList[$k]["pace_txt"] = $v["finish_number"]."/".$v["number"];
+                    }
+                }
+            }
+        }
+
+        $this->success("获取成功!",$taskList);
+    }
+
+    /**
+     * 领取奖励
+     */
+    public function getReward() {
+        $task_id = $this->request->request("task_id"); // 任务ID
+        if (!$task_id) {
+            $this->error(__('Invalid parameters'));
+        }
+        // 查询任务信息
+        $taskInfo = $this->taskModel->where(["id"=>$task_id])->find();
+        if(!$taskInfo) {
+            $this->error("任务信息未找到!");
+        }
+        // 查询用户完成情况
+        $where = [];
+        $where["user_id"] = $this->auth->id;
+        $where["task_id"] = $task_id;
+        $where["is_finish"] = 1;
+        $taskInfo["type_id"] == 2 && $where["finish_date"] = date("Ymd");
+        $tasklogInfo = $this->tasklogModel->where($where)->find();
+        if(!$tasklogInfo) {
+            $this->error("任务还未完成哦!");
+        }
+        if($tasklogInfo["is_reward"] == 1) {
+            $this->error("任务奖励已领取,请勿重复领取!");
+        }
+        Db::startTrans();
+        try{
+            // 增加用户经验值
+            $res1 = \app\common\model\User::addEmpirical($this->auth->id,$taskInfo["exp"]);
+            // 更新奖励领取状态
+            $res2 = $this->tasklogModel->update(["is_reward"=>1],["task_id"=>$task_id,"user_id"=>$this->auth->id]);
+            if($res1 && $res2) {
+                Db::commit();
+                $this->success("领取成功!");
+            } else {
+                $this->error("领取失败!");
+            }
+        }catch (ValidateException $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        } catch (PDOException $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+    }
+
+    /**
+     * 完成任务
+     */
+    public function finishTask() {
+        $code = $this->request->request("code"); // 任务编码
+        $number = $this->request->request("number",1); // 完成次数
+        if (!$code) {
+            $this->error(__('Invalid parameters'));
+        }
+        \app\common\model\TaskLog::tofinish($this->auth->id,$code,$number);
+        $this->success("成功!");
+
+    }
+
+}

+ 17 - 0
application/common/model/Task.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 模型
+ */
+class Task extends Model
+{
+
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+    // 定义时间戳字段名
+    protected $createTime = 'createtime';
+}