<?php
|
|
namespace app\api\model\branch;
|
|
use app\common\exception\BaseException;
|
use app\common\model\branch\Activity as ActivityModel;
|
use app\common\model\branch\ActivityUser as ActivityUserModel;
|
|
/**
|
* 活动模型
|
*/
|
class Activity extends ActivityModel
|
{
|
/**
|
* 隐藏字段
|
* @var array
|
*/
|
protected $hidden = [
|
'is_delete',
|
'app_id',
|
'update_time'
|
];
|
|
/**
|
* 活动详情:HTML实体转换回普通字符
|
*/
|
public function getActivityContentAttr($value)
|
{
|
$value = htmlspecialchars_decode($value);
|
return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
|
}
|
|
/**
|
* 详情
|
*/
|
public static function detail($activity_id)
|
{
|
if (!$model = parent::detail($activity_id)) {
|
throw new BaseException(['msg' => '活动不存在']);
|
}
|
$model["reg_num"] = ActivityUserModel::getRegNum($activity_id);
|
return $model;
|
}
|
|
/**
|
* 获取列表
|
*/
|
public function getList($category_id = 0, $params, $user = [])
|
{
|
$model = $this;
|
$category_id > 0 && $model = $model->where('category_id', '=', $category_id);
|
if (!empty($params['branch_id'])) {
|
$model = $model->where('branch_id', '=', $params['branch_id']);
|
}
|
$list = $model ->with(['image', 'category'])
|
->where('is_delete', '=', 0)
|
->where('status', '=', 1)
|
->order(['sort' => 'asc', 'create_time' => 'desc'])
|
->paginate($params);
|
// 检查用户有没有报名
|
foreach($list as &$item) {
|
if (!$user) {
|
$item['is_reg'] = false;
|
} else {
|
$item['is_reg'] = ActivityUserModel::isReg($user['user_id'], $item['activity_id']);
|
}
|
// 签到人数
|
$item['verify_num'] = ActivityUserModel::getVerifyNum($item['activity_id']);
|
}
|
return $list;
|
}
|
|
/**
|
* 获取列表
|
*/
|
public function getListForAdmin($params = [])
|
{
|
$model = $this;
|
if (!empty($params['category_id'])) {
|
$model = $model->where('category_id', '=', $params['category_id']);
|
}
|
if (!empty($params['branch_id'])) {
|
$model = $model->where('branch_id', '=', $params['branch_id']);
|
}
|
if (isset($params['status'])) {
|
if ($params['status'] == 0) {
|
$model = $model->where('activity_start_time', '>', time());
|
}
|
if ($params['status'] == 1) {
|
$model = $model->where('activity_start_time', '<', time())
|
->where('activity_end_time', '>', time());
|
}
|
if ($params['status'] == 2) {
|
$model = $model->where('activity_end_time', '<', time());
|
}
|
}
|
$list = $model ->with(['image', 'category'])
|
->where('is_delete', '=', 0)
|
->where('status', '=', 1)
|
->order(['sort' => 'asc', 'create_time' => 'desc'])
|
->paginate($params);
|
foreach($list as &$item) {
|
// 签到人数
|
$item['verify_num'] = ActivityUserModel::getVerifyNum($item['activity_id']);
|
}
|
return $list;
|
}
|
|
/**
|
* 新增记录
|
*/
|
public function add($data, $branch_id)
|
{
|
$data['app_id'] = self::$app_id;
|
$data['branch_id'] = $branch_id;
|
//活动时间
|
$data['activity_start_time'] = strtotime($data['activity_start_time']);
|
$data['activity_end_time'] = strtotime($data['activity_end_time']);
|
//报名时间
|
$data['register_start_time'] = strtotime($data['register_start_time']);
|
$data['register_end_time'] = strtotime($data['register_end_time']);
|
// 处理emoji表情符号
|
$data = $this->filterEmoji($data);
|
return $this->save($data);
|
}
|
|
/**
|
* 更新记录
|
*/
|
public function edit($data)
|
{
|
//活动时间
|
$data['activity_start_time'] = strtotime($data['activity_start_time']);
|
$data['activity_end_time'] = strtotime($data['activity_end_time']);
|
//报名时间
|
$data['register_start_time'] = strtotime($data['register_start_time']);
|
$data['register_end_time'] = strtotime($data['register_end_time']);
|
// 处理emoji表情符号
|
$data = $this->filterEmoji($data);
|
return $this->save($data);
|
}
|
|
/**
|
* 软删除
|
*/
|
public function setDelete()
|
{
|
return $this->save(['is_delete' => 1]);
|
}
|
|
/**
|
* 检查用户是否能参与促销活动
|
*/
|
public function checkUserShopping($user_id) {
|
if (!$this['is_verify']) {
|
$this->error = '您还没签到,无法参与购物';
|
}
|
return true;
|
}
|
|
/**
|
* 过滤emoji表情符号,转换为HTML实体
|
* @param array $data
|
* @return array
|
*/
|
private function filterEmoji($data)
|
{
|
if (isset($data['content'])) {
|
$data['content'] = $this->emojiToHtml($data['content']);
|
}
|
if (isset($data['name'])) {
|
$data['name'] = $this->emojiToHtml($data['name']);
|
}
|
if (isset($data['describe'])) {
|
$data['describe'] = $this->emojiToHtml($data['describe']);
|
}
|
return $data;
|
}
|
|
/**
|
* 将emoji表情符号转换为HTML实体
|
* @param string $str
|
* @return string
|
*/
|
private function emojiToHtml($str)
|
{
|
if (empty($str)) {
|
return $str;
|
}
|
$str = preg_replace_callback(
|
'/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{2600}-\x{26FF}]|[\x{2700}-\x{27BF}]/u',
|
function ($matches) {
|
return mb_convert_encoding($matches[0], 'HTML-ENTITIES', 'UTF-8');
|
},
|
$str
|
);
|
return $str;
|
}
|
|
}
|