user-delivery-address.mt.schema.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { z } from '@hono/zod-openapi';
  2. import { UserSchemaMt } from '@d8d/user-module-mt/schemas';
  3. import { getAreaSchemaMt } from '@d8d/geo-areas-mt/schemas';
  4. // 状态枚举
  5. export const DeliveryAddressStatusEnum = {
  6. NORMAL: 1,
  7. DISABLED: 2,
  8. DELETED: 3
  9. } as const;
  10. export const IsDefaultEnum = {
  11. NOT_DEFAULT: 0,
  12. IS_DEFAULT: 1
  13. } as const;
  14. // 用户专用基础实体Schema - 移除userId字段
  15. export const UserDeliveryAddressSchema = z.object({
  16. id: z.number()
  17. .int('ID必须是整数')
  18. .positive('ID必须是正整数')
  19. .openapi({
  20. description: '收货地址ID',
  21. example: 1
  22. }),
  23. tenantId: z.number()
  24. .int('租户ID必须是整数')
  25. .positive('租户ID必须是正整数')
  26. .openapi({
  27. description: '租户ID',
  28. example: 1
  29. }),
  30. userId: z.number()
  31. .int('用户ID必须是整数')
  32. .positive('用户ID必须是正整数')
  33. .openapi({
  34. description: '用户ID',
  35. example: 1
  36. }),
  37. // 注意:移除了userId字段,自动使用当前登录用户ID
  38. name: z.string()
  39. .min(1, '姓名不能为空')
  40. .max(255, '姓名最多255个字符')
  41. .openapi({
  42. description: '收货人姓名',
  43. example: '张三'
  44. }),
  45. phone: z.string()
  46. .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号')
  47. .openapi({
  48. description: '收货人手机号',
  49. example: '13800138000'
  50. }),
  51. address: z.string()
  52. .min(1, '详细地址不能为空')
  53. .max(255, '详细地址最多255个字符')
  54. .openapi({
  55. description: '详细地址',
  56. example: '北京市朝阳区建国门外大街1号'
  57. }),
  58. receiverProvince: z.coerce.number<number>()
  59. .int('省份ID必须是整数')
  60. .min(0, '省份ID不能为负数')
  61. .default(0)
  62. .openapi({
  63. description: '收货省份ID',
  64. example: 110000
  65. }),
  66. receiverCity: z.coerce.number<number>()
  67. .int('城市ID必须是整数')
  68. .min(0, '城市ID不能为负数')
  69. .default(0)
  70. .openapi({
  71. description: '收货城市ID',
  72. example: 110100
  73. }),
  74. receiverDistrict: z.coerce.number<number>()
  75. .int('区县ID必须是整数')
  76. .min(0, '区县ID不能为负数')
  77. .default(0)
  78. .openapi({
  79. description: '收货区县ID',
  80. example: 110105
  81. }),
  82. receiverTown: z.coerce.number<number>()
  83. .int('街道ID必须是整数')
  84. .min(0, '街道ID不能为负数')
  85. .default(0)
  86. .openapi({
  87. description: '收货街道ID',
  88. example: 110105001
  89. }),
  90. state: z.coerce.number<number>()
  91. .int('状态必须是整数')
  92. .min(1, '状态最小值为1')
  93. .max(3, '状态最大值为3')
  94. .default(1)
  95. .openapi({
  96. description: '状态:1正常,2禁用,3删除',
  97. example: 1
  98. }),
  99. isDefault: z.coerce.number<number>()
  100. .int('是否默认必须是整数')
  101. .min(0, '最小值为0')
  102. .max(1, '最大值为1')
  103. .default(0)
  104. .openapi({
  105. description: '是否默认地址:0否,1是',
  106. example: 0
  107. }),
  108. createdBy: z.number()
  109. .int('创建人ID必须是整数')
  110. .positive('创建人ID必须是正整数')
  111. .nullable()
  112. .openapi({
  113. description: '创建用户ID',
  114. example: 1
  115. }),
  116. updatedBy: z.number()
  117. .int('更新人ID必须是整数')
  118. .positive('更新人ID必须是正整数')
  119. .nullable()
  120. .openapi({
  121. description: '更新用户ID',
  122. example: 1
  123. }),
  124. createdAt: z.coerce.date('创建时间格式不正确')
  125. .openapi({
  126. description: '创建时间',
  127. example: '2024-01-01T12:00:00Z'
  128. }),
  129. updatedAt: z.coerce.date('更新时间格式不正确')
  130. .openapi({
  131. description: '更新时间',
  132. example: '2024-01-01T12:00:00Z'
  133. }),
  134. user: UserSchemaMt.optional().openapi({
  135. description: '关联用户信息'
  136. }),
  137. province: getAreaSchemaMt.optional().openapi({
  138. description: '关联省份信息'
  139. }),
  140. city: getAreaSchemaMt.optional().openapi({
  141. description: '关联城市信息'
  142. }),
  143. district: getAreaSchemaMt.optional().openapi({
  144. description: '关联区县信息'
  145. }),
  146. town: getAreaSchemaMt.optional().openapi({
  147. description: '关联街道信息'
  148. })
  149. });
  150. // 用户专用创建DTO - 移除userId字段
  151. export const CreateUserDeliveryAddressDto = z.object({
  152. // 注意:移除了userId字段,自动使用当前登录用户ID
  153. name: z.string()
  154. .min(1, '收货人姓名不能为空')
  155. .max(255, '收货人姓名最多255个字符')
  156. .openapi({
  157. description: '收货人姓名',
  158. example: '张三'
  159. }),
  160. phone: z.string()
  161. .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号')
  162. .openapi({
  163. description: '收货人手机号',
  164. example: '13800138000'
  165. }),
  166. address: z.string()
  167. .min(1, '详细地址不能为空')
  168. .max(255, '详细地址最多255个字符')
  169. .openapi({
  170. description: '详细地址',
  171. example: '北京市朝阳区建国门外大街1号'
  172. }),
  173. receiverProvince: z.coerce.number<number>()
  174. .int('省份ID必须是整数')
  175. .min(0, '省份ID不能为负数')
  176. .default(0)
  177. .openapi({
  178. description: '收货省份ID',
  179. example: 110000
  180. }),
  181. receiverCity: z.coerce.number<number>()
  182. .int('城市ID必须是整数')
  183. .min(0, '城市ID不能为负数')
  184. .default(0)
  185. .openapi({
  186. description: '收货城市ID',
  187. example: 110100
  188. }),
  189. receiverDistrict: z.coerce.number<number>()
  190. .int('区县ID必须是整数')
  191. .min(0, '区县ID不能为负数')
  192. .default(0)
  193. .openapi({
  194. description: '收货区县ID',
  195. example: 110105
  196. }),
  197. receiverTown: z.coerce.number<number>()
  198. .int('街道ID必须是整数')
  199. .min(0, '街道ID不能为负数')
  200. .default(0)
  201. .openapi({
  202. description: '收货街道ID',
  203. example: 110105001
  204. }),
  205. isDefault: z.coerce.number<number>()
  206. .int('是否默认必须是整数')
  207. .min(0, '最小值为0')
  208. .max(1, '最大值为1')
  209. .default(0)
  210. .openapi({
  211. description: '是否默认地址:0否,1是',
  212. example: 0
  213. })
  214. });
  215. // 用户专用更新DTO
  216. export const UpdateUserDeliveryAddressDto = z.object({
  217. name: z.string()
  218. .min(1, '收货人姓名不能为空')
  219. .max(255, '收货人姓名最多255个字符')
  220. .optional()
  221. .openapi({
  222. description: '收货人姓名',
  223. example: '张三'
  224. }),
  225. phone: z.string()
  226. .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号')
  227. .optional()
  228. .openapi({
  229. description: '收货人手机号',
  230. example: '13800138000'
  231. }),
  232. address: z.string()
  233. .min(1, '详细地址不能为空')
  234. .max(255, '详细地址最多255个字符')
  235. .optional()
  236. .openapi({
  237. description: '详细地址',
  238. example: '北京市朝阳区建国门外大街1号'
  239. }),
  240. receiverProvince: z.coerce.number<number>()
  241. .int('省份ID必须是整数')
  242. .positive('省份ID必须是正整数')
  243. .optional()
  244. .openapi({
  245. description: '收货省份ID',
  246. example: 110000
  247. }),
  248. receiverCity: z.coerce.number<number>()
  249. .int('城市ID必须是整数')
  250. .positive('城市ID必须是正整数')
  251. .optional()
  252. .openapi({
  253. description: '收货城市ID',
  254. example: 110100
  255. }),
  256. receiverDistrict: z.coerce.number<number>()
  257. .int('区县ID必须是整数')
  258. .positive('区县ID必须是正整数')
  259. .optional()
  260. .openapi({
  261. description: '收货区县ID',
  262. example: 110105
  263. }),
  264. receiverTown: z.coerce.number<number>()
  265. .int('街道ID必须是整数')
  266. .positive('街道ID必须是正整数')
  267. .optional()
  268. .openapi({
  269. description: '收货街道ID',
  270. example: 110105001
  271. }),
  272. state: z.coerce.number<number>()
  273. .int('状态必须是整数')
  274. .min(1, '状态最小值为1')
  275. .max(3, '状态最大值为3')
  276. .optional()
  277. .openapi({
  278. description: '状态:1正常,2禁用,3删除',
  279. example: 1
  280. }),
  281. isDefault: z.coerce.number<number>()
  282. .int('是否默认必须是整数')
  283. .min(0, '最小值为0')
  284. .max(1, '最大值为1')
  285. .optional()
  286. .openapi({
  287. description: '是否默认地址:0否,1是',
  288. example: 0
  289. })
  290. });