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;
|
2020-06-27 22:48:16 +08:00
|
|
|
|
use app\service\UserService;
|
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 MessageService
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 消息添加
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2018-09-21
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [int] $user_id [用户id]
|
|
|
|
|
* @param [string] $title [标题]
|
|
|
|
|
* @param [string] $detail [内容]
|
2020-07-31 18:55:47 +08:00
|
|
|
|
* @param [string] $business_type [业务类型(默认, 如:订单, 充值, 提现, ...)]
|
2018-12-28 18:58:37 +08:00
|
|
|
|
* @param [int] $business_id [业务id]
|
|
|
|
|
* @param [int] $type [类型(默认0 普通消息)]
|
|
|
|
|
* @return [boolean] [成功true, 失败false]
|
|
|
|
|
*/
|
2020-07-31 18:55:47 +08:00
|
|
|
|
public static function MessageAdd($user_id, $title, $detail, $business_type = '默认', $business_id = 0, $type = 0)
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
$data = array(
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'detail' => $detail,
|
|
|
|
|
'user_id' => intval($user_id),
|
2020-07-31 18:55:47 +08:00
|
|
|
|
'business_type' => trim($business_type),
|
2018-12-28 18:58:37 +08:00
|
|
|
|
'business_id' => intval($business_id),
|
|
|
|
|
'type' => intval($type),
|
|
|
|
|
'is_read' => 0,
|
|
|
|
|
'add_time' => time(),
|
|
|
|
|
);
|
2020-10-25 00:45:09 +08:00
|
|
|
|
$message_id = Db::name('Message')->insertGetId($data);
|
|
|
|
|
if($message_id > 0)
|
|
|
|
|
{
|
|
|
|
|
// 消息添加钩子
|
|
|
|
|
$hook_name = 'plugins_service_message_add';
|
2021-07-18 23:42:10 +08:00
|
|
|
|
MyEventTrigger($hook_name, [
|
2020-10-25 00:45:09 +08:00
|
|
|
|
'hook_name' => $hook_name,
|
|
|
|
|
'is_backend' => true,
|
|
|
|
|
'data' => $data,
|
|
|
|
|
'data_id' => $message_id,
|
|
|
|
|
]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-05-18 21:35:39 +08:00
|
|
|
|
return false;
|
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 [输入参数]
|
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
|
public static function MessageListWhere($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
2019-01-10 11:32:15 +08:00
|
|
|
|
// 条件初始化
|
2018-12-28 18:58:37 +08:00
|
|
|
|
$where = [
|
|
|
|
|
['is_delete_time', '=', 0],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// id
|
|
|
|
|
if(!empty($params['id']))
|
|
|
|
|
{
|
|
|
|
|
$where[] = ['id', '=', $params['id']];
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 11:43:53 +08:00
|
|
|
|
// 用户id
|
|
|
|
|
if(!empty($params['user']))
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
2019-01-10 11:43:53 +08:00
|
|
|
|
$where[] = ['user_id', '=', $params['user']['id']];
|
2018-12-28 18:58:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关键字
|
|
|
|
|
if(!empty($params['keywords']))
|
|
|
|
|
{
|
|
|
|
|
$where[] = ['title|detail', 'like', '%'.$params['keywords'].'%'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否更多条件
|
|
|
|
|
if(isset($params['is_more']) && $params['is_more'] == 1)
|
|
|
|
|
{
|
|
|
|
|
// 等值
|
2020-07-31 18:55:47 +08:00
|
|
|
|
if(!empty($params['business_type']))
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
2020-07-31 18:55:47 +08:00
|
|
|
|
$where[] = ['business_type', '=', $params['business_type']];
|
2018-12-28 18:58:37 +08:00
|
|
|
|
}
|
|
|
|
|
if(isset($params['type']) && $params['type'] > -1)
|
|
|
|
|
{
|
|
|
|
|
$where[] = ['type', '=', intval($params['type'])];
|
|
|
|
|
}
|
|
|
|
|
if(isset($params['is_read']) && $params['is_read'] > -1)
|
|
|
|
|
{
|
|
|
|
|
$where[] = ['is_read', '=', intval($params['is_read'])];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 消息总数
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2018-09-29
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $where [条件]
|
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
|
public static function MessageTotal($where = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
return (int) Db::name('Message')->where($where)->count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户消息总数
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2018-09-29
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
2020-01-06 18:53:24 +08:00
|
|
|
|
* @return [int|string] [超过99则返回 99+]
|
2018-12-28 18:58:37 +08:00
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
|
public static function UserMessageTotal($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'user',
|
|
|
|
|
'error_msg' => '用户信息有误',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-01-06 18:53:24 +08:00
|
|
|
|
$total = self::MessageTotal(self::MessageListWhere($params));
|
|
|
|
|
return ($total > 99) ? '99+' : $total;
|
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 [输入参数]
|
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
|
public static function MessageList($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
$where = empty($params['where']) ? [] : $params['where'];
|
2020-10-29 00:33:25 +08:00
|
|
|
|
$field = empty($params['field']) ? '*' : $params['field'];
|
|
|
|
|
$order_by = empty($params['order_by']) ? 'id desc' : $params['order_by'];
|
2018-12-28 18:58:37 +08:00
|
|
|
|
$m = isset($params['m']) ? intval($params['m']) : 0;
|
|
|
|
|
$n = isset($params['n']) ? intval($params['n']) : 10;
|
|
|
|
|
|
|
|
|
|
// 获取数据列表
|
2021-07-18 23:42:10 +08:00
|
|
|
|
$data = Db::name('Message')->where($where)->field($field)->limit($m, $n)->order($order_by)->select()->toArray();
|
2018-12-28 18:58:37 +08:00
|
|
|
|
if(!empty($data))
|
|
|
|
|
{
|
2020-12-09 14:48:33 +08:00
|
|
|
|
// 字段列表
|
|
|
|
|
$keys = ArrayKeys($data);
|
|
|
|
|
|
|
|
|
|
// 用户列表
|
|
|
|
|
if(in_array('user_id', $keys) && isset($params['is_public']) && $params['is_public'] == 0)
|
|
|
|
|
{
|
|
|
|
|
$user_list = UserService::GetUserViewInfo(array_column($data, 'user_id'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 17:17:45 +08:00
|
|
|
|
$common_is_read_list = MyConst('common_is_read_list');
|
|
|
|
|
$common_message_type_list = MyConst('common_message_type_list');
|
2018-12-28 18:58:37 +08:00
|
|
|
|
foreach($data as &$v)
|
|
|
|
|
{
|
2020-06-27 22:48:16 +08:00
|
|
|
|
// 用户信息
|
|
|
|
|
if(isset($v['user_id']))
|
|
|
|
|
{
|
|
|
|
|
if(isset($params['is_public']) && $params['is_public'] == 0)
|
|
|
|
|
{
|
2020-12-09 14:48:33 +08:00
|
|
|
|
$v['user'] = (!empty($user_list) && is_array($user_list) && array_key_exists($v['user_id'], $user_list)) ? $user_list[$v['user_id']] : [];
|
2020-06-27 22:48:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 18:58:37 +08:00
|
|
|
|
// 消息类型
|
2020-06-27 22:48:16 +08:00
|
|
|
|
$v['type_text'] = $common_message_type_list[$v['type']]['name'];
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
|
|
// 是否已读
|
2020-06-27 22:48:16 +08:00
|
|
|
|
$v['is_read_text'] = $common_is_read_list[$v['is_read']]['name'];
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
2020-06-27 22:48:16 +08:00
|
|
|
|
// 用户是否已删除
|
|
|
|
|
$v['user_is_delete_time_text'] = ($v['user_is_delete_time'] == 0) ? '否' : '是';
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
|
|
// 时间
|
|
|
|
|
$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']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return DataReturn('处理成功', 0, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 消息更新未已读
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2018-10-15
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
|
public static function MessageRead($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'user',
|
|
|
|
|
'error_msg' => '用户信息有误',
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新用户未读消息为已读
|
|
|
|
|
$where = ['user_id'=>$params['user']['id'], 'is_read'=>0];
|
|
|
|
|
$ret = Db::name('Message')->where($where)->update(['is_read'=>1]);
|
|
|
|
|
return DataReturn('处理成功', 0, $ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2018-12-18
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
|
public static function MessageDelete($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
2020-06-27 22:48:16 +08:00
|
|
|
|
// 参数是否有误
|
|
|
|
|
if(empty($params['ids']))
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
2020-06-27 22:48:16 +08:00
|
|
|
|
return DataReturn('操作id有误', -1);
|
|
|
|
|
}
|
|
|
|
|
// 是否数组
|
|
|
|
|
if(!is_array($params['ids']))
|
|
|
|
|
{
|
|
|
|
|
$params['ids'] = explode(',', $params['ids']);
|
2018-12-28 18:58:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除操作
|
2020-06-27 22:48:16 +08:00
|
|
|
|
if(Db::name('Message')->where(['id'=>$params['ids']])->delete())
|
2018-12-28 18:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
return DataReturn('删除成功');
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 22:48:16 +08:00
|
|
|
|
return DataReturn('删除失败', -100);
|
2018-12-28 18:58:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
?>
|