281 lines
8.7 KiB
PHP
281 lines
8.7 KiB
PHP
<?php
|
|
defined('WXAPP_CONTENT') OR exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Created by Vim
|
|
* User: lcc
|
|
* Date: 2021/06/24
|
|
* Time: 20:00
|
|
*/
|
|
require_once APPPATH.'controllers/wxapp/Wxapp.php';
|
|
class Subject extends Wxapp{
|
|
|
|
function __construct($inputs, $app_key){
|
|
parent::__construct($inputs, $app_key);
|
|
|
|
$this->login_white = array('get');//登录白名单
|
|
|
|
$this->load->model('subjects_model');
|
|
$this->load->model("subject_user_fond_model", 'fond_model');
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取好评列表
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
protected function get(){
|
|
$id = $this->input_param('id');
|
|
|
|
if($id){
|
|
$this->hit($id);
|
|
return $this->row();
|
|
} else {
|
|
return $this->rows();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 点赞/取消赞
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
protected function put_zan(){
|
|
$id = $this->input_param('id');
|
|
|
|
if(!$id){
|
|
throw new Exception('请求超时', API_CODE_INVILD_PARAM);
|
|
}
|
|
|
|
$row = $this->subjects_model->get(array('id' => $id));
|
|
|
|
if($this->app_id != $row['app_id']){
|
|
debug_log("[error] ". __FUNCTION__ . ": data not belong to this app({$this->app_id}), subject.app_id={$row['app_id']}", $this->log_file);
|
|
throw new Exception('好评不存在', API_CODE_FORB);
|
|
}
|
|
if(self::STATUS_NOR != $row['status']){
|
|
throw new Exception('好评可能下架了', API_CODE_FAIL);
|
|
}
|
|
$zanc = intval($row['zan_num']);
|
|
|
|
$uid = $this->session['uid'];
|
|
|
|
//是否已经存在该用户的赞
|
|
$where = array(
|
|
'app_id' => $this->app_id,
|
|
'app_uid' => $uid,
|
|
'type' => self::FOND_ZAN,
|
|
'ftype' => 0,
|
|
'ftype_id' => $id,
|
|
);
|
|
$count = $this->fond_model->count($where);
|
|
if($count){//存在,取消点赞
|
|
$ret = $this->fond_model->delete($where);
|
|
if(!$ret){
|
|
debug_log("[error] ". __FUNCTION__ . ": " . $this->fond_model->db->last_query(), $this->log_file);
|
|
throw new Exception('操作失败', API_CODE_FAIL);
|
|
}
|
|
$zan = 0;
|
|
$zanc--;
|
|
$upd = array('zan_num = zan_num-1' => null);
|
|
$where = array('id' => $id, 'zan_num >' => 0);
|
|
} else {//不存在,新增点赞
|
|
$add = $where;
|
|
$ret = $this->fond_model->add($add);
|
|
if(!$ret){
|
|
debug_log("[error] ". __FUNCTION__ . ": " . $this->fond_model->db->last_query(), $this->log_file);
|
|
throw new Exception('操作失败', API_CODE_FAIL);
|
|
}
|
|
$zan = 1;
|
|
$zanc++;
|
|
$upd = array('zan_num = zan_num+1' => null);
|
|
$where = array('id' => $id);
|
|
}
|
|
|
|
//变更推荐总数
|
|
$ret = $this->subjects_model->update($upd, $where);
|
|
if(!$ret){
|
|
debug_log("[error] ". __FUNCTION__ . ": " . $this->subjects_model->db->last_query(), $this->log_file);
|
|
}
|
|
|
|
$data = array(
|
|
'zan' => $zan,
|
|
'zanc' => $zanc,
|
|
);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 分享
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
protected function put_share(){
|
|
$id = $this->input_param('id');
|
|
|
|
if(!$id){
|
|
throw new Exception('请求超时', API_CODE_INVILD_PARAM);
|
|
}
|
|
|
|
$row = $this->subjects_model->get(array('id' => $id));
|
|
if($this->app_id != $row['app_id']){
|
|
debug_log("[error] ". __FUNCTION__ . ": data not belong to this app({$this->app_id}), subject.app_id={$row['app_id']}", $this->log_file);
|
|
throw new Exception('好评不存在', API_CODE_FORB);
|
|
}
|
|
if(self::STATUS_NOR != $row['status']){
|
|
throw new Exception('好评可能下架了', API_CODE_FAIL);
|
|
}
|
|
$sharec = intval($row['share_num']);
|
|
|
|
//分享总数
|
|
$upd = array('share_num = share_num+1' => null);
|
|
$where = array('id' => $id);
|
|
$ret = $this->subjects_model->update($upd, $where);
|
|
if(!$ret){
|
|
debug_log("[error] ". __FUNCTION__ . ": " . $this->subjects_model->db->last_query(), $this->log_file);
|
|
throw new Exception('操作失败', API_CODE_FAIL);
|
|
}
|
|
$sharec++;
|
|
$data = array(
|
|
'sharec' => $sharec
|
|
);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 好评详情
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
private function row(){
|
|
$id = $this->input_param('id');
|
|
if(!$id){
|
|
throw new Exception('请求超时', API_CODE_INVILD_PARAM);
|
|
}
|
|
|
|
$uid = $this->session['uid'];
|
|
|
|
$row_sub = $this->subjects_model->get(array('id' => $id,'app_id' => $this->app_id));
|
|
if(!$row_sub){
|
|
throw new Exception('数据不存在', API_CODE_FORB);
|
|
}
|
|
|
|
//获取作者
|
|
$author = $this->app_user_model->get(array('id' => $row_sub['app_uid']));
|
|
//是否点赞
|
|
$zan = 0;
|
|
if($uid){
|
|
//是否点赞
|
|
$where = array('app_id' => $this->app_id, 'type' => self::FOND_ZAN, 'app_uid' => $uid, 'ftype' => 0, 'ftype_id' => $row_sub['id']);
|
|
$count = $this->fond_model->count($where);
|
|
$zan = $count ? 1 : 0;
|
|
}
|
|
|
|
$json_imgs = json_decode($row_sub['imgs'],true);
|
|
$imgs = [];
|
|
if(is_array($json_imgs)){
|
|
foreach($json_imgs as $val){
|
|
$val['img'] && $imgs[] = build_qiniu_image_url($val['img'],750);
|
|
}
|
|
}
|
|
|
|
//去掉内容样式
|
|
$content = preg_replace("/class=['|\"].*?['|\"]/i", '', $row_sub['content']);
|
|
|
|
$data = array(
|
|
'id' => $row_sub['id'],
|
|
'headimg' => $author['headimg'],
|
|
'author' => $author['nickname'],
|
|
'title' => $row_sub['title'],
|
|
'content' => $content,
|
|
'time' => friendly_date($row_sub['c_time']),
|
|
'zan' => $zan,
|
|
'zanc' => intval($row_sub['zan_num']),
|
|
'sharec' => intval($row_sub['share_num']),
|
|
"type" => intval($row_sub["type"]),
|
|
'imgs' => $imgs,
|
|
'video' => $row_sub['video'],
|
|
'status' => isset($v['status']) ? intval($v['status']) : 1,
|
|
);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 好评列表
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
private function rows(){
|
|
$page = $this->input_param('page');
|
|
$size = $this->input_param('size');
|
|
|
|
!$page && $page = 1;
|
|
!$size && $size = 20;
|
|
|
|
$where = array('app_id' => $this->app_id, 'status' => self::STATUS_NOR);
|
|
|
|
$lists = $rows = array();
|
|
$total = $this->subjects_model->count($where);
|
|
if($total){
|
|
$select = 'id, title, type, app_uid, imgs, video, c_time';
|
|
$orderby = "sort desc, id desc";
|
|
$rows = $this->subjects_model->select($where, $orderby, $page, $size, $select);
|
|
$authors = [];
|
|
foreach($rows as $v){
|
|
//获取作者
|
|
$author = $authors[$v['app_uid']];
|
|
if(!$author){
|
|
$authors[$v['app_uid']] = $author = $this->app_user_model->get(array('id' => $v['app_uid']));
|
|
}
|
|
$json_imgs = json_decode($v['imgs'],true);
|
|
$imgs = [];
|
|
if(is_array($json_imgs)){
|
|
foreach($json_imgs as $val){
|
|
$val['img'] && $imgs[] = build_qiniu_image_url($val['img'],750);
|
|
}
|
|
}
|
|
if($v['type'] && $v['video']){
|
|
$imgs[] = $v['video'].'?vframe/jpg/offset/1';
|
|
}
|
|
$lists[] = array(
|
|
'id' => $v['id'],
|
|
'title' => $v['title'],
|
|
"type" => intval($v["type"]),
|
|
'imgs' => $imgs,
|
|
'video' => $v["video"],
|
|
'headimg' => $author['headimg'],
|
|
'author' => $author['nickname'],
|
|
'time' => friendly_date($v['c_time']),
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
$data = array(
|
|
'list' => $lists,
|
|
'total' => intval($total),
|
|
);
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 点击量
|
|
* @param $id
|
|
*/
|
|
private function hit($id){
|
|
$upd = array(
|
|
'hit_num = hit_num+1' => null,
|
|
);
|
|
|
|
$ret = $this->subjects_model->update($upd, array('id' => $id));
|
|
if(!$ret || is_bool($ret)){
|
|
debug_log('[error]' .__FUNCTION__.'# ' . $this->subjects_model->db->last_query(), $this->log_file);
|
|
}
|
|
}
|
|
}
|