CommonContent.ts 15 KB

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