Subscribe.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Act\Lib;
  3. use Dever;
  4. class Subscribe
  5. {
  6. private $key = 'use_sub_';
  7. # 获取小刊是否被人订阅
  8. public function get($id, $uid, $type = 4, $cache = false)
  9. {
  10. $key = $this->key . $uid . '_' . $id;
  11. $data = Dever::cache($key);
  12. if ($data == 1) {
  13. return 1;
  14. } else {
  15. if (!$cache) {
  16. $where['uid'] = $uid;
  17. $where['type'] = $type;
  18. $where['data_id'] = $id;
  19. $where['state'] = 1;
  20. $data = Dever::db('act/subscribe')->one($where);
  21. if ($data) {
  22. Dever::cache($key, 1, 864000);
  23. return 1;
  24. } else {
  25. return 2;
  26. }
  27. }
  28. return 2;
  29. }
  30. }
  31. public function getInfo($id, $uid, $type = 4)
  32. {
  33. $where['uid'] = $uid;
  34. $where['type'] = $type;
  35. $where['data_id'] = $id;
  36. $where['state'] = 1;
  37. $data = Dever::db('act/subscribe')->one($where);
  38. return $data;
  39. }
  40. # 获取小刊订阅列表
  41. public function getList($id, $type = 4)
  42. {
  43. $key = 'subscribe_' . $type . '_' . $id;
  44. //$data = Dever::cache($key);
  45. $data = array();
  46. if (!$data) {
  47. $where['type'] = $type;
  48. $where['data_id'] = $id;
  49. $where['avatar'] = 'null';
  50. $data = Dever::db('act/subscribe')->getAll($where);
  51. if ($data) {
  52. //Dever::cache($key, $data, 900);
  53. }
  54. }
  55. /*
  56. if ($data) {
  57. foreach ($data as $k => $v) {
  58. $user = Dever::load('passport/api')->info($v['uid']);
  59. $data[$k]['username'] = $user['username'];
  60. $data[$k]['avatar'] = $user['avatar'];
  61. }
  62. }
  63. */
  64. return $data;
  65. }
  66. # 订阅
  67. public function submit($uid, $id, $source = 1, $type = 4)
  68. {
  69. $where['uid'] = $uid;
  70. $where['data_id'] = $id;
  71. $where['type'] = $type;
  72. $table = 'act/subscribe';
  73. $info = Dever::db($table)->one($where);
  74. $key = $this->key . $uid . '_' . $id;
  75. Dever::cache($key, 1, 864000);
  76. if (!$info) {
  77. $where['source'] = $source;
  78. Dever::db($table)->insert($where);
  79. return true;
  80. }
  81. return false;
  82. # 更新订阅数
  83. $where = array();
  84. $where['data_id'] = $id;
  85. $where['type'] = $type;
  86. $where['state'] = 1;
  87. $total = Dever::db($table)->total($where);
  88. $table = Dever::config('base')->type_table[$type];
  89. Dever::db($table)->update(array('where_id' => $id, 'num_ding' => $total));
  90. return false;
  91. }
  92. }