Task.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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); // 任务类型:1=实时任务,2=每日任务
  23. $plat = $this->request->request("plat",1); // 平台:1=安卓,2=ios
  24. $urlArr = ["1"=>"android_url","2"=>"ios_url"];
  25. $jump_url = $urlArr[$plat];
  26. $bili = config('site.money_to_gold'); //钱和金币兑换比例
  27. if($type_id == 2) {
  28. // 获取所有基础任务
  29. $where = [];
  30. $where["type_id"] = 2;
  31. $where["is_show"] = 1;
  32. $taskList = Db::name('task')
  33. ->field("id,type_id,image,name,exp,number,".$jump_url." as jump_url,url_type")
  34. ->where($where)
  35. // ->autopage()
  36. ->select();
  37. $taskList = list_domain_image($taskList,['image']);
  38. $today = strtotime(date("Y-m-d 00:00:00"));
  39. //今日开始的任务
  40. $where = [];
  41. $where["user_id"] = $this->auth->id;
  42. $where["createtime"] = ["gt",$today];
  43. $finishInfo = \app\common\model\TaskLog::where($where)->select();
  44. $finishIds = [];
  45. if($finishInfo) foreach($finishInfo as $k => $v) {
  46. $finishIds[$v["task_id"]] = $v;
  47. }
  48. if($taskList) {
  49. foreach($taskList as $k => $v) {
  50. $finishInfo = isset($finishIds[$v["id"]])?$finishIds[$v["id"]]:[];
  51. if($finishInfo) {
  52. $taskList[$k]["pace"] = $finishInfo->pace;
  53. $taskList[$k]["is_finish"] = $finishInfo->is_finish;
  54. $taskList[$k]["finish_number"] = $finishInfo->finish_number;
  55. $taskList[$k]["is_reward"] = $finishInfo->is_reward;
  56. if($finishInfo->is_finish != 1 && $finishInfo->pace != 0) {
  57. $taskList[$k]["pace_txt"] = $finishInfo->finish_number."/".$v["number"];
  58. }
  59. } else {
  60. $taskList[$k]["pace"] = 0;
  61. $taskList[$k]["is_finish"] = 0;
  62. $taskList[$k]["finish_number"] = 0;
  63. $taskList[$k]["is_reward"] = 0;
  64. $taskList[$k]["pace_txt"] = "0/".$v["number"];
  65. }
  66. //女用户获得钱
  67. if ($this->auth->gender == 0) {
  68. $taskList[$k]['exp'] = bcdiv($v['exp'], $bili,2); //对应人民币
  69. }
  70. }
  71. }
  72. } else {
  73. // 获取基本信息
  74. $where = [];
  75. $where["a.type_id"] = 1;
  76. $where["a.is_show"] = 1;
  77. $taskList = Db::name('task')->alias("a")
  78. ->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")
  79. ->join("task_log l", "l.task_id = a.id and l.user_id = ".$this->auth->id, "left")
  80. ->where($where)
  81. // ->autopage()
  82. ->order("is_finish, a.id")
  83. ->select();
  84. $taskList = list_domain_image($taskList,['image']);
  85. if($taskList) {
  86. foreach($taskList as $k => $v) {
  87. $v["pace"] || $taskList[$k]["pace"] = 0;
  88. $v["is_finish"] || $taskList[$k]["is_finish"] = 0;
  89. $v["finish_number"] || $taskList[$k]["finish_number"] = 0;
  90. $v["is_reward"] || $taskList[$k]["is_reward"] = 0;
  91. if($v["is_finish"] != 1 && $v["pace"] != 0) {
  92. $taskList[$k]["pace_txt"] = $v["finish_number"]."/".$v["number"];
  93. }
  94. //男女用户同意获得金币
  95. /*if ($this->auth->gender == 0) {
  96. $taskList[$k]['exp'] = bcdiv($v['exp'], $bili,2); //对应人民币
  97. }*/
  98. }
  99. }
  100. }
  101. $this->success("获取成功!",$taskList);
  102. }
  103. /**
  104. * 领取奖励
  105. */
  106. public function getReward() {
  107. $task_id = $this->request->request("task_id"); // 任务ID
  108. if (!$task_id) {
  109. $this->error(__('Invalid parameters'));
  110. }
  111. // 查询任务信息
  112. $taskInfo = $this->taskModel->where(["id"=>$task_id])->find();
  113. if(!$taskInfo) {
  114. $this->error("任务信息未找到!");
  115. }
  116. // 查询用户完成情况
  117. $where = [];
  118. $where["user_id"] = $this->auth->id;
  119. $where["task_id"] = $task_id;
  120. $where["is_finish"] = 1;
  121. if($taskInfo["type_id"] == 2){
  122. $where["finish_date"] = date("Ymd");
  123. }
  124. $tasklogInfo = $this->tasklogModel->where($where)->find();
  125. if(!$tasklogInfo) {
  126. $this->error("任务还未完成哦!");
  127. }
  128. if($tasklogInfo["is_reward"] == 1) {
  129. $this->error("任务奖励已领取,请勿重复领取!");
  130. }
  131. Db::startTrans();
  132. try{
  133. //增加用户金币
  134. $res1 = model('wallet')->lockChangeAccountRemain($this->auth->id,0, 'gold', $taskInfo["exp"], 20, '完成' . $taskInfo['name'] . '任务', 'task_log', $tasklogInfo['id']);
  135. if ($res1['status'] === false) {
  136. Db::rollback();
  137. $this->error($res1['msg']);
  138. }
  139. // 更新奖励领取状态
  140. $res2 = $this->tasklogModel->update(["is_reward"=>1],["task_id"=>$task_id,"user_id"=>$this->auth->id]);
  141. //系统消息
  142. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'任务奖励','完成' . $taskInfo['name'] . '任务,获得任务奖励');
  143. if($res1['status'] === true && $res2) {
  144. Db::commit();
  145. $this->success("领取成功!");
  146. } else {
  147. Db::rollback();
  148. $this->error("领取失败!");
  149. }
  150. }catch (ValidateException $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. } catch (PDOException $e) {
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. } catch (Exception $e) {
  157. Db::rollback();
  158. $this->error($e->getMessage());
  159. }
  160. }
  161. /**
  162. * 完成任务
  163. */
  164. public function finishTask() {
  165. $task_id = $this->request->request("task_id"); // 任务编码
  166. $number = $this->request->request("number",1); // 完成次数
  167. if (!$task_id) {
  168. $this->error(__('Invalid parameters'));
  169. }
  170. \app\common\model\TaskLog::tofinish($this->auth->id,$task_id,$number);
  171. $this->success("成功!");
  172. }
  173. }