Cron.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Task\Lib;
  3. use Dever;
  4. # 放到计划任务中的:
  5. # task/lib/cron.clearTask 一天一次 86400
  6. # task/lib/cron.task_commit 任务报告完成 1个小时1次 3600
  7. # task/lib/cron.cash_commit 兑现审核完成 1个小时1次 3600
  8. class Cron
  9. {
  10. /**
  11. * 将过期的任务,置为已结束,建议一天运行一次
  12. *
  13. * @return mixed
  14. */
  15. public function clearTask()
  16. {
  17. $data = Dever::db('task/info')->gets();
  18. $time = time();
  19. foreach ($data as $k => $v) {
  20. # 未开始的变成进行中
  21. $update['where_id'] = $v['id'];
  22. if ($v['status'] == 2 && $v['sdate'] >= $time) {
  23. $update['status'] = 1;
  24. Dever::db('task/info')->update($update);
  25. } elseif ($v['status'] == 1 && $v['edate'] < $time) {
  26. $update['status'] = 3;
  27. Dever::db('task/info')->update($update);
  28. }
  29. }
  30. }
  31. /**
  32. * 资料认证审核后的操作
  33. *
  34. * @return mixed
  35. */
  36. public function submit_commit()
  37. {
  38. }
  39. /**
  40. * 任务报告完成后的操作
  41. *
  42. * @return mixed
  43. */
  44. public function task_commit()
  45. {
  46. # 审核成功 入账
  47. $where['status'] = 2;
  48. $where['score_status'] = 1;
  49. $data = Dever::db('task/user_report')->state($where);
  50. if ($data) {
  51. foreach ($data as $k => $v) {
  52. $state = $this->updateUserScore($v['uid'], $v['score']);
  53. if ($state) {
  54. $update['where_id'] = $v['id'];
  55. $update['score_status'] = 2;
  56. Dever::db('task/user_report')->update($update);
  57. $this->updateScoreLog($v['uid'], $v['score'], 4, 2, $v['task_id'], $v['id']);
  58. $this->updateMsg($v['uid'], $v['score'], 4, $v['task_id']);
  59. $this->group($v['uid'], $v['group_score'], $v['task_id'], $v['id'], 1);
  60. }
  61. }
  62. }
  63. return 'yes';
  64. }
  65. /**
  66. * 兑现审核完成后的操作
  67. *
  68. * @return mixed
  69. */
  70. public function cash_commit()
  71. {
  72. # 成功
  73. $where['status'] = 3;
  74. $where['cron'] = 1;
  75. $data = Dever::db('task/user_cash')->state($where);
  76. if ($data) {
  77. foreach ($data as $k => $v) {
  78. $update['where_id'] = $v['id'];
  79. $update['cron'] = 2;
  80. Dever::db('task/user_cash')->update($update);
  81. $this->updateScoreLog($v['uid'], $v['score'], 2, 1, -1, -1, $v['type'], $v['cash'], $v['cdate']);
  82. $this->updateMsg($v['uid'], $v['score'], 2, false, false, $v['type'], $v['cash'], $v['cdate']);
  83. $this->sendSms($v['uid'], $v['cash']);
  84. }
  85. }
  86. # 失败
  87. $where['status'] = 4;
  88. $where['cron'] = 1;
  89. $data = Dever::db('task/user_cash')->state($where);
  90. if ($data) {
  91. foreach ($data as $k => $v) {
  92. $update['where_id'] = $v['id'];
  93. $update['cron'] = 2;
  94. Dever::db('task/user_cash')->update($update);
  95. # 失败了就把积分再加回去吧
  96. $this->updateUserScore($v['uid'], $v['score']);
  97. $this->updateScoreLog($v['uid'], $v['score'], 3, 1, -1, -1, $v['type'], $v['cash'], $v['cdate'], $v['status_desc']);
  98. $this->updateMsg($v['uid'], $v['score'], 3, false, false, $v['type'], $v['cash'], $v['cdate'], $v['status_desc']);
  99. }
  100. }
  101. return 'yes';
  102. }
  103. # 发短信
  104. private function sendSms($uid, $cash)
  105. {
  106. $user = Dever::db('passport/user')->one($uid);
  107. if ($user && isset($user['mobile']) && $user['mobile']) {
  108. $url = Dever::load("passport/reg.getUrl", "passport/reg.getMCodeLogin", array("skin" => "cash", "sid" => Dever::id()));
  109. $url .= '&mobile=' . $user['mobile'] . '&type=msg&content=' . $cash;
  110. return Dever::daemon($url);
  111. }
  112. }
  113. /**
  114. * 寻找这个用户的上线 给上线带来收益
  115. *
  116. * @return mixed
  117. */
  118. private function group($uid, $score, $task_id, $report_id, $level = 1)
  119. {
  120. $group_member = Dever::db('task/user_group_member')->one(array('uid' => $uid));
  121. if ($group_member) {
  122. $group_info = Dever::db('task/user_group')->one($group_member['group_id']);
  123. if ($group_info && $group_info['state'] == 1 && $group_info['uid'] > 0 && $group_info['uid'] != $uid) {
  124. $this->updateUserScore($group_info['uid'], $score);
  125. $this->updateScoreLog($group_info['uid'], $score, 5, 2, $task_id, $report_id, 1, 0, 0, '', $uid);
  126. $this->updateMsg($group_info['uid'], $score, 5, $task_id, $uid);
  127. # 继续寻找上线,带来收益
  128. $config = Dever::config('base')->level;
  129. $level = $level + 1;
  130. if ($config >= $level) {
  131. $this->group($group_info['uid'], $score, $task_id, $report_id, $level);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * 更新用户积分数量
  138. *
  139. * @return mixed
  140. */
  141. private function updateUserScore($uid, $score)
  142. {
  143. if ($score > 0) {
  144. $update['where_uid'] = $uid;
  145. $update['score'] = $score;
  146. $update['time'] = Dever::id();
  147. return Dever::db('task/user_score')->updateScore($update);
  148. }
  149. return false;
  150. }
  151. /**
  152. * 日志记录
  153. *
  154. * @return mixed
  155. */
  156. private function updateScoreLog($uid, $score, $type, $status, $task_id = -1, $report_id = -1, $cash_type = 1, $cash = 0, $cash_date = 0, $type_desc = '', $group_uid = false)
  157. {
  158. $insert = array();
  159. $insert['type'] = $type;
  160. $insert['status'] = $status;
  161. $insert['score'] = $score;
  162. $insert['uid'] = $uid;
  163. $insert['group_uid'] = $group_uid;
  164. $insert['task_id'] = $task_id;
  165. $insert['report_id'] = $report_id;
  166. $insert['cash_type'] = $cash_type;
  167. $insert['cash'] = $cash;
  168. if ($cash_date) {
  169. $insert['cash_date'] = $cash_date;
  170. }
  171. $insert['type_desc'] = $type_desc;
  172. Dever::db('task/user_score_log')->insert($insert);
  173. }
  174. /**
  175. * 日志记录
  176. *
  177. * @return mixed
  178. */
  179. private function updateMsg($uid, $score, $type, $task_id = -1, $group_uid = false, $cash_type = 1, $cash = 0, $cash_date = 0, $desc = '')
  180. {
  181. $config = Dever::db('main/config')->one();
  182. if ($type == 4 || $type == 5) {
  183. $task = Dever::db('task/info')->one($task_id);
  184. # 完成任务
  185. $desc = '';
  186. if ($type == 5) {
  187. $desc = '提成';
  188. $user = Dever::db('passport/user')->one($group_uid);
  189. } else {
  190. $user = Dever::db('passport/user')->one($uid);
  191. }
  192. $name = $config['score_name'] . '入账通知';
  193. $content = '入账'.$config['score_name'].':'.$score.$config['score_name'].'
  194. <br /> '.$config['score_name'].'来源:'.$user['username'].'完成任务#'.$task['name'].'的'.$desc.'奖励
  195. <br /> 入账时间:' . date('Y-m-d H:i');
  196. }
  197. if ($cash_type == 1) {
  198. $cash_type = '微信';
  199. } else {
  200. $cash_type = '支付宝';
  201. }
  202. if ($type == 2) {
  203. $name = $config['score_name'].'兑现成功通知';
  204. $content = '本次兑现:'.$score.$config['score_name'].'
  205. <br />兑现途径:'.$cash_type.'
  206. <br />申请兑现时间:' . date('Y-m-d H:i', $cash_date).'
  207. <br />兑现完成时间:' . date('Y-m-d H:i');
  208. }
  209. if ($type == 3) {
  210. $name = $config['score_name'].'兑现失败通知';
  211. $content = '本次兑现:'.$score.$config['score_name'].'
  212. <br />兑现途径:'.$cash_type.'
  213. <br />申请兑现时间:' . date('Y-m-d H:i', $cash_date).'
  214. <br />'.$desc;
  215. }
  216. $type = 11;
  217. if (isset($name)) {
  218. Dever::load('message/lib/data.push', -1, $uid, $name, $content, $type);
  219. }
  220. }
  221. }