| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- namespace Task\Src;
- use Dever;
- use Task\Lib\Base;
- class Info extends Base
- {
- /**
- * 任务列表
- *
- * @return mixed
- */
- public function all()
- {
- $this->checkUser();
-
- $this->data['cate'] = Dever::db('task/cate')->state();
- $data = Dever::db('task/info')->getAll();
- if ($data) {
- foreach ($data as $k => $v) {
- $this->getOne($data[$k]);
- }
- }
- return $data;
- }
- /**
- * 用户任务列表
- *
- * @return mixed
- */
- public function user()
- {
- $this->checkUser();
- $this->data['cate'] = Dever::db('task/cate')->state();
- $type = Dever::input('type', 1);
- $data = Dever::db('task/user_task')->getAll(array('where_uid' => $this->data['uid'], 'where_status' => $type));
- if ($data) {
- foreach ($data as $k => $v) {
- $this->getOne($data[$k]);
- }
- }
- return $data;
- }
- /**
- * 任务详情
- *
- * @return mixed
- */
- public function one()
- {
- $this->checkUser();
- $id = Dever::input('id');
- $info = array();
- if ($id > 0) {
- $this->data['cate'] = Dever::db('task/cate')->state();
- $info = Dever::db('task/info')->one($id);
- # 检测用户是否登录,是否认证,仅显示认证信息
- $report_id = Dever::input('report_id', -1);
-
- if ($report_id) {
- $info['report'] = Dever::db('task/user_report')->one($report_id);
- }
- if (!$info['report'] || ($info['report'] && $info['report']['status'] == 3)) {
- $info['report'] = array
- (
- 'id' => -1,
- 'username' => '',
- 'pic' => '',
- 'link' => '',
- 'desc' => '',
- );
- }
- $info['report_submit'] = array();
- if (strstr($info['report_demand'], 'username')) {
- $info['report_submit']['username'] = '#username';
- }
- if (strstr($info['report_demand'], 'pic')) {
- $info['report_submit']['pic'] = '#pic';
- }
- if (strstr($info['report_demand'], 'link')) {
- $info['report_submit']['link'] = '#link';
- }
- if (strstr($info['report_demand'], 'desc')) {
- $info['report_submit']['desc'] = '#desc';
- }
- $info['report_submit']['id'] = '#id';
- $info['report_submit']['report_id'] = '#report_id';
- $info['report_submit'] = str_replace('"', '\'', json_encode($info['report_submit']));
- $info['report']['submit'] = $this->url('task/report.submit', array('id' => $info['id'], 'report_id' => $report_id));
- $this->getOne($info, $button = true);
- $info['button_status'] = $info['button']['status'];
- }
-
- return $info;
- }
- /**
- * 领取任务
- *
- * @return mixed
- */
- public function submit()
- {
- # 检测用户是否登录,是否认证
- $this->checkUser(true);
- $id = Dever::input('id');
- $info = array();
- if ($id > 0) {
- $this->data['cate'] = Dever::db('task/cate')->state();
- $info = Dever::db('task/info')->one($id);
- $this->getOne($info, $button = true);
- if (isset($info['button']) && is_array($info['button']) && isset($info['button']['status']) && $info['button']['status'] > 0) {
- if ($info['button']['status'] == 1) {
- # 可以领取
- return $this->submit_action($info);
- } else {
- Dever::alert($info['button']['desc']);
- }
- } else {
- Dever::alert('领取失败');
- }
- }
- Dever::alert('领取失败');
- }
- /**
- * 立刻领取
- *
- * @return mixed
- */
- private function submit_action($info)
- {
- $user = Dever::db('task/user_task')->one(array('task_id' => $info['id'], 'uid' => $this->data['uid'], 'rand' => Dever::id()));
- if (!$user) {
- $insert['uid'] = $this->data['uid'];
- $insert['task_id'] = $info['id'];
- $insert['company_id'] = $info['company_id'];
- $insert['cate_id'] = $info['cate_id'];
- $insert['level_id'] = $info['level_id'];
- $insert['status'] = 1;
- $info['user_task'] = Dever::db('task/user_task')->insert($insert);
- return '领取成功';
- } else {
- Dever::alert('领取失败');
- }
- }
- /**
- * 任务详情信息
- *
- * @return mixed
- */
- private function getOne(& $data, $button = false)
- {
- # 链接
- $data['link'] = Dever::url('task/view?id=' . $data['id'], 'main');
- # 多少人领取
- $data['user_num'] = Dever::db('task/user_task')->total(array('task_id' => $data['id']));
- # 3 金币/项
- $data['list_score'] = '<strong class="ico-coin">' . $data['score'] . ' ' . $this->data['config']['score_name'] . '</strong>/' . $this->data['config']['report_name'];
- # 组员贡献 1 金币/项
- $data['list_group_score'] = '组员贡献<em>' . $data['group_score'] . '</em>' . $this->data['config']['score_name'] . '/' . $this->data['config']['report_name'];
- #3 金币 + 1 金币
- $data['total_score'] = $data['score'] . ' ' . $this->data['config']['score_name'] . ' + ' . $data['group_score'] . ' ' . $this->data['config']['score_name'];
- # 任务类别
- $data['cate'] = $this->data['cate'][$data['cate_id']];
- # 任务时间
- $data['sdate_str'] = date('Y.m.d', $data['sdate']);
- $data['edate_str'] = date('Y.m.d', $data['edate']);
- $data['report_link'] = Dever::url('task/submit?id=' . $data['id'] . '&nonce=' . Dever::id(), 'main');
- if ($button) {
- $data['button'] = $this->button($data);
- }
- if (isset($data['uid']) && $data['uid'] > 0 && $this->data['uid'] > 0 && $data['uid'] == $this->data['uid']) {
- $data['score'] = $this->score($data);
- }
- }
- /**
- * 当前用户获取的积分
- *
- * @return mixed
- */
- private function score($data)
- {
- # 查看该用户完成的报告数
- $where = array();
- $where['uid'] = $data['uid'];
- $where['status'] = 2;
- $log = Dever::db('task/user_score_log')->getAll($where);
- # 三项描述
- $result = array();
- $result['report'] = $result['group'] = $result['total'] = '';
- $result['report_num'] = $result['report_score'] = $result['group_score'] = $result['total_score'] = 0;
- if ($log) {
- $result['report_num'] = $result['group_report_num'] = 0;
- foreach ($log as $k => $v) {
- if ($v['type'] == 4 && $v['report_id'] > 0) {
- $result['report_score'] += $v['score'];
- $result['report_num']++;
- } elseif ($v['type'] == 5) {
- $result['group_score'] += $v['score'];
- $result['group_report_num']++;
- }
- }
- $result['total_score'] = $result['report_score'] + $result['group_score'];
- if ($result['report_score'] > 0) {
- $result['report'] = '个人已完成 '.$result['report_num'].' 项任务报告,获得 <em>'.$result['report_score'].' ' . $this->data['config']['score_name'] . '</em>';
- }
-
- if ($result['group_score'] > 0) {
- $result['group'] = '组员已完成 '.$result['group_report_num'].' 项任务报告,贡献 <em>'.$result['report_score'].' ' . $this->data['config']['score_name'] . '</em>';
- }
-
- if ($result['total_score'] > 0) {
- $result['total'] = '合计获得 <strong>'.$result['total_score'].'</strong> ' . $this->data['config']['score_name'];
- }
- }
- return $result;
- }
- /**
- * 领取任务按钮的状态
- *
- * @return mixed
- */
- private function button($data)
- {
- # 大于1都不能点击
- $button = 1;
- if ($this->data['time'] > $data['edate']) {
- # 任务已到期
- $button = 2;
- } elseif ($data['user_num'] >= $data['num']) {
- # 抢光了
- $button = 3;
- } elseif (isset($this->data['uid']) && $this->data['uid']) {
- # 判断等级
- $level = Dever::db('task/level')->one($data['level_id']);
- if ($level && isset($this->data['user_score']) && $this->data['user_score'] && $level['score'] > $this->data['user_score']['score']) {
- # 等级不够
- $button = 4;
- } else {
- # 判断是否领取
- $user = Dever::db('task/user_task')->one(array('task_id' => $data['id'], 'uid' => $this->data['uid']));
- if ($user) {
- # 已领取
- $button = 5;
- # 判断是否已经超过报告上限
- $where = array();
- $where['task_id'] = $data['id'];
- $total = Dever::db('task/user_report')->getTotal($where);
- if ($total >= $data['report_num']) {
- $button = 8;
- }
- $where['uid'] = $this->data['uid'];
- $total = Dever::db('task/user_report')->getTotal($where);
- if ($total >= $data['report_user_num']) {
- $button = 9;
- }
- } else {
- # 判断类别领取次数
- $cate = Dever::db('task/user_task')->total(array('uid' => $this->data['uid'], 'cate_id' => $data['cate_id'], 'status' => 3));
- if ($cate >= $data['cate_num']) {
- $button = 6;
- } else {
- # 判断商家领取次数
- $company = Dever::db('task/user_task')->total(array('uid' => $this->data['uid'], 'company_id' => $data['company_id'], 'status' => 3));
- if ($company >= $data['company_num']) {
- $button = 7;
- }
- }
- }
- }
- }
- $config = array
- (
- 1 => '可以领取',
- 2 => '任务已过期',
- 3 => '抢光了',
- 4 => '等级不够',
- 5 => '已领取',
- 6 => '已领取过该商家'.$data['cate']['name'],
- 7 => '已领取过该商家任务',
- 8 => '已超过报告数量上限',
- 9 => '已超过人均报告数量上限',
- );
- $result['status'] = $button;
- $result['name'] = '领取任务';
- $result['desc'] = $config[$button];
- $result['icon'] = '';
- $result['class'] = 'button dever_task_submit';
- $result['link'] = 'javascript:;';
- $result['submit'] = '';
- $result['report_link'] = $data['report_link'];
- if ($result['status'] == 5) {
- $result['name'] = '提交报告';
- $result['link'] = $result['report_link'];
- $result['class'] = 'button';
- $result['desc'] = '<p>' . $result['desc'] . '</p>';
- } elseif ($result['status'] >= 8) {
- $result['name'] = '提交报告';
- $result['class'] = 'button';
- } elseif ($result['status'] > 1) {
- if ($result['status'] == 3) {
- $result['name'] = '抢光了';
- }
- $result['class'] = 'button disable';
- $result['desc'] = '<p>' . $result['desc'] . '</p>';
- } else {
- $result['icon'] = 'ico-coin';
- $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>';
- $result['submit'] = $this->url('task/info.submit', array('id' => $data['id']));
- }
- if ($result['icon']) {
- $result['desc'] = '<em class="'.$result['icon'].'"></em><section>'.$result['desc'].'</section>';
- }
- return $result;
- }
- }
|