Example #1
0
 function save($ctx)
 {
     _render('form');
     $id = intval($_POST['id']);
     $m = WxReplyItem::get($id);
     if ($id && !$m) {
         _redirect($this->_list_url());
         return;
     }
     $type = $_POST['type'];
     $title = $_POST['title'];
     $desc = $_POST['desc'];
     $img_url = $_POST['img_url'];
     $link = $_POST['link'];
     $content = $_POST['content'];
     if (!$type) {
         $ctx->errmsg = "空内容1.";
         return;
     }
     $reply_list = array();
     foreach ($type as $index => $mt) {
         if ($mt == 'text') {
             $c = $content[$index];
             if (!strlen($c)) {
                 continue;
             }
             $reply_list[] = array('type' => $mt, 'content' => $c);
         } else {
             if ($mt == 'news') {
                 $t = $title[$index];
                 $d = $desc[$index];
                 $i = $img_url[$index];
                 $l = $link[$index];
                 if (!strlen($t) || !strlen($i) || !strlen($l)) {
                     continue;
                 }
                 $channel_info = WxReplyKeyword::check_channel_subscribe_keyword($_POST['keywords']);
                 if ($channel_info) {
                     // 所有链接带上pcode
                     $arr_query = parse_url($l);
                     parse_str($arr_query['query'], $output);
                     $output['pcode'] = $channel_info->promotion_code;
                     $str_query = http_build_query($output);
                     $l = $arr_query['scheme'] . '://' . $arr_query['host'] . $arr_query['path'] . '?' . $str_query;
                 }
                 $reply_list[] = array('type' => $mt, 'title' => $t, 'desc' => $d, 'img_url' => $i, 'link' => $l);
             } else {
                 $ctx->errmsg = "异常错误!";
                 return;
             }
         }
     }
     if (!$reply_list) {
         $ctx->errmsg = "空内容2.";
         return;
     }
     $content = Text::json_encode($reply_list);
     if (!$m) {
         $m = WxReplyItem::save(array('status' => 0, 'type' => 'mixed', 'content' => $content));
     } else {
         $m->update(array('content' => $content));
         $m = WxReplyItem::get($id);
     }
     $keyword_type = $_POST['keyword_type'];
     $m->reset_keywords($_POST['keywords'], $keyword_type);
     _redirect($this->_list_url());
     return;
 }