WeChat development, adding multi-graphics interface and message group display interface

1. First, create the Qunfa folder under the home file in the html file, and write the HTML files named newlist and addnews, which are the front pages.

2. Create the controller QunfaController.class.php and write the namespace and _initialize method to detect whether the official account is used. code show as below:

<?php
namespace Home\Controller;
use Think\Controller;

class QunfaController extends Controller{

	private $mp;

    //initialization
    public function _initialize(){
        $mp = getCurrentMp();
        if (empty($mp)) {
            $this->error('Unused official account',U('mp/index'));
            exit();
        }else{
            $this->mp = $mp;
        }
    }

3. Write the addnews method to realize the multi-image upload interface. code show as below:

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'=>'Added successfully'));    

        }
    }

To upload images, you need to use the upload method. The code is as follows:

public function upload(){
        $upload = new \Think\Upload();//Instantiate the upload class
        $upload->maxSize = 3145728;//Set the size of the attachment upload
        $upload->exts = array('jpg','gif','png','jpeg');//Set the type of attachment upload
        $upload->rootPath = './Uploads/';//Set attachment upload root directory
        $upload->savePath = '';//Set attachment upload (sub) directory
        //upload files
        $info = $upload->uploadOne($_FILES['file']);
        if (!$info) {//Upload error display error message
          $this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
        }else{
          $file = '/Uploads/'.$info['savepath'].$info['savename'];
          $this->ajaxReturn(array('code'=>0,'msg'=>'Upload successful!','url'=>$file));
        }
    }

4. Write the newslist method to display the graphic and text group sending interface. The code is as follows:

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();
	}

      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669468&siteId=291194637