Browse Source

新增收货地址

zs 1 year ago
parent
commit
d70752e3e5

+ 68 - 0
src/controller/system/address.controller.ts

@@ -0,0 +1,68 @@
+import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';
+import { BaseController } from 'free-midway-component';
+import { AddressService } from '../../service/system/address.service';
+import { CDTO_address, CVO_address, FVO_address, QDTO_address, QVO_address, UDTO_address, UVAO_address } from '../../interface/system/address.interface';
+import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
+import { Validate } from '@midwayjs/validate';
+@ApiTags(['收货地址'])
+@Controller('/address')
+export class AddressController extends BaseController {
+  @Inject()
+  service: AddressService;
+
+  @Post('/')
+  @Validate()
+  @ApiResponse({ type: CVO_address })
+  async create(@Body() data: CDTO_address) {
+    const dbData = await this.service.create(data);
+    const result = new CVO_address(dbData);
+    return result;
+  }
+  @Get('/')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: QVO_address })
+  async query(@Query() filter: QDTO_address, @Query('skip') skip: number, @Query('limit') limit: number) {
+    const list = await this.service.query(filter, { skip, limit });
+    const data = [];
+    for (const i of list) {
+      const newData = new QVO_address(i);
+      data.push(newData);
+    }
+    const total = await this.service.count(filter);
+    return { data, total };
+  }
+
+  @Get('/:id')
+  @ApiResponse({ type: FVO_address })
+  async fetch(@Param('id') id: string) {
+    const data = await this.service.fetch(id);
+    const result = new FVO_address(data);
+    return result;
+  }
+
+  @Post('/:id')
+  @Validate()
+  @ApiResponse({ type: UVAO_address })
+  async update(@Param('id') id: string, @Body() body: UDTO_address) {
+    const result = await this.service.updateOne(id, body);
+    return result;
+  }
+
+  @Del('/:id')
+  @Validate()
+  async delete(@Param('id') id: string) {
+    await this.service.delete(id);
+    return 'ok';
+  }
+  async createMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async updateMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async deleteMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+}

+ 23 - 0
src/entity/system/address.entity.ts

@@ -0,0 +1,23 @@
+import { modelOptions, prop } from '@typegoose/typegoose';
+import { BaseModel } from 'free-midway-component';
+@modelOptions({
+  schemaOptions: { collection: 'address' },
+})
+export class Address extends BaseModel {
+  @prop({ required: false, index: false, zh: '用户' })
+  user: string;
+  @prop({ required: false, index: false, zh: '收货人' })
+  name: string;
+  @prop({ required: false, index: false, zh: '收货人联系电话' })
+  phone: string;
+  @prop({ required: false, index: false, zh: '省份' })
+  province: string;
+  @prop({ required: false, index: false, zh: '城市' })
+  city: string;
+  @prop({ required: false, index: false, zh: '区' })
+  area: string;
+  @prop({ required: false, index: false, zh: '详细地址' })
+  address: string;
+  @prop({ required: false, index: false, zh: '是否默认', default: '0' })
+  is_default: string;
+}

+ 96 - 0
src/interface/system/address.interface.ts

@@ -0,0 +1,96 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import { ApiProperty } from '@midwayjs/swagger';
+import { SearchBase } from 'free-midway-component';
+import get = require('lodash/get');
+const dealVO = (cla, data) => {
+  for (const key in cla) {
+    const val = get(data, key);
+    if (val || val === 0) cla[key] = val;
+  }
+};
+export class FVO_address {
+  constructor(data: object) {
+    dealVO(this, data);
+  }
+  @ApiProperty({ description: '数据id' })
+  _id: string = undefined;
+  @ApiProperty({ description: '用户' })
+  'user': string = undefined;
+  @ApiProperty({ description: '收货人' })
+  'name': string = undefined;
+  @ApiProperty({ description: '收货人联系电话' })
+  'phone': string = undefined;
+  @ApiProperty({ description: '省份' })
+  'province': string = undefined;
+  @ApiProperty({ description: '城市' })
+  'city': string = undefined;
+  @ApiProperty({ description: '区' })
+  'area': string = undefined;
+  @ApiProperty({ description: '详细地址' })
+  'address': string = undefined;
+  @ApiProperty({ description: '是否默认' })
+  'is_default': string = undefined;
+}
+
+export class QDTO_address extends SearchBase {
+  constructor() {
+    const like_prop = [];
+    const props = [];
+    const mapping = [];
+    super({ like_prop, props, mapping });
+  }
+}
+
+export class QVO_address extends FVO_address {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class CDTO_address {
+  @ApiProperty({ description: '用户' })
+  @Rule(RuleType['string']().empty(''))
+  'user': string = undefined;
+  @ApiProperty({ description: '收货人' })
+  @Rule(RuleType['string']().empty(''))
+  'name': string = undefined;
+  @ApiProperty({ description: '收货人联系电话' })
+  @Rule(RuleType['string']().empty(''))
+  'phone': string = undefined;
+  @ApiProperty({ description: '省份' })
+  @Rule(RuleType['string']().empty(''))
+  'province': string = undefined;
+  @ApiProperty({ description: '城市' })
+  @Rule(RuleType['string']().empty(''))
+  'city': string = undefined;
+  @ApiProperty({ description: '区' })
+  @Rule(RuleType['string']().empty(''))
+  'area': string = undefined;
+  @ApiProperty({ description: '详细地址' })
+  @Rule(RuleType['string']().empty(''))
+  'address': string = undefined;
+  @ApiProperty({ description: '是否默认' })
+  @Rule(RuleType['string']().empty(''))
+  'is_default': string = undefined;
+}
+
+export class CVO_address extends FVO_address {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class UDTO_address extends CDTO_address {
+  @ApiProperty({ description: '数据id' })
+  @Rule(RuleType['string']().empty(''))
+  _id: string = undefined;
+}
+
+export class UVAO_address extends FVO_address {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}

+ 11 - 0
src/service/system/address.service.ts

@@ -0,0 +1,11 @@
+import { Provide } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import { BaseService } from 'free-midway-component';
+import { Address } from '../../entity/system/address.entity';
+type modelType = ReturnModelType<typeof Address>;
+@Provide()
+export class AddressService extends BaseService<modelType> {
+  @InjectEntityModel(Address)
+  model: modelType;
+}