wwf
2 天以前 791a96ae03cf92478244127b294c1fe520d31e89
entry/src/main/ets/pages/account/UserInfoPage.ets
@@ -1,12 +1,87 @@
import { promptAction, router } from "@kit.ArkUI"
import { http } from "@kit.NetworkKit"
import { HttpResponseResult } from "../../data/HttpResponse"
import { DOMAIN } from "../../utils/config"
import { UserInfo } from "./AccountPage"
@Entry
@Component
export struct UserInfo {
struct UserInfoPage {
  @State nickName: string = ''
  @State gender: string = ''
  @State age: string = ''
  @State preferJob: string = ''
  @State job: string = ''
  @State introduce: string = ''
  @State userInfo: UserInfo = {
    nickName: '',
    mobilePhone: '',
    age: '',
    gender: '',
    job: '',
    preferJob: '',
    introduce: '',
  }
  aboutToAppear(): void {
    this.getUserInfo()
  }
  getUserInfo() {
    let httpRequest = http.createHttp();
    httpRequest.request(
      `${DOMAIN}/quiz-community/public/v1.0/users/userinfo`,
      {
        method: http.RequestMethod.GET,
        header: { 'Content-Type': 'application/json', 'x-jwt-token': `Bearer ${AppStorage.get('x-jwt-token')}` },
      },
      (err, data) => {
        console.log('response', '/users/userinfo')
        console.log(JSON.stringify(data.result))
        if (data.responseCode == 200) {
          const resData = (typeof data.result == 'string' ? JSON.parse(data.result) : data.result) as HttpResponseResult<UserInfo>
          if (resData.code == 200) {
            this.userInfo = resData.data as UserInfo
          } else {
            promptAction.showToast({ message: resData.msg })
          }
        }
      }
    )
  }
  saveInfo() {
    let httpRequest = http.createHttp();
    let postData: UserInfo = {
      nickName: this.userInfo.nickName,
      mobilePhone: '',
      age: this.userInfo.age,
      gender: this.userInfo.gender,
      job: this.userInfo.job,
      preferJob: this.userInfo.preferJob,
      introduce: this.userInfo.introduce
    }
    httpRequest.request(
      `${DOMAIN}/quiz-community/public/v1.0/users/userinfo`,
      {
        method: http.RequestMethod.PUT,
        header: { 'Content-Type': 'application/json' },
        extraData: postData
      },
      (err, data) => {
        console.log('response', '/users/userinfo')
        console.log(JSON.stringify(data.result))
        if (data.responseCode == 200) {
          const resData = (typeof data.result == 'string' ? JSON.parse(data.result) : data.result) as HttpResponseResult<string>
          if (resData.code == 200) {
            promptAction.showToast({ message: '保存成功' })
          } else {
            promptAction.showToast({ message: resData.msg })
          }
        }
      }
    )
  }
  build() {
    Column() {
@@ -14,6 +89,9 @@
        Image($r('app.media.left_icon'))
          .width(20)
          .height(20)
          .onClick(() => {
            router.back()
          })
        Text('个人信息')
          .fontSize(18)
          .fontWeight(700)
@@ -31,7 +109,7 @@
            .fontSize(14)
            .fontColor('#666666')
            .width('100%')
          TextInput({ placeholder: '请输入', text: this.nickName })
          TextInput({ placeholder: '请输入', text: $$this.userInfo.nickName })
            .placeholderFont({ size: 14 })
            .showUnderline(true)
            .margin({ top: -6 })
@@ -42,7 +120,7 @@
            .fontSize(14)
            .fontColor('#666666')
            .width('100%')
          TextInput({ placeholder: '请输入', text: this.gender })
          TextInput({ placeholder: '请输入', text: $$this.userInfo.gender })
            .placeholderFont({ size: 14 })
            .showUnderline(true)
            .margin({ top: -6 })
@@ -53,7 +131,7 @@
            .fontSize(14)
            .fontColor('#666666')
            .width('100%')
          TextInput({ placeholder: '请输入', text: this.age })
          TextInput({ placeholder: '请输入', text: $$this.userInfo.age })
            .placeholderFont({ size: 14 })
            .showUnderline(true)
            .margin({ top: -6 })
@@ -64,7 +142,7 @@
            .fontSize(14)
            .fontColor('#666666')
            .width('100%')
          TextInput({ placeholder: '请输入', text: this.job })
          TextInput({ placeholder: '请输入', text: $$this.userInfo.job })
            .placeholderFont({ size: 14 })
            .showUnderline(true)
            .margin({ top: -6 })
@@ -75,7 +153,7 @@
            .fontSize(14)
            .fontColor('#666666')
            .width('100%')
          TextInput({ placeholder: '请输入', text: this.preferJob })
          TextInput({ placeholder: '请输入', text: $$this.userInfo.preferJob })
            .placeholderFont({ size: 14 })
            .showUnderline(true)
            .margin({ top: -6 })
@@ -86,13 +164,13 @@
            .fontColor('#666666')
            .fontSize(14)
            .width('100%')
          TextArea({ placeholder: '介绍一下你自己吧~', text: this.job })
          TextArea({ placeholder: '介绍一下你自己吧~', text: this.userInfo.introduce })
            .placeholderFont({ size: 14 })
            .backgroundColor('#fff')
            .border({ width: 1, color: '#666666' })
            .borderRadius(5)
            .minLines(10)
            .maxLength(200)
            .constraintSize({ minHeight: 200 })
            .showCounter(true)
            .margin({ top: 10 })
        }
@@ -105,6 +183,9 @@
          .width('100%')
          .fontColor(Color.White)
          .backgroundColor('#1761f4')
          .onClick(() => {
            this.saveInfo()
          })
      }
      .height('100%')