PageModel.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. //放入回收站
  133. $login_user = session('login_user');
  134. $page = D("Page")->field("item_id,page_title")->where(" page_id = '$page_id' ")->find() ;
  135. D("Recycle")->add(array(
  136. "item_id" =>$page['item_id'],
  137. "page_id" =>$page_id,
  138. "page_title" =>$page['page_title'],
  139. "del_by_uid" =>$login_user['uid'],
  140. "del_by_username" =>$login_user['username'],
  141. "del_time" =>time()
  142. ));
  143. $ret = M("Page")->where(" page_id = '$page_id' ")->save(array("is_del"=>1 ,"addtime"=>time()));
  144. return $ret;
  145. }
  146. //删除页面
  147. public function deletePage($page_id){
  148. $ret = M("Page")->where(" page_id = '$page_id' ")->delete();
  149. return $ret;
  150. }
  151. public function deleteFile($file_id){
  152. return D("Attachment")->deleteFile($file_id) ;
  153. }
  154. //把runapi的格式内容转换为markdown格式。如果不是runapi格式,则会返回false
  155. //参数content为json字符串或者数组
  156. public function runapiToMd($content){
  157. if(!is_array($content) ){
  158. $content_json = htmlspecialchars_decode($content) ;
  159. $content = json_decode($content_json , true) ;
  160. }
  161. if(!$content || !$content['info'] || !$content['info']['url'] ){
  162. return false ;
  163. }
  164. $new_content = "
  165. ##### 简要描述
  166. - ".($content['info']['description'] ? $content['info']['description'] :'无') ."
  167. ##### 请求URL
  168. - `{$content['info']['url']}`
  169. ##### 请求方式
  170. - {$content['info']['method']}
  171. ";
  172. if($content['request']['headers'] && $content['request']['headers'][0] && $content['request']['headers'][0]['name']){
  173. $new_content .= "
  174. ##### Header
  175. |header|必选|类型|说明|
  176. |:----- |:-----|-----|
  177. ";
  178. foreach ($content['request']['headers'] as $key => $value) {
  179. $value['require'] = $value['require'] > 0 ? "是" : "否" ;
  180. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  181. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  182. }
  183. }
  184. $params = $content['request']['params'][$content['request']['params']['mode']];
  185. if ($params && is_array($params) && $params[0] && $params[0]['name']){
  186. $new_content .= "
  187. ##### 请求参数
  188. |参数名|必选|类型|说明|
  189. |:----- |:-----|-----|
  190. ";
  191. foreach ($params as $key => $value) {
  192. $value['require'] = $value['require'] > 0 ? "是" : "否" ;
  193. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  194. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  195. }
  196. }
  197. //如果参数类型为json
  198. if($content['request']['params']['mode'] == 'json' && $params){
  199. $params = $this->_indent_json($params);
  200. $new_content .= "
  201. ##### 请求参数示例
  202. ```
  203. {$params}
  204. ```
  205. ";
  206. }
  207. // json字段说明
  208. $jsonDesc = $content['request']['params']['jsonDesc'] ;
  209. if ($content['request']['params']['mode'] == 'json' && $jsonDesc && $jsonDesc[0] && $jsonDesc[0]['name']){
  210. $new_content .= "
  211. ##### json字段说明
  212. |字段名|必选|类型|说明|
  213. |:----- |:-----|-----|
  214. ";
  215. foreach ($jsonDesc as $key => $value) {
  216. $value['require'] = $value['require'] > 0 ? "是" : "否" ;
  217. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  218. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  219. }
  220. }
  221. //返回示例
  222. if($content['response']['responseExample']){
  223. $responseExample = $this->_indent_json($content['response']['responseExample']);
  224. $responseExample = $responseExample ? $responseExample : $content['response']['responseExample'] ;
  225. $new_content .= "
  226. ##### 返回示例
  227. ```
  228. {$responseExample}
  229. ```
  230. ";
  231. }
  232. //返回示例说明
  233. if($content['response']['responseParamsDesc'] && $content['response']['responseParamsDesc'][0] && $content['response']['responseParamsDesc'][0]['name']){
  234. $new_content .= "
  235. ##### 返回参数说明
  236. |参数名|类型|说明|
  237. |:----- |:-----|-----|
  238. ";
  239. foreach ($content['response']['responseParamsDesc'] as $key => $value) {
  240. $value['remark'] = $value['remark'] ? $value['remark'] : '无' ;
  241. $new_content .= "|{$value['name']}| {$value['type']} | {$value['remark']} | \n";
  242. }
  243. }
  244. $new_content .= "
  245. ##### 备注
  246. {$content['info']['remark']}
  247. ";
  248. return $new_content ;
  249. }
  250. /**
  251. * Indents a flat JSON string to make it more human-readable.
  252. *
  253. * @param string $json The original JSON string to process.
  254. *
  255. * @return string Indented version of the original JSON string.
  256. */
  257. private function _indent_json($json) {
  258. $result = '';
  259. $pos = 0;
  260. $strLen = strlen($json);
  261. $indentStr = ' ';
  262. $newLine = "\n";
  263. $prevChar = '';
  264. $outOfQuotes = true;
  265. for ($i=0; $i<=$strLen; $i++) {
  266. // Grab the next character in the string.
  267. $char = substr($json, $i, 1);
  268. // Are we inside a quoted string?
  269. if ($char == '"' && $prevChar != '\\') {
  270. $outOfQuotes = !$outOfQuotes;
  271. // If this character is the end of an element,
  272. // output a new line and indent the next line.
  273. } else if(($char == '}' || $char == ']') && $outOfQuotes) {
  274. $result .= $newLine;
  275. $pos --;
  276. for ($j=0; $j<$pos; $j++) {
  277. $result .= $indentStr;
  278. }
  279. }
  280. // Add the character to the result string.
  281. $result .= $char;
  282. // If the last character was the beginning of an element,
  283. // output a new line and indent the next line.
  284. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  285. $result .= $newLine;
  286. if ($char == '{' || $char == '[') {
  287. $pos ++;
  288. }
  289. for ($j = 0; $j < $pos; $j++) {
  290. $result .= $indentStr;
  291. }
  292. }
  293. $prevChar = $char;
  294. }
  295. return $result;
  296. }
  297. }