فهرست منبع

Merge pull request #918 from visuddhinanda/laravel

完成pr 增加,修改,删除
visuddhinanda 3 سال پیش
والد
کامیت
ada16d45e6

+ 34 - 7
app/Http/Controllers/SentPrController.php

@@ -6,6 +6,7 @@ use App\Models\SentPr;
 use App\Models\Channel;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
 
 class SentPrController extends Controller
 {
@@ -37,7 +38,8 @@ class SentPrController extends Controller
         $data = $request->all();
 
 		
-		#新建
+		#查询是否存在 
+		#同样的内容只能提交一次
 		$exists = SentPr::where('book_id',$data['book'])
 						->where('paragraph',$data['para'])
 						->where('word_start',$data['begin'])
@@ -168,13 +170,20 @@ class SentPrController extends Controller
         }else{
 			$user_uid = $_COOKIE['user_uid'];
 		}
-		
-		if($sentPr->editor_uid==$user_uid){
-			$sentPr->update([
+		$sentPr = SentPr::where('id',$request->get('id'));
+		if($sentPr->value('editor_uid')==$user_uid){
+			$update = $sentPr->update([
 				"content"=>$request->get('text'),
 				"modify_time"=>time()*1000,
 			]);
-			return $this->ok($sentPr);
+			if($update >= 0){
+				$data = SentPr::where('id',$request->get('id'))->first();
+				$data->id = sprintf("%d",$data->id);
+				return $this->ok($data);
+			}else{
+				return $this->error('没有更新');
+			}
+			
 		}else{
 			return $this->error('not power');
 		}
@@ -184,11 +193,29 @@ class SentPrController extends Controller
     /**
      * Remove the specified resource from storage.
      *
-     * @param  \App\Models\SentPr  $sentPr
+     * @param  int $id
      * @return \Illuminate\Http\Response
      */
-    public function destroy(SentPr $sentPr)
+    public function destroy($id)
     {
         //
+		Log::info("user_uid=" .$_COOKIE['user_uid']);
+		$old = SentPr::where('id', $id)->first();
+		$result = SentPr::where('id', $id)
+							->where('editor_uid', $_COOKIE["user_uid"])
+							->delete();
+		Log::info("delete=" .$result);
+		if($result>0){
+					#同时返回此句子pr数量
+		$count = SentPr::where('book_id' , $old->book_id)
+						->where('paragraph' , $old->paragraph)
+						->where('word_start' , $old->word_start)
+						->where('word_end' , $old->word_end)
+						->where('channel_uid' , $old->channel_uid)
+						->count();
+			return $this->ok($count);
+		}else{
+			return $this->error('not power');
+		}
     }
 }

+ 3 - 2
app/Http/Middleware/EnableCrossRequestMiddleware.php

@@ -16,7 +16,7 @@ class EnableCrossRequestMiddleware
      */
     public function handle(Request $request, Closure $next)
     {
-                $response = $next($request);
+        $response = $next($request);
         $origin = $request->server('HTTP_ORIGIN') ? $request->server('HTTP_ORIGIN') : '';
         $allow_origin = [
             env("CROSS_REQUEST_ALLOW_ORIGIN",'http://localhost:8001'),
@@ -34,6 +34,7 @@ class EnableCrossRequestMiddleware
         转自链接:https://learnku.com/articles/6504/laravel-cross-domain-solution
         版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。
         */
-        return $next($request);
+        //return $next($request);
+		return $response;
     }
 }

+ 3 - 0
app/Models/SentPr.php

@@ -8,4 +8,7 @@ use Illuminate\Database\Eloquent\Model;
 class SentPr extends Model
 {
     use HasFactory;
+	protected $casts = [
+		'id' => 'string'
+	];
 }

+ 11 - 0
public/app/article/article.js

@@ -700,4 +700,15 @@ function gotoPara(paragraph) {
 		url += "&direction=" + _direction;
 	}
 	location.assign(url);
+}
+
+function show_channel_detail_pannal(){
+	if($("#right_pannal").css("display")=="none"){
+		$("#right_pannal").show();
+		$(".contents_div").css("width","70%");
+	}else{
+		$("#right_pannal").hide();
+		$(".contents_div").css("width","100%");		
+	}
+
 }

+ 3 - 0
public/app/article/index.php

@@ -374,6 +374,9 @@ function set_toc_visible(isVisible){
 						<span></span>
 					</div>
 				</div>
+				<div>
+					<button id="btn_show_channel" onclick='show_channel_detail_pannal()'>Details</button>
+				</div>
 			</div>
 		</div>
 		<div id="article_title" class="term_word_head_pali"></div>

+ 4 - 0
public/app/article/mobile.css

@@ -96,4 +96,8 @@ note:hover .ref {
 }
 .nav_bnt{
 	width: 100%;
+}
+
+#btn_show_channel{
+	display:none;
 }

+ 113 - 12
public/app/term/note.js

@@ -91,9 +91,17 @@ function note_sent_edit_dlg_init() {
 function note_init(input,channel="",editor="",lang="en") {
 	if (input) {
 		let output = "<div>";
-		//output += marked(input);
-		output += marked(term_std_str_to_tran(input, channel, editor, lang), { renderer: note_renderer });
-
+		/*
+		 * **[[术语]]** marked不会渲染成黑体
+		 * 所以要在渲染markdown前先把[[]]两边加中文引号
+		 *  在渲染后再去掉中文引号
+		 */
+		let newText = input.replace(/\[\[/g,'“[[');
+		newText = newText.replace(/\]\]/g,']]”');
+		let markdown = marked(newText, { renderer: note_renderer });
+		markdown = markdown.replace(/“\[\[/g,'[[');
+		markdown = markdown.replace(/\]\]”/g,']]');
+		output += term_std_str_to_tran(markdown, channel, editor, lang);
 		output += "</div>";
 
 		let newString = output.replace(/\{\{/g, '<span class="note_shell"><note style="" info="');
@@ -1077,7 +1085,7 @@ function render_one_sent_tran_a(iterator, diff = false) {
 			//修改按钮
 			html += render_icon_button("ic_mode_edit", "sent_tran_edit(this,'update')", gLocal.gui.modify);
 			//删除按钮
-			html += render_icon_button("ic_delete", "sent_pr_del(this)", gLocal.gui.delete);
+			html += render_icon_button("ic_delete", "note_pr_delete(this)", gLocal.gui.delete);
 		} else {
 			//非提交人
 			if (parseInt(iterator.mypower) >= 20) {
@@ -1513,7 +1521,10 @@ function myEndingTooltip(inStr){
 }
 //渲染pr按钮里面的数字
 function render_pr_number(pr_new,pr_all){
- let html = "<span class='icon'>✋</span><span class='num'>" + pr_new + "/" + pr_all + "</span>";
+	let html = "";
+	if(pr_all > 0){
+		html = "<span class='icon'>✋</span><span class='num'>" + pr_new + "/" + pr_all + "</span>";
+	}
  return html;	
 }
 function tran_sent_textarea_event_init() {
@@ -2042,12 +2053,7 @@ function note_pr_update(obj) {
 
 	let id = $(obj).attr("dbid");
 	let channel_type = $(obj).attr("channel_type");
-	let sid = $(obj).attr("sid").split("-");
-	let book = sid[0];
-	let para = sid[1];
-	let begin = sid[2];
-	let end = sid[3];
-	let channel = $(obj).attr("channel");
+
 	let text = $(obj).val();
 	let sent_tran_div = find_sent_tran_div(obj);
 
@@ -2072,7 +2078,45 @@ function note_pr_update(obj) {
 		})
     })
   .then(response => response.json())
-  .then(data => console.log(data));
+  .then(function(data){
+	pr_update_callback(data);
+  });
+
+	if (sent_tran_div) {
+		$(sent_tran_div).addClass("loading");
+	}
+}
+//修改pr句子
+function note_pr_delete(obj) {
+	if(!confirm("要删除此修改建议吗?此操作无法恢复。")){
+		return;
+	}
+
+	let sent_tran_div = find_sent_tran_div(obj);
+	let id = sent_tran_div.attr("dbid");
+
+    fetch('/api/v2/sentpr/'+id,{
+        method: 'DELETE',
+        credentials: 'same-origin',
+        headers: {
+            'Content-Type': 'application/json'
+        },
+        body: JSON.stringify({
+			id: id,
+		})
+    })
+  .then(response => response.json())
+  .then(function(data){
+	console.log("note_pr_delete",data);
+	if(data.ok){
+		let sent_tran_parent = find_sent_tran_div(sent_tran_div.parent());
+		sent_tran_parent.find(".tag_list").first().children(".pr").first().html(render_pr_number(1,data.data));
+
+		sent_tran_div.removeClass("loading");
+		sent_tran_div.parent().html("");
+		ntf_show("删除成功");
+	}
+  });
 
 	if (sent_tran_div) {
 		$(sent_tran_div).addClass("loading");
@@ -2293,6 +2337,61 @@ function pr_create_callback(data) {
 	}
 }
 
+function pr_update_callback(data) {
+	let response;
+	if(typeof data=="string"){
+		try {
+			response = JSON.parse(data);
+		} catch (e) {
+			alert(e.message);
+			console.error('pr_create_callback',data);
+			return;
+		}			
+		
+	}else{
+		response = data;
+	}
+
+	
+	if (!response.ok) {
+		ntf_show("修改建议更新失败");
+		console.log("pr_update_callback", response.message);
+		return;
+	}
+	let result = response.data;
+	{
+		let sid = result.book_id + "-" + result.paragraph + "-" + result.word_start + "-" + result.word_end;
+
+		let sent_tran_div = $(
+			".sent_tran[dbid='" + result.id + "']"
+		);
+		if (sent_tran_div) {
+			sent_tran_div.removeClass("loading");
+			let orgText = "";
+			for (const oneSent of _arrData) {
+				if (
+					oneSent.book == result.book_id &&
+					oneSent.para == result.paragraph &&
+					oneSent.begin == result.word_start &&
+					oneSent.end == result.word_end
+				) {
+					for (const tran of oneSent.translation) {
+						if (tran.channal == result.channel_uid) {
+							orgText = tran.text;
+							break;
+						}
+					}
+					break;
+				}
+			}
+			let tranText = str_diff(orgText, result.content);
+			sent_tran_div.find(".preview").html(tranText);
+		}
+		
+		ntf_show("成功更新修改建议");
+	}
+}
+
 //保存译文句子
 function note_sent_save() {
 	let id = $("#edit_dialog_text").attr("sent_id");
@@ -2549,7 +2648,9 @@ function note_get_pr(channel, id) {
 			if (result.length > 0) {
 				let html = "<div class='compact pr'>";
 				for (const iterator of result) {
+					html += "<div class='pr_shell'>";
 					html += render_one_sent_tran_a(iterator, true);
+					html += "</div>";
 				}
 				html += "</div>";
 				$(".sent_tran[channel='" + channel + "'][sid='" + id + "']")

+ 5 - 0
public/app/usent/function.php

@@ -88,6 +88,11 @@ class SentPr{
             $stmt->execute(array($book,$para,$begin,$end,$channel));
             $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
 			if($result){
+				foreach ($result as $key => $value) {
+					# code...
+					$result[$key]['id'] = sprintf('%d',$result[$key]['id']);
+				}
+				
 				return $result;
 			}
 			else{