CommonContent.ts 12 KB

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