jiqing9006

自定义菜单,很嗨皮!

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013

需要json传递数据!

微信提供三个接口,一个是创建菜单,一个是查看菜单,一个是删除菜单!

<?php
/**
 * Created by PhpStorm.
 * User: jiqing
 * Date: 18-7-19
 * Time: 下午9:15
 */

class MenuAction extends BaseAction
{
    // 创建自定义菜单
    const API_CREATE_MENU = \'https://api.weixin.qq.com/cgi-bin/menu/create\';
    // 查询自定义菜单
    const API_GET_MENU = \'https://api.weixin.qq.com/cgi-bin/menu/get\';
    // 删除自定义菜单
    const API_DELETE_MENU = \'https://api.weixin.qq.com/cgi-bin/menu/delete\';

    public function create() {
        $access_token = $this->_get_access_token();
        $uri = self::API_CREATE_MENU.\'?access_token=\'.$access_token;

        $params = [
            \'button\' => [
                [
                   \'type\'=>\'click\',
                   \'name\'=>\'今日歌曲\',
                   \'key\'=>\'V1001_TODAY_MUSIC\'
                ],
                [
                    \'name\'=> \'菜单\',
                    \'sub_button\' => [
                        [
                            \'type\' => \'view\',
                            \'name\' => \'百度\',
                            \'url\' => \'http://www.baidu.com/\'
                        ],
                        [
                            \'type\' => \'view\',
                            \'name\' => \'Bing\',
                            \'url\' => \'http://www.bing.com/\'
                        ],
                        [
                            \'type\' => \'view\',
                            \'name\' => \'搜搜\',
                            \'url\' => \'http://www.soso.com/\'
                        ],
                    ]
                ]
            ]
        ];

        $res_data = Http::doPostJson($uri, $params);
        vendor("Log.Clog");
        Clog::setLog($res_data);
        $res_data = json_decode($res_data, true);
        if ($res_data[\'errcode\'] != 0) {
            $this->ajaxReturn($this->jsonError(10001, \'接口请求失败\'));
        }

        $this->ajaxReturn($this->jsonSuccess(1, \'成功\', $res_data));
    }

    public function get() {
        $access_token = $this->_get_access_token();
        $params = [
            \'access_token\' => $access_token
        ];
        $res_data = Http::newDoGet(self::API_GET_MENU, $params);
        $res_data = json_decode($res_data, true);
        if ($res_data[\'errcode\'] != 0) {
            $this->ajaxReturn($this->jsonError(10001, \'接口请求失败\'));
        }

        $this->ajaxReturn($this->jsonSuccess(1, \'成功\', $res_data));
    }

    public function delete() {
        $access_token = $this->_get_access_token();
        $params = [
            \'access_token\' => $access_token
        ];
        $res_data = Http::newDoGet(self::API_DELETE_MENU, $params);
        $res_data = json_decode($res_data, true);
        if ($res_data[\'errcode\'] != 0) {
            $this->ajaxReturn($this->jsonError(10001, \'接口请求失败\'));
        }

        $this->ajaxReturn($this->jsonSuccess(1, \'成功\', $res_data));
    }


    private function _get_access_token() {
        vendor(\'Func.Http\');

        if (preg_match(\'/(http:\/\/)|(https:\/\/)/i\', C(\'SELF_HOSTNAME\'))) {
            $uri = C(\'SELF_HOSTNAME\').\'/AccessToken/get\';
        } else {
            $uri = \'http://\'.C(\'SELF_HOSTNAME\').\'/AccessToken/get\';
        }

        // 获取access_token
        $access_token_result = Http::doGet($uri);
        $access_token_result = json_decode($access_token_result, true);
        $access_token = $access_token_result[\'data\'][\'access_token\'];

        return $access_token;
    }
}

创建的时候会失败!

// 通过POST方式发送json数据
static public function doPostJson($url = \'\', $param = [] ,$contentType = \'json\') {
        $ch = curl_init();
        // 请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        // 请求参数类型
        $param = $contentType == \'json\' ? urldecode(json_encode($param,JSON_UNESCAPED_UNICODE)) : http_build_query($param);
        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // post提交
        if($param){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        // 返回的数据是否自动显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 执行并接收响应结果
        $output = curl_exec($ch);
        // 关闭curl
        curl_close($ch);
        return $output !== false ? $output : false;
}

这里的

 $param = $contentType == \'json\' ? urldecode(json_encode($param,JSON_UNESCAPED_UNICODE)) : http_build_query($param);

是关键!!!

很有意思!!!

可以结合自动回复来实现更加复杂的功能!!!

菜单还可以跳转到小程序!!!

模板消息也可以跳转到小程序!!!

再来一个小案例,

$params = [
    \'button\' => [
        [
            \'name\'=> \'从"新"出发\',
            \'sub_button\' => [
                [
                    \'type\' => \'view\',
                    \'name\' => \'甜蜜新品\',
                    \'url\' => \'http://www.breadtalk.com.cn/\'
                ],
                [
                    \'type\' => \'view\',
                    \'name\' => \'送!月饼券\',
                    \'url\' => \'http://www.breadtalk.com.cn/\'
                ],
                [
                    \'type\' => \'view\',
                    \'name\' => \'新语之旅\',
                    \'url\' => \'http://www.breadtalk.com.cn/\'
                ]
            ]
        ],
        [
            \'type\'=>\'miniprogram\',
            \'name\'=>\'甜蜜商城\',
            \'url\'=>\'http://mp.weixin.qq.com\',
            \'appid\'=>\'xxx\',
            \'pagepath\'=>\'pages/index/index\'
        ],
        [
            \'name\'=> \'我的新语\',
            \'sub_button\' => [
                [
                    \'type\'=>\'miniprogram\',
                    \'name\'=>\'我的订单\',
                    \'url\'=>\'http://mp.weixin.qq.com\',
                    \'appid\'=>\'xxx\',
                    \'pagepath\'=>\'pages/order/index\'
                ],
                [
                    \'type\'=>\'miniprogram\',
                    \'name\'=>\'优惠券\',
                    \'url\'=>\'http://mp.weixin.qq.com\',
                    \'appid\'=>\'xxx\',
                    \'pagepath\'=>\'pages/coupon/my_coupon\'
                ],
                [
                    \'type\'=>\'miniprogram\',
                    \'name\'=>\'会员充值\',
                    \'url\'=>\'http://mp.weixin.qq.com\',
                    \'appid\'=>\'xxx\',
                    \'pagepath\'=>\'pages/balance/add_balance\'
                ]
            ]
        ]
    ]
];

分类:

技术点:

相关文章: