Core.php 15 KB

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