Core.php 6.4 KB

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