perf: 优化 phone

pull/10718/head
ibuler 2023-06-14 20:22:41 +08:00
parent a3d32c901d
commit fca3936a79
1 changed files with 5 additions and 2 deletions

View File

@ -214,8 +214,11 @@ class BitChoicesField(TreeChoicesField):
class PhoneField(serializers.CharField): class PhoneField(serializers.CharField):
def to_representation(self, value): def to_representation(self, value):
if value: if value:
phone = phonenumbers.parse(value, 'CN') try:
value = {'code': '+%s' % phone.country_code, 'phone': phone.national_number} phone = phonenumbers.parse(value, 'CN')
value = {'code': '+%s' % phone.country_code, 'phone': phone.national_number}
except phonenumbers.NumberParseException:
value = {'code': '+86', 'phone': value}
return value return value