Num.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Act\Lib;
  3. use Dever;
  4. class Num
  5. {
  6. # 获取我在这个小刊中的本数
  7. public function get($id, $uid)
  8. {
  9. $where['uid'] = $uid;
  10. $where['journal_id'] = $id;
  11. $where['state'] = 1;
  12. $data = Dever::db('act/journal_num')->one($where);
  13. if ($data) {
  14. return $data['num'];
  15. } else {
  16. return 0;
  17. }
  18. }
  19. # 获取小刊的积分排行
  20. public function getList($id, $uid = false)
  21. {
  22. $key = 'journal_num_' . $id;
  23. $data = Dever::cache($key);
  24. if (!$data) {
  25. $where['journal_id'] = $id;
  26. if ($uid > 0) {
  27. $where['uid'] = $uid;
  28. }
  29. $data = Dever::db('act/journal_num')->getAll($where);
  30. if ($data) {
  31. foreach ($data as $k => $v) {
  32. $user = Dever::load('passport/api')->info($v['uid']);
  33. $data[$k]['username'] = $user['username'];
  34. $data[$k]['avatar'] = $user['avatar'];
  35. $data[$k]['mobile'] = $user['mobile'];
  36. }
  37. Dever::cache($key, $data, 600);
  38. }
  39. }
  40. return $data;
  41. }
  42. public function submit($id, $uid = false, $num = false, $update = true)
  43. {
  44. $where['uid'] = $uid;
  45. $where['journal_id'] = $id;
  46. $table = 'act/journal_num';
  47. $db = Dever::db($table);
  48. $info = $db->one($where);
  49. if (!$info) {
  50. $where['num'] = $num;
  51. $db->insert($where);
  52. } else {
  53. if ($update) {
  54. $where['where_id'] = $info['id'];
  55. $where['num'] = $info['num'] + $num;
  56. $db->update($where);
  57. }
  58. }
  59. return true;
  60. }
  61. # 矫正数据用的,放到计划任务中
  62. public function cron()
  63. {
  64. return true;
  65. }
  66. /**
  67. * 导出成excel
  68. *
  69. * @return mixed
  70. */
  71. public function out_api()
  72. {
  73. $id = Dever::input('id');
  74. $journal = Dever::db('journal/info')->one($id);
  75. if (!$journal) {
  76. return;
  77. }
  78. $score = $this->getList($id);
  79. $header = $data = array();
  80. $name = $journal['name'];
  81. if ($score) {
  82. $i = 0;
  83. foreach ($score as $k => $v) {
  84. $data[$i] = array
  85. (
  86. $i+1,
  87. $v['username'],
  88. $v['mobile'],
  89. $v['num'],
  90. );
  91. $i++;
  92. }
  93. }
  94. $header = array
  95. (
  96. '排序', '用户名', '手机号', '本数'
  97. );
  98. $file = $name . '_排行榜';
  99. if (Dever::input('test')) {
  100. print_r($header);
  101. print_r($data);die;
  102. }
  103. Dever::excelExport($data, $header, $file, $sheet = 0, $sheetName = $name);
  104. return;
  105. }
  106. }