mock平台

interface.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. const yapi = require('../yapi.js');
  2. const baseModel = require('./base.js');
  3. class interfaceModel extends baseModel {
  4. getName() {
  5. return 'interface';
  6. }
  7. getSchema() {
  8. return {
  9. title: { type: String, required: true },
  10. uid: { type: Number, required: true },
  11. path: { type: String, required: true },
  12. method: { type: String, required: true },
  13. project_id: { type: Number, required: true },
  14. catid: { type: Number, required: true },
  15. edit_uid: { type: Number, default: 0 },
  16. status: { type: String, enum: ['undone', 'done'], default: 'undone' },
  17. desc: String,
  18. markdown: String,
  19. add_time: Number,
  20. up_time: Number,
  21. type: { type: String, enum: ['static', 'var'], default: 'static' },
  22. query_path: {
  23. path: String,
  24. params: [
  25. {
  26. name: String,
  27. value: String
  28. }
  29. ]
  30. },
  31. req_query: [
  32. {
  33. name: String,
  34. value: String,
  35. example: String,
  36. desc: String,
  37. required: {
  38. type: String,
  39. enum: ['1', '0'],
  40. default: '1'
  41. }
  42. }
  43. ],
  44. req_headers: [
  45. {
  46. name: String,
  47. value: String,
  48. example: String,
  49. desc: String,
  50. required: {
  51. type: String,
  52. enum: ['1', '0'],
  53. default: '1'
  54. }
  55. }
  56. ],
  57. req_params: [
  58. {
  59. name: String,
  60. desc: String,
  61. example: String
  62. }
  63. ],
  64. req_body_type: {
  65. type: String,
  66. enum: ['form', 'json', 'text', 'file', 'raw']
  67. },
  68. req_body_is_json_schema: { type: Boolean, default: false },
  69. req_body_form: [
  70. {
  71. name: String,
  72. type: { type: String, enum: ['text', 'file'] },
  73. example: String,
  74. value: String,
  75. desc: String,
  76. required: {
  77. type: String,
  78. enum: ['1', '0'],
  79. default: '1'
  80. }
  81. }
  82. ],
  83. req_body_other: String,
  84. res_body_type: {
  85. type: String,
  86. enum: ['json', 'text', 'xml', 'raw', 'json-schema']
  87. },
  88. res_body: String,
  89. res_body_is_json_schema: { type: Boolean, default: false },
  90. custom_field_value: String,
  91. field2: String,
  92. field3: String,
  93. api_opened: { type: Boolean, default: false },
  94. index: { type: Number, default: 0 },
  95. tag: Array
  96. };
  97. }
  98. save(data) {
  99. let m = new this.model(data);
  100. return m.save();
  101. }
  102. get(id) {
  103. return this.model
  104. .findOne({
  105. _id: id
  106. })
  107. .exec();
  108. }
  109. getBaseinfo(id) {
  110. return this.model
  111. .findOne({
  112. _id: id
  113. })
  114. .select('path method uid title project_id cat_id status ')
  115. .exec();
  116. }
  117. getVar(project_id, method) {
  118. return this.model
  119. .find({
  120. project_id: project_id,
  121. type: 'var',
  122. method: method
  123. })
  124. .select('_id path')
  125. .exec();
  126. }
  127. getByQueryPath(project_id, path, method) {
  128. return this.model
  129. .find({
  130. project_id: project_id,
  131. 'query_path.path': path,
  132. method: method
  133. })
  134. .exec();
  135. }
  136. getByPath(project_id, path, method, select) {
  137. select =
  138. select ||
  139. '_id title uid path method project_id catid edit_uid status add_time up_time type query_path req_query req_headers req_params req_body_type req_body_form req_body_other res_body_type custom_field_value res_body res_body_is_json_schema req_body_is_json_schema';
  140. return this.model
  141. .find({
  142. project_id: project_id,
  143. path: path,
  144. method: method
  145. })
  146. .select(select)
  147. .exec();
  148. }
  149. checkRepeat(id, path, method) {
  150. return this.model.countDocuments({
  151. project_id: id,
  152. path: path,
  153. method: method
  154. });
  155. }
  156. countByProjectId(id) {
  157. return this.model.countDocuments({
  158. project_id: id
  159. });
  160. }
  161. list(project_id, select) {
  162. select =
  163. select || '_id title uid path method project_id catid edit_uid status add_time up_time';
  164. return this.model
  165. .find({
  166. project_id: project_id
  167. })
  168. .select(select)
  169. .sort({ title: 1 })
  170. .exec();
  171. }
  172. listWithPage(project_id, page, limit) {
  173. page = parseInt(page);
  174. limit = parseInt(limit);
  175. return this.model
  176. .find({
  177. project_id: project_id
  178. })
  179. .sort({ title: 1 })
  180. .skip((page - 1) * limit)
  181. .limit(limit)
  182. .select(
  183. '_id title uid path method project_id catid api_opened edit_uid status add_time up_time tag'
  184. )
  185. .exec();
  186. }
  187. listByPid(project_id) {
  188. return this.model
  189. .find({
  190. project_id: project_id
  191. })
  192. .sort({ title: 1 })
  193. .exec();
  194. }
  195. //获取全部接口信息
  196. getInterfaceListCount() {
  197. return this.model.countDocuments({});
  198. }
  199. listByCatid(catid, select) {
  200. select =
  201. select || '_id title uid path method project_id catid edit_uid status add_time up_time index tag';
  202. return this.model
  203. .find({
  204. catid: catid
  205. })
  206. .select(select)
  207. .sort({ index: 1 })
  208. .exec();
  209. }
  210. listByCatidWithPage(catid, page, limit) {
  211. page = parseInt(page);
  212. limit = parseInt(limit);
  213. return this.model
  214. .find({
  215. catid: catid
  216. })
  217. .sort({ index: 1 })
  218. .skip((page - 1) * limit)
  219. .limit(limit)
  220. .select(
  221. '_id title uid path method project_id catid edit_uid api_opened status add_time up_time, index, tag'
  222. )
  223. .exec();
  224. }
  225. listByOptionWithPage(option, page, limit) {
  226. page = parseInt(page);
  227. limit = parseInt(limit);
  228. return this.model
  229. .find(option)
  230. .sort({index: 1})
  231. .skip((page - 1) * limit)
  232. .limit(limit)
  233. .select(
  234. '_id title uid path method project_id catid edit_uid api_opened status add_time up_time, index, tag'
  235. )
  236. .exec();
  237. }
  238. listByInterStatus(catid, status) {
  239. let option = {};
  240. if (status === 'open') {
  241. option = {
  242. catid: catid,
  243. api_opened: true
  244. };
  245. } else {
  246. option = {
  247. catid: catid
  248. };
  249. }
  250. return this.model
  251. .find(option)
  252. .select()
  253. .sort({ title: 1 })
  254. .exec();
  255. }
  256. del(id) {
  257. return this.model.remove({
  258. _id: id
  259. });
  260. }
  261. delByCatid(id) {
  262. return this.model.remove({
  263. catid: id
  264. });
  265. }
  266. delByProjectId(id) {
  267. return this.model.remove({
  268. project_id: id
  269. });
  270. }
  271. up(id, data) {
  272. data.up_time = yapi.commons.time();
  273. return this.model.update(
  274. {
  275. _id: id
  276. },
  277. data,
  278. { runValidators: true }
  279. );
  280. }
  281. upEditUid(id, uid) {
  282. return this.model.update(
  283. {
  284. _id: id
  285. },
  286. { edit_uid: uid },
  287. { runValidators: true }
  288. );
  289. }
  290. getcustomFieldValue(id, value) {
  291. return this.model
  292. .find({
  293. project_id: id,
  294. custom_field_value: value
  295. })
  296. .select(
  297. 'title uid path method edit_uid status desc add_time up_time type query_path req_query req_headers req_params req_body_type req_body_form req_body_other res_body_type custom_field_value'
  298. )
  299. .exec();
  300. }
  301. listCount(option) {
  302. return this.model.countDocuments(option);
  303. }
  304. upIndex(id, index) {
  305. return this.model.update(
  306. {
  307. _id: id
  308. },
  309. {
  310. index: index
  311. }
  312. );
  313. }
  314. search(keyword) {
  315. return this.model
  316. .find({
  317. $or: [
  318. { 'title': new RegExp(keyword, 'ig') },
  319. { 'path': new RegExp(keyword, 'ig') }
  320. ]
  321. })
  322. .limit(10);
  323. }
  324. }
  325. module.exports = interfaceModel;