Core.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Score\Lib;
  3. use Dever;
  4. class Core
  5. {
  6. /**
  7. * 增加日志 Dever::load('score/lib/core')->log($uid, $key, $name);
  8. *
  9. * @return mixed
  10. */
  11. public function log($uid, $action_key, $action_name, $content = '')
  12. {
  13. $action = Dever::db('score/action')->one(array('key' => $action_key));
  14. if (!$action) {
  15. $action = array();
  16. $action['id'] = Dever::db('score/action')->insert(array('key' => $action_key, 'name' => $action_name));
  17. }
  18. if (isset($action['id'])) {
  19. $log_id = Dever::db('score/action_log')->insert(array('uid' => $uid, 'action_id' => $action['id'], 'content' => $content));
  20. //Dever::deamon('lib/core.oper?log_id='.$log_id, 'score');
  21. Dever::load('score/lib/core.oper?log_id='.$log_id);
  22. }
  23. }
  24. /**
  25. * 根据用户行为,增加积分
  26. *
  27. * @return mixed
  28. */
  29. public function oper()
  30. {
  31. $log_id = Dever::input('log_id');
  32. $log = Dever::db('score/action_log')->one($log_id);
  33. $this->operAction($log);
  34. }
  35. private function rule($log, $info)
  36. {
  37. if ($info['num'] == 0) {
  38. return;
  39. }
  40. $uid = $log['uid'];
  41. $user = Dever::db('score/user')->one(array('uid' => $uid, 'config_id' => $info['config_id']));
  42. if (!$user) {
  43. $user = array();
  44. $user['id'] = Dever::db('score/user')->insert(array('uid' => $uid, 'config_id' => $info['config_id'], 'score' => 0, 'no_score' => 0));
  45. $user['score'] = 0;
  46. }
  47. $num = $info['num'];
  48. if ($info['upper'] == 2 && $info['upper_limit'] >= 1) {
  49. $upper = $this->upper($uid, $info, $log);
  50. if (!$upper) {
  51. Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 3, 'score' => '0'));
  52. return;
  53. }
  54. Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 2, 'score' => $num));
  55. }
  56. $update = array();
  57. $update['where_id'] = $user['id'];
  58. $update['score'] = $user['score'] + $num;
  59. $insert['uid'] = $uid;
  60. $insert['config_id'] = $info['config_id'];
  61. $insert['action_id'] = $info['action_id'];
  62. $insert['status'] = 1;
  63. $insert['num'] = $num;
  64. $insert['total'] = $update['score'];
  65. Dever::db('score/user_log')->insert($insert);
  66. Dever::db('score/user')->update($update);
  67. }
  68. private function upper($uid, $info)
  69. {
  70. # 有上限限制
  71. $limit = $info['upper_limit'];
  72. # 获取用户最新一次积分变化日志
  73. $time = time();
  74. $where = array();
  75. $where['config_id'] = $info['config_id'];
  76. $where['action_id'] = $info['action_id'];
  77. $where['uid'] = $uid;
  78. $where['status'] = 1;
  79. $user_log = Dever::db('score/user_log')->getNew($where);
  80. if ($user_log) {
  81. if ($info['upper_type'] == 1) {
  82. # 按天
  83. $where['start'] = Dever::maketime(date('Y-m-d 00:00:00', $time));
  84. //$where['end'] = $end;
  85. } elseif ($info['upper_type'] == 2) {
  86. # 按小时
  87. $where['start'] = $time - ($info['upper_time'] * 3600);
  88. //$where['end'] = $end;
  89. } elseif ($info['upper_type'] == 3) {
  90. # 永久
  91. } else {
  92. return true;
  93. }
  94. $user_log_num = Dever::db('score/user_log')->getNewTotal($where);
  95. if ($user_log_num >= $info['upper_limit']) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. private function operAction($log)
  102. {
  103. if ($log && $log['score_type'] == 1) {
  104. $where = array();
  105. $where['state'] = 1;
  106. $where['id'] = $log['action_id'];
  107. $action = Dever::db('score/action')->one($where);
  108. if ($action) {
  109. $rule = Dever::db('score/rule')->state(array('action_id' => $action['id']));
  110. if ($rule) {
  111. foreach ($rule as $k => $v) {
  112. $this->rule($log, $v);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * 根据积分日志和规则,增加积分,这是一个定时任务,暂时不开启了
  120. *
  121. * @return mixed
  122. */
  123. public function cron()
  124. {
  125. $where['score_type'] = 1;
  126. $log = Dever::db('score/action_log')->state($where);
  127. if ($log) {
  128. $this->operAction($log['id']);
  129. }
  130. }
  131. /**
  132. * 根据积分算等级
  133. *
  134. * @return mixed
  135. */
  136. public function level()
  137. {
  138. $level = Dever::db('score/level')->state();
  139. foreach ($level as $k => $v) {
  140. $config = Dever::array_decode($v['score']);
  141. if ($config) {
  142. foreach ($config as $k1 => $v1) {
  143. $score = $v1['config'];
  144. $num = $v1['num'];
  145. $user = Dever::db('score/user')->state(array('config_id' => $score, 'score' => $num));
  146. if ($user) {
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }