帝国CMS全站alert弹窗改JSON格式输出,不影响原版功能。
1、在要输出json头部代码
define('API_WORK', true);
2.改造printerror和printerror2,增加一个参数$statuscode,不写的状态200
/** * cighsen02 365182575 弹窗改 json接口,还支持 404, 401等 * time: 2022.09.15 */ //错误提示 function printerror($error="",$gotourl="",$ecms=0,$noautourl=0,$novar=0, $statuscode = 200){ global $empire,$editor,$public_r,$ecms_config; if($editor==1){$a="../";} elseif($editor==2){$a="../../";} elseif($editor==3){$a="../../../";} else{$a="";} if($ecms==1||$ecms==9) { $a=ECMS_PATH.'e/data/'; } if(strstr($gotourl,"(")||empty($gotourl)) { if(strstr($gotourl,"(-2")) { $gotourl_js="history.go(-2)"; $gotourl="javascript:history.go(-2)"; } else { $gotourl_js="history.go(-1)"; $gotourl="javascript:history.go(-1)"; } } else {$gotourl_js="self.location.href='$gotourl';";} if(empty($error)) {$error="DbError";} if($ecms==9)//前台弹出对话框 { @include $a.LoadLang("pub/q_message.php"); $error=empty($novar)?$qmessage_r[$error]:$error; //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); echo"<script>alert('".$error."');".$gotourl_js."</script>"; db_close(); $empire=null; exit(); } elseif($ecms==8)//后台弹出对话框 { @include $a.LoadLang("pub/message.php"); $error=empty($novar)?$message_r[$error]:$error; //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); echo"<script>alert('".$error."');".$gotourl_js."</script>"; db_close(); $empire=null; exit(); } elseif($ecms==7)//前台弹出对话框并关闭窗口 { @include $a.LoadLang("pub/q_message.php"); $error=empty($novar)?$qmessage_r[$error]:$error; //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); echo"<script>alert('".$error."');window.close();</script>"; db_close(); $empire=null; exit(); } elseif($ecms==6)//后台弹出对话框并关闭窗口 { @include $a.LoadLang("pub/message.php"); $error=empty($novar)?$message_r[$error]:$error; echo"<script>alert('".$error."');window.close();</script>"; db_close(); $empire=null; exit(); } elseif($ecms==0) { @include $a.LoadLang("pub/message.php"); $error=empty($novar)?$message_r[$error]:$error; //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); @include($a."message.php"); } else { @include $a.LoadLang("pub/q_message.php"); $error=empty($novar)?$qmessage_r[$error]:$error; //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); @include($a."../message/index.php"); } db_close(); $empire=null; exit(); } /** * cighsen02 365182575 弹窗改 json接口,还支持 404, 401等 * time: 2022.09.15 */ //错误提示2:直接文字 function printerror2($error='',$gotourl='',$ecms=0,$noautourl=0, $statuscode = 200){ global $empire,$public_r; if(strstr($gotourl,"(")||empty($gotourl)) { if(strstr($gotourl,"(-2")) { $gotourl_js="history.go(-2)"; $gotourl="javascript:history.go(-2)"; } else { $gotourl_js="history.go(-1)"; $gotourl="javascript:history.go(-1)"; } } else {$gotourl_js="self.location.href='$gotourl';";} if($ecms==9)//弹出对话框 { //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); echo"<script>alert('".$error."');".$gotourl_js."</script>"; } elseif($ecms==7)//弹出对话框并关闭窗口 { //cighsen02 365182575 if(defined('API_WORK') && API_WORK) throw new Exception($error, '1' . $statuscode); echo"<script>alert('".$error."');window.close();</script>"; } else { @include(ECMS_PATH.'e/message/index.php'); } db_close(); exit(); }
3、使用自动加载功能和异常处理,以上3个步骤完成,一起来体验吧