Task.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 任务接口
  7. */
  8. class Task extends Api
  9. {
  10. protected $noNeedLogin = [''];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->taskModel = new \app\common\model\Task();
  16. $this->tasklogModel = new \app\common\model\TaskLog();
  17. }
  18. /**
  19. * 获取任务列表
  20. */
  21. public function getTaskList() {
  22. $type_id = $this->request->request("type_id"); // 任务类型:1=新手任务,2=每日任务,3=成长任务
  23. $plat = $this->request->request("plat",1); // 平台:1=安卓,2=ios,3=小程序
  24. $page = $this->request->request('page',1); // 分页
  25. $pageNum = $this->request->request('pageNum',10); // 分页
  26. // 分页搜索构建
  27. $pageStart = ($page-1)*$pageNum;
  28. $urlArr = ["1"=>"android_url","2"=>"ios_url","3"=>"mini_url"];
  29. $jump_url = $urlArr[$plat];
  30. if($type_id == 2) {
  31. // 获取基本信息
  32. $where = [];
  33. $type_id && $where["type_id"] = $type_id;
  34. $where["is_show"] = 1;
  35. $taskList = $this->taskModel
  36. ->field("id,type_id,image,name,exp,number,".$jump_url." as jump_url")
  37. ->where($where)
  38. ->limit($pageStart,$pageNum)
  39. ->select();
  40. $today = strtotime(date("Y-m-d 00:00:00"));
  41. $where = [];
  42. $where["user_id"] = $this->auth->id;
  43. $where["createtime"] = ["gt",$today];
  44. $finishInfo = \app\common\model\TaskLog::where($where)->select();
  45. $finishIds = [];
  46. if($finishInfo) foreach($finishInfo as $k => $v) {
  47. $finishIds[$v["task_id"]] = $v;
  48. }
  49. if($taskList) {
  50. foreach($taskList as $k => $v) {
  51. $finishInfo = isset($finishIds[$v["id"]])?$finishIds[$v["id"]]:[];
  52. if($finishInfo) {
  53. $taskList[$k]["pace"] = $finishInfo->pace;
  54. $taskList[$k]["is_finish"] = $finishInfo->is_finish;
  55. $taskList[$k]["finish_number"] = $finishInfo->finish_number;
  56. $taskList[$k]["is_reward"] = $finishInfo->is_reward;
  57. if($finishInfo->is_finish != 1 && $finishInfo->pace != 0) {
  58. $taskList[$k]["pace_txt"] = $finishInfo->finish_number."/".$v["number"];
  59. }
  60. } else {
  61. $taskList[$k]["pace"] = 0;
  62. $taskList[$k]["is_finish"] = 0;
  63. $taskList[$k]["finish_number"] = 0;
  64. $taskList[$k]["is_reward"] = 0;
  65. $taskList[$k]["pace_txt"] = "0/".$v["number"];
  66. }
  67. }
  68. }
  69. } else {
  70. // 获取基本信息
  71. $where = [];
  72. $type_id && $where["a.type_id"] = $type_id;
  73. $where["a.is_show"] = 1;
  74. $taskList = $this->taskModel->alias("a")
  75. ->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")
  76. ->join("hx_task_log l", "l.task_id = a.id and l.user_id = ".$this->auth->id, "left")
  77. ->where($where)
  78. ->limit($pageStart,$pageNum)
  79. ->order("is_finish","asc")
  80. ->select();
  81. if($taskList) {
  82. foreach($taskList as $k => $v) {
  83. $v["pace"] || $taskList[$k]["pace"] = 0;
  84. $v["is_finish"] || $taskList[$k]["is_finish"] = 0;
  85. $v["finish_number"] || $taskList[$k]["finish_number"] = 0;
  86. $v["is_reward"] || $taskList[$k]["is_reward"] = 0;
  87. if($v["is_finish"] != 1 && $v["pace"] != 0) {
  88. $taskList[$k]["pace_txt"] = $v["finish_number"]."/".$v["number"];
  89. }
  90. }
  91. }
  92. }
  93. $this->success("获取成功!",$taskList);
  94. }
  95. /**
  96. * 领取奖励
  97. */
  98. public function getReward() {
  99. $task_id = $this->request->request("task_id"); // 任务ID
  100. if (!$task_id) {
  101. $this->error(__('Invalid parameters'));
  102. }
  103. // 查询任务信息
  104. $taskInfo = $this->taskModel->where(["id"=>$task_id])->find();
  105. if(!$taskInfo) {
  106. $this->error("任务信息未找到!");
  107. }
  108. // 查询用户完成情况
  109. $where = [];
  110. $where["user_id"] = $this->auth->id;
  111. $where["task_id"] = $task_id;
  112. $where["is_finish"] = 1;
  113. $taskInfo["type_id"] == 2 && $where["finish_date"] = date("Ymd");
  114. $tasklogInfo = $this->tasklogModel->where($where)->find();
  115. if(!$tasklogInfo) {
  116. $this->error("任务还未完成哦!");
  117. }
  118. if($tasklogInfo["is_reward"] == 1) {
  119. $this->error("任务奖励已领取,请勿重复领取!");
  120. }
  121. Db::startTrans();
  122. try{
  123. // 增加用户经验值
  124. $res1 = \app\common\model\User::addEmpirical($this->auth->id,$taskInfo["exp"]);
  125. // 更新奖励领取状态
  126. $res2 = $this->tasklogModel->update(["is_reward"=>1],["task_id"=>$task_id,"user_id"=>$this->auth->id]);
  127. if($res1 && $res2) {
  128. Db::commit();
  129. $this->success("领取成功!");
  130. } else {
  131. $this->error("领取失败!");
  132. }
  133. }catch (ValidateException $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (PDOException $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. } catch (Exception $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. }
  144. /**
  145. * 完成任务
  146. */
  147. public function finishTask() {
  148. $code = $this->request->request("code"); // 任务编码
  149. $number = $this->request->request("number",1); // 完成次数
  150. if (!$code) {
  151. $this->error(__('Invalid parameters'));
  152. }
  153. \app\common\model\TaskLog::tofinish($this->auth->id,$code,$number);
  154. $this->success("成功!");
  155. }
  156. }