VillageInfoApi.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
  2. import { AppServerRequestModule } from '../RequestModules';
  3. import CommonContent from '../CommonContent';
  4. export class CategoryListItem extends DataModel<CategoryListItem> {
  5. constructor() {
  6. super(CategoryListItem, "分类列表");
  7. this.setNameMapperCase('Camel', 'Snake');
  8. this._convertTable = {
  9. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  10. pid: { clientSide: 'number', serverSide: 'number' },
  11. haschild: { clientSide: 'boolean', serverSide: 'number' },
  12. }
  13. }
  14. id !: number;
  15. pid !: number;
  16. title = '';
  17. status = 'normal';
  18. weight = 0;
  19. spacer = '';
  20. haschild = false;
  21. children?: CategoryListItem[];
  22. }
  23. export class CommonInfoModel extends DataModel<CommonInfoModel> {
  24. constructor() {
  25. super(CommonInfoModel, "信息详情");
  26. this.setNameMapperCase('Camel', 'Snake');
  27. this._convertTable = {
  28. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  29. keywords: { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  30. culturalType: { clientSide: 'number', serverSide: 'number' },
  31. productType: { clientSide: 'number', serverSide: 'number' },
  32. type: { clientSide: 'number', serverSide: 'number' },
  33. nature: { clientSide: 'number', serverSide: 'number' },
  34. folkCultureType: { clientSide: 'number', serverSide: 'number' },
  35. landforms: [
  36. { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  37. { clientSide: 'arrayInt', serverSide: 'original' },
  38. ],
  39. villageType: { clientSide: 'number', serverSide: 'number' },
  40. },
  41. this._blackList.toServer.push(
  42. 'updatedAt', 'createdAt', 'deletedAt',
  43. );
  44. this._convertKeyType = (key, direction) => {
  45. if (key.endsWith('At'))
  46. return {
  47. clientSide: 'date',
  48. serverSide: 'string',
  49. };
  50. return undefined;
  51. };
  52. this._afterSolveServer = () => {
  53. if (this.province && this.city && this.district) {
  54. this.cityAddress = [this.province as string, this.city as string, this.district as string];
  55. }
  56. if (!this.title && this.name)
  57. this.title = this.name;
  58. if (this.longitude && this.latitude) {
  59. this.lonlat = [this.longitude as number, this.latitude as number];
  60. }
  61. };
  62. this._afterSolveClient = (data) => {
  63. if (this.cityAddress) {
  64. data.province = this.cityAddress[0];
  65. data.city = this.cityAddress[1];
  66. data.district = this.cityAddress[2];
  67. }
  68. if (this.lonlat) {
  69. data.longitude = this.lonlat[0];
  70. data.latitude = this.lonlat[1];
  71. }
  72. };
  73. }
  74. id !: number;
  75. cityAddress?: string[];
  76. title = '';
  77. desc = '';
  78. image = '';
  79. images = [] as string[];
  80. content = '';
  81. villageId = 0;
  82. villageVolunteerId = 0;
  83. villageName = '';
  84. villageVolunteerName = '';
  85. contentId = 0;
  86. name = '';
  87. type = 1;
  88. audio = '';
  89. video = '';
  90. archives = '';
  91. annex = [] as string[];
  92. keywords = [] as string[];
  93. createdAt = new Date();
  94. updatedAt = new Date();
  95. publishAt = new Date();
  96. lonlat?: number[];
  97. landforms = [] as string[];
  98. }
  99. export class VillageListItem extends DataModel<VillageListItem> {
  100. constructor() {
  101. super(VillageListItem, "村社信息");
  102. this.setNameMapperCase('Camel', 'Snake');
  103. this._convertTable = {
  104. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  105. },
  106. this._blackList.toServer.push(
  107. 'updatedAt', 'createdAt', 'deletedAt',
  108. );
  109. this._convertKeyType = (key, direction) => {
  110. if (key.endsWith('At'))
  111. return {
  112. clientSide: 'date',
  113. serverSide: 'string',
  114. };
  115. return undefined;
  116. };
  117. this._afterSolveServer = () => {
  118. if (!this.title) {
  119. if (this.name) this.title = this.name as string;
  120. if (typeof this.content === 'object' && (this.content as any)?.title) this.title = (this.content as any).title as string;
  121. if (this.content) this.title = this.content as string;
  122. if (this.structure) this.title = this.structure as string;
  123. if (this.wisdom) this.title = this.wisdom as string;
  124. }
  125. if (!this.image) {
  126. if (this.distribution) this.image = this.distribution as string;
  127. }
  128. };
  129. }
  130. id !: number;
  131. createdAt = new Date();
  132. updatedAt = new Date();
  133. title = '';
  134. desc = '';
  135. image = '';
  136. }
  137. export class VillageBulidingInfo extends DataModel<VillageBulidingInfo> {
  138. constructor() {
  139. super(VillageBulidingInfo, "历史建筑信息");
  140. this.setNameMapperCase('Camel', 'Snake');
  141. this._convertTable = {
  142. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  143. }
  144. this._blackList.toServer.push(
  145. 'updatedAt', 'createdAt', 'deletedAt',
  146. );
  147. const commaArrayKeys = [
  148. 'purpose','floorType','wallType','roofForm','bearingType',
  149. ]
  150. this._convertKeyType = (key, direction) => {
  151. if (commaArrayKeys.includes(key))
  152. return [
  153. { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  154. { clientSide: 'arrayInt', serverSide: 'original' },
  155. ];
  156. return undefined;
  157. };
  158. }
  159. id !: number;
  160. }
  161. export class VillageInfoApi extends AppServerRequestModule<DataModel> {
  162. constructor() {
  163. super();
  164. }
  165. /**
  166. * 获取分类列表
  167. * @param type 根级类型:1=区域、2=级别、3=文物类型、4=非遗类型、42=事件类型
  168. * @param withself 是否返回包含自己:true=是,false=否 ,默认false
  169. * @returns
  170. */
  171. getCategoryList(
  172. type?: number,
  173. withself?: boolean,
  174. ) {
  175. return CommonContent.getCategoryList(type, withself);
  176. }
  177. /**
  178. * 用于获取某一个分类需要用的子级
  179. * @param pid 父级
  180. * @returns
  181. */
  182. getCategoryChildList(pid?: number) {
  183. return CommonContent.getCategoryChildList(pid);
  184. }
  185. async getInfo<T extends DataModel>(
  186. collectModuleId: number|undefined,
  187. subType: string,
  188. subId: number|undefined,
  189. subKey: string|undefined,
  190. villageId: number,
  191. villageVolunteerId: number,
  192. catalogId?: number|undefined,
  193. id?: number,
  194. modelClassCreator: (new () => T) = CommonInfoModel as any
  195. ) {
  196. return (await this.post(`/village/collect/info`, {
  197. collect_module_id: collectModuleId,
  198. id,
  199. }, '通用获取信息详情', undefined, modelClassCreator)).data as T
  200. }
  201. async getList<T extends DataModel = VillageListItem>(
  202. collectModuleId: number|undefined,
  203. subType: string,
  204. subId: number|undefined,
  205. subKey: string|undefined,
  206. villageId: number,
  207. villageVolunteerId: number,
  208. catalogId?: number|undefined,
  209. modelClassCreator: (new () => T) = VillageListItem as any
  210. ) {
  211. return (this.post(`/village/collect/list`, {
  212. collect_module_id: collectModuleId,
  213. [subKey ? subKey : 'type']: subId,
  214. village_id: villageId,
  215. village_volunteer_id: villageVolunteerId,
  216. }, '获取信息详情'))
  217. .then(res => transformArrayDataModel<T>(modelClassCreator, (res.data2.data || res.data2) ?? [], `获取分类列表`, true))
  218. .catch(e => { throw e });
  219. }
  220. async getListForDiscover(page: number, pageSize: number, keywords?: string) {
  221. return (this.post(`/village/collect/list`, {
  222. page,
  223. page_size: pageSize,
  224. keywords,
  225. status: 4,
  226. }, '获取信息详情'))
  227. .then(res => ({
  228. total: res.data2.total as number,
  229. list: transformArrayDataModel<CommonInfoModel>(CommonInfoModel, (res.data2.data || res.data2) ?? [], `获取分类列表`, true)
  230. }))
  231. .catch(e => { throw e });
  232. }
  233. async getInfoForDiscover(id: number) {
  234. return (await this.post(`/village/collect/info`, {
  235. id,
  236. status: 4,
  237. }, '通用获取信息详情', undefined, CommonInfoModel)).data as CommonInfoModel
  238. }
  239. async updateInfo<T extends DataModel>(
  240. collectModuleId: number|undefined,
  241. subType: string,
  242. subKey: string,
  243. subId: number,
  244. villageId: number,
  245. villageVolunteerId: number,
  246. catalogId: number|undefined,
  247. data: T,
  248. ) {
  249. const res : Record<string, any> = {
  250. ...data.toServerSide(),
  251. collect_module_id: collectModuleId,
  252. village_id: villageId,
  253. village_volunteer_id: villageVolunteerId,
  254. catalog_id: catalogId,
  255. };
  256. if (subKey)
  257. res[subKey] = subId;
  258. return (await this.post(`/village/collect/save`, res, '通用更新信息详情'));
  259. }
  260. }
  261. export default new VillageInfoApi();