Core.php 6.5 KB

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