Info.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Task\Src;
  3. use Dever;
  4. use Task\Lib\Base;
  5. class Info extends Base
  6. {
  7. /**
  8. * 任务列表
  9. *
  10. * @return mixed
  11. */
  12. public function all()
  13. {
  14. $this->checkUser();
  15. $this->data['cate'] = Dever::db('task/cate')->state();
  16. $data = Dever::db('task/info')->getAll();
  17. if ($data) {
  18. foreach ($data as $k => $v) {
  19. $this->getOne($data[$k]);
  20. }
  21. }
  22. return $data;
  23. }
  24. /**
  25. * 用户任务列表
  26. *
  27. * @return mixed
  28. */
  29. public function user()
  30. {
  31. $this->checkUser();
  32. $this->data['cate'] = Dever::db('task/cate')->state();
  33. $type = Dever::input('type', 1);
  34. $data = Dever::db('task/user_task')->getAll(array('where_uid' => $this->data['uid'], 'where_status' => $type));
  35. if ($data) {
  36. foreach ($data as $k => $v) {
  37. $this->getOne($data[$k]);
  38. }
  39. }
  40. return $data;
  41. }
  42. /**
  43. * 任务详情
  44. *
  45. * @return mixed
  46. */
  47. public function one()
  48. {
  49. $this->checkUser();
  50. $id = Dever::input('id');
  51. $info = array();
  52. if ($id > 0) {
  53. $this->data['cate'] = Dever::db('task/cate')->state();
  54. $info = Dever::db('task/info')->one($id);
  55. # 检测用户是否登录,是否认证,仅显示认证信息
  56. $report_id = Dever::input('report_id', -1);
  57. if ($report_id) {
  58. $info['report'] = Dever::db('task/user_report')->one($report_id);
  59. }
  60. if (!$info['report'] || ($info['report'] && $info['report']['status'] == 3)) {
  61. $info['report'] = array
  62. (
  63. 'id' => -1,
  64. 'username' => '',
  65. 'pic' => '',
  66. 'link' => '',
  67. 'desc' => '',
  68. );
  69. }
  70. $info['report_submit'] = array();
  71. if (strstr($info['report_demand'], 'username')) {
  72. $info['report_submit']['username'] = '#username';
  73. }
  74. if (strstr($info['report_demand'], 'pic')) {
  75. $info['report_submit']['pic'] = '#pic';
  76. }
  77. if (strstr($info['report_demand'], 'link')) {
  78. $info['report_submit']['link'] = '#link';
  79. }
  80. if (strstr($info['report_demand'], 'desc')) {
  81. $info['report_submit']['desc'] = '#desc';
  82. }
  83. $info['report_submit']['id'] = '#id';
  84. $info['report_submit']['report_id'] = '#report_id';
  85. $info['report_submit'] = str_replace('"', '\'', json_encode($info['report_submit']));
  86. $info['report']['submit'] = $this->url('task/report.submit', array('id' => $info['id'], 'report_id' => $report_id));
  87. $this->getOne($info, $button = true);
  88. $info['button_status'] = $info['button']['status'];
  89. }
  90. return $info;
  91. }
  92. /**
  93. * 领取任务
  94. *
  95. * @return mixed
  96. */
  97. public function submit()
  98. {
  99. # 检测用户是否登录,是否认证
  100. $this->checkUser(true);
  101. $id = Dever::input('id');
  102. $info = array();
  103. if ($id > 0) {
  104. $this->data['cate'] = Dever::db('task/cate')->state();
  105. $info = Dever::db('task/info')->one($id);
  106. $this->getOne($info, $button = true);
  107. if (isset($info['button']) && is_array($info['button']) && isset($info['button']['status']) && $info['button']['status'] > 0) {
  108. if ($info['button']['status'] == 1) {
  109. # 可以领取
  110. return $this->submit_action($info);
  111. } else {
  112. Dever::alert($info['button']['desc']);
  113. }
  114. } else {
  115. Dever::alert('领取失败');
  116. }
  117. }
  118. Dever::alert('领取失败');
  119. }
  120. /**
  121. * 立刻领取
  122. *
  123. * @return mixed
  124. */
  125. private function submit_action($info)
  126. {
  127. $user = Dever::db('task/user_task')->one(array('task_id' => $info['id'], 'uid' => $this->data['uid'], 'rand' => Dever::id()));
  128. if (!$user) {
  129. $insert['uid'] = $this->data['uid'];
  130. $insert['task_id'] = $info['id'];
  131. $insert['company_id'] = $info['company_id'];
  132. $insert['cate_id'] = $info['cate_id'];
  133. $insert['level_id'] = $info['level_id'];
  134. $insert['status'] = 1;
  135. $info['user_task'] = Dever::db('task/user_task')->insert($insert);
  136. return '领取成功';
  137. } else {
  138. Dever::alert('领取失败');
  139. }
  140. }
  141. /**
  142. * 任务详情信息
  143. *
  144. * @return mixed
  145. */
  146. private function getOne(& $data, $button = false)
  147. {
  148. # 链接
  149. $data['link'] = Dever::url('task/view?id=' . $data['id'], 'main');
  150. # 多少人领取
  151. $data['user_num'] = Dever::db('task/user_task')->total(array('task_id' => $data['id']));
  152. # 3 金币/项
  153. $data['list_score'] = '<strong class="ico-coin">' . $data['score'] . ' ' . $this->data['config']['score_name'] . '</strong>/' . $this->data['config']['report_name'];
  154. # 组员贡献 1 金币/项
  155. $data['list_group_score'] = '组员贡献<em>' . $data['group_score'] . '</em>' . $this->data['config']['score_name'] . '/' . $this->data['config']['report_name'];
  156. #3 金币 + 1 金币
  157. $data['total_score'] = $data['score'] . ' ' . $this->data['config']['score_name'] . ' + ' . $data['group_score'] . ' ' . $this->data['config']['score_name'];
  158. # 任务类别
  159. $data['cate'] = $this->data['cate'][$data['cate_id']];
  160. # 任务时间
  161. $data['sdate_str'] = date('Y.m.d', $data['sdate']);
  162. $data['edate_str'] = date('Y.m.d', $data['edate']);
  163. $data['report_link'] = Dever::url('task/submit?id=' . $data['id'] . '&nonce=' . Dever::id(), 'main');
  164. if ($button) {
  165. $data['button'] = $this->button($data);
  166. }
  167. if (isset($data['uid']) && $data['uid'] > 0 && $this->data['uid'] > 0 && $data['uid'] == $this->data['uid']) {
  168. $data['score'] = $this->score($data);
  169. }
  170. }
  171. /**
  172. * 当前用户获取的积分
  173. *
  174. * @return mixed
  175. */
  176. private function score($data)
  177. {
  178. # 查看该用户完成的报告数
  179. $where = array();
  180. $where['uid'] = $data['uid'];
  181. $where['status'] = 2;
  182. $log = Dever::db('task/user_score_log')->getAll($where);
  183. # 三项描述
  184. $result = array();
  185. $result['report'] = $result['group'] = $result['total'] = '';
  186. $result['report_num'] = $result['report_score'] = $result['group_score'] = $result['total_score'] = 0;
  187. if ($log) {
  188. $result['report_num'] = $result['group_report_num'] = 0;
  189. foreach ($log as $k => $v) {
  190. if ($v['type'] == 4 && $v['report_id'] > 0) {
  191. $result['report_score'] += $v['score'];
  192. $result['report_num']++;
  193. } elseif ($v['type'] == 5) {
  194. $result['group_score'] += $v['score'];
  195. $result['group_report_num']++;
  196. }
  197. }
  198. $result['total_score'] = $result['report_score'] + $result['group_score'];
  199. if ($result['report_score'] > 0) {
  200. $result['report'] = '个人已完成 '.$result['report_num'].' 项任务报告,获得 <em>'.$result['report_score'].' ' . $this->data['config']['score_name'] . '</em>';
  201. }
  202. if ($result['group_score'] > 0) {
  203. $result['group'] = '组员已完成 '.$result['group_report_num'].' 项任务报告,贡献 <em>'.$result['report_score'].' ' . $this->data['config']['score_name'] . '</em>';
  204. }
  205. if ($result['total_score'] > 0) {
  206. $result['total'] = '合计获得 <strong>'.$result['total_score'].'</strong> ' . $this->data['config']['score_name'];
  207. }
  208. }
  209. return $result;
  210. }
  211. /**
  212. * 领取任务按钮的状态
  213. *
  214. * @return mixed
  215. */
  216. private function button($data)
  217. {
  218. # 大于1都不能点击
  219. $button = 1;
  220. if ($this->data['time'] > $data['edate']) {
  221. # 任务已到期
  222. $button = 2;
  223. } elseif ($data['user_num'] >= $data['num']) {
  224. # 抢光了
  225. $button = 3;
  226. } elseif (isset($this->data['uid']) && $this->data['uid']) {
  227. # 判断等级
  228. $level = Dever::db('task/level')->one($data['level_id']);
  229. if ($level && isset($this->data['user_score']) && $this->data['user_score'] && $level['score'] > $this->data['user_score']['score']) {
  230. # 等级不够
  231. $button = 4;
  232. } else {
  233. # 判断是否领取
  234. $user = Dever::db('task/user_task')->one(array('task_id' => $data['id'], 'uid' => $this->data['uid']));
  235. if ($user) {
  236. # 已领取
  237. $button = 5;
  238. # 判断是否已经超过报告上限
  239. $where = array();
  240. $where['task_id'] = $data['id'];
  241. $total = Dever::db('task/user_report')->getTotal($where);
  242. if ($total >= $data['report_num']) {
  243. $button = 8;
  244. }
  245. $where['uid'] = $this->data['uid'];
  246. $total = Dever::db('task/user_report')->getTotal($where);
  247. if ($total >= $data['report_user_num']) {
  248. $button = 9;
  249. }
  250. } else {
  251. # 判断类别领取次数
  252. $cate = Dever::db('task/user_task')->total(array('uid' => $this->data['uid'], 'cate_id' => $data['cate_id'], 'status' => 3));
  253. if ($cate >= $data['cate_num']) {
  254. $button = 6;
  255. } else {
  256. # 判断商家领取次数
  257. $company = Dever::db('task/user_task')->total(array('uid' => $this->data['uid'], 'company_id' => $data['company_id'], 'status' => 3));
  258. if ($company >= $data['company_num']) {
  259. $button = 7;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. $config = array
  266. (
  267. 1 => '可以领取',
  268. 2 => '任务已过期',
  269. 3 => '抢光了',
  270. 4 => '等级不够',
  271. 5 => '已领取',
  272. 6 => '已领取过该商家'.$data['cate']['name'],
  273. 7 => '已领取过该商家任务',
  274. 8 => '已超过报告数量上限',
  275. 9 => '已超过人均报告数量上限',
  276. );
  277. $result['status'] = $button;
  278. $result['name'] = '领取任务';
  279. $result['desc'] = $config[$button];
  280. $result['icon'] = '';
  281. $result['class'] = 'button dever_task_submit';
  282. $result['link'] = 'javascript:;';
  283. $result['submit'] = '';
  284. $result['report_link'] = $data['report_link'];
  285. if ($result['status'] == 5) {
  286. $result['name'] = '提交报告';
  287. $result['link'] = $result['report_link'];
  288. $result['class'] = 'button';
  289. $result['desc'] = '<p>' . $result['desc'] . '</p>';
  290. } elseif ($result['status'] >= 8) {
  291. $result['name'] = '提交报告';
  292. $result['class'] = 'button';
  293. } elseif ($result['status'] > 1) {
  294. if ($result['status'] == 3) {
  295. $result['name'] = '抢光了';
  296. }
  297. $result['class'] = 'button disable';
  298. $result['desc'] = '<p>' . $result['desc'] . '</p>';
  299. } else {
  300. $result['icon'] = 'ico-coin';
  301. $result['desc'] = '<p>任务奖励' . $data['score'] . $this->data['config']['score_name'] . '<span>/' . $this->data['config']['report_name'] . '</span></p><p>组员贡献' . $data['group_score'] . $this->data['config']['score_name'] . '<span>/' . $this->data['config']['report_name'] . '</span></p>';
  302. $result['submit'] = $this->url('task/info.submit', array('id' => $data['id']));
  303. }
  304. if ($result['icon']) {
  305. $result['desc'] = '<em class="'.$result['icon'].'"></em><section>'.$result['desc'].'</section>';
  306. }
  307. return $result;
  308. }
  309. }