VillageInfoApi.ts 9.0 KB

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