2023-01-29 10:26:52 +08:00

640 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | 小说系统 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\common\controller\Admin;
class Customservice extends Admin {
public function _initialize() {
parent::_initialize();
//$this->getContentMenu();
$this->model_id = $model_id = 55;
//$this->model_id = $model_id = $this->request->param('model_id') = 55;
$list = db('Model')->column('*', 'id');
if (empty($list[$model_id])) {
return $this->error("无此模型!");
} else {
$this->modelInfo = $list[$model_id];
$this->model = M($this->modelInfo['name']);
}
$this->assign('model_id', $model_id);
$this->assign('model_list', $list);
}
/**
* 内容列表
* @return [html] [页面内容]
* @author netlife <40150501@qq.com>
*/
public function index() {
if ($this->modelInfo['list_grid'] == '') {
return $this->error("列表定义不正确!", url('admin/model/edit', array('id' => $this->modelInfo['id'])));
}
$grid_list = get_grid_list($this->modelInfo['list_grid']);
// 关键字搜索
$search_key =$this->modelInfo['search_key']?$this->modelInfo['search_key']:'title';
$order = "id desc";
$map = $this->buildMap($search_key);
$field = array_filter($grid_list['fields']);
$map['type'] = 1;
if(session('user_auth.group')!=999){
$map['uid'] = session('user_auth.uid');
if(session('wxmpid')){
$map['wxmpid'] =session('wxmpid');
}
}
$list = $this->model->where($map)->order($order)->paginate($this->modelInfo['list_row']);
$data = array(
'grid' => $grid_list,
'list' => $list,
'page' => $list->render(),
);
if ($this->modelInfo['template_list']) {
$template = 'content/' . $this->modelInfo['template_list'];
} else {
$template = 'customservice/index';
}
$this->assign($data);
$this->setMeta($this->modelInfo['title'] . "列表");
return $this->fetch($template);
}
/**
* text [文本消息]
*
* @return type
**/
public function text() {
if ($this->modelInfo['list_grid'] == '') {
return $this->error("列表定义不正确!", url('admin/model/edit', array('id' => $this->modelInfo['id'])));
}
$grid_list = get_grid_list($this->modelInfo['list_grid']);
// 关键字搜索
$search_key =$this->modelInfo['search_key']?$this->modelInfo['search_key']:'title';
$order = "id desc";
$map = $this->buildMap($search_key);
$field = array_filter($grid_list['fields']);
$map['type'] = 2;
if(session('user_auth.group')!=999){
$map['uid'] = session('user_auth.uid');
if(session('wxmpid')){
$map['wxmpid'] =session('wxmpid');
}
}
$list = $this->model->where($map)->order($order)->paginate($this->modelInfo['list_row']);
$data = array(
'grid' => $grid_list,
'list' => $list,
'page' => $list->render(),
);
$template = 'customservice/text';
$this->assign($data);
$this->setMeta("文本消息列表");
return $this->fetch($template);
}
/**
* 内容添加
* @author netlife <40150501@qq.com>
*/
public function add() {
if (IS_POST) {
$this->param['model_id'] = 55;
$this->param['wxmpid'] = session('wxmpid');
$this->param['uid'] = session('user_auth.uid');
if($this->param['scheduled_at']){
$this->param['scheduled_at'] =strtotime($this->param['scheduled_at']);
}
foreach ($this->param['param']["articles"] as $k=>$v){
$this->param['param']["articles"][$k]["picurl"] = config('web_site_url').$v["picurl"];
}
$this->param['param']=json_encode($this->param['param']);
$this->param['create_time'] =time();
$this->param['update_time'] =time();
//unset($this->param['id']);
$result = $this->model->save($this->param);
if ($result) {
//记录行为
//action_log('add_content', 'content', $result, session('user_auth.uid'));
return $this->success("添加成功!", url('admin/customservice/index', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/customservice/add', array('model_id' => $this->modelInfo['id'])));
}
} else {
$uid = session('user_auth.uid');
$info = db('wxmp')->where(array('uid'=>$uid))->find();
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
$turl = db('tuiguang')->where('uid',$uid)->select();
$info = array(
'model_id' => $this->modelInfo['id'],
);
$data = array(
'info' => $info,
'turl' => $turl,
'fieldGroup' => $this->getField($this->modelInfo),
);
if ($this->modelInfo['template_add']) {
$template = 'customservice/' . $this->modelInfo['template_add'];
} else {
$template = 'customservice/add';
}
$this->assign($data);
$this->setMeta("添加" . $this->modelInfo['title']);
return $this->fetch($template);
}
}
public function addtext() {
if (IS_POST) {
//$this->param['model_id'] = 55;
$this->param['wxmpid'] = session('wxmpid');
$this->param['uid'] = session('user_auth.uid');
if($this->param['scheduled_at']){
$this->param['scheduled_at'] =strtotime($this->param['scheduled_at']);
}
$this->param['create_time'] =time();
$this->param['update_time'] =time();
//unset($this->param['id']);
$result = $this->model->save($this->param);
if ($result) {
//记录行为
//action_log('add_content', 'content', $result, session('user_auth.uid'));
return $this->success("添加成功!", url('admin/customservice/text', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/customservice/addtext', array('model_id' => $this->modelInfo['id'])));
}
} else {
$uid = session('user_auth.uid');
$info = db('wxmp')->where(array('uid'=>$uid))->find();
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
$info = array(
'model_id' => $this->modelInfo['id'],
);
$data = array(
'info' => $info,
'fieldGroup' => $this->getField($this->modelInfo),
);
$template = 'customservice/addtext';
$this->assign($data);
$this->setMeta("添加文本消息");
return $this->fetch($template);
}
}
/**
* 内容修改
* @author netlife <40150501@qq.com>
*/
public function edit($id) {
if (IS_POST) {
$this->param['model_id'] = 55;
$this->param['scheduled_at'] =strtotime($this->param['scheduled_at']);
foreach ($this->param['param']["articles"] as $k=>$v){
if(strpos($v["picurl"],'http')===false){
$this->param['param']["articles"][$k]["picurl"] = config('web_site_url').$v["picurl"];
}
}
$this->param['param']=json_encode($this->param['param']);
$result = $this->model->save($this->param,array('id'=>$this->param['id']));
if ($result !== false) {
//记录行为
//action_log('update_content', 'content', $result, session('user_auth.uid'));
return $this->success("更新成功!", url('admin/customservice/index', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/customservice/edit', array('model_id' => $this->modelInfo['id'], 'id' => $id)));
}
} else {
if (!$id) {
return $this->error("非法操作!");
}
$info = $this->model->find($id);
if (!$info) {
return $this->error($this->model->getError());
}
$info['model_id'] = $this->modelInfo['id'];
$data = array(
'info' => $info,
'fieldGroup' => $this->getField($this->modelInfo),
);
if ($this->modelInfo['template_edit']) {
$template = 'customservice/' . $this->modelInfo['template_edit'];
} else {
$template = 'customservice/edit';
}
$this->assign($data);
$this->setMeta("编辑" . $this->modelInfo['title']);
return $this->fetch($template);
}
}
public function edittext($id) {
if (IS_POST) {
$this->param['model_id'] = 55;
$this->param['scheduled_at'] =strtotime($this->param['scheduled_at']);
$result = $this->model->save($this->param,array('id'=>$this->param['id']));
if ($result !== false) {
//记录行为
//action_log('update_content', 'content', $result, session('user_auth.uid'));
return $this->success("更新成功!", url('admin/customservice/text', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/customservice/edittext', array('model_id' => $this->modelInfo['id'], 'id' => $id)));
}
} else {
if (!$id) {
return $this->error("非法操作!");
}
$info = $this->model->find($id);
if (!$info) {
return $this->error($this->model->getError());
}
$info['model_id'] = $this->modelInfo['id'];
$data = array(
'info' => $info,
'fieldGroup' => $this->getField($this->modelInfo),
);
$this->assign($data);
$this->setMeta("编辑文本消息");
return $this->fetch();
}
}
/**
* 内容删除
* @author netlife <40150501@qq.com>
*/
public function del() {
$id = $this->getArrayParam('id');
if (empty($id)) {
return $this->error("非法操作!");
}
$map['id'] = array('IN', $id);
$result = $this->model->where($map)->delete();
if (false !== $result) {
//记录行为
action_log('delete_content', 'content', $result, session('user_auth.uid'));
return $this->success("删除成功!");
} else {
return $this->error("删除失败!");
}
}
/**
* 设置状态
* @author netlife <40150501@qq.com>
*/
public function status($id, $status) {
$map['id'] = $id;
$result = $this->model->where($map)->setField('status', $status);
if (false !== $result) {
return $this->success("操作成功!");
} else {
return $this->error("操作失败!!");
}
}
/**
* 设置置顶
* @author netlife <40150501@qq.com>
*/
public function settop($id, $is_top) {
$map['id'] = $id;
$result = $this->model->where($map)->setField('is_top', $is_top);
if (false !== $result) {
return $this->success("操作成功!");
} else {
return $this->error("操作失败!!");
}
}
/**
* 获取字段信息
* @return array 字段数组
* @author netlife <40150501@qq.com>
*/
protected function getField() {
$field_group = parse_config_attr($this->modelInfo['attribute_group']);
$map['model_id'] = $this->modelInfo['id'];
if ($this->request->action() == 'add') {
$map['is_show'] = array('in', array('1', '2'));
} elseif ($this->request->action() == 'edit') {
$map['is_show'] = array('in', array('1', '3'));
}
//获得数组的第一条数组
$rows = model('Attribute')->getFieldlist($map, 'id');
if (!empty($rows)) {
foreach ($rows as $key => $value) {
$list[$value['group_id']][] = $value;
}
foreach ($field_group as $key => $value) {
$fields[$value] = isset($list[$key]) ? $list[$key] : array();
}
}else{
$fields = array();
}
return $fields;
}
/**
* 创建搜索
* @return [array] [查询条件]
*/
protected function buildMap($search_key) {
$map = array();
$data = $this->request->param();
foreach ($data as $key => $value) {
if ($value) {
if ($key == 'keyword') {
$map[$search_key] = array("LIKE", "%$value%");
} elseif ($key == 'category') {
$map['category_id'] = $value;
} elseif ($key == 'create_time') {
$map['create_time'] = array('BETWEEN', array(strtotime($value[0]), strtotime($value[1])));
} else {
$map[$key] = $value;
}
}
}
if (isset($map['page'])) {
unset($map['page']);
}
if (isset($map['model_id'])) {
unset($map['model_id']);
}
$this->assign($data);
return $map;
}
/**
* 检测需要动态判断的文档类目有关的权限
*
* @return boolean|null
* 返回true则表示当前访问有权限
* 返回false则表示当前访问无权限
* 返回null则会进入checkRule根据节点授权判断权限
*
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
protected function checkDynamic() {
$model_id = $this->request->param('model_id');
if (IS_ROOT) {
return true; //管理员允许访问任何页面
}
$models = model('AuthGroup')->getAuthModels(session('user_auth.uid'));
if (!$model_id) {
return false;
} elseif (in_array($model_id, $models)) {
//返回null继续判断操作权限
return null;
} else {
return false; //无权限
}
return false;
}
/**
* api_get_titles [desc]
*
* @return type
**/
public function api_get_titles() {
$titles = db('articles_titles')->where('status=1')->order('id desc')->select();
header('Content-type: text/json');
echo json_encode($titles);
exit();
}
public function api_get_covers() {
$covers = db('articles_covers')->where('status=1')->order('id desc')->select();
header('Content-type: text/json');
foreach ($covers as $k=>$v){
$result[$k]=$v;
$result[$k]['cover_url']=get_cover($v['cover_url'],'path');
}
echo json_encode($result);
exit();
}
public function api_suggest() {
$uid = session('user_auth.uid');
$turl = db('tuiguang')->where('uid',$uid)->field('id,types,title as description,uid')->select();
if($turl){
$url =[];
foreach ($turl as $k=>$vo){
switch($vo['types']){
case 1:
$types = '首页推广';
break;
case 2:
$types = '热门推荐';
break;
default:
$types = $vo['description'];
}
$url[$k]['id'] =$vo['id'];
$url[$k]['description'] =$types;
$url[$k]['url'] =get_agent($vo['id'],$vo['uid']);
}
}
header('Content-type: text/json');
echo json_encode($url);
exit();
}
public function api_check_mp_settings() {
$uid = session('user_auth.uid');
$info = db('wxmp')->where(array('uid'=>$uid))->find();
header('Content-type: text/json');
if(empty($info)){
echo json_encode(array('valid'=>false));
}else{
echo json_encode(array('valid'=>true));
}
exit();
}
public function api_get_templates() {
$message = &load_wechat(1,'Message');
$msg = $message->getAllPrivateTemplate();
header('Content-type: text/json');
echo json_encode($msg["template_list"]);
exit();
}
public function api_get() {
$id= input('get.id');
$info = db('customservice')->where("id=$id")->find();
$info['create_time'] = date('Y-m-d H:i',$info['create_time']);
$info['update_time'] = date('Y-m-d H:i',$info['update_time']);
$info['scheduled_at'] = date('Y-m-d H:i',$info['scheduled_at']);
$info['param'] = json_decode($info['param'],true);
header('Content-type: text/json');
echo json_encode($info);
exit();
}
/**
* api_test [测试]
*
* @return type
**/
public function api_test() {
$data = $this->request->param();
if($data["url"]&&$data["param"]["articles"]){
foreach ($data["param"]["articles"] as $k=>$v){
if($k==0){
$data["param"]["articles"][$k]["url"] = $data["url"];
}
if(strpos($v["picurl"],'http')===false){
$data["param"]["articles"][$k]["picurl"] = config('web_site_url').$v['picurl'];
}
}
}
$uid = session('user_auth.uid');
$appid = db('wxmp')->where(array('uid'=>$uid))->value('appid');
$openid = db('MemberUserapi')->where(array('uid'=>$data['test_member_id'],"appid"=>$appid))->value('wxmpid');
if(empty($openid)){
echo json_encode(array("code"=>1099));
exit();
}
$result = $this->send_message($openid,$data["param"]);
if($result["errmsg"]=="ok"){
echo json_encode(array("code"=>0));
exit();
}
}
/*****发送客服消息******/
protected function send_message($openid,$data){
$uid = session('user_auth.uid');
$receive = &load_wechat($uid,'Receive');
if (count($data["articles"]) > 1){
$content = "";
foreach ($data["articles"] as &$item) {
$content .= emoji("\ue315").emoji("\ue231").'<a href="'.$item["url"].'">'.$item["title"].'</a>'.PHP_EOL.PHP_EOL;
}
$template = array(
"touser"=>$openid,
"msgtype"=>"text",
"text"=>array('content'=>trim($content.emoji("\ue003")."为方便阅读请置顶公众号!每天点击菜单栏,签到天天送书币!".emoji("\ue003")))
);
}else{
$template = array(
"touser"=>$openid,
"msgtype"=>"news",
"news"=>$data
);
}
$msg = $receive->sendCustomMessage($template);
return $msg;
}
public function rich_msg() {
$id =input('id');
$info = db('customservice')->where("id=".$id)->find();
$param = json_decode($info["param"],true);
//合并头条链接
if($info["url"]&&$param["articles"]){
foreach ($param["articles"] as $kk=>$vv){
if($kk==0){
$param["articles"][$kk]["url"] = $info["url"];
}
}
}
//获取推送用户
$appid= db('wxmp')->where('uid='.$info['uid'])->value('appid');
if($info["andience"]==1){//付费用户
$fans = db('wechat_fans')->alias('f')->join(' cps_member_userapi u','u.wxmpid = f.openid','LEFT')->join(' cps_member m','m.uid = u.uid','LEFT')->where(array('f.appid'=>$appid,'f.subscribe'=>1,'m.pay'=>array('>','0'),'f.update_at'=>array('>=',time()-48*3600)))->column('f.id,f.openid');
}elseif($info["andience"]==3){//未付费用户
$fans = db('wechat_fans')->alias('f')->join(' cps_member_userapi u','u.wxmpid = f.openid','LEFT')->join(' cps_member m','m.uid = u.uid','LEFT')->where(array('f.appid'=>$appid,'f.subscribe'=>1,'m.pay'=>0,'f.update_at'=>array('>=',time()-48*3600)))->column('f.id,f.openid');
}else{
$fans = db('wechat_fans')->where(array('appid'=>$appid,'subscribe'=>1,'update_at'=>array('>=',time()-48*3600)))->column('id,openid');
}
//开始推送
if($fans){
foreach ($fans as $vo){
$this->send_customservice($info['uid'],$vo,$param);
}
db('customservice')->where("id=".$id)->update(['completed' =>1,'scheduled_at'=>time()]);
return $this->success("推送成功!");
}
}
/*****发送客服消息函数******/
protected function send_customservice($uid,$openid,$data){
$receive = &load_wechat($uid,'Receive');
$template = array(
"touser"=>$openid,
"msgtype"=>"news",
"news"=>$data
);
$result = $receive->sendCustomMessage($template);
/**
if($result["errmsg"]=="ok"){
echo json_encode(array("code"=>0));
}
**/
}
}