<?php

Doo::loadController('base', true);

# 自定义菜单

class menu extends base{
    
    public function index(){  
        $this->renderc('users/wx_menu', $this->data);
    }
    
    public function create(){
        $menu = $this->request->getPost('content');
        Doo::loadModel('AdminOption', false, true);
        $AO = new AdminOption();
        $AO->option_name = 'wxMenuParams';
        $db = $AO->getOne();
        if ($db){
            $db->option_value = $menu;
            $db->dateline = time();
            $db->update();
            Doo::loadLibrary('Wechat');
            $wxappId = service::get_option('wxappId');
            $wxappSecret = service::get_option('wxappSecret');
            $wxtoken = service::get_option('wxtoken');
            $wxAESKey = service::get_option('wxAESKey');
            $wx = new Wechat(array(
                'token' => $wxtoken,
                'encodingaeskey' => $wxAESKey,
                'appid' => $wxappId,
                'appsecret' => $wxappSecret
            ));
            $menu = json_decode($menu, true);
            if ($wx->createMenu($menu)){
                echo '菜单推送成功';
            }else{
                echo '菜单推送失败';
            }
        }
    }
}