init($name, $uid, $type, $type_id); } return static::$instance[$key]; } public function init($name = '', $uid = 0, $type = false, $type_id = false) { $this->uid = $uid; $this->type = Dever::input('type', '', '', $type); $this->type_id = Dever::input('type_id', '', '', $type_id); $this->db = Dever::db('pact/' . $name); } protected function check() { if (!$this->type || !$this->type_id) { Dever::out()->error('传入参数错误'); } } # 更新互动信息 public function up($data = [], $msg = '') { if ($this->getInfo($data)) { $msg && Dever::error($msg); } else { $state = $this->db->insert($this->data); if ($state) { # 这里可以判断一下是否优质的评价 以后加 Dever::load(\Pscore\Lib\Log::class)->action($this->db->config['name'])->add($this->uid); } } return 'ok'; } # 删除 public function del($data = []) { $info = $this->getInfo($data); if ($info) { $state = $this->db->delete($info['id']); if ($state) { Dever::load(\Pscore\Lib\Log::class)->action('取消' . $this->db->config['name'])->add($this->uid); } } return 'ok'; } # 获取用户互动相关的信息 public function getInfo($data = [], $get = false, $field = []) { $this->check(); if ($data) { $this->data = $data; } $this->data['type'] = $this->type; $this->data['type_id'] = $this->type_id; $this->data['uid'] = $this->uid; $info = $this->db->find($this->data); if ($get) { return $this->handleInfo($info, $field); } return $info; } # 获取列表 public function getList($set, $col = 'content') { $where['type'] = $this->type; $where['type_id'] = $this->type_id; $set['col'] = 'id,uid,cdate,' . $col; $data = $this->db->select($where, $set); if ($data) { foreach ($data as &$v) { $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']); unset($v['cdate']); $v['oper'] = 2; if ($this->uid == $v['uid']) { $v['oper'] = 1; } if (isset($v['pic'])) { if ($v['pic']) { $v['pic'] = explode(',', $v['pic']); } else { $v['pic'] = []; } } if (isset($v['open']) && $v['open'] == 2) { $v['user']['name'] = '匿名'; $v['user']['avatar'] = Dever::load(\Puser\Lib\Info::class)->createAvatar('niming'); } else { $v['user'] = Dever::db('puser/info')->find($v['uid'], ['col' => 'name,avatar']); } } } return $data; } # 获取总数 public function getTotal() { $where['type'] = $this->type; $where['type_id'] = $this->type_id; $data = $this->db->count($where); return $data; } # 获取用户的互动列表 public function getUserList($field = [], $page = 10) { $where['uid'] = $this->uid; if ($this->type) { $where['type'] = $this->type; } # 每页10条 $set['num'] = $page; $data = $this->db->select($where, $set); $result = []; if ($data) { foreach ($data as $k => $v) { $info = $this->handleInfo($v, $field); if ($info) { $result[] = $info; } } } return $result; } private function getTypeInfo($type, $type_id) { $app = Dever::config('setting')['type'][$type]; if ($type > 0) { $info = Dever::load($app)->get($type_id); return $info; } } private function handleInfo($v, $field = []) { if (!$v) { return []; } $info = $this->getTypeInfo($v['type'], $v['type_id']); if ($info) { $info['type'] = $v['type']; $info['type_id'] = $v['type_id']; $info['data_id'] = $v['id']; $info['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']); if ($field) { foreach ($field as $v1) { $info[$v1] = $v[$v1]; } } return $info; } } }