VillageInfoApi.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { DataModel, transformArrayDataModel, type NewDataModel } 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. },
  30. this._blackList.toServer.push(
  31. 'updatedAt', 'createdAt', 'deletedAt',
  32. );
  33. this._afterSolveServer = () => {
  34. if (this.province && this.city && this.district) {
  35. this.cityAddress = [this.province as string, this.city as string, this.district as string];
  36. }
  37. };
  38. this._afterSolveClient = (data) => {
  39. if (this.cityAddress) {
  40. data.province = this.cityAddress[0];
  41. data.city = this.cityAddress[1];
  42. data.district = this.cityAddress[2];
  43. }
  44. };
  45. }
  46. id !: number;
  47. cityAddress?: string[];
  48. }
  49. export class VillageEnvInfo extends DataModel<VillageEnvInfo> {
  50. constructor() {
  51. super(VillageEnvInfo, "地理信息");
  52. this.setNameMapperCase('Camel', 'Snake');
  53. this._convertTable = {
  54. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  55. landforms: [
  56. { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  57. { clientSide: 'arrayInt', serverSide: 'original' },
  58. ],
  59. villageType: { clientSide: 'number', serverSide: 'number' },
  60. },
  61. this._blackList.toServer.push(
  62. 'updatedAt', 'createdAt', 'deletedAt',
  63. );
  64. this._afterSolveServer = () => {
  65. if (this.longitude && this.latitude) {
  66. this.lonlat = [this.longitude as number, this.latitude as number];
  67. }
  68. };
  69. this._afterSolveClient = (data) => {
  70. if (this.lonlat) {
  71. data.longitude = this.lonlat[0];
  72. data.latitude = this.lonlat[1];
  73. }
  74. };
  75. }
  76. id !: number;
  77. lonlat?: number[];
  78. landforms = [] as string[];
  79. }
  80. export class VillageListItem extends DataModel<VillageListItem> {
  81. constructor() {
  82. super(VillageListItem, "村庄信息");
  83. this.setNameMapperCase('Camel', 'Snake');
  84. this._convertTable = {
  85. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  86. },
  87. this._blackList.toServer.push(
  88. 'updatedAt', 'createdAt', 'deletedAt',
  89. );
  90. this._convertKeyType = (key, direction) => {
  91. if (key.endsWith('At'))
  92. return {
  93. clientSide: 'date',
  94. serverSide: 'string',
  95. };
  96. return undefined;
  97. };
  98. this._afterSolveServer = () => {
  99. if (!this.title) {
  100. if (this.name) this.title = this.name as string;
  101. if (typeof this.content === 'object' && (this.content as any)?.title) this.title = (this.content as any).title as string;
  102. if (this.content) this.title = this.content as string;
  103. if (this.structure) this.title = this.structure as string;
  104. if (this.wisdom) this.title = this.wisdom as string;
  105. }
  106. if (!this.image) {
  107. if (this.distribution) this.image = this.distribution as string;
  108. }
  109. };
  110. }
  111. id !: number;
  112. createdAt = new Date();
  113. updatedAt = new Date();
  114. title = '';
  115. image = '';
  116. }
  117. export class VillageBulidingInfo extends DataModel<VillageBulidingInfo> {
  118. constructor() {
  119. super(VillageBulidingInfo, "历史建筑信息");
  120. this.setNameMapperCase('Camel', 'Snake');
  121. this._convertTable = {
  122. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  123. }
  124. this._blackList.toServer.push(
  125. 'updatedAt', 'createdAt', 'deletedAt',
  126. );
  127. const commaArrayKeys = [
  128. 'purpose','floorType','wallType','roofForm','bearingType',
  129. ]
  130. this._convertKeyType = (key, direction) => {
  131. if (commaArrayKeys.includes(key))
  132. return [
  133. { clientSide: 'splitCommaArray', serverSide: 'commaArrayMerge' },
  134. { clientSide: 'arrayInt', serverSide: 'original' },
  135. ];
  136. return undefined;
  137. };
  138. }
  139. id !: number;
  140. }
  141. export class VillageInfoApi extends AppServerRequestModule<DataModel> {
  142. constructor() {
  143. super();
  144. }
  145. /**
  146. * 获取分类列表
  147. * @param type 根级类型:1=区域、2=级别、3=文物类型、4=非遗类型、42=事件类型
  148. * @param withself 是否返回包含自己:true=是,false=否 ,默认false
  149. * @returns
  150. */
  151. getCategoryList(
  152. type?: number,
  153. withself?: boolean,
  154. ) {
  155. return CommonContent.getCategoryList(type, withself);
  156. }
  157. /**
  158. * 用于获取某一个分类需要用的子级
  159. * @param pid 父级
  160. * @returns
  161. */
  162. getCategoryChildList(pid?: number) {
  163. return CommonContent.getCategoryChildList(pid);
  164. }
  165. async getInfo<T extends DataModel>(
  166. sub: string,
  167. subId: number,
  168. villageId: number,
  169. villageVolunteerId: number,
  170. id?: number,
  171. modelClassCreator: (new () => T) = CommonInfoModel as any
  172. ) {
  173. return (await this.post(`/village/${sub}/getInfo`, {
  174. type: subId,
  175. village_id: villageId,
  176. village_volunteer_id: villageVolunteerId,
  177. id,
  178. }, '获取信息详情', undefined, modelClassCreator)).data as T
  179. }
  180. async getList<T extends DataModel = VillageListItem>(
  181. sub: string,
  182. subId: number|undefined,
  183. subKey: string|undefined,
  184. villageId: number,
  185. villageVolunteerId: number,
  186. modelClassCreator: (new () => T) = VillageListItem as any
  187. ) {
  188. return (this.post(`/village/${sub}/getList`, {
  189. [subKey ? subKey : 'type']: subId,
  190. village_id: villageId,
  191. village_volunteer_id: villageVolunteerId,
  192. }, '获取信息详情'))
  193. .then(res => transformArrayDataModel<T>(modelClassCreator, (res.data2.data || res.data2) ?? [], `获取分类列表`, true))
  194. .catch(e => { throw e });
  195. }
  196. async updateInfo<T extends DataModel>(
  197. sub: string,
  198. villageId: number,
  199. villageVolunteerId: number,
  200. data: T,
  201. ) {
  202. return (await this.post(`/village/${sub}/save`, {
  203. sub,
  204. village_id: villageId,
  205. village_volunteer_id: villageVolunteerId,
  206. ...data.toServerSide(),
  207. }, '更新信息详情'));
  208. }
  209. }
  210. export default new VillageInfoApi();