Core.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php namespace Pact\Lib;
  2. use Dever;
  3. class Core
  4. {
  5. private $db;
  6. private $type;
  7. private $type_id;
  8. private $uid;
  9. protected $data;
  10. public static $instance = [];
  11. public static function load($name, $uid, $type = false, $type_id = false)
  12. {
  13. $key = $name . $uid;
  14. if (empty(static::$instance[$key])) {
  15. static::$instance[$key] = new self();
  16. static::$instance[$key]->init($name, $uid, $type, $type_id);
  17. }
  18. return static::$instance[$key];
  19. }
  20. public function init($name = '', $uid = 0, $type = false, $type_id = false)
  21. {
  22. $this->uid = $uid;
  23. $this->type = Dever::input('type', '', '', $type);
  24. $this->type_id = Dever::input('type_id', '', '', $type_id);
  25. $this->db = Dever::db('pact/' . $name);
  26. }
  27. protected function check()
  28. {
  29. if (!$this->type || !$this->type_id) {
  30. Dever::out()->error('传入参数错误');
  31. }
  32. }
  33. # 更新互动信息
  34. public function up($data = [], $msg = '')
  35. {
  36. if ($this->getInfo($data)) {
  37. $msg && Dever::error($msg);
  38. } else {
  39. $state = $this->db->insert($this->data);
  40. if ($state) {
  41. # 这里可以判断一下是否优质的评价 以后加
  42. Dever::load(\Pscore\Lib\Log::class)->action($this->db->config['name'])->add($this->uid);
  43. }
  44. }
  45. return 'ok';
  46. }
  47. # 删除
  48. public function del($data = [])
  49. {
  50. $info = $this->getInfo($data);
  51. if ($info) {
  52. $state = $this->db->delete($info['id']);
  53. if ($state) {
  54. Dever::load(\Pscore\Lib\Log::class)->action('取消' . $this->db->config['name'])->add($this->uid);
  55. }
  56. }
  57. return 'ok';
  58. }
  59. # 获取用户互动相关的信息
  60. public function getInfo($data = [], $get = false, $field = [])
  61. {
  62. $this->check();
  63. if ($data) {
  64. $this->data = $data;
  65. }
  66. $this->data['type'] = $this->type;
  67. $this->data['type_id'] = $this->type_id;
  68. $this->data['uid'] = $this->uid;
  69. $info = $this->db->find($this->data);
  70. if ($get) {
  71. return $this->handleInfo($info, $field);
  72. }
  73. return $info;
  74. }
  75. # 获取列表
  76. public function getList($set, $col = 'content')
  77. {
  78. $where['type'] = $this->type;
  79. $where['type_id'] = $this->type_id;
  80. $set['col'] = 'id,uid,cdate,' . $col;
  81. $data = $this->db->select($where, $set);
  82. if ($data) {
  83. foreach ($data as &$v) {
  84. $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
  85. unset($v['cdate']);
  86. $v['oper'] = 2;
  87. if ($this->uid == $v['uid']) {
  88. $v['oper'] = 1;
  89. }
  90. if (isset($v['pic'])) {
  91. if ($v['pic']) {
  92. $v['pic'] = explode(',', $v['pic']);
  93. } else {
  94. $v['pic'] = [];
  95. }
  96. }
  97. if (isset($v['open']) && $v['open'] == 2) {
  98. $v['user']['name'] = '匿名';
  99. $v['user']['avatar'] = Dever::load(\Puser\Lib\Info::class)->createAvatar('niming');
  100. } else {
  101. $v['user'] = Dever::db('puser/info')->find($v['uid'], ['col' => 'name,avatar']);
  102. }
  103. }
  104. }
  105. return $data;
  106. }
  107. # 获取总数
  108. public function getTotal()
  109. {
  110. $where['type'] = $this->type;
  111. $where['type_id'] = $this->type_id;
  112. $data = $this->db->count($where);
  113. return $data;
  114. }
  115. # 获取用户的互动列表
  116. public function getUserList($field = [], $page = 10)
  117. {
  118. $where['uid'] = $this->uid;
  119. if ($this->type) {
  120. $where['type'] = $this->type;
  121. }
  122. # 每页10条
  123. $set['num'] = $page;
  124. $data = $this->db->select($where, $set);
  125. $result = [];
  126. if ($data) {
  127. foreach ($data as $k => $v) {
  128. $info = $this->handleInfo($v, $field);
  129. if ($info) {
  130. $result[] = $info;
  131. }
  132. }
  133. }
  134. return $result;
  135. }
  136. private function getTypeInfo($type, $type_id)
  137. {
  138. $app = Dever::config('setting')['type'][$type];
  139. if ($type > 0) {
  140. $info = Dever::load($app)->get($type_id);
  141. return $info;
  142. }
  143. }
  144. private function handleInfo($v, $field = [])
  145. {
  146. if (!$v) {
  147. return [];
  148. }
  149. $info = $this->getTypeInfo($v['type'], $v['type_id']);
  150. if ($info) {
  151. $info['type'] = $v['type'];
  152. $info['type_id'] = $v['type_id'];
  153. $info['data_id'] = $v['id'];
  154. $info['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
  155. if ($field) {
  156. foreach ($field as $v1) {
  157. $info[$v1] = $v[$v1];
  158. }
  159. }
  160. return $info;
  161. }
  162. }
  163. }