shopxo/app/service/AnswerService.php

469 lines
15 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2021-07-18 23:42:10 +08:00
use think\facade\Db;
2019-07-18 22:49:55 +08:00
use app\service\UserService;
use app\service\AdminService;
2021-12-10 00:26:00 +08:00
use app\service\SystemBaseService;
2018-12-28 18:58:37 +08:00
/**
* 问答/留言服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class AnswerService
{
/**
* 总数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [array] $where [条件]
*/
public static function AnswerTotal($where = [])
2018-12-28 18:58:37 +08:00
{
return (int) Db::name('Answer')->where($where)->count();
}
/**
* 列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function AnswerList($params = [])
2018-12-28 18:58:37 +08:00
{
$where = empty($params['where']) ? [] : $params['where'];
2019-03-07 18:28:01 +08:00
$field = empty($params['field']) ? '*' : $params['field'];
2018-12-28 18:58:37 +08:00
$order_by = empty($params['order_by']) ? 'id desc' : $params['order_by'];
$m = isset($params['m']) ? intval($params['m']) : 0;
$n = isset($params['n']) ? intval($params['n']) : 10;
2018-12-28 18:58:37 +08:00
// 获取数据列表
2021-07-18 23:42:10 +08:00
$data = Db::name('Answer')->field($field)->where($where)->limit($m, $n)->order($order_by)->select()->toArray();
2022-08-02 17:23:10 +08:00
return DataReturn('处理成功', 0, self::AnswerListHandle($data, $params));
}
/**
* 列表数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-08-01
* @desc description
* @param [array] $data [数据列表]
* @param [array] $params [输入参数]
*/
public static function AnswerListHandle($data, $params = [])
{
2018-12-28 18:58:37 +08:00
if(!empty($data))
{
2021-12-10 00:26:00 +08:00
// 用户默认头像
$default_avatar = SystemBaseService::AttachmentHost().'/static/index/'.strtolower(MyFileConfig('common_default_theme', '', 'default', true)).'/images/default-user-avatar.jpg';
2018-12-28 18:58:37 +08:00
foreach($data as &$v)
{
// 用户信息
2019-03-07 18:28:01 +08:00
if(isset($v['user_id']))
{
2019-05-15 16:50:28 +08:00
$user = UserService::GetUserViewInfo($v['user_id']);
if(!isset($params['is_public']) || $params['is_public'] == 1)
{
$v['user'] = [
2021-12-10 00:26:00 +08:00
'avatar' => empty($user['avatar']) ? $default_avatar : $user['avatar'],
'user_name_view' => empty($user['user_name_view']) ? '网友' : $user['user_name_view'],
2019-05-15 16:50:28 +08:00
];
} else {
$v['user'] = $user;
}
2019-03-07 18:28:01 +08:00
}
2018-12-28 18:58:37 +08:00
2019-04-07 22:27:39 +08:00
// 内容
2020-06-22 23:21:23 +08:00
if(!empty($v['content']) && APPLICATION == 'web')
2019-04-07 22:27:39 +08:00
{
$v['content'] = str_replace("\n", '<br />', $v['content']);
}
// 回复内容
2020-06-22 23:21:23 +08:00
if(!empty($v['reply']) && APPLICATION == 'web')
2019-04-07 22:27:39 +08:00
{
$v['reply'] = str_replace("\n", '<br />', $v['reply']);
}
2019-03-11 10:58:09 +08:00
// 回复时间
2020-08-15 23:36:45 +08:00
if(isset($v['reply_time']))
{
$v['reply_time_time'] = empty($v['reply_time']) ? null : date('Y-m-d H:i:s', $v['reply_time']);
$v['reply_time_date'] = empty($v['reply_time']) ? null : date('Y-m-d', $v['reply_time']);
}
2019-03-11 10:58:09 +08:00
2020-08-15 23:36:45 +08:00
// 添加时间
if(isset($v['add_time']))
{
$v['add_time_time'] = date('Y-m-d H:i:s', $v['add_time']);
$v['add_time_date'] = date('Y-m-d', $v['add_time']);
}
2018-12-28 18:58:37 +08:00
// 更新时间
2020-08-15 23:36:45 +08:00
if(isset($v['upd_time']))
{
$v['upd_time_time'] = empty($v['upd_time']) ? null : date('Y-m-d H:i:s', $v['upd_time']);
$v['upd_time_date'] = empty($v['upd_time']) ? null : date('Y-m-d', $v['upd_time']);
}
2018-12-28 18:58:37 +08:00
}
}
2022-08-02 17:23:10 +08:00
return $data;
2018-12-28 18:58:37 +08:00
}
/**
* 列表条件
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function AnswerListWhere($params = [])
2018-12-28 18:58:37 +08:00
{
2019-05-15 16:50:28 +08:00
$where = [];
2018-12-28 18:58:37 +08:00
// id
if(!empty($params['id']))
{
$where[] = ['id', '=', $params['id']];
}
// 用户id
if(!empty($params['user']))
{
$where[] = ['user_id', '=', $params['user']['id']];
}
if(!empty($params['keywords']))
{
2019-03-11 17:57:15 +08:00
$where[] = ['name|tel|title|content', 'like', '%'.$params['keywords'].'%'];
2018-12-28 18:58:37 +08:00
}
// 是否更多条件
if(isset($params['is_more']) && $params['is_more'] == 1)
{
// 等值
if(isset($params['is_show']) && $params['is_show'] > -1)
{
$where[] = ['is_show', '=', intval($params['is_show'])];
}
2019-03-08 17:36:45 +08:00
if(isset($params['is_reply']) && $params['is_reply']> -1)
{
$where[] = ['is_reply', '=', intval($params['is_reply'])];
}
2018-12-28 18:58:37 +08:00
if(!empty($params['time_start']))
{
$where[] = ['add_time', '>', strtotime($params['time_start'])];
}
if(!empty($params['time_end']))
{
$where[] = ['add_time', '<', strtotime($params['time_end'])];
}
}
return $where;
}
/**
2019-03-31 01:22:56 +08:00
* 用户留言保存
2018-12-28 18:58:37 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-07-17
* @desc description
* @param [array] $params [输入参数]
*/
2019-03-31 01:22:56 +08:00
public static function AnswerSave($params = [])
2018-12-28 18:58:37 +08:00
{
2019-07-18 22:49:55 +08:00
// 是否开启登录留言,管理员登录状态可继续操作
2022-05-16 16:35:50 +08:00
$admin = AdminService::LoginInfo();
if(MyC('common_is_login_answer') == 1 && empty($admin))
2019-07-18 22:49:55 +08:00
{
$user = UserService::LoginUserInfo();
if(empty($user))
{
return DataReturn('请先登录', -400);
}
}
2018-12-28 18:58:37 +08:00
// 参数校验
$p = [
[
2019-03-11 10:58:09 +08:00
'checked_type' => 'length',
2018-12-28 18:58:37 +08:00
'key_name' => 'name',
2019-03-11 10:58:09 +08:00
'checked_data' => '30',
'is_checked' => 1,
'error_msg' => '联系人最多30个字符',
2018-12-28 18:58:37 +08:00
],
[
2019-03-07 18:28:01 +08:00
'checked_type' => 'isset',
2018-12-28 18:58:37 +08:00
'key_name' => 'tel',
'error_msg' => '联系电话有误',
],
2019-03-11 10:58:09 +08:00
[
'checked_type' => 'length',
'key_name' => 'title',
'checked_data' => '60',
'is_checked' => 1,
'error_msg' => '标题最多60个字符',
],
2018-12-28 18:58:37 +08:00
[
'checked_type' => 'empty',
'key_name' => 'content',
2019-04-07 22:27:39 +08:00
'error_msg' => '详细内容不能为空',
],
[
'checked_type' => 'length',
'key_name' => 'content',
'checked_data' => '1000',
'error_msg' => '详细内容格式 2~1000 个字符',
],
2018-12-28 18:58:37 +08:00
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 开始操作
$data = [
2019-03-31 01:22:56 +08:00
'user_id' => isset($params['user']['id']) ? intval($params['user']['id']) : (isset($params['user_id']) ? intval($params['user_id']) : 0),
2019-06-21 09:50:51 +08:00
'name' => isset($params['name']) ? $params['name'] : '',
'tel' => isset($params['tel']) ? $params['tel'] : '',
'title' => isset($params['title']) ? $params['title'] : '',
'content' => $params['content'],
'reply' => isset($params['reply']) ? $params['reply'] : '',
2019-03-31 01:22:56 +08:00
'access_count' => isset($params['access_count']) ? intval($params['access_count']) : 0,
'is_reply' => isset($params['is_reply']) ? intval($params['is_reply']) : 0,
'is_show' => isset($params['is_show']) ? intval($params['is_show']) : 0,
2018-12-28 18:58:37 +08:00
'add_time' => time(),
];
2019-03-31 01:22:56 +08:00
// 回复时间
$data['reply_time'] = (isset($data['is_reply']) && $data['is_reply'] == 1) ? time() : 0;
// 不存在添加,则更新
if(empty($params['id']))
2018-12-28 18:58:37 +08:00
{
2019-03-31 01:22:56 +08:00
$data['add_time'] = time();
if(Db::name('Answer')->insertGetId($data) > 0)
{
return DataReturn('提交成功', 0);
}
return DataReturn('提交失败', -100);
} else {
$data['upd_time'] = time();
if(Db::name('Answer')->where(['id'=>intval($params['id'])])->update($data))
{
return DataReturn('编辑成功', 0);
}
return DataReturn('编辑失败', -100);
2018-12-28 18:58:37 +08:00
}
}
/**
* 删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-30
* @desc description
* @param [array] $params [输入参数]
*/
public static function AnswerDelete($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
2020-06-22 23:21:23 +08:00
'key_name' => 'ids',
2018-12-28 18:58:37 +08:00
'error_msg' => '操作id有误',
],
[
'checked_type' => 'empty',
'key_name' => 'user_type',
'error_msg' => '用户类型有误',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
2020-06-22 23:21:23 +08:00
// 是否数组
if(!is_array($params['ids']))
{
$params['ids'] = explode(',', $params['ids']);
}
2018-12-28 18:58:37 +08:00
// 条件
$where = [
2020-06-22 23:21:23 +08:00
'id' => $params['ids'],
2018-12-28 18:58:37 +08:00
];
// 用户类型
if($params['user_type'] == 'user')
{
if(empty($params['user']))
{
return DataReturn('用户信息有误', -1);
}
$where['user_id'] = $params['user']['id'];
}
// 开始删除
2019-05-15 16:50:28 +08:00
if(Db::name('Answer')->where($where)->delete())
2018-12-28 18:58:37 +08:00
{
return DataReturn('删除成功', 0);
}
2020-06-22 23:21:23 +08:00
return DataReturn('删除失败', -1);
2018-12-28 18:58:37 +08:00
}
/**
* 回复
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-18
* @desc description
* @param [array] $params [输入参数]
*/
public static function AnswerReply($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'id',
'error_msg' => '操作id有误',
],
[
'checked_type' => 'empty',
'key_name' => 'reply',
'error_msg' => '回复内容不能为空',
],
[
'checked_type' => 'length',
'key_name' => 'reply',
2019-04-07 22:27:39 +08:00
'checked_data' => '2,1000',
'error_msg' => '回复内容格式 2~1000 个字符',
2018-12-28 18:58:37 +08:00
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 条件
$where = [
'id' => intval($params['id']),
];
// 问答是否存在
$temp = Db::name('Answer')->where($where)->field('id')->find();
if(empty($temp))
{
return DataReturn('资源不存在或已被删除', -2);
}
// 更新问答
$data = [
2019-03-11 10:58:09 +08:00
'reply' => $params['reply'],
'is_reply' => 1,
'reply_time' => time(),
'upd_time' => time()
2018-12-28 18:58:37 +08:00
];
if(Db::name('Answer')->where($where)->update($data))
{
return DataReturn('操作成功');
}
return DataReturn('操作失败', -100);
}
/**
* 状态更新
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-06T21:31:53+0800
* @param [array] $params [输入参数]
*/
public static function AnswerStatusUpdate($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'id',
'error_msg' => '操作id有误',
],
2020-06-22 23:21:23 +08:00
[
'checked_type' => 'empty',
'key_name' => 'field',
'error_msg' => '字段有误',
],
2018-12-28 18:58:37 +08:00
[
'checked_type' => 'in',
'key_name' => 'state',
'checked_data' => [0,1],
'error_msg' => '状态有误',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 数据更新
2020-06-22 23:21:23 +08:00
if(Db::name('Answer')->where(['id'=>intval($params['id'])])->update([$params['field']=>intval($params['state']), 'upd_time'=>time()]))
2018-12-28 18:58:37 +08:00
{
2020-06-22 23:21:23 +08:00
return DataReturn('编辑成功');
2018-12-28 18:58:37 +08:00
}
2020-06-22 23:21:23 +08:00
return DataReturn('编辑失败', -100);
2018-12-28 18:58:37 +08:00
}
/**
* 访问统计加1
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-10-15
* @desc description
* @param [array] $params [输入参数]
*/
public static function AnswerAccessCountInc($params = [])
{
if(!empty($params['answer_id']))
{
2021-07-21 16:35:38 +08:00
return Db::name('Answer')->where(['id'=>intval($params['answer_id'])])->inc('access_count')->update();
}
return false;
}
2018-12-28 18:58:37 +08:00
}
?>