Core.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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, $type = false, $type_id = false)
  12. {
  13. if (strstr($action_key, '.')) {
  14. $temp = explode('.', $action_key);
  15. $action_key = $temp[0];
  16. $type = $temp[1];
  17. $type_id = $temp[2];
  18. }
  19. $action = Dever::db('score/action')->one(array('key' => $action_key));
  20. if (!$action) {
  21. $action = array();
  22. $action['id'] = Dever::db('score/action')->insert(array('key' => $action_key, 'name' => $action_name));
  23. }
  24. if (isset($action['id']) && $action['id'] > 0) {
  25. $insert = array();
  26. $parent_uid = 0;
  27. if (strstr($uid, '_')) {
  28. $temp = explode('_', $uid);
  29. $uid = $temp[1];
  30. $parent_uid = $temp[0];
  31. }
  32. $insert['uid'] = $uid;
  33. $insert['action_id'] = $action['id'];
  34. $insert['callback'] = $callback;
  35. if ($score > 0) {
  36. $insert['mscore'] = $score;
  37. }
  38. if ($num > 0) {
  39. $insert['num'] = $num;
  40. }
  41. if ($parent_uid > 0) {
  42. $insert['parent_uid'] = $parent_uid;
  43. }
  44. if ($type) {
  45. $insert['type'] = $type;
  46. }
  47. if ($type_id) {
  48. $insert['type_id'] = $type_id;
  49. }
  50. $insert['cron_type'] = 1;
  51. $log_id = Dever::db('score/action_log')->insert($insert);
  52. Dever::load('score/lib/core.oper_commit?log_id='.$log_id);
  53. return;
  54. $sync = Dever::config('base', 'project')->score['sync'];
  55. if ($sync) {
  56. Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
  57. }
  58. //Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
  59. //Dever::load('score/lib/core.oper?log_id='.$log_id);
  60. }
  61. }
  62. # 获取积分列表
  63. public function getScore()
  64. {
  65. $data = Dever::db('score/config')->state();
  66. return $data;
  67. }
  68. # 获取某一个用户的积分
  69. public function getUserScore($uid, $config_id = false, $type = false, $type_id = false)
  70. {
  71. if ($config_id && $type && $type_id) {
  72. $where['config_id'] = $config_id;
  73. $where['type'] = $type;
  74. $where['type_id'] = $type_id;
  75. $where['state'] = 1;
  76. $child = Dever::db('score/config_child')->one($where);
  77. if ($child) {
  78. $where = array();
  79. $where['uid'] = $uid;
  80. $where['config_child_id'] = $child['id'];
  81. $score = Dever::db('score/user_child')->one($where);
  82. return array('name' => $child['name'], 'score' => $score['score'] ? $score['score'] : 0);
  83. }
  84. }
  85. if (!$config_id) {
  86. $config = Dever::db('score/config')->one(array('type' => 1));
  87. } else {
  88. $config = Dever::db('score/config')->one($config_id);
  89. }
  90. if ($config) {
  91. $where = array();
  92. $where['uid'] = $uid;
  93. $where['config_id'] = $config['id'];
  94. $score = Dever::db('score/user')->one($where);
  95. return array('name' => $config['name'], 'score' => $score['score'] ? $score['score'] : 0);
  96. }
  97. return false;
  98. }
  99. # 获取用户积分日志列表
  100. public function getUserScoreList($uid, $config_id = false, $type = false, $type_id = false)
  101. {
  102. $result = array();
  103. if ($config_id && $type && $type_id) {
  104. $where['config_id'] = $config_id;
  105. $where['type'] = $type;
  106. $where['type_id'] = $type_id;
  107. $where['state'] = 1;
  108. $child = Dever::db('score/config_child')->one($where);
  109. if ($child) {
  110. $where = array();
  111. $where['uid'] = $uid;
  112. $where['config_child_id'] = $child['id'];
  113. $result = Dever::db('score/user_log')->getList($where);
  114. }
  115. }
  116. if (!$result) {
  117. if (!$config_id) {
  118. $config = Dever::db('score/config')->one(array('type' => 1));
  119. } else {
  120. $config = Dever::db('score/config')->one($config_id);
  121. }
  122. if ($config) {
  123. $where = array();
  124. $where['uid'] = $uid;
  125. $where['config_id'] = $config['id'];
  126. $result = Dever::db('score/user_log')->getList($where);
  127. }
  128. }
  129. if ($result) {
  130. $action = Dever::db('score/action')->getData();
  131. foreach ($result as $k => $v) {
  132. $result[$k]['cdate'] = date('Y-m-d H:i:s', $v['cdate']);
  133. if (isset($action[$v['action_id']])) {
  134. $result[$k]['title'] = $action[$v['action_id']]['name'];
  135. } else {
  136. $result[$k]['title'] = '未知';
  137. }
  138. }
  139. }
  140. return $result;
  141. }
  142. # 设置子积分
  143. public function setScoreChild($config_type, $config_id, $type, $type_id, $name)
  144. {
  145. $where['config_id'] = $config_id;
  146. $where['type'] = $type;
  147. $where['type_id'] = $type_id;
  148. $info = Dever::db('score/config_child')->one($where);
  149. if ($info && $config_type == 1) {
  150. $update['where_id'] = $info['id'];
  151. $update['state'] = 2;
  152. Dever::db('score/config_child')->update($update);
  153. } elseif ($config_type == 2) {
  154. if (!$name) {
  155. $config = Dever::db('score/config')->one($config_id);
  156. $name = $config['name'];
  157. }
  158. if ($info) {
  159. $update['where_id'] = $info['id'];
  160. $update['name'] = $name;
  161. $update['state'] = 1;
  162. Dever::db('score/config_child')->update($update);
  163. } else {
  164. $insert = $where;
  165. $insert['name'] = $name;
  166. Dever::db('score/config_child')->insert($insert);
  167. }
  168. }
  169. return true;
  170. }
  171. public function oper_commit_api()
  172. {
  173. }
  174. /**
  175. * 根据用户行为,增加积分
  176. *
  177. * @return mixed
  178. */
  179. public function oper_commit()
  180. {
  181. $log_id = Dever::input('log_id');
  182. $log = Dever::db('score/action_log')->one($log_id);
  183. $this->operAction($log);
  184. return $log;
  185. }
  186. private function rule($log, $info)
  187. {
  188. $config = Dever::db('score/config')->one($info['config_id']);
  189. if (!$config) {
  190. return;
  191. }
  192. if ($config && $config['state'] == 2) {
  193. return;
  194. }
  195. # 手动增加积分
  196. if (isset($log['mscore']) && $log['mscore'] > 0) {
  197. $info['num'] = $log['mscore'];
  198. }
  199. # 积分乘以倍数
  200. if (isset($log['num']) && $log['num'] > 0) {
  201. $info['num'] = $info['num'] * $log['num'];
  202. }
  203. if ($info['num'] == 0) {
  204. # 未加数据
  205. return $this->finish($log);
  206. }
  207. $uid = $log['uid'];
  208. $num = $info['num'];
  209. $this->user = 'score/user';
  210. $this->user_log = 'score/user_log';
  211. $info['config_child_id'] = false;
  212. # 验证是否有子积分设置
  213. if ($log['type'] && $log['type_id']) {
  214. $child = Dever::db('score/config_child')->one(array(
  215. 'config_id' => $info['config_id'], 'type' => $log['type'], 'type_id' => $log['type_id'], 'state' => 1));
  216. if ($child) {
  217. $this->user = 'score/user_child';
  218. $this->user_log = 'score/user_child_log';
  219. $info['config_child_id'] = $child['id'];
  220. }
  221. }
  222. if ($info['upper'] == 2 && $info['upper_limit'] >= 1) {
  223. $upper = $this->upper($uid, $info, $log);
  224. if (!$upper) {
  225. # 达到积分上限
  226. return $this->finish($log, 3, '0');
  227. }
  228. }
  229. # 完成
  230. $this->finish($log, 2, $num);
  231. $user_log = $this->add($uid, $num, $info, $log);
  232. if (!$user_log['id']) {
  233. return;
  234. }
  235. $user_log['type'] = $log['type'];
  236. $user_log['type_id'] = $log['type_id'];
  237. # 增加返利
  238. if ($info['rebate_type'] > 1) {
  239. $this->rebate($uid, $num, $info, $log);
  240. }
  241. if ($log['callback']) {
  242. Dever::load($log['callback'], $user_log);
  243. /*
  244. if (strstr($log['callback'], '?')) {
  245. Dever::load($log['callback'] . '&user_log_id=' . $user_log_id);
  246. } else {
  247. Dever::load($log['callback'], $user_log_id);
  248. }
  249. */
  250. } elseif ($config['callback_type'] > 1 && $config['callback_value']) {
  251. if ($config['callback_type'] == 2) {
  252. Dever::load($config['callback_value'], $user_log);
  253. } elseif ($config['callback_type'] == 3) {
  254. Dever::curl($config['callback_value'], $user_log, 'post');
  255. } elseif ($config['callback_type'] == 4) {
  256. $temp = explode('/', $config['callback_value']);
  257. $config['callback_value'] = str_replace($temp[0] . '/', '', $config['callback_value']);
  258. Dever::daemon($config['callback_value'] . '?log_id='.$user_log['id'], $temp[0]);
  259. }
  260. }
  261. }
  262. # 增加积分
  263. private function add($uid, $num, $info, $log, $rebate_uid = false)
  264. {
  265. $where = array();
  266. $where['uid'] = $uid;
  267. $where['config_id'] = $info['config_id'];
  268. if ($info['config_child_id']) {
  269. $where['config_child_id'] = $info['config_child_id'];
  270. }
  271. $user = Dever::db($this->user)->one($where);
  272. if (!$user) {
  273. $user = array();
  274. $user['id'] = Dever::db($this->user)->insert($where);
  275. $user['score'] = 0;
  276. }
  277. $update = array();
  278. $update['where_id'] = $user['id'];
  279. $update['score'] = $user['score'] + $num;
  280. $insert = $where;
  281. $insert['action_log_id'] = $log['id'];
  282. $insert['action_id'] = $info['action_id'];
  283. if ($rebate_uid) {
  284. $insert['rebate_uid'] = $rebate_uid;
  285. }
  286. $insert['status'] = 1;
  287. $insert['num'] = $num;
  288. $insert['total'] = $update['score'];
  289. $insert['cdate'] = time();
  290. $insert['id'] = Dever::db($this->user_log)->insert($insert);
  291. if ($insert['id']) {
  292. Dever::db($this->user)->update($update);
  293. # 提升等级
  294. $level_type = Dever::db('score/level_type')->state();
  295. if ($level_type) {
  296. foreach ($level_type as $k => $v) {
  297. $this->setLevel($uid, $v);
  298. }
  299. }
  300. }
  301. return $insert;
  302. }
  303. # 返利
  304. private function rebate($uid, $num, $info, $log)
  305. {
  306. if (!strstr($info['rebate_value'], ':')) {
  307. $info['rebate_value'] = '1:' . $info['rebate_value'];
  308. }
  309. $temp = explode(',', $info['rebate_value']);
  310. $invite = Dever::project('invite');
  311. $parent_uid = false;
  312. foreach ($temp as $k => $v) {
  313. $config = explode(':', $v);
  314. $value = $config[1];
  315. if ($info['rebate_type'] == 2) {
  316. # 百分比
  317. $value = round($num*($value/100), 2);
  318. }
  319. # 直属上级
  320. if ($config[0] == 1) {
  321. if ($log['parent_uid'] > 0) {
  322. $parent_uid = $log['parent_uid'];
  323. } elseif ($invite) {
  324. $parent_uid = Dever::load('invite/api')->getParent($uid, $config[0]);
  325. }
  326. if ($parent_uid > 0) {
  327. $this->add($parent_uid, $value, $info, $log, $uid);
  328. }
  329. } elseif ($invite) {
  330. # 上级的上级
  331. if ($parent_uid > 0) {
  332. $rebate_uid = $parent_uid;
  333. $level = $config[0]-1;
  334. } else {
  335. $rebate_uid = $uid;
  336. $level = $config[0];
  337. }
  338. $other_parent_uid = Dever::load('invite/api')->getParent($rebate_uid, $level);
  339. if ($other_parent_uid > 0) {
  340. $this->add($other_parent_uid, $value, $info, $log, $rebate_uid);
  341. }
  342. }
  343. }
  344. }
  345. # 迁移完成的数据
  346. private function finish($log, $score_type = 1, $score = 'no')
  347. {
  348. $id = $log['id'];
  349. if ($score_type == -1) {
  350. Dever::db('score/action_log')->update(array('where_id' => $id, 'cron_type' => 3));
  351. } else {
  352. unset($log['id']);
  353. $log['log_id'] = $id;
  354. $log['cron_type'] = 3;
  355. $log['score_type'] = $score_type;
  356. if ($score != 'no') {
  357. $log['score'] = $score;
  358. }
  359. Dever::db('score/action_log_finish')->insert($log);
  360. Dever::db('score/action_log')->delete($id);
  361. }
  362. return true;
  363. }
  364. # 设置上限限制
  365. private function upper($uid, $info, $log)
  366. {
  367. # 有上限限制
  368. $limit = $info['upper_limit'];
  369. # 获取用户最新一次积分变化日志
  370. $time = time();
  371. $where = array();
  372. if ($info['config_child_id']) {
  373. $where['config_child_id'] = $info['config_child_id'];
  374. }
  375. $where['config_id'] = $info['config_id'];
  376. $where['action_id'] = $info['action_id'];
  377. $where['uid'] = $uid;
  378. $where['status'] = 1;
  379. $user_log = Dever::db($this->user_log)->getNew($where);
  380. if ($user_log) {
  381. if ($info['upper_type'] == 1) {
  382. # 按天
  383. $where['start'] = Dever::maketime(date('Y-m-d 00:00:00', $time));
  384. //$where['end'] = $end;
  385. } elseif ($info['upper_type'] == 2) {
  386. # 按小时
  387. $where['start'] = $time - ($info['upper_time'] * 3600);
  388. //$where['end'] = $end;
  389. } elseif ($info['upper_type'] == 3) {
  390. # 永久
  391. } else {
  392. return true;
  393. }
  394. $user_log_num = Dever::db($this->user_log)->getNewTotal($where);
  395. if ($user_log_num >= $info['upper_limit']) {
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. private function operAction($log)
  402. {
  403. if ($log && $log['cron_type']) {
  404. Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'cron_type' => 2));
  405. $where = array();
  406. $where['state'] = 1;
  407. $where['id'] = $log['action_id'];
  408. $action = Dever::db('score/action')->one($where);
  409. if ($action) {
  410. $rule = Dever::db('score/rule')->state(array('action_id' => $action['id']));
  411. if ($rule) {
  412. foreach ($rule as $k => $v) {
  413. $this->rule($log, $v);
  414. }
  415. }
  416. }
  417. }
  418. }
  419. /**
  420. * 根据积分日志和规则,增加积分,这是一个定时任务,可以设置每天凌晨启动
  421. *
  422. * @return mixed
  423. */
  424. public function cron()
  425. {
  426. $where['cron_type'] = 1;
  427. $log = Dever::db('score/action_log')->all($where);
  428. if ($log) {
  429. foreach ($log as $k => $v) {
  430. $this->operAction($v);
  431. }
  432. }
  433. }
  434. /**
  435. * 根据积分算等级,如果是自动升级,则直接升级,如果是手动升级,则将其标识置为可以升级状态。
  436. *
  437. * @return mixed
  438. */
  439. public function setLevel($uid, $level_type)
  440. {
  441. # 获取当前用户的等级
  442. $where['uid'] = $uid;
  443. $where['level_type_id'] = $level_type['id'];
  444. $user_level = Dever::db('score/user_level')->one($where);
  445. /*
  446. if ($user_level && $user_level['type'] == 2) {
  447. return;
  448. } else
  449. */
  450. $update = array();
  451. if ($user_level) {
  452. $config = Dever::db('score/level')->one($user_level['level_id']);
  453. $level = $config['level'] + 1;
  454. $method = 'update';
  455. $update['where_id'] = $user_level['id'];
  456. } else {
  457. $level = 1;
  458. $method = 'insert';
  459. }
  460. $config = Dever::db('score/level')->one(array('level' => $level, 'level_type_id' => $level_type['id'], 'state' => 1));
  461. if ($config) {
  462. # 是否能升级
  463. $yes = false;
  464. if ($config['total_type'] == 1 && $config['total'] > 0) {
  465. # 查看是否超过总数
  466. $total = Dever::db('score/user_level')->total(array('level_id' => $config['id']));
  467. if ($total >= $config['total']) {
  468. return $yes;
  469. }
  470. }
  471. $rule = Dever::array_decode($config['score']);
  472. if ($rule) {
  473. $user_score = Dever::db($this->user)->getScoreByConfig($where);
  474. if ($user_score) {
  475. foreach ($rule as $k => $v) {
  476. $score = $v['config'];
  477. $num = $v['num'];
  478. if (isset($user_score[$score]) && $user_score[$score]['score'] && $user_score[$score]['score'] >= $num) {
  479. $yes = $config['id'];
  480. # 是否清空积分 当type为自动并且score为2时
  481. if ($level_type['score'] == 2 && $level_type['type'] == 1) {
  482. $user_score_update['where_id'] = $user_score[$score]['id'];
  483. $user_score_update['score'] = $user_score[$score]['score'] - $num;
  484. Dever::db($this->user)->update($user_score_update);
  485. }
  486. } else {
  487. $yes = false;
  488. }
  489. }
  490. }
  491. }
  492. # 可以升级
  493. if ($yes) {
  494. if ($level_type['type'] == 2) {
  495. $update['next_level_id'] = $yes;
  496. $update['level_status'] = 1;
  497. } else {
  498. $update['level_id'] = $update['next_level_id'] = $yes;
  499. $update['level_status'] = 2;
  500. }
  501. $update['uid'] = $uid;
  502. $update['level_type_id'] = $level_type['id'];
  503. Dever::db('score/user_level')->$method($update);
  504. }
  505. }
  506. }
  507. }