Level.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Score\Lib;
  3. use Dever;
  4. class Level
  5. {
  6. # 获取当前用户的等级信息
  7. public function get($uid, $level_type_id = 1, $prefix = 'vip')
  8. {
  9. $result = $this->config($prefix);
  10. $userLevel = Dever::db('score/user_level')->one(array('uid' => $uid, 'level_type_id' => $level_type_id));
  11. if ($userLevel) {
  12. $level = Dever::db('score/level')->one($userLevel['level_id']);
  13. $next_level = Dever::db('score/level')->one(array('level_type_id' => $level_type_id, 'level' => $level['level'] + 1));
  14. $result['current']['id'] = $level['id'];
  15. $result['current']['name'] = $level['name'];
  16. $result['current']['level'] = $level['level'];
  17. $result['current']['key'] .= $level['level'];
  18. $result['current']['status'] = $userLevel['level_status'];
  19. if ($next_level) {
  20. $result['next']['id'] = $next_level['id'];
  21. $result['next']['name'] = $next_level['name'];
  22. $result['next']['level'] = $next_level['level'];
  23. $result['next']['key'] .= $next_level['level'];
  24. }
  25. }
  26. return $result;
  27. }
  28. # 手动升级接口
  29. public function update($uid, $level_type_id)
  30. {
  31. $level_type = Dever::db('score/level_type')->one($level_type_id);
  32. if ($level_type) {
  33. # 将手动转为自动
  34. $level_type['type'] = 1;
  35. Dever::load('score/lib/core')->setLevel($uid, $level_type);
  36. }
  37. return true;
  38. }
  39. private function config($prefix)
  40. {
  41. $result = array
  42. (
  43. # 当前等级
  44. 'current' => array
  45. (
  46. # id
  47. 'id' => '',
  48. # 名称
  49. 'name' => '',
  50. # 等级数字
  51. 'level' => 0,
  52. # 唯一key
  53. 'key' => $prefix,
  54. ),
  55. # 下一级
  56. 'next' => array
  57. (
  58. # id
  59. 'id' => '',
  60. # 名称
  61. 'name' => '',
  62. # 等级数字
  63. 'level' => 0,
  64. # 唯一key
  65. 'key' => $prefix,
  66. ),
  67. );
  68. return $result;
  69. }
  70. }