微擎写API接口


最近公司接了微擎二开的项目
我决定采用接口的方式进行数据交互,该方法应该不是最好的。
首先在模块的目录下的site.php文件新建一个方法
例如

    public function doMobileHzwpostend()
    {
     require_once(dirname(__FILE__)."/inc/mobile/Hzwpostend.inc.php");
    }

然后在模块的根目录(与site.php同级)下新建一个inc文件夹,里面在新建一个mobile文件夹,新建Hzwpostend.inc.php文件

Hzwpostend.inc.php文件即可处理你的业务逻辑

<?php
    global $_W, $_GPC;
    $acid = $_GPC['uniacid'];
    $uniacid = $_GPC['uniacid'];
    $op = $_GPC['op'];
    if (empty($_GPC['uniacid'])) {
        $this->show($this->res, "公众号ID不能为空", '-1');
    }
    if (empty($_GPC['op'])) {
        $this->show($this->res, "op操作不能为空", '-1');
    }
    if (empty($_GPC['openid'])) {
        $this->show($this->res, "openid不能为空", '-1');
    }
    //申请加盟
    if($_GPC['op']=="addRejoindre"){
        if(empty($_GPC['name'])) $this->show($this->res, "名字不能为空", '-1');
        if(empty($_GPC['phone'])) $this->show($this->res, "手机不能为空", '-1');
        if(empty($_GPC['reason'])) $this->show($this->res, "申请理由不能为空", '-1');
        if(empty($_GPC['time'])) $this->show($this->res, "加盟时间不能为空", '-1');

        //入库
        $insert['uniacid']  = $_GPC['uniacid'];
        $insert['openid']  = $_GPC['openid'];
        $insert['name']  = $_GPC['name'];
        $insert['phone']  = $_GPC['phone'];
        $insert['reason']  = $_GPC['reason'];
        $insert['time']  = $_GPC['time'];
        $insert['create_time'] = time();
        $result = pdo_insert('hzw_rejoindre', $insert);

        if($result){
            $this->show($this->res, "申请成功", '1');
        }else{
            $this->show($this->res, "申请失败", '-1');
        }

    }
    //申请加盟
    if($_GPC['op']=="RejoindreList"){
         $hzw_rejoindre = pdo_fetchall('SELECT * FROM ' . tablename('hzw_rejoindre') . " WHERE uniacid = {$_W['uniacid']} AND openid = '{$_GPC['openid']}'");
        if($hzw_rejoindre){
            $this->res['hzw_rejoindre'] = $hzw_rejoindre;
            $this->show($this->res, "获取申请加盟列表", '1');
        }else{
            $this->show($this->res, "没有结果", '-1');
        }

    }
    $this->show($this->res, "op参数非法", '-1');

由于微擎是可以绑定多个公众号一般每个接口都要带有uniacid公众号ID这个参数
op参数是我自定义的,由于微擎没有控制器这个概念(可能我不懂)用于区别不同“方法”
封装的返回json方法show放在site里面即可

    public function show($data,$mes,$code){
        $json = array('code' => $code, 'mes' => $mes, 'data' => $data);
        exit(json_encode($json));
    }

接口路由
http://www.xxx.com/app/index.php?i=2&c=entry&id=337&do=Hzwpostend&m=hzw_toutiao
//Hzwpostend 方法名

Pasa吴技术博客
请先登录后发表评论
  • latest comments
  • 总共0条评论