model = model('Templatemessage');
$this->view->assign("statueList", $this->model->getStatueList());
$this->assign('admin_id', $this->auth->id);
$this->assignconfig('admin_id', $this->auth->id);
}
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$this->assignconfig('nowtime',time());
if ($this->request->isAjax()) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->join('template_collect', 'templatemessage.id = template_collect.templetemessage_id', 'left')
->field('templatemessage.*, template_collect.pv, template_collect.uv, template_collect.recharge_orders, template_collect.recharge_money')
->where($where)
->where('admin_id','in',$this->auth->id)
->order('templatemessage.'.$sort, $order)
->count();
$list = $this->model
->join('template_collect', 'templatemessage.id = template_collect.templetemessage_id', 'left')
->field('templatemessage.*, template_collect.pv, template_collect.uv, template_collect.recharge_orders, template_collect.recharge_money')
->where($where)
->where('admin_id','in',$this->auth->id)
->order('templatemessage.'.$sort, $order)
->limit($offset, $limit)
->select();
if (VisitLimitService::instance()->checkMigrated()) {
$ids = array_column($list, 'id');
$collectList = TemplateService::instance()->getMigratedCollectAnalysis($ids)->data;
foreach ($list as $index => $item) {
if (array_key_exists($item['id'], $collectList)) {
$list[$index]['uv'] = $collectList[$item['id']]['uv'];
$list[$index]['recharge_money'] = $collectList[$item['id']]['recharge_money'];
$list[$index]['day_uv'] = $collectList[$item['id']]['day_uv'];
$list[$index]['day_money'] = $collectList[$item['id']]['day_recharge_money'];
} else {
$list[$index]['uv'] = 0;
$list[$index]['recharge_money'] = 0;
$list[$index]['day_uv'] = 0;
$list[$index]['day_money'] = 0;
}
}
} else {
$redis = Redis::instance();
foreach($list as $item){
$item['day_uv'] = intval($redis->get(TemplateMsgConstants::getTemplateUvToDayKey($item['id'])));
$item['day_money'] = floatval($redis->get(TemplateMsgConstants::getTemplateRechargeMoneyToDayKey($item['id'])));
if(empty($item['recharge_money'])){
$item['recharge_money'] = '0.00';
}
if(empty($item['day_money'])){
$item['day_money'] = '0.00';
}
}
}
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
//测试发送模板消息
public function sent()
{
// refresh_token adminConfig表获取
$admin_id = $this->auth->id;
$user_id = $_POST['user_id'];
if (preg_match("/\d+/i", $user_id, $matches)) {
if(!empty($matches)){
if(empty($matches)){
//没有匹配到数字。错误
return json(['errcode' => 1, 'errmsg' => '无法匹配到此用户']);
}else{
$user_id = intval($matches[0]);
}
}
} else {
//没有匹配到数组,错误
return json(['errcode' => 1, 'errmsg' => '无法匹配到此用户']);
}
$user = model('User')->setConnect($user_id)->where('id', $user_id)->find();
if (!$user) {
return json(['errcode' => 1, 'errmsg' => '无此用户']);
}
$openid = $user->openid;
$Config = model('AdminConfig')->getAdminInfoAll($admin_id); //where('admin_id', '=', $admin_id)->find();
$wechat = new WeChatObject($Config);
$officialAccount = $wechat->getOfficialAccount();
$user_nickname = $user->nickname;
$user_id = $user->id;
$user_vip_endtime = $user->vip_endtime;
$df_config = $Config;
$wx_nickname = $df_config['json']['authorizer_info']['nick_name'] ?: $df_config['json']['authorizer_info']['user_name'];
$book_name = '';
$redis = Redis::instance();
$key = 'U-R:'.$user_id;
if($redis->exists($key)){
$readkey = $redis->zrevrange($key,0,1);
$book_name = $redis->hget($readkey[0],'chapter_name');
}
$message = json_decode($_POST['message_html'],true);
$message_json['touser'] = '{$openid}';
$message_json['template_id'] = $_POST['template_id'];
$message_json['url'] = trim($_POST['url']);
$message_json['data'] = [];
foreach ($message as $v){
foreach ($v as $value){
if((preg_match ("/^[a-z]/i", $value['key'])) && !empty($value['fieldname'])){
$value['fieldname'] = stripslashes($value['fieldname']);
$value['fieldname'] = preg_replace("/\">(.*?)<\/strong>/",'',$value['fieldname']);
$value['fieldname'] = str_replace('','',$value['fieldname']);
$value['fieldname'] = str_replace('">','',$value['fieldname']);
$value['fieldname'] = str_replace(' ','',$value['fieldname']);
$value['fieldname'] = str_replace('model->get($ids);
if (!$row)
$this->error(__('No Results were found'));
if($row['sendtime'] <= time()){
$canEdit = 1;
}
if($row['sendtime'] > time() && $row['sendtime']-time() <= 600){
$canEdit = 1;
}
//解析占位符
$row['message_json'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $row['message_json'], false)->data;
$row['url'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $row['url'], false)->data;
if ($this->request->isAjax())
{
$params = $this->request->post("row/a");
if ($params)
{
unset($row['type']);
if(isset($params['sub_from'])){
unset($params['sub_from']);
}
if(isset($params['sub_to'])){
unset($params['sub_to']);
}
$params['url'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $params['url'])->data;
$params['url'] = trim($this->_appendUrlExt($params['url'], BigData::BIG_DATA_TEMPLATE_MSG, $row['sendtime'], $ids));
$params['message_json'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $this->message_json($params))->data;
try
{
//是否采用模型验证
if ($this->modelValidate)
{
$name = basename(str_replace('\\', '/', get_class($this->model)));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
$row->validate($validate);
}
$result = $row->allowField(true)->save($params);
if ($result !== false)
{
$this->success();
}
else
{
$this->error($row->getError());
}
}
catch (\think\exception\PDOException $e)
{
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$row['sendtime'] = date('Y-m-d H:i:s',$row['sendtime']);
//获取独家书籍id
$bnotin= [];
ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
$this->assignconfig('bnotin', $bnotin);
$this->assign("canedit",$canEdit);
$json = json_decode($row['user_json'], true);
$from = $to = '';
if ($json['all'] == 0 && !$json['subscribe_time'] && $json['subscribe_range']) {
list($from, $to) = explode('-', $json['subscribe_range']);
if($from == 'NaN'){
$from = '';
}
if($to == 'NaN'){
$to = '';
}
if($from){
$from = date('Y-m-d H:i:s', $from);
}
if($to){
$to = date('Y-m-d H:i:s', $to);
}
}
$this->view->assign('from', $from);
$this->view->assign('to', $to);
$this->view->assign("row", $row);
return $this->view->fetch();
}
/*
* 处理message_json返回的数据
*/
public function message_json($row){
$message = json_decode($row['message_html'],true);
$message_json['touser'] = '{$openid}';
$message_json['template_id'] = $row['template_id'];
$message_json['url'] = trim($row['url']);
$message_json['data'] = [];
foreach ($message as $v){
foreach ($v as $value){
if((preg_match ("/^[a-z]/i", $value['key'])) && !empty($value['fieldname'])){
$value['fieldname'] = stripslashes($value['fieldname']);
$value['fieldname'] = preg_replace("/\">(.*?)<\/strong>/",'',$value['fieldname']);
$value['fieldname'] = str_replace('','',$value['fieldname']);
$value['fieldname'] = str_replace('">','',$value['fieldname']);
$value['fieldname'] = str_replace(' ','',$value['fieldname']);
$value['fieldname'] = str_replace('