<commit_before><?php
// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

namespace app\wechat\controller;

use controller\BasicAdmin;
use service\ToolsService;
use think\Db;

/**
 * 微信菜单管理
 * Class Menu
 * @package app\wechat\controller
 * @author Anyon <<EMAIL>>
 * @date 2017/03/27 14:43
 */
class Menu extends BasicAdmin {

    /**
     * 指定当前页面标题
     * @var string
     */
    protected $title = '微信菜单定制';

    /**
     * 指定默认操作的数据表
     * @var string
     */
    protected $table = 'WechatMenu';

    /**
     * 微信菜单的类型
     * @var array
     */
    protected $menu_type = array(
        'view'               => '跳转URL',
        'click'              => '点击推事件',
        'scancode_push'      => '扫码推事件',
        'scancode_waitmsg'   => '扫码推事件且弹出“消息接收中”提示框',
        'pic_sysphoto'       => '弹出系统拍照发图',
        'pic_photo_or_album' => '弹出拍照或者相册发图',
        'pic_weixin'         => '弹出微信相册发图器',
        'location_select'    => '弹出地理位置选择器',
    );

    /**
     * 显示列表操作
     */
    public function index() {
        parent::_list(Db::name($this->table), false, true);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data) {
        $data = ToolsService::arr2tree($data, 'index', 'pindex');
    }

    /**
     * 微信菜单编辑
     */
    public function edit() {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            !isset($post['data']) && $this->error('访问出错，请稍候再试！');
            $data = $post['data'];
            if (empty($data)) {
                Db::name($this->table)->where('1=1')->delete();
                load_wechat('Menu')->deleteMenu();
                $this->success('删除并取消微信菜单成功！', '');
            }
            if (Db::name($this->table)->where('1=1')->delete() !== false && Db::name($this->table)->insertAll($data) !== false) {
                $result = $this->_push();
                if ($result['status']) {
                    $this->success('保存发布菜单成功！', '');
                }
                $this->error('菜单发布失败，' . $result['errmsg']);
            }
            $this->error('保存发布菜单失败！');
        }
    }

    /**
     * 取消菜单
     */
    public function cancel() {
        $wehcat = &load_wechat('Menu');
        if (false !== $wehcat->deleteMenu()) {
            $this->success('菜单取消成功，重新关注可立即生效！', '');
        }
        $this->error('菜单取消失败，' . $wehcat->errMsg);
    }

    /**
     * 菜单推送
     */
    protected function _push() {
        $result = Db::name($this->table)
            ->field('id,index,pindex,name,type,content')
            ->where('status', '1')
            ->order('sort ASC,id ASC')
            ->select();<commit_msg>[更新]简化代码，优化结构<commit_after><?php
// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

namespace app\wechat\controller;

use controller\BasicAdmin;
use service\LogService;
use service\ToolsService;
use think\Db;

/**
 * 微信菜单管理
 * Class Menu
 * @package app\wechat\controller
 * @author Anyon <<EMAIL>>
 * @date 2017/03/27 14:43
 */
class Menu extends BasicAdmin {

    /**
     * 指定当前页面标题
     * @var string
     */
    protected $title = '微信菜单定制';

    /**
     * 指定默认操作的数据表
     * @var string
     */
    protected $table = 'WechatMenu';

    /**
     * 微信菜单的类型
     * @var array
     */
    protected $menu_type = array(
        'view'               => '跳转URL',
        'click'              => '点击推事件',
        'scancode_push'      => '扫码推事件',
        'scancode_waitmsg'   => '扫码推事件且弹出“消息接收中”提示框',
        'pic_sysphoto'       => '弹出系统拍照发图',
        'pic_photo_or_album' => '弹出拍照或者相册发图',
        'pic_weixin'         => '弹出微信相册发图器',
        'location_select'    => '弹出地理位置选择器',
    );

    /**
     * 显示列表操作
     */
    public function index() {
        parent::_list(Db::name($this->table), false, true);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data) {
        $data = ToolsService::arr2tree($data, 'index', 'pindex');
    }

    /**
     * 微信菜单编辑
     */
    public function edit() {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            !isset($post['data']) && $this->error('访问出错，请稍候再试！');
            $data = $post['data'];
            if (empty($data)) {
                Db::name($this->table)->where('1=1')->delete();
                load_wechat('Menu')->deleteMenu();
                $this->success('删除并取消微信菜单成功！', '');
            }
            if (Db::name($this->table)->where('1=1')->delete() !== false && Db::name($this->table)->insertAll($data) !== false) {
                $result = $this->_push();
                if ($result['status']) {
                    LogService::write('微信管理', '发布微信菜单成功');
                    $this->success('保存发布菜单成功！', '');
                }
                $this->error('菜单发布失败，' . $result['errmsg']);
            }
            $this->error('保存发布菜单失败！');
        }
    }

    /**
     * 取消菜单
     */
    public function cancel() {
        $wehcat = &load_wechat('Menu');
        if (false !== $wehcat->deleteMenu()) {
            $this->success('菜单取消成功，重新关注可立即生效！', '');
        }
        $this->error('菜单取消失败，' . $wehcat->errMsg);
    }

    /**
     * 菜单推送
     */
    protected function _push() {
        $result = Db::name($this->table)
            ->field('id,index,pindex,name,type,content')
            ->where('status', '1')
            ->order('sort ASC,id ASC')
            ->select();
{'fraction_non_alphanumeric': 0.2974769602659012, 'fraction_numerical': 0.007856171627134008, 'mean_word_length': 1.2717913520933424, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 81, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067108', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2397, 'n_words': 285}