getContentMenu();
$this->model_id = $model_id = 26;
//$this->model_id = $model_id = $this->request->param('model_id') = 26;
$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']);
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 = 'template/' . $this->modelInfo['template_list'];
} else {
$template = 'template/index';
}
$this->assign($data);
$this->setMeta($this->modelInfo['title'] . "列表");
return $this->fetch($template);
}
/**
* 内容添加
* @author netlife <40150501@qq.com>
*/
public function add() {
if (IS_POST) {
if(empty($this->param['template_name'])){
$this->ReturnJson(0,"模板未选择");
}
if(empty($this->param['template_value'])){
$this->ReturnJson(0,"模板内容为空");
}
$this->param['wxmpid'] = session('wxmpid');
$this->param['uid'] = session('user_auth.uid');
$this->param['scheduled_at'] =strtotime($this->param['scheduled_at']);
$this->param['create_time'] =time();
$this->param['update_time'] =time();
$result = $this->model->save($this->param);
if ($result) {
//记录行为
action_log('add_content', 'content', $result, session('user_auth.uid'));
return $this->success("添加成功!", url('admin/template/index', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/template/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'],
);
//获取模板配置信息
$message = &load_wechat($uid,'Message');
$msg = $message->getAllPrivateTemplate();
$data = array(
'info' => $info,
'msg' => $msg["template_list"],
'turl' => $turl,
'fieldGroup' => $this->getField($this->modelInfo),
);
if ($this->modelInfo['template_add']) {
$template = 'template/' . $this->modelInfo['template_add'];
} else {
$template = 'template/add';
}
$this->assign($data);
$this->setMeta("添加" . $this->modelInfo['title']);
return $this->fetch($template);
}
}
/**
* 内容修改
* @author netlife <40150501@qq.com>
*/
public function edit($id) {
if (IS_POST) {
$this->param['scheduled_at'] =strtotime($this->param['scheduled_at']);
$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/template/index', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/template/edit', array('model_id' => $this->modelInfo['id'], 'id' => $id)));
}
} else {
if (!$id) {
return $this->error("非法操作!");
}
$info = $this->model->find($id);
$info['template_example'] = str_replace(PHP_EOL,'
',$info['template_example']);
if (!$info) {
return $this->error($this->model->getError());
}
$info['model_id'] = $this->modelInfo['id'];
$uid = session('user_auth.uid');
$turl = db('tuiguang')->where('uid',$uid)->select();
//获取模板配置信息
$message = &load_wechat($uid,'Message');
$msg = $message->getAllPrivateTemplate();
$data = array(
'info' => $info,
'msg' => $msg["template_list"],
'template_value'=> json_decode($info['template_value'],true),
'turl' => $turl,
'fieldGroup' => $this->getField($this->modelInfo),
);
if ($this->modelInfo['template_edit']) {
$template = 'template/' . $this->modelInfo['template_edit'];
} else {
$template = 'template/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_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 朱亚杰
*/
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;
}
/**
* chapter [模板]
*
* @param type $id [desc]
*
* @return type
**/
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 testsend(){
$data = $this->request->param();
//var_dump(json_decode($data["template_value"],true));
//exit();
$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();
}
$egold = db('member')->where('uid',$data['test_member_id'])->value('egold');
$result = $this->send_message($openid,$data,$egold);
if($result["errmsg"]=="ok"){
echo json_encode(array("code"=>0));
exit();
}else{
echo json_encode($result);
exit();
}
}
/*****发送模板消息******/
protected function send_message($openid,$data,$egold=0){
$uid = session('user_auth.uid');
$message = &load_wechat($uid,'Message');
/**
$template = array(
"touser"=>$openid,
"template_id"=>$data["template_id"],
"url"=>$data["url"],
"data"=>str_replace("{nickname}","轻舞飞扬",json_decode($data["template_value"],true))
);
**/
$username = get_nickname($uid);
$template = '{
"touser":"'.$openid.'",
"template_id":"'.$data["template_id"].'",
"url":"'.$data["url"].'",
"data":'.str_replace(array("{nickname}","{time}"," {coinnum}"),array($username,date('Y-m-d H:i:s'),$egold),$data["template_value"]).'
}';
$msg = $message->sendTemplateMessage($template);
return $msg;
}
}