123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php namespace Pact\Lib;
- use Dever;
- class Core
- {
- private $db;
- private $type;
- private $type_id;
- private $uid;
- protected $data;
- public static $instance = [];
- public static function load($name, $uid, $type = false, $type_id = false)
- {
- $key = $name . $uid;
- if (empty(static::$instance[$key])) {
- static::$instance[$key] = new self();
- static::$instance[$key]->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;
- }
- }
- }
|