user-delivery-address.schema.ts 7.8 KB

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