Core.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, $callback = '', $score = false, $num = false)
  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']) && $action['id'] > 0) {
  19. $insert = array();
  20. $insert['uid'] = $uid;
  21. $insert['action_id'] = $action['id'];
  22. $insert['callback'] = $callback;
  23. if ($score > 0) {
  24. $insert['mscore'] = $score;
  25. }
  26. if ($num > 0) {
  27. $insert['num'] = $num;
  28. }
  29. $log_id = Dever::db('score/action_log')->insert($insert);
  30. Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
  31. //Dever::load('score/lib/core.oper?log_id='.$log_id);
  32. }
  33. }
  34. /**
  35. * 根据用户行为,增加积分
  36. *
  37. * @return mixed
  38. */
  39. public function oper()
  40. {
  41. $log_id = Dever::input('log_id');
  42. $log = Dever::db('score/action_log')->one($log_id);
  43. $this->operAction($log);
  44. }
  45. private function rule($log, $info)
  46. {
  47. # 手动增加积分
  48. if (isset($log['mscore']) && $log['mscore'] > 0) {
  49. $info['num'] = $log['mscore'];
  50. }
  51. # 积分乘以倍数
  52. if (isset($log['num']) && $log['num'] > 0) {
  53. $info['num'] = $info['num'] * $log['num'];
  54. }
  55. if ($info['num'] == 0) {
  56. return;
  57. }
  58. $uid = $log['uid'];
  59. $user = Dever::db('score/user')->one(array('uid' => $uid, 'config_id' => $info['config_id']));
  60. if (!$user) {
  61. $user = array();
  62. $user['id'] = Dever::db('score/user')->insert(array('uid' => $uid, 'config_id' => $info['config_id'], 'score' => 0, 'no_score' => 0));
  63. $user['score'] = 0;
  64. }
  65. $num = $info['num'];
  66. if ($info['upper'] == 2 && $info['upper_limit'] >= 1) {
  67. $upper = $this->upper($uid, $info, $log);
  68. if (!$upper) {
  69. Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 3, 'score' => '0'));
  70. return;
  71. }
  72. Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 2, 'score' => $num));
  73. }
  74. $update = array();
  75. $update['where_id'] = $user['id'];
  76. $update['score'] = $user['score'] + $num;
  77. $insert['uid'] = $uid;
  78. $insert['config_id'] = $info['config_id'];
  79. $insert['action_id'] = $info['action_id'];
  80. $insert['status'] = 1;
  81. $insert['num'] = $num;
  82. $insert['total'] = $update['score'];
  83. $user_log_id = Dever::db('score/user_log')->insert($insert);
  84. Dever::db('score/user')->update($update);
  85. # 提升等级
  86. $this->setLevel($uid);
  87. if ($log['callback']) {
  88. if (strstr($log['callback'], '?')) {
  89. Dever::load($log['callback'] . '&user_log_id=' . $user_log_id);
  90. } else {
  91. Dever::load($log['callback'], $user_log_id);
  92. }
  93. }
  94. }
  95. private function upper($uid, $info)
  96. {
  97. # 有上限限制
  98. $limit = $info['upper_limit'];
  99. # 获取用户最新一次积分变化日志
  100. $time = time();
  101. $where = array();
  102. $where['config_id'] = $info['config_id'];
  103. $where['action_id'] = $info['action_id'];
  104. $where['uid'] = $uid;
  105. $where['status'] = 1;
  106. $user_log = Dever::db('score/user_log')->getNew($where);
  107. if ($user_log) {
  108. if ($info['upper_type'] == 1) {
  109. # 按天
  110. $where['start'] = Dever::maketime(date('Y-m-d 00:00:00', $time));
  111. //$where['end'] = $end;
  112. } elseif ($info['upper_type'] == 2) {
  113. # 按小时
  114. $where['start'] = $time - ($info['upper_time'] * 3600);
  115. //$where['end'] = $end;
  116. } elseif ($info['upper_type'] == 3) {
  117. # 永久
  118. } else {
  119. return true;
  120. }
  121. $user_log_num = Dever::db('score/user_log')->getNewTotal($where);
  122. if ($user_log_num >= $info['upper_limit']) {
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. private function operAction($log)
  129. {
  130. if ($log && $log['score_type'] == 1) {
  131. $where = array();
  132. $where['state'] = 1;
  133. $where['id'] = $log['action_id'];
  134. $action = Dever::db('score/action')->one($where);
  135. if ($action) {
  136. $rule = Dever::db('score/rule')->state(array('action_id' => $action['id']));
  137. if ($rule) {
  138. foreach ($rule as $k => $v) {
  139. $this->rule($log, $v);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * 根据积分日志和规则,增加积分,这是一个定时任务,暂时不开启了
  147. *
  148. * @return mixed
  149. */
  150. public function cron()
  151. {
  152. $where['score_type'] = 1;
  153. $log = Dever::db('score/action_log')->all($where);
  154. if ($log) {
  155. foreach ($log as $k => $v) {
  156. $this->operAction($v);
  157. }
  158. }
  159. }
  160. /**
  161. * 根据积分算等级
  162. *
  163. * @return mixed
  164. */
  165. public function setLevel($uid)
  166. {
  167. # 获取当前用户的等级
  168. $where['uid'] = $uid;
  169. $user_level = Dever::db('score/user_level')->one($where);
  170. if ($user_level && $user_level['type'] == 2) {
  171. return;
  172. } elseif ($user_level && $user_level['type'] == 1) {
  173. $config = Dever::db('score/level')->one($user_level['level_id']);
  174. $level = $config['level'] + 1;
  175. } else {
  176. $level = 1;
  177. }
  178. $config = Dever::db('score/level')->one(array('level' => $level, 'state' => 1));
  179. if ($config) {
  180. # 是否能升级
  181. $yes = false;
  182. $rule = Dever::array_decode($config['score']);
  183. if ($rule) {
  184. $user_score = Dever::db('score/user')->getScoreByConfig($where);
  185. if ($user_score) {
  186. foreach ($rule as $k => $v) {
  187. $score = $v['config'];
  188. $num = $v['num'];
  189. if (isset($user_score[$score]) && $user_score[$score]['score'] && $user_score[$score]['score'] >= $num) {
  190. $yes = $config['id'];
  191. } else {
  192. $yes = false;
  193. }
  194. }
  195. }
  196. }
  197. if ($yes) {
  198. # 可以升级
  199. if ($user_level) {
  200. Dever::db('score/user_level')->update(array('where_id' => $user_level['id'], 'level_id' => $yes));
  201. } else {
  202. Dever::db('score/user_level')->insert(array('uid' => $uid,'level_id' => $yes, 'type' => 1));
  203. }
  204. }
  205. }
  206. }
  207. }