CommonContent.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import { DataModel, transformArrayDataModel, type NewDataModel } from '@imengyu/js-request-transform';
  2. import { AppServerRequestModule } from './RequestModules';
  3. import type { QueryParams } from "@imengyu/imengyu-utils";
  4. import { transformSomeToArray } from './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 = 4;
  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 = 4;
  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 GetContentListItem extends DataModel<GetContentListItem> {
  127. constructor() {
  128. super(GetContentListItem, "内容列表");
  129. this.setNameMapperCase('Camel', 'Snake');
  130. this._convertTable = {
  131. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  132. mainBodyColumnId: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  133. title: { clientSide: 'string', serverSide: 'string', clientSideRequired: true },
  134. isGuest: { clientSide: 'boolean', serverSide: 'number' },
  135. isLogin: { clientSide: 'boolean', serverSide: 'number' },
  136. isComment: { clientSide: 'boolean', serverSide: 'number' },
  137. isLike: { clientSide: 'boolean', serverSide: 'number' },
  138. isCollect: { clientSide: 'boolean', serverSide: 'number' },
  139. latitude: { clientSide: 'number', serverSide: 'number' },
  140. longitude: { clientSide: 'number', serverSide: 'number' },
  141. publishAt: { clientSide: 'date', serverSide: 'string' },
  142. flag: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  143. tags: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  144. keywords: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  145. type: { clientSide: 'number', serverSide: 'number' },
  146. };
  147. this._convertKeyType = (key, direction) => {
  148. if (key.endsWith('Time') || key.endsWith('At'))
  149. return {
  150. clientSide: 'date',
  151. serverSide: 'string',
  152. };
  153. return undefined;
  154. };
  155. }
  156. id = 0;
  157. mainBodyColumnId = 0;
  158. latitude = 0;
  159. longitude = 0;
  160. mapX = '';
  161. mapY = '';
  162. from = '';
  163. modelId = 0;
  164. title = '!title';
  165. typeText = '';
  166. region = 0;
  167. image = '';
  168. thumbnail = '';
  169. desc = '!desc';
  170. content = '!content';
  171. type = 0;
  172. keywords ?: string[];
  173. images ?: string[];
  174. flag ?: string[];
  175. tags ?: string[];
  176. views = 0;
  177. comments = 0;
  178. likes = 0;
  179. collects = 0;
  180. dislikes = 0;
  181. district = '';
  182. publishAt = new Date();
  183. }
  184. export class GetContentDetailItem extends DataModel<GetContentDetailItem> {
  185. constructor() {
  186. super(GetContentDetailItem, "内容详情");
  187. this.setNameMapperCase('Camel', 'Snake');
  188. this._convertTable = {
  189. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  190. title: { clientSide: 'string', serverSide: 'string' },
  191. content: { clientSide: 'string', serverSide: 'string' },
  192. isGuest: { clientSide: 'boolean', serverSide: 'number' },
  193. isLogin: { clientSide: 'boolean', serverSide: 'number' },
  194. isComment: { clientSide: 'boolean', serverSide: 'number' },
  195. isLike: { clientSide: 'boolean', serverSide: 'number' },
  196. isCollect: { clientSide: 'boolean', serverSide: 'number' },
  197. publishAt: { clientSide: 'date', serverSide: 'string' },
  198. flag: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  199. tags: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  200. type: { clientSide: 'number', serverSide: 'number' },
  201. ichSitesList: { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem },
  202. inheritorsList: { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem },
  203. otherLevel: { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem },
  204. }
  205. this._convertKeyType = (key, direction) => {
  206. if (key.endsWith('Time') || key.endsWith('At'))
  207. return {
  208. clientSide: 'date',
  209. serverSide: 'string',
  210. };
  211. else if (key.endsWith('List')) {
  212. return [
  213. { clientSide: 'map', serverSide: 'original'},
  214. { clientSide: 'array', clientSideChildDataModel: GetContentDetailItem, serverSide: 'original' },
  215. ]
  216. }
  217. return undefined;
  218. };
  219. this._afterSolveServer = () => {
  220. if (this.image === 'https://mncdn.wenlvti.net')
  221. this.image = '';
  222. if (!this.image && this.images && this.images && this.images.length > 0 ) {
  223. this.image = this.images[0]
  224. }
  225. if ((!this.images || this.images.length == 0) && this.image && this.image != '') {
  226. this.images = [ this.image ]
  227. }
  228. if (!this.images)
  229. this.images = []
  230. if (this.publishVideo)
  231. this.video = this.publishVideo;
  232. }
  233. }
  234. id = 0;
  235. from = '';
  236. modelId = 0;
  237. type = 0;
  238. title = '';
  239. region = 0;
  240. image = '';
  241. images = [] as string[];
  242. audio = '';
  243. video = '';
  244. desc = '';
  245. flag ?: string[];
  246. tags ?: string[];
  247. publishVideo?: string;
  248. views = 0;
  249. comments = 0;
  250. likes = 0;
  251. collects = 0;
  252. dislikes = 0;
  253. isLogin = false;
  254. isGuest = false;
  255. isComment = false;
  256. isLike = false;
  257. isCollect = false;
  258. content = '';
  259. inheritor = '';
  260. value = '';
  261. intro = '';
  262. publishAt = new Date();
  263. inheritorsList = [] as GetContentDetailItem[];
  264. ichSitesList = [] as GetContentDetailItem[];
  265. associationMeList = [] as {
  266. id: number,
  267. title: string,
  268. image: string,
  269. thumbnail: string,
  270. }[];
  271. otherLevel : GetContentDetailItem[] = [];
  272. }
  273. export class CategoryListItem extends DataModel<CategoryListItem> {
  274. constructor() {
  275. super(CategoryListItem, "分类列表");
  276. this.setNameMapperCase('Camel', 'Snake');
  277. this._convertTable = {
  278. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  279. pid: { clientSide: 'number', serverSide: 'number' },
  280. haschild: { clientSide: 'boolean', serverSide: 'number' },
  281. }
  282. }
  283. id !: number;
  284. pid !: number;
  285. title = '';
  286. status = 'normal';
  287. weight = 0;
  288. spacer = '';
  289. haschild = false;
  290. children?: CategoryListItem[];
  291. }
  292. export class CommonContentApi extends AppServerRequestModule<DataModel> {
  293. constructor(modelId: number, debugName: string, mainBodyColumnId?: number|number[]) {
  294. super();
  295. this.modelId = modelId;
  296. this.mainBodyColumnId = mainBodyColumnId;
  297. this.debugName = debugName;
  298. }
  299. public mainBodyColumnId?: number|number[];
  300. public modelId: number;
  301. protected debugName: string;
  302. /**
  303. * 获取分类列表
  304. * @param type 根级类型:1=区域、2=级别、3=文物类型、4=非遗类型、42=事件类型
  305. * @param withself 是否返回包含自己:true=是,false=否 ,默认false
  306. * @returns
  307. */
  308. async getCategoryList(
  309. type?: number,
  310. withself?: boolean,
  311. ) {
  312. return (this.get('/content/category/getCategoryList', '获取分类列表', {
  313. type,
  314. is_tree: false,
  315. withself,
  316. }))
  317. .then(res => transformArrayDataModel<CategoryListItem>(CategoryListItem, res.data2, `获取分类列表`, true))
  318. .catch(e => { throw e });
  319. }
  320. /**
  321. * 用于获取某一个分类需要用的子级
  322. * @param pid 父级
  323. * @returns
  324. */
  325. async getCategoryChildList(pid?: number) {
  326. return (this.get('/content/category/getCategoryOnlyChildList', '获取分类子级列表', {
  327. pid,
  328. }))
  329. .then(res => transformArrayDataModel<CategoryListItem>(
  330. CategoryListItem,
  331. transformSomeToArray(res.data2),
  332. `获取分类列表`,
  333. true
  334. ))
  335. .catch(e => { throw e });
  336. }
  337. private toStringArray(arr: number|number[]|undefined) {
  338. if (typeof arr === 'undefined')
  339. return '';
  340. return typeof arr === 'object' ? arr.join(',') : arr.toString();
  341. }
  342. /**
  343. * 主体栏目列表
  344. * @param params 参数
  345. * @param querys 额外参数
  346. * @returns
  347. */
  348. getColumList<T extends DataModel = GetColumContentList>(params: GetColumListParams, modelClassCreator: NewDataModel = GetColumContentList, querys?: QueryParams) {
  349. return this.get('/content/content/getMainBodyColumnContentList', `${this.debugName} 主体栏目列表`, {
  350. model_id: this.modelId,
  351. ...params.toServerSide(),
  352. ...querys,
  353. })
  354. .then(res => ({
  355. list: transformArrayDataModel<T>(modelClassCreator, res.data2.list, `${this.debugName} 主体栏目列表`, true),
  356. total: res.data2.total as number,
  357. }))
  358. .catch(e => { throw e });
  359. }
  360. /**
  361. * 模型内容列表
  362. * @param params 参数
  363. * @param page 页码
  364. * @param pageSize 页大小
  365. * @param querys 额外参数
  366. * @returns
  367. */
  368. getContentList<T extends DataModel = GetContentListItem>(params: GetContentListParams, page: number, pageSize: number = 10, modelClassCreator: NewDataModel = GetContentListItem, querys?: QueryParams) {
  369. return this.get('/content/content/getContentList', `${this.debugName} 模型内容列表`, {
  370. ...params.toServerSide(),
  371. ...querys,
  372. model_id: params.modelId || this.modelId,
  373. main_body_column_id: this.toStringArray(params.mainBodyColumnId || this.mainBodyColumnId),
  374. page,
  375. pageSize,
  376. })
  377. .then(res => ({
  378. list: transformArrayDataModel<T>(modelClassCreator, res.data2.list, `${this.debugName} 模型内容列表`, true),
  379. total: res.data2.total as number,
  380. }))
  381. .catch(e => { throw e });
  382. }
  383. /**
  384. * 内容详情
  385. * @param id id
  386. * @param querys 额外参数
  387. * @returns
  388. */
  389. getContentDetail<T extends DataModel = GetContentDetailItem>(id: number, modelId?: number, modelClassCreator: NewDataModel = GetContentDetailItem, querys?: QueryParams) {
  390. return this.get('/content/content/getContentDetail', `${this.debugName} (${id}) 内容详情`, {
  391. model_id: modelId ?? this.modelId,
  392. id,
  393. ...querys,
  394. }, modelClassCreator)
  395. .then(res => res.data as T)
  396. .catch(e => { throw e });
  397. }
  398. }
  399. export default new CommonContentApi(0, '默认通用内容');