1.首先在html文件中home文件下创建Qunfa文件夹,写入HTML文件名为newlist和addnews,为前台页面。
2.创建控制器QunfaController.class.php 写入命名空间和_initialize方法检测有无公众号使用。代码如下:
<?php
namespace Home\Controller;
use Think\Controller;
class QunfaController extends Controller{
private $mp;
//初始化
public function _initialize(){
$mp = getCurrentMp();
if (empty($mp)) {
$this->error('无使用的公众号',U('mp/index'));
exit();
}else{
$this->mp = $mp;
}
}
3.写入addnews方法实现多图文上传界面。代码如下:
public function addnews(){
if(IS_GET){
$this->display();
}else{
$data = I('post.data');
$mp = $this->mp;
$mp_id = $mp['id'];
foreach ($data as $key => &$v) {
$v['mp_id'] = $mp_id;
$v['create_time'] = time();
$data['media_id'] = $v['thumb_media_id'];
if($key==0){
$news_id = M('media_news')->add($v);
}
$v['news_id'] = $news_id;
M('media_news_list')->add($v);
}
$this->ajaxReturn(array('error'=>0,'msg'=>'添加成功'));
}
}
上传图片需使用upload方法 代码如下:
public function upload(){
$upload = new \Think\Upload();//实例化上传类
$upload->maxSize = 3145728;//设置附件上传的大小
$upload->exts = array('jpg','gif','png','jpeg');//设置附件上传的类型
$upload->rootPath = './Uploads/';//设置附件上传根目录
$upload->savePath = '';//设置附件上传(子)目录
//上传文件
$info = $upload->uploadOne($_FILES['file']);
if (!$info) {//上传错误显示错误信息
$this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
}else{
$file = '/Uploads/'.$info['savepath'].$info['savename'];
$this->ajaxReturn(array('code'=>0,'msg'=>'上传成功!','url'=>$file));
}
}
4.写入newslist方法显示图文群发界面,代码如下:
public function newslist(){
$mp = getCurrentMp();
$where['mp_id']=$mp['id'];
$data = M('media_news')->where($where)->select();
// print_r($data);
foreach ($data as $key => $value) {
$where['news_id']=$value['news_id'];
$arr=M('media_news_list')->where($where)->select();
// print_r($arr);
// exit;
$data[$key]['list']=$arr;
}
// print_r($data);
// exit();
$this->assign('data',$data);
$this->display();
}