PageModel.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class PageModel extends BaseModel {
  9. protected $cat_name_id = array();
  10. //搜索某个项目下的页面
  11. public function search($item_id,$keyword){
  12. $return_pages = array() ;
  13. $item = D("Item")->where("item_id = '%d' and is_del = 0 ",array($item_id))->find();
  14. $pages = $this->where("item_id = '$item_id' and is_del = 0")->order(" s_number asc ")->select();
  15. if (!empty($pages)) {
  16. foreach ($pages as $key => &$value) {
  17. $page_content = $value['page_content'];
  18. if (strpos( strtolower($item['item_name']."-". $value['page_title']." ".$page_content) ,strtolower ($keyword) ) !== false) {
  19. $value['page_content'] = $page_content ;
  20. $return_pages[] = $value;
  21. }
  22. }
  23. }
  24. unset($pages);
  25. return $return_pages;
  26. }
  27. //根据内容更新页面
  28. //其中cat_name参数特别说明下,传递各格式如 '二级目录/三级目录/四级目录'
  29. public function update_by_content($item_id,$page_title,$page_content,$cat_name='',$s_number = 99){
  30. $item_id = intval($item_id);
  31. if (!$item_id) {
  32. return false;
  33. }
  34. if ($this->cat_name_id && isset($this->cat_name_id[$cat_name])) {
  35. $cat_id = $this->cat_name_id[$cat_name] ;
  36. $cat_name = '' ; //如果已经有缓存了则设置为空
  37. }else{
  38. $cat_id = 0 ;
  39. }
  40. $catalog_array = explode('/', $cat_name);
  41. $cat_name = $catalog_array[0] ;
  42. $cat_name_sub = !empty($catalog_array[1])? $catalog_array[1] : '';
  43. $cat_name_sub_sub = !empty($catalog_array[2])? $catalog_array[2] : '';
  44. //如果传送了二级目录
  45. if ($cat_name) {
  46. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  47. //如果不存在则新建
  48. if (!$cat_name_array) {
  49. $add_data = array(
  50. "cat_name" => $cat_name,
  51. "item_id" => $item_id,
  52. "addtime" => time(),
  53. "level" => 2
  54. );
  55. D("Catalog")->add($add_data);
  56. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  57. }
  58. }
  59. //如果传送了三级目录
  60. if ($cat_name_sub) {
  61. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub,$cat_name_array['cat_id']))->find();
  62. //如果不存在则新建
  63. if (!$cat_name_sub_array) {
  64. $add_data = array(
  65. "cat_name" => $cat_name_sub,
  66. "item_id" => $item_id,
  67. "parent_cat_id" => $cat_name_array['cat_id'],
  68. "addtime" => time(),
  69. "level" => 3
  70. );
  71. D("Catalog")->add($add_data);
  72. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub,$cat_name_array['cat_id']))->find();
  73. }
  74. }
  75. //如果传送了四级目录
  76. if ($cat_name_sub_sub) {
  77. $cat_name_sub_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 4 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub_sub,$cat_name_sub_array['cat_id']))->find();
  78. //如果不存在则新建
  79. if (!$cat_name_sub_sub_array) {
  80. $add_data = array(
  81. "cat_name" => $cat_name_sub_sub,
  82. "item_id" => $item_id,
  83. "parent_cat_id" => $cat_name_sub_array['cat_id'],
  84. "addtime" => time(),
  85. "level" => 4
  86. );
  87. D("Catalog")->add($add_data);
  88. $cat_name_sub_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 4 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub_sub,$cat_name_sub_array['cat_id']))->find();
  89. }
  90. }
  91. if ($cat_name_array && $cat_name_array['cat_id'] > 0 ) {
  92. $cat_id = $cat_name_array['cat_id'] ;
  93. }
  94. if ($cat_name_sub_array && $cat_name_sub_array['cat_id'] > 0 ) {
  95. $cat_id = $cat_name_sub_array['cat_id'] ;
  96. }
  97. if ($cat_name_sub_sub_array && $cat_name_sub_sub_array['cat_id'] > 0 ) {
  98. $cat_id = $cat_name_sub_sub_array['cat_id'] ;
  99. }
  100. $this->cat_name_id[$cat_name] = $cat_id ;
  101. if ($page_content) {
  102. $page_array = D("Page")->where(" item_id = '$item_id' and is_del = 0 and cat_id = '$cat_id' and page_title ='%s' ",array($page_title))->find();
  103. //如果不存在则新建
  104. if (!$page_array) {
  105. $add_data = array(
  106. "author_username" => "update_by_content",
  107. "item_id" => $item_id,
  108. "cat_id" => $cat_id,
  109. "page_title" => $page_title,
  110. "page_content" => $page_content,
  111. "s_number" => $s_number,
  112. "addtime" => time(),
  113. );
  114. $page_id = D("Page")->add($add_data);
  115. }else{
  116. $page_id = $page_array['page_id'] ;
  117. $update_data = array(
  118. "author_username" => "update_by_content",
  119. "item_id" => $item_id,
  120. "cat_id" => $cat_id,
  121. "page_title" => $page_title,
  122. "page_content" => $page_content,
  123. "s_number" => $s_number,
  124. );
  125. D("Page")->where(" page_id = '$page_id' ")->save($update_data);
  126. }
  127. }
  128. return $page_id ;
  129. }
  130. //软删除页面
  131. public function softDeletePage($page_id){
  132. $page_id = intval($page_id) ;
  133. //放入回收站
  134. $login_user = session('login_user');
  135. $page = D("Page")->field("item_id,page_title")->where(" page_id = '$page_id' ")->find() ;
  136. D("Recycle")->add(array(
  137. "item_id" =>$page['item_id'],
  138. "page_id" =>$page_id,
  139. "page_title" =>$page['page_title'],
  140. "del_by_uid" =>$login_user['uid'],
  141. "del_by_username" =>$login_user['username'],
  142. "del_time" =>time()
  143. ));
  144. $ret = M("Page")->where(" page_id = '$page_id' ")->save(array("is_del"=>1 ,"addtime"=>time()));
  145. return $ret;
  146. }
  147. //删除页面
  148. public function deletePage($page_id){
  149. $page_id = intval($page_id) ;
  150. $ret = M("Page")->where(" page_id = '$page_id' ")->delete();
  151. return $ret;
  152. }
  153. public function deleteFile($file_id){
  154. $file_id = intval($file_id) ;
  155. return D("Attachment")->deleteFile($file_id) ;
  156. }
  157. //把runapi的格式内容转换为markdown格式。如果不是runapi格式,则会返回false
  158. //参数content为json字符串或者数组
  159. public function runapiToMd($content){
  160. if(!is_array($content) ){
  161. $content_json = htmlspecialchars_decode($content) ;
  162. $content = json_decode($content_json , true) ;
  163. }
  164. if(!$content || !$content['info'] || !$content['info']['url'] ){
  165. return false ;
  166. }
  167. $new_content = "
  168. ##### 简要描述
  169. - ".($content['info']['description'] ? $content['info']['description'] :'无') ."
  170. ##### 请求URL
  171. - `{$content['info']['url']}`
  172. ##### 请求方式
  173. - {$content['info']['method']}
  174. ";
  175. if($content['request']['headers'] && $content['request']['headers'][0] && $content['request']['headers'][0]['name']){
  176. $new_content .= "
  177. ##### Header
  178. |header|必选|类型|说明|
  179. |:----- |:-----|-----|
  180. ";
  181. foreach ($content['request']['headers'] as $key => $value) {
  182. $value['require'] = $value['require'] > 0 ? "是" : "否" ;
  183. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  184. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  185. }
  186. }
  187. $params = $content['request']['params'][$content['request']['params']['mode']];
  188. if ($params && is_array($params) && $params[0] && $params[0]['name']){
  189. $new_content .= "
  190. ##### 请求参数
  191. |参数名|必选|类型|说明|
  192. |:----- |:-----|-----|
  193. ";
  194. foreach ($params as $key => $value) {
  195. $value['require'] = $value['require'] > 0 ? "是" : "否" ;
  196. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  197. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  198. }
  199. }
  200. //如果参数类型为json
  201. if($content['request']['params']['mode'] == 'json' && $params){
  202. $params = $this->_indent_json($params);
  203. $new_content .= "
  204. ##### 请求参数示例
  205. ```
  206. {$params}
  207. ```
  208. ";
  209. }
  210. // json字段说明
  211. $jsonDesc = $content['request']['params']['jsonDesc'] ;
  212. if ($content['request']['params']['mode'] == 'json' && $jsonDesc && $jsonDesc[0] && $jsonDesc[0]['name']){
  213. $new_content .= "
  214. ##### json字段说明
  215. |字段名|必选|类型|说明|
  216. |:----- |:-----|-----|
  217. ";
  218. foreach ($jsonDesc as $key => $value) {
  219. $value['require'] = $value['require'] > 0 ? "是" : "否" ;
  220. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  221. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  222. }
  223. }
  224. //返回示例
  225. if($content['response']['responseExample']){
  226. $responseExample = $this->_indent_json($content['response']['responseExample']);
  227. $responseExample = $responseExample ? $responseExample : $content['response']['responseExample'] ;
  228. $new_content .= "
  229. ##### 返回示例
  230. ```
  231. {$responseExample}
  232. ```
  233. ";
  234. }
  235. //返回示例说明
  236. if($content['response']['responseParamsDesc'] && $content['response']['responseParamsDesc'][0] && $content['response']['responseParamsDesc'][0]['name']){
  237. $new_content .= "
  238. ##### 返回参数说明
  239. |参数名|类型|说明|
  240. |:----- |:-----|-----|
  241. ";
  242. foreach ($content['response']['responseParamsDesc'] as $key => $value) {
  243. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  244. $new_content .= "|{$value['name']}| {$value['type']} | {$value['remark']} | \n";
  245. }
  246. }
  247. $new_content .= "
  248. ##### 备注
  249. {$content['info']['remark']}
  250. ";
  251. return $new_content ;
  252. }
  253. /**
  254. * Indents a flat JSON string to make it more human-readable.
  255. *
  256. * @param string $json The original JSON string to process.
  257. *
  258. * @return string Indented version of the original JSON string.
  259. */
  260. private function _indent_json($json) {
  261. $result = '';
  262. $pos = 0;
  263. $strLen = strlen($json);
  264. $indentStr = ' ';
  265. $newLine = "\n";
  266. $prevChar = '';
  267. $outOfQuotes = true;
  268. for ($i=0; $i<=$strLen; $i++) {
  269. // Grab the next character in the string.
  270. $char = substr($json, $i, 1);
  271. // Are we inside a quoted string?
  272. if ($char == '"' && $prevChar != '\\') {
  273. $outOfQuotes = !$outOfQuotes;
  274. // If this character is the end of an element,
  275. // output a new line and indent the next line.
  276. } else if(($char == '}' || $char == ']') && $outOfQuotes) {
  277. $result .= $newLine;
  278. $pos --;
  279. for ($j=0; $j<$pos; $j++) {
  280. $result .= $indentStr;
  281. }
  282. }
  283. // Add the character to the result string.
  284. $result .= $char;
  285. // If the last character was the beginning of an element,
  286. // output a new line and indent the next line.
  287. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  288. $result .= $newLine;
  289. if ($char == '{' || $char == '[') {
  290. $pos ++;
  291. }
  292. for ($j = 0; $j < $pos; $j++) {
  293. $result .= $indentStr;
  294. }
  295. }
  296. $prevChar = $char;
  297. }
  298. return $result;
  299. }
  300. }