Num.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (!$info['num'] || $info['num'] < 0) {
  56. $info['num'] = 0;
  57. }
  58. $where['num'] = $info['num'] + $num;
  59. $db->update($where);
  60. }
  61. }
  62. return true;
  63. }
  64. # 矫正数据用的,放到计划任务中
  65. public function cron()
  66. {
  67. return true;
  68. }
  69. # 订阅数计数器
  70. public function setCounter($id, $num = 0)
  71. {
  72. $key = 'journal_buy_num3_' . $id;
  73. /*
  74. $callback = function() use($id) {
  75. $journal = Dever::db('journal/info')->one($id);
  76. return $journal['num_ding'];
  77. };
  78. Dever::counter($key, $num, $callback);
  79. */
  80. Dever::cache($key, $num);
  81. return true;
  82. }
  83. # 订阅数计数器
  84. public function getCounter($id)
  85. {
  86. $key = 'journal_buy_num3_' . $id;
  87. //return Dever::counter($key);
  88. return Dever::cache($key);
  89. }
  90. /**
  91. * 导出成excel
  92. *
  93. * @return mixed
  94. */
  95. public function out_api()
  96. {
  97. $id = Dever::input('id');
  98. $journal = Dever::db('journal/info')->one($id);
  99. if (!$journal) {
  100. return;
  101. }
  102. $score = $this->getList($id);
  103. $header = $data = array();
  104. $name = $journal['name'];
  105. if ($score) {
  106. $i = 0;
  107. foreach ($score as $k => $v) {
  108. $data[$i] = array
  109. (
  110. $i+1,
  111. $v['username'],
  112. $v['mobile'],
  113. $v['num'],
  114. );
  115. $i++;
  116. }
  117. }
  118. $header = array
  119. (
  120. '排序', '用户名', '手机号', '本数'
  121. );
  122. $file = $name . '_排行榜';
  123. if (Dever::input('test')) {
  124. print_r($header);
  125. print_r($data);die;
  126. }
  127. Dever::excelExport($data, $header, $file, $sheet = 0, $sheetName = $name);
  128. return;
  129. }
  130. }