审查视图

application/admin/controller/wechat/Autoreply.php 1.8 KB
Karson authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

namespace app\admin\controller\wechat;

use app\common\controller\Backend;
use app\common\model\WechatResponse;

/**
 * 微信自动回复管理
 *
 * @icon fa fa-circle-o
 */
class Autoreply extends Backend
{

    protected $model = null;
17
    protected $noNeedRight = ['check_text_unique'];
Karson authored
18 19 20 21 22 23 24 25 26 27 28 29

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('WechatAutoreply');
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
Karson authored
30
        $row = $this->model->get(['id' => $ids]);
Karson authored
31 32 33 34 35 36 37 38 39 40 41
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $this->code = -1;
            $params = $this->request->post("row/a");
            if ($params)
            {
                $row->save($params);
                $this->code = 1;
            }
42
            return;
Karson authored
43 44 45 46
        }
        $response = WechatResponse::get(['eventkey' => $row['eventkey']]);
        $this->view->assign("response", $response);
        $this->view->assign("row", $row);
47
        return $this->view->fetch();
Karson authored
48 49
    }
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    /**
     * 判断文本是否唯一
     * @internal
     */
    public function check_text_unique()
    {
        $row = $this->request->post("row/a");
        $except = $this->request->post("except");
        $text = isset($row['text']) ? $row['text'] : '';
        if ($this->model->where('text', $text)->where(function($query) use($except) {
                    if ($except)
                    {
                        $query->where('text', '<>', $except);
                    }
                })->count() == 0)
        {
            return json(['ok' => '']);
        }
        else
        {
            return json(['error' => __('Text already exists')]);
        }
    }
Karson authored
74
}