|
|
@@ -1,21 +1,35 @@
|
|
|
-import { BasicType, DiaLogSheetControl, YTAvoid, YTHeader, yTRouter } from 'basic'
|
|
|
+import { BasicType, DiaLogSheetControl, IBestToast, userInfo, YTAvoid, YTHeader, yTRouter } from 'basic'
|
|
|
import { buttonComp } from '../../components/BuilderIndex'
|
|
|
import { ChooseAddress } from '../../components/chooseAddress'
|
|
|
import { AddressAddPageState } from '../../model/EnumState'
|
|
|
+import { UserAddressStorage } from '../../model/Storage'
|
|
|
import { CustomTextStyle } from '../../style/CustomTextStyle'
|
|
|
+import { AppStorageV2 } from '@kit.ArkUI'
|
|
|
+import { Address } from '../../model/Address'
|
|
|
|
|
|
/**
|
|
|
* 新增地址页面
|
|
|
*/
|
|
|
@ComponentV2
|
|
|
struct IncreaseAddressPage {
|
|
|
- @Param pageState: AddressAddPageState = AddressAddPageState.ADD
|
|
|
+ @Param @Require addressIndex?: number
|
|
|
|
|
|
@Local safeBottom: number = AppStorage.get(YTAvoid.SAFE_BOTTOM_KEY) as number
|
|
|
+ // 页面状态
|
|
|
+ @Local pageState: AddressAddPageState = AddressAddPageState.ADD
|
|
|
// 是否设置为默认地址
|
|
|
@Local isDefaultAddress: boolean = false
|
|
|
- // 选择的地址
|
|
|
+
|
|
|
+ // 选择的地址 以 - 进行分割
|
|
|
@Local address: string = ''
|
|
|
+ // 收件人名称
|
|
|
+ @Local userName: string = ''
|
|
|
+ // 收货人手机号
|
|
|
+ @Local phone: string = ''
|
|
|
+ // 详细地址
|
|
|
+ @Local region: string = ''
|
|
|
+
|
|
|
+ addressConnect: UserAddressStorage = AppStorageV2.connect(UserAddressStorage, () => new UserAddressStorage())!
|
|
|
|
|
|
forEach: Array<BasicType> = [
|
|
|
{ text: '收件人', message: '请输入收件人', type: 'Input' },
|
|
|
@@ -29,16 +43,23 @@ struct IncreaseAddressPage {
|
|
|
let control: DiaLogSheetControl = new DiaLogSheetControl(this.getUIContext(), false, false)
|
|
|
yTRouter.router2BottomDialog({
|
|
|
control: control,
|
|
|
- builder: () => { this.chooseAddress(control, this.address) }
|
|
|
+ builder: () => { this.chooseAddress(control, this.region) }
|
|
|
}, (ans) => {
|
|
|
let res = ans.result as Array<string>
|
|
|
- this.address = res.join('-')
|
|
|
+ this.region = res.join('-')
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 保存地址
|
|
|
- saveAddress() {
|
|
|
-
|
|
|
+ async saveAddress() {
|
|
|
+ const addressItem: Address = this.getAddressObject()
|
|
|
+ if(this.pageState == AddressAddPageState.ADD) {
|
|
|
+ await this.addressConnect.addUserAddress(addressItem)
|
|
|
+ } else {
|
|
|
+ await this.addressConnect.updateUserAddress(addressItem)
|
|
|
+ }
|
|
|
+ IBestToast.show('保存成功')
|
|
|
+ yTRouter.pop()
|
|
|
}
|
|
|
|
|
|
// 设置为默认地址
|
|
|
@@ -53,12 +74,42 @@ struct IncreaseAddressPage {
|
|
|
}, (info) => {
|
|
|
let ans = info.result as number
|
|
|
if(ans == 1) {
|
|
|
- // todo 执行删除的逻辑
|
|
|
+ let id = this.addressConnect.addressList[this.addressIndex!].id
|
|
|
+ this.addressConnect.deleteUserAddress(id!)
|
|
|
yTRouter.pop()
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ // 初始化页面数据
|
|
|
+ initData() {
|
|
|
+ if(this.addressIndex != undefined) {
|
|
|
+ this.pageState = AddressAddPageState.EDIT
|
|
|
+ const addressItem: Address = this.addressConnect.addressList[this.addressIndex]
|
|
|
+ this.address = addressItem.address ?? ''
|
|
|
+ this.userName = addressItem.userName ?? ''
|
|
|
+ this.phone = addressItem.phone ?? ''
|
|
|
+ this.region = addressItem.region ?? ''
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成地址对象
|
|
|
+ getAddressObject(): Address {
|
|
|
+ let addressItem: Address = this.addressIndex ? this.addressConnect.addressList[this.addressIndex] : {}
|
|
|
+
|
|
|
+ addressItem.address = this.address
|
|
|
+ addressItem.userName = this.userName
|
|
|
+ addressItem.phone = this.phone
|
|
|
+ addressItem.region = this.region
|
|
|
+ addressItem.userId = userInfo.getId()!
|
|
|
+
|
|
|
+ return addressItem
|
|
|
+ }
|
|
|
+
|
|
|
+ aboutToAppear(): void {
|
|
|
+ this.initData()
|
|
|
+ }
|
|
|
+
|
|
|
build() {
|
|
|
NavDestination() {
|
|
|
Column() {
|
|
|
@@ -75,13 +126,16 @@ struct IncreaseAddressPage {
|
|
|
.fontColor('#FF000000')
|
|
|
|
|
|
if(item.type == "Event"){
|
|
|
- Text(this.address || item.message)
|
|
|
+ Text(this.region || item.message)
|
|
|
.fontSize(14)
|
|
|
.fontWeight(500)
|
|
|
.fontColor('#FF000000')
|
|
|
.onClick(item.click)
|
|
|
- } else {
|
|
|
- TextInput({placeholder: item.message})
|
|
|
+ } else if(item.type == "Input" || item.type == "InputNumber") {
|
|
|
+ TextInput({
|
|
|
+ placeholder: item.message,
|
|
|
+ text: item.text == '收件人' ? this.userName : item.text == '联系电话' ? this.phone : this.address,
|
|
|
+ })
|
|
|
.padding(0)
|
|
|
.fontSize(14)
|
|
|
.borderRadius(0)
|
|
|
@@ -90,6 +144,15 @@ struct IncreaseAddressPage {
|
|
|
.placeholderFont({size: 14})
|
|
|
.backgroundColor(Color.Transparent)
|
|
|
.type(item.type == "Input" ? undefined : InputType.PhoneNumber)
|
|
|
+ .onChange((text) => {
|
|
|
+ if(item.text == '收件人') {
|
|
|
+ this.userName = text
|
|
|
+ } else if(item.text == '联系电话') {
|
|
|
+ this.phone = text
|
|
|
+ } else {
|
|
|
+ this.address = text
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
.width('100%')
|
|
|
@@ -104,27 +167,29 @@ struct IncreaseAddressPage {
|
|
|
.borderRadius(8)
|
|
|
.backgroundColor(Color.White)
|
|
|
|
|
|
-
|
|
|
- Row(){
|
|
|
- Text('设为默认地址')
|
|
|
- .attributeModifier(new CustomTextStyle({size: 14, weight: 500}))
|
|
|
-
|
|
|
- if(this.isDefaultAddress) {
|
|
|
- Image($r('[basic].media.icon_select'))
|
|
|
- .width(15).aspectRatio(1)
|
|
|
- } else {
|
|
|
- Row()
|
|
|
- .borderRadius(15)
|
|
|
- .border({width: 1})
|
|
|
- .width(15).aspectRatio(1)
|
|
|
+ // todo 是否保留
|
|
|
+ if(true) {
|
|
|
+ Row(){
|
|
|
+ Text('设为默认地址')
|
|
|
+ .attributeModifier(new CustomTextStyle({size: 14, weight: 500}))
|
|
|
+
|
|
|
+ if(this.isDefaultAddress) {
|
|
|
+ Image($r('[basic].media.icon_select'))
|
|
|
+ .width(15).aspectRatio(1)
|
|
|
+ } else {
|
|
|
+ Row()
|
|
|
+ .borderRadius(15)
|
|
|
+ .border({width: 1})
|
|
|
+ .width(15).aspectRatio(1)
|
|
|
+ }
|
|
|
}
|
|
|
+ .padding(15)
|
|
|
+ .width('100%')
|
|
|
+ .borderRadius(8)
|
|
|
+ .backgroundColor(Color.White)
|
|
|
+ .justifyContent(FlexAlign.SpaceBetween)
|
|
|
+ .onClick(() =>{ this.setDefaultAddress() })
|
|
|
}
|
|
|
- .padding(15)
|
|
|
- .width('100%')
|
|
|
- .borderRadius(8)
|
|
|
- .backgroundColor(Color.White)
|
|
|
- .justifyContent(FlexAlign.SpaceBetween)
|
|
|
- .onClick(() =>{ this.setDefaultAddress() })
|
|
|
}
|
|
|
.width('100%')
|
|
|
.layoutWeight(1)
|
|
|
@@ -162,6 +227,6 @@ struct IncreaseAddressPage {
|
|
|
}
|
|
|
|
|
|
@Builder
|
|
|
-function IncreaseAddressBuilder(_: string, state?: AddressAddPageState) {
|
|
|
- IncreaseAddressPage({pageState: state})
|
|
|
+function IncreaseAddressBuilder(_: string, addressIndex?: number) {
|
|
|
+ IncreaseAddressPage({addressIndex})
|
|
|
}
|