CommonContent.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import { DataModel, transformArrayDataModel, type NewDataModel } from '@imengyu/js-request-transform';
  2. import { AppServerRequestModule } from './RequestModules';
  3. import ApiCofig from '@/common/config/ApiCofig';
  4. import { transformSomeToArray } from './Utils';
  5. import type { QueryParams } from '@imengyu/imengyu-utils';
  6. export class GetColumListParams extends DataModel<GetColumListParams> {
  7. public constructor() {
  8. super(GetColumListParams);
  9. this.setNameMapperCase('Camel', 'Snake');
  10. }
  11. setModelId(val: number) {
  12. this.modelId = val;
  13. return this;
  14. }
  15. setMainBodyColumnId(val: number) {
  16. this.mainBodyColumnId = val;
  17. return this;
  18. }
  19. setFlag(val: 'hot'|'recommend'|'top') {
  20. this.flag = val;
  21. return this;
  22. }
  23. setSize(val: number) {
  24. this.size = val;
  25. return this;
  26. }
  27. modelId?: number;
  28. /**
  29. * 主体栏目id
  30. */
  31. mainBodyColumnId: number = 0;
  32. /**
  33. * 标志:hot=热门,recommend=推荐,top=置顶
  34. */
  35. flag ?: 'hot'|'recommend'|'top';
  36. /**
  37. * 内容数量,默认4
  38. */
  39. size?: number;
  40. }
  41. export class GetContentListParams extends DataModel<GetContentListParams> {
  42. public constructor() {
  43. super(GetContentListParams);
  44. this.setNameMapperCase('Camel', 'Snake');
  45. this._convertTable = {
  46. ids: {
  47. customToServerFn: (val) => (val as number[]).join(','),
  48. customToClientFn: (val) => (val as string).split(',').map((item) => parseInt(item)),
  49. },
  50. }
  51. }
  52. setMainBodyColumnId(val: number|number[]) {
  53. this.mainBodyColumnId = val;
  54. return this;
  55. }
  56. setFlag(val: 'hot'|'recommend'|'top') {
  57. this.flag = val;
  58. return this;
  59. }
  60. setIds(val: number[]) {
  61. this.ids = val;
  62. return this;
  63. }
  64. setType(val: 1|2|3|4) {
  65. this.type = val;
  66. return this;
  67. }
  68. setSize(val: number) {
  69. this.size = val;
  70. return this;
  71. }
  72. setKeywords(val: string) {
  73. this.keywords = val;
  74. return this;
  75. }
  76. setModelId(val: number) {
  77. this.modelId = val;
  78. return this;
  79. }
  80. static TYPE_ARTICLE = 1;
  81. static TYPE_AUDIO = 2;
  82. static TYPE_VIDEO = 3;
  83. static TYPE_IMAGE = 4;
  84. modelId ?: number;
  85. /**
  86. * 主体栏目id
  87. */
  88. mainBodyColumnId: number|number[] = 0;
  89. /**
  90. * 标志:hot=热门,recommend=推荐,top=置顶
  91. */
  92. flag ?: 'hot'|'recommend'|'top';
  93. /**
  94. * 内容id(逗号隔开)如:3 或者 1,2,3
  95. */
  96. ids?: number[];
  97. /**
  98. * 类型:1=文章,2=音频,3=视频,4=相册
  99. */
  100. type?: 1|2|3|4;
  101. /**
  102. * 内容数量,默认4
  103. */
  104. size ?: number;
  105. /**
  106. * 关键字查询
  107. */
  108. keywords?: string;
  109. }
  110. export class GetColumContentList extends DataModel<GetColumContentList> {
  111. constructor() {
  112. super(GetColumContentList, "主体栏目列表");
  113. this.setNameMapperCase('Camel', 'Snake');
  114. this._convertTable = {
  115. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  116. name: { clientSide: 'string', serverSide: 'string', clientSideRequired: true },
  117. content_list: {
  118. clientSide: 'array',
  119. clientSideRequired: true,
  120. clientSideChildDataModel: GetContentListItem,
  121. },
  122. }
  123. }
  124. name = '';
  125. overview = '';
  126. }
  127. export class GetModelColumContentList extends DataModel<GetColumContentList> {
  128. constructor() {
  129. super(GetColumContentList, "模型的主体栏目列表");
  130. this.setNameMapperCase('Camel', 'Snake');
  131. this._convertTable = {
  132. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  133. name: { clientSide: 'string', serverSide: 'string', clientSideRequired: true },
  134. iscontribute: { clientSide: 'boolean' },
  135. }
  136. }
  137. id = 0;
  138. name = '';
  139. modelId = 0;
  140. image = '';
  141. diyname = '';
  142. iscontribute = false;
  143. statusText = '';
  144. }
  145. export class GetContentListItem extends DataModel<GetContentListItem> {
  146. constructor() {
  147. super(GetContentListItem, "内容列表");
  148. this.setNameMapperCase('Camel', 'Snake');
  149. this._convertTable = {
  150. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  151. mainBodyColumnId: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  152. title: { clientSide: 'string', serverSide: 'string', clientSideRequired: true },
  153. isGuest: { clientSide: 'boolean', serverSide: 'number' },
  154. isLogin: { clientSide: 'boolean', serverSide: 'number' },
  155. isComment: { clientSide: 'boolean', serverSide: 'number' },
  156. isLike: { clientSide: 'boolean', serverSide: 'number' },
  157. isCollect: { clientSide: 'boolean', serverSide: 'number' },
  158. latitude: { clientSide: 'number', serverSide: 'number' },
  159. longitude: { clientSide: 'number', serverSide: 'number' },
  160. publishAt: { clientSide: 'date', serverSide: 'string' },
  161. flag: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  162. tags: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  163. keywords: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  164. type: { clientSide: 'number', serverSide: 'number' },
  165. };
  166. this._convertKeyType = (key, direction) => {
  167. if (key.endsWith('Time'))
  168. return {
  169. clientSide: 'date',
  170. serverSide: 'string',
  171. };
  172. return undefined;
  173. };
  174. }
  175. id = 0;
  176. mainBodyColumnId = 0;
  177. latitude = 0;
  178. longitude = 0;
  179. mapX = '';
  180. mapY = '';
  181. from = '';
  182. modelId = 0;
  183. title = '!title';
  184. region = 0;
  185. image = '';
  186. thumbnail = '';
  187. desc = '!desc';
  188. content = '!content';
  189. type = 0;
  190. keywords ?: string[];
  191. flag ?: string[];
  192. tags ?: string[];
  193. views = 0;
  194. comments = 0;
  195. likes = 0;
  196. collects = 0;
  197. dislikes = 0;
  198. district = '';
  199. publishAt = new Date();
  200. }
  201. export class GetContentDetailItem extends DataModel<GetContentDetailItem> {
  202. constructor() {
  203. super(GetContentDetailItem, "内容详情");
  204. this.setNameMapperCase('Camel', 'Snake');
  205. this._beforeSolveServer = (data) => {
  206. if (!data.id && data.content_id)
  207. data.id = Number(data.content_id);
  208. return data;
  209. }
  210. this._convertTable = {
  211. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  212. title: { clientSide: 'string', serverSide: 'string', clientSideRequired: true },
  213. isGuest: { clientSide: 'boolean', serverSide: 'number' },
  214. isLogin: { clientSide: 'boolean', serverSide: 'number' },
  215. isComment: { clientSide: 'boolean', serverSide: 'number' },
  216. isLike: { clientSide: 'boolean', serverSide: 'number' },
  217. isCollect: { clientSide: 'boolean', serverSide: 'number' },
  218. publishAt: { clientSide: 'date', serverSide: 'string' },
  219. flag: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  220. tags: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  221. type: { clientSide: 'number', serverSide: 'number' },
  222. ichSitesList: { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem },
  223. inheritorsList: { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem },
  224. otherLevel: { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem },
  225. }
  226. this._convertKeyType = (key, direction) => {
  227. if (key.endsWith('Time'))
  228. return {
  229. clientSide: 'date',
  230. serverSide: 'string',
  231. };
  232. else if (key.endsWith('List')) {
  233. return [
  234. { clientSide: 'map', serverSide: 'original'},
  235. { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem, serverSide: 'original' },
  236. ]
  237. }
  238. return undefined;
  239. };
  240. this._afterSolveServer = () => {
  241. if (!this.image && this.images && this.images && this.images.length > 0 ) {
  242. this.image = this.images[0]
  243. }
  244. if ((!this.images || this.images.length == 0) && this.image) {
  245. this.images = [ this.image ]
  246. }
  247. if (this.publishVideo) {
  248. this.video = this.publishVideo
  249. }
  250. }
  251. }
  252. id = 0;
  253. from = '';
  254. modelId = 0;
  255. type = 0;
  256. title = '';
  257. region = 0;
  258. image = '';
  259. images = [] as string[];
  260. audio = '';
  261. video = '';
  262. publishVideo?: string;
  263. desc = '';
  264. flag ?: string[];
  265. tags ?: string[];
  266. views = 0;
  267. comments = 0;
  268. likes = 0;
  269. collects = 0;
  270. dislikes = 0;
  271. isLogin = false;
  272. isGuest = false;
  273. isComment = false;
  274. isLike = false;
  275. isCollect = false;
  276. content = '';
  277. publishAt = new Date();
  278. associationMeList = [] as {
  279. id: number,
  280. title: string,
  281. image: string,
  282. thumbnail: string,
  283. }[];
  284. otherLevel : GetContentDetailItem[] = [];
  285. }
  286. export class CategoryListItem extends DataModel<CategoryListItem> {
  287. constructor() {
  288. super(CategoryListItem, "分类列表");
  289. this.setNameMapperCase('Camel', 'Snake');
  290. this._convertTable = {
  291. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  292. pid: { clientSide: 'number', serverSide: 'number' },
  293. haschild: { clientSide: 'boolean', serverSide: 'number' },
  294. }
  295. }
  296. id !: number;
  297. pid !: number;
  298. title = '';
  299. status = 'normal';
  300. weight = 0;
  301. spacer = '';
  302. haschild = false;
  303. children?: CategoryListItem[];
  304. }
  305. export class CommonContentApi extends AppServerRequestModule<DataModel> {
  306. constructor(
  307. mainBodyId = ApiCofig.mainBodyId,
  308. modelId = 0, debugName = 'CommonContent',
  309. mainBodyColumnId?: number|number[]) {
  310. super();
  311. this.modelId = modelId;
  312. this.mainBodyId = mainBodyId;
  313. this.mainBodyColumnId = mainBodyColumnId;
  314. this.debugName = debugName;
  315. }
  316. public mainBodyId: number;
  317. public mainBodyColumnId?: number|number[];
  318. public modelId: number;
  319. protected debugName: string;
  320. private toStringArray(arr: number|number[]|undefined) {
  321. if (typeof arr === 'undefined')
  322. return '';
  323. return typeof arr === 'object' ? arr.join(',') : arr.toString();
  324. }
  325. /**
  326. * 获取分类列表
  327. * @param type 根级类型:1=区域、2=级别、3=文物类型、4=非遗类型、42=事件类型
  328. * @param withself 是否返回包含自己:true=是,false=否 ,默认false
  329. * @returns
  330. */
  331. async getCategoryList(
  332. type?: number,
  333. withself?: boolean,
  334. ) {
  335. return (this.get('/content/category/getCategoryList', '获取分类列表', {
  336. type,
  337. is_tree: false,
  338. withself,
  339. }))
  340. .then(res => transformArrayDataModel<CategoryListItem>(CategoryListItem, res.data2, `获取分类列表`, true))
  341. .catch(e => { throw e });
  342. }
  343. /**
  344. * 用于获取某一个分类需要用的子级
  345. * @param pid 父级
  346. * @returns
  347. */
  348. async getCategoryChildList(pid?: number) {
  349. return (this.get('/content/category/getCategoryOnlyChildList', '获取分类子级列表', {
  350. pid,
  351. }))
  352. .then(res => transformArrayDataModel<CategoryListItem>(
  353. CategoryListItem,
  354. transformSomeToArray(res.data2),
  355. `获取分类列表`,
  356. true
  357. ))
  358. .catch(e => { throw e });
  359. }
  360. /**
  361. * 模型的主体栏目列表
  362. * @param params 参数
  363. * @param querys 额外参数
  364. * @returns
  365. */
  366. getModelColumList<T extends DataModel = GetModelColumContentList>(model_id: number, page: number, pageSize: number = 10,querys?: QueryParams) {
  367. return this.get('/content/main_body_column/getColumnList', `${this.debugName} 模型的主体栏目列表`, {
  368. main_body_id: this.mainBodyId,
  369. model_id: model_id ?? this.modelId,
  370. page,
  371. pageSize,
  372. ...querys
  373. })
  374. .then(res => transformArrayDataModel<T>(GetModelColumContentList, res.data2, `${this.debugName} 模型的主体栏目列表`, true))
  375. .catch(e => { throw e });
  376. }
  377. /**
  378. * 主体栏目列表
  379. * @param params 参数
  380. * @param querys 额外参数
  381. * @returns
  382. */
  383. getColumList<T extends DataModel = GetColumContentList>(params: GetColumListParams, modelClassCreator: NewDataModel = GetColumContentList, querys?: QueryParams) {
  384. return this.get('/content/content/getMainBodyColumnContentList', `${this.debugName} 主体栏目列表`, {
  385. main_body_id: this.mainBodyId,
  386. model_id: this.modelId,
  387. ...params.toServerSide(),
  388. ...querys,
  389. })
  390. .then(res => ({
  391. list: transformArrayDataModel<T>(modelClassCreator, res.data2.list, `${this.debugName} 主体栏目列表`, true),
  392. total: res.data2.total as number,
  393. }))
  394. .catch(e => { throw e });
  395. }
  396. /**
  397. * 模型内容列表
  398. * @param params 参数
  399. * @param page 页码
  400. * @param pageSize 页大小
  401. * @param querys 额外参数
  402. * @returns
  403. */
  404. getContentList<T extends DataModel = GetContentListItem>(params: GetContentListParams, page: number, pageSize: number = 10, modelClassCreator: NewDataModel = GetContentListItem, querys?: QueryParams) {
  405. return this.get('/content/content/getContentList', `${this.debugName} 模型内容列表`, {
  406. ...params.toServerSide(),
  407. model_id: params.modelId || this.modelId,
  408. main_body_id: params.mainBodyId || this.mainBodyId,
  409. main_body_column_id: this.toStringArray(params.mainBodyColumnId || this.mainBodyColumnId),
  410. page,
  411. pageSize,
  412. ...querys,
  413. })
  414. .then(res => {
  415. let resList : any = null;
  416. let resTotal : any = null;
  417. if (res.data2?.list && Array.isArray(res.data2.list)) {
  418. resList = res.data2.list;
  419. resTotal = res.data2.total ?? resList.length;
  420. }
  421. else if (res.data2 && Array.isArray(res.data2)) {
  422. resList = res.data2;
  423. resTotal = resList.length;
  424. } else
  425. resList = res.data;
  426. if (resList === null)
  427. return { list: [], total: 0 };
  428. return {
  429. list: transformArrayDataModel<T>(modelClassCreator, resList, `${this.debugName} 模型内容列表`, true),
  430. total: resTotal as number,
  431. }
  432. })
  433. .catch(e => { throw e });
  434. }
  435. /**
  436. * 内容详情
  437. * @param id id
  438. * @param querys 额外参数
  439. * @returns
  440. */
  441. getContentDetail<T extends DataModel = GetContentDetailItem>(id: number, modelClassCreator: NewDataModel = GetContentDetailItem, modelId?: number, querys?: QueryParams) {
  442. return this.get('/content/content/getContentDetail', `${this.debugName} (${id}) 内容详情`, {
  443. main_body_id: this.mainBodyId,
  444. model_id: modelId ?? this.modelId,
  445. id,
  446. ...querys,
  447. }, modelClassCreator)
  448. .then(res => res.data as T)
  449. .catch(e => { throw e });
  450. }
  451. }
  452. export default new CommonContentApi(undefined, 0, '默认通用内容');