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

788 lines
23 KiB
PHP
Raw Permalink 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
/**
公众号模型
**/
namespace app\admin\controller;
use app\common\controller\Admin;
use \think\Session;
use \think\Db;
use service\WechatService;
use wxopen\WxComponentService;
class Wxmp extends Admin {
public function _initialize() {
parent::_initialize();
//$this->getContentMenu();
$this->model_id = $model_id = 18;
//$this->model_id = $model_id = $this->request->param('model_id');
$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']);
$order = "id desc";
// 关键字搜索
$search_key =$this->modelInfo['search_key']?$this->modelInfo['search_key']:'title';
$map = $this->buildMap($search_key);
$field = array_filter($grid_list['fields']);
$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 = 'content/index';
}
$this->assign($data);
$this->setMeta($this->modelInfo['title'] . "列表");
return $this->fetch($template);
}
public function autoconfig() {
$uid= session('user_auth.uid');
$group= session('user_auth.group');
//已经授权的公众号列表
if(session('user_auth.group')==999){
$autoconfig = db('wxmp')->alias('w')->join('wxopen_account o','w.open_appid = o.appid')->where("w.autoconfig=1")->field('w.id,w.nickname,w.uid,w.type,w.appid,w.subscribe_url,w.webname,w.open_appid,o.title,o.id as from_site,o.domain')->paginate(10);
}else{
$autoconfig = db('wxmp')->alias('w')->join('wxopen_account o','w.open_appid = o.appid')->where("w.uid=".$uid." and w.autoconfig=1")->field('w.id,w.nickname,w.uid,w.type,w.appid,w.subscribe_url,w.webname,w.open_appid,o.title,o.id as from_site,o.domain')->paginate(10);
}
//微信开放平台列表
$wxopen = db('wxopen_account')->where('status=1')->column('id,title,domain');
$this->assign('autoconfig',$autoconfig);
$this->assign('page',$autoconfig->render());
$this->assign('uid',$uid);
$this->assign('wxopen',$wxopen);
$this->setMeta("公众号设置");
return $this->fetch();
}
/*****手动配置******/
public function settings() {
if (IS_POST) {
$this->param['model_id'] = 18;
if($this->param['id']){
$message = "修改成功";
$result = $this->model->save($this->param, array('id'=> $this->param['id']));
}else{
$message = "添加成功";
$this->param['uid'] = session('user_auth.uid');
$result = $this->model->save($this->param);
}
if ($result) {
return $this->success($message, url('admin/wxmp/settings', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/wxmp/settings', array('model_id' => $this->modelInfo['id'])));
}
} else {
$uid= session('user_auth.uid');
$info = $this->model->where(array('uid'=>$uid))->find();
if(empty($info)){
$info = array(
'model_id' => $this->modelInfo['id'],
'id' => 0,
);
}
$data = array(
'info' => $info,
'fieldGroup' => $this->getField($this->modelInfo),
);
$this->assign($data);
$this->setMeta($this->modelInfo['title']."设置");
return $this->fetch();
}
}
/**
* 公众号接入设置
* @author netlife <40150501@qq.com>
*/
public function integrate() {
$uid = session('user_auth.uid');
$wxmpid = session('wxmpid');
$info = db('wxmp')->where(array('uid'=>$uid,'status'=>1))->find();
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
$uid= session('user_auth.uid');
$this->setMeta($this->modelInfo['title']."接入设置");
$this->assign('uid',$uid);
if($uid ==1){
$agent_url = str_replace("http://","",config('web_site_url'));
}else{
$agent_url = str_replace("{0}",$uid,config('agent_tuiguangurl'));
}
$this->assign('agenthost',$agent_url);
$urlarr = explode(".", $_SERVER['HTTP_HOST']);
$this->assign('host',$urlarr[1].".".$urlarr[2]);
return $this->fetch();
}
/*******公众号菜单栏配置******/
public function menu() {
$uid = session('user_auth.uid');
$wxmpid = session('wxmpid');
if($uid==1){
$info = db('wxmp')->where(array('uid'=>$uid))->find();
}else{
$info = db('wxmp')->where(array('id'=>$wxmpid,'status'=>1))->find();
}
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
$menu = array();
$cmenu = array();
//自定义菜单
$customizemenu = db('wechat_customizemenu')->where(['status' => '1','uid'=>$uid,'wxmpid'=>$wxmpid])->order('`index` ASC,id ASC')->select();
if($customizemenu){
$cmenu = arr2tree($customizemenu, 'index', 'pindex');
}else{
if(session('group')==3){
$arrayurl =[0,"/?channel=1&source=wechat_menu","/?channel=2&source=wechat_menu","/featured.html?source=wechat_menu&channel=1","/featured.html?source=wechat_menu&channel=2","/top.html?source=wechat_menu&channel=1","/top.html?source=wechat_menu&channel=2","/focushistory?source=wechat_menu","/personal/info?source=wechat_menu","/pay/index?source=wechat_menu","/personal/bookcase?source=wechat_menu","/search.html?source=wechat_menu","kefu_qrcode_url"];
$wechat_menu = db('wechat_menu')->where('status=1 and pid=0 and uid=1')->order('sort ASC,id ASC')->select();
$menu = array();
if($wechat_menu){
foreach ($wechat_menu as $k=>$v){
$customizemenu[] = array(
'name' => $v['title'],
'url' =>$v['url'],
'uid' => $uid,
'wxmpid' => $wxmpid,
'index' => $k+1,
'pindex' => 0,
'type' => array_search($v['url'],$arrayurl,true),
'sort' => $k,
'create_at' => time(),
);
$childs = db('wechat_menu')->where('status=1 and pid='.$v['id'])->order('sort ASC,id ASC')->select();
if($childs){
foreach ($childs as $kk=>$vv){
$customizemenu[] = array(
'name' => $vv['title'],
'url' => $vv['url'],
'uid' => $uid,
'wxmpid' => $wxmpid,
'index' => ($k+1).($kk+1),
'pindex' => $k+1,
'type' => array_search($vv['url'],$arrayurl,true),
'sort' => $k,
'create_at' => time(),
);
}
}
}
}
$cmenu = arr2tree($customizemenu, 'index', 'pindex');
}
}
//授权菜单
$wechat_menu = db('wechat_menu')->where('status=1 and pid=0 and uid=1')->order('sort ASC,id ASC')->select();
if($wechat_menu){
foreach ($wechat_menu as $k=>$v){
$childs = db('wechat_menu')->where('status=1 and pid='.$v['id'])->order('sort ASC,id ASC')->select();
if($childs){
$menu[$k]['name'] = $v['title'];
foreach ($childs as $kk=>$vv){
if($vv['url']=="kefu_qrcode_url"){
$menu[$k]['sub_button'][$kk]['name'] = $vv['title'];
}else{
$menu[$k]['sub_button'][$kk]['name'] = $vv['title'];
}
}
}else{
if($v['url']=="kefu_qrcode_url"){
$menu[$k]['name'] = $v['title'];
}else{
$menu[$k]['name'] = $v['title'];
}
}
}
}
if($cmenu){
$type=2;
}else{
$type=1;
}
$this->assign('type',$type);
$this->assign('menu',$menu);
$this->assign('cmenu',$cmenu);
$this->setMeta("公众号运营");
return $this->fetch();
}
/**
* menuedit [公众号菜单栏编辑]
*
* @return type
**/
public function menuedit() {
$data = $_REQUEST['data'];
$wxmpid=session('wxmpid');
$uid= session('user_auth.uid');
//$tree = arr2tree($data, 'index', 'pindex');
if($data){
foreach ($data as $k=>&$v){
$data[$k]['uid']=$uid;
$data[$k]['wxmpid']=$wxmpid;
$data[$k]['create_at']=time();
}
}
if (Db::name('wechat_customizemenu')->where(['uid'=>$uid,'wxmpid'=>$wxmpid])->delete() !== false && Db::name('wechat_customizemenu')->insertAll($data) !== false) {
$this->push();
}
}
/**
* menucopy [复制授权菜单]
*
* @return type
**/
public function menucopy() {
$uid= session('user_auth.uid');
$wxmpid = session('wxmpid');
$arrayurl =[0,"/?channel=1&source=wechat_menu","/?channel=2&source=wechat_menu","/featured.html?source=wechat_menu&channel=1","/featured.html?source=wechat_menu&channel=2","/top.html?source=wechat_menu&channel=1","/top.html?source=wechat_menu&channel=2","/focushistory?source=wechat_menu","/personal/info?source=wechat_menu","/pay/index?source=wechat_menu","/personal/bookcase?source=wechat_menu","/search.html?source=wechat_menu","kefu_qrcode_url"];
$wechat_menu = db('wechat_menu')->where('status=1 and pid=0 and uid=1')->order('sort ASC,id ASC')->select();
$menu = array();
if($wechat_menu){
foreach ($wechat_menu as $k=>$v){
$menu[] = array(
'name' => $v['title'],
'url' =>$v['url'],
'uid' => $uid,
'wxmpid' => $wxmpid,
'index' => $k+1,
'pindex' => 0,
'type' => array_search($v['url'],$arrayurl,true),
'sort' => $k,
'create_at' => time(),
);
$childs = db('wechat_menu')->where('status=1 and pid='.$v['id'])->order('sort ASC,id ASC')->select();
if($childs){
foreach ($childs as $kk=>$vv){
$menu[] = array(
'name' => $vv['title'],
'url' => $vv['url'],
'uid' => $uid,
'wxmpid' => $wxmpid,
'index' => ($k+1).($kk+1),
'pindex' => $k+1,
'type' => array_search($vv['url'],$arrayurl,true),
'sort' => $k,
'create_at' => time(),
);
}
}
}
}
if(Db::name('wechat_customizemenu')->insertAll($menu) !== false) {
return $this->success("复制成功!", url('wxmp/menu'));
}
}
public function subscribe() {
$uid= session('user_auth.uid');
$wxmpid = session('wxmpid');
if($uid==1){
$info = db('wxmp')->where(array('uid'=>$uid))->find();
}else{
$info = db('wxmp')->where(array('id'=>$wxmpid,'status'=>1))->find();
}
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
if(IS_POST){
$post = input('post.');
if($post['id']){
$result = db('WechatKeys')->update($post);
}else{
$post['keys'] = 'subscribe';
$post['wxmpid'] = session('wxmpid');
$post['appid'] = $info['appid'];
$result = db('WechatKeys')->insert($post);
}
if ($result !== false) {
return $this->success("更新成功!", url('admin/wxmp/subscribe', array('model_id' =>18)));
} else {
return $this->error($this->model->getError(), url('admin/wxmp/subscribe', array('model_id' => 18,'id'=>$cartoonid)));
}
exit();
}
$subscribe_info = db('WechatKeys')->where(array('appid'=>$info['appid'],'keys'=>'subscribe'))->find();
$type=$subscribe_info?2:1;
$novel = db('novel')->where('status=1')->field('id,title')->select();
$data = array(
'info' => $subscribe_info,
'wxmpid'=>$info['id'],
'wxmp_reply'=>implode(PHP_EOL,config('wxmp_reply')),
'type'=>$type,
'novel'=>$novel,
'url'=>get_menuurl($uid,$wxmpid),
'uid'=>$uid
);
$this->assign($data);
$this->setMeta("被关注回复");
return $this->fetch();
}
public function defaults() {
$uid= session('user_auth.uid');
$wxmpid = session('wxmpid');
if($uid==1){
$info = db('wxmp')->where(array('uid'=>$uid))->find();
}else{
$info = db('wxmp')->where(array('id'=>$wxmpid,'status'=>1))->find();
}
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
if(IS_POST){
$post = input('post.');
if($post['id']){
$result = db('WechatKeys')->update($post);
}else{
$post['keys'] = 'default';
$post['appid'] = $info['appid'];
$result = db('WechatKeys')->insert($post);
}
if ($result !== false) {
return $this->success("更新成功!", url('admin/wxmp/subscribe', array('model_id' =>18)));
} else {
return $this->error($this->model->getError(), url('admin/wxmp/subscribe', array('model_id' => 18,'id'=>$cartoonid)));
}
exit();
}
$subscribe_info = db('WechatKeys')->where(array('appid'=>$info['appid'],'keys'=>'default'))->find();
$data = array(
'info' => $subscribe_info
);
$this->assign($data);
$this->setMeta("默认回复");
return $this->fetch();
}
/**
* 内容修改
* @author netlife <40150501@qq.com>
*/
public function edit($id) {
if (IS_POST) {
$this->param['model_id'] = 18;
$result = $this->model->save($this->param, array('id'=> $id));
if ($result !== false) {
//记录行为
action_log('update_content', 'content', $result, session('user_auth.uid'));
return $this->success("更新成功!", url('admin/wxmp/autoconfig', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/wxmp/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 = 'content/' . $this->modelInfo['template_edit'];
} else {
$template = 'cartoon/edit';
}
$this->assign($data);
$this->setMeta("编辑" . $this->modelInfo['title']);
return $this->fetch($template);
}
}
/**
* 内容删除
* @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_autoconfig', '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' || $this->request->action() == 'settings') {
$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;
}
/**
* 菜单推送
*/
public function push()
{
$uid = session('user_auth.uid');
$wxmpid = session('wxmpid');
$webname = session('webname');
if($wxmpid){
$agent_url = get_menuurl($uid,$wxmpid);
}else{
$agent_url = get_menuurl($uid);
}
$menu = array();
if($wxmpid){
$result = (array)db('wechat_customizemenu')->field('id,index,pindex,name,url')->where(['status' => '1','uid'=>$uid,'wxmpid'=>$wxmpid])->order('`index` ASC,id ASC')->select();
if($result){
foreach ($result as &$m){
if($m['url']=="kefu_qrcode_url"){
$m['type']="click";
$m['key']=$m['url'];
}else{
$m['type']="view";
}
if (preg_match('#^(\w+:)?//#', $m['url'])) {
$m['url'] = $m['url'];
} else {
$m['url'] = $agent_url.$m['url'];
}
}
}
$menu = arr2tree($result, 'index', 'pindex','sub_button');
//去除无效的字段
$k=1;
foreach ($menu as &$menus) {
unset($menus['index'], $menus['pindex'], $menus['id']);
if (empty($menus['sub_button'])) {
continue;
}else{
unset($menus['url']);
$menus['key'] = "wechat_menu#id#".$k;
}
foreach ($menus['sub_button'] as &$submenu) {
unset($submenu['index'], $submenu['pindex'], $submenu['id']);
}
unset($menus['type']);
$k++;
}
}
if(empty($menus)){
$wechat_menu = db('wechat_menu')->where('status=1 and pid=0 and uid=1')->order('sort ASC,id ASC')->select();
if($wechat_menu){
foreach ($wechat_menu as $k=>$v){
$childs = db('wechat_menu')->where('status=1 and pid='.$v['id'])->order('sort ASC,id ASC')->select();
if($childs){
$menu[$k]['name'] = $v['title'];
$menu[$k]['key'] = "wechat_menu#id#".$k;
foreach ($childs as $kk=>$vv){
if($vv['url']=="kefu_qrcode_url"){
$menu[$k]['sub_button'][$kk]['name'] = $vv['title'];
$menu[$k]['sub_button'][$kk]['type'] = "click";
$menu[$k]['sub_button'][$kk]['key'] = "kefu_qrcode_url";
}else{
$menu[$k]['sub_button'][$kk]['name'] = $vv['title'];
$menu[$k]['sub_button'][$kk]['type'] = "view";
$menu[$k]['sub_button'][$kk]['url'] = $agent_url.$vv['url'];
}
}
}else{
if($v['url']=="kefu_qrcode_url"){
$menu[$k]['name'] = $v['title'];
$menu[$k]['type'] = 'click';
$menu[$k]['key'] = "kefu_qrcode_url";
}else{
$menu[$k]['name'] = $v['title'];
$menu[$k]['type'] = 'view';
$menu[$k]['url'] = $agent_url.$v['url'];
}
}
}
}
}
if($wxmpid){
$auto = db('wxmp')->where(['uid'=>$uid,'id'=>$wxmpid,'status'=>1])->field('appid,autoconfig,open_appid')->find();
//公众号自动配置
if(array_key_exists("autoconfig",$auto)){
$account = db('wxopen_account')->where('appid',$auto['open_appid'])->find();
$wxComponentConfig = array(
'component_appid'=>$account["appid"],
'component_appsecret'=>$account["appsecret"],
'encodingAesKey'=>$account["encodingKey"],
'token'=>$account["token"]
);
$wxAuth = new WxComponentService($wxComponentConfig);
$wechat = $wxAuth->getWechat($auto["appid"]);
}
}else{
$wechat = load_wechat($uid,'Menu');
}
if (false !== $wechat->createMenu(['button' => $menu])) {
return $this->success("推送成功!", url('wxmp/menu'));
}
return $this->error($wechat->getError(), url('wxmp/menu'));
}
/*******上传微信图片测试**********/
public function uploadwechatimg() {
WechatService::uploadImage(1, "http://m.youyandm.com/uploads/picture/20171216/b2fd72330b7ef0cf02c07ee868d4881c.png");
}
/***智能推送****/
public function rich_msg_setting() {
if(IS_POST){
$uid = session('user_auth.uid');
$wxmpid = session('wxmpid');
if($uid==1){
$info = db('wxmp')->where(array('uid'=>$uid))->find();
}else{
$info = db('wxmp')->where(array('id'=>$wxmpid,'status'=>1))->find();
}
//$info = db('wxmp')->where(array('id'=>$wxmpid,'status'=>1))->find();
if(empty($info)) return $this->error("您的公众号配置不完整,请务必配置好公众号信息。");
$data = $this->request->param();
if($data["method"]=='api_enable'){
if($data["id"]>0){
db('wechat_intelligent')->where('id',$data["id"])->update(['open' => 1]);
}else{
$intelligent = [
"create_time"=>time(),
"update_time"=>time(),
"uid"=>session('user_auth.uid'),
"type"=>$data["type"],
"wxmpid"=>$wxmpid,
"open"=>1
];
db('wechat_intelligent')->insert($intelligent);
}
return $this->success("设置成功!", url('wxmp/rich_msg_setting'));
}else{
$intelligent_id = db('wechat_intelligent')->where(['uid'=>session('user_auth.uid'),'id'=>$data["id"]])->value('id');
if($intelligent_id){
db('wechat_intelligent')->where('id',$intelligent_id)->update(['open' => 0]);
return $this->success("取消成功!",url('wxmp/rich_msg_setting'));
}else{
return $this->success("取消失败!",url('wxmp/rich_msg_setting'));
}
}
exit();
}
$wxmpid = session('wxmpid');
$list = db('wechat_intelligent')->where(array("status"=>1,"uid"=>session('user_auth.uid'),"wxmpid"=>$wxmpid))->select();
$intelligent = config('intelligent');
$type = array();
if($list){
foreach ($list as $k=>$v){
$type[$v["type"]]["id"]=$v["id"];
$type[$v["type"]]["open"]=$v["open"];
}
}
foreach ($intelligent as $k=>$v){
$data[$k-1] = $v;
$data[$k-1]["type"] = $k;
if(array_key_exists($k,$type)){
$data[$k-1]["id"] =$type[$k]["id"];
$data[$k-1]["open"] =$type[$k]["open"];
}else{
$data[$k-1]["id"] =0;
$data[$k-1]["open"] =0;
}
}
$this->assign('list',$data);
$this->setMeta("智能推送");
return $this->fetch();
}
}