SettingPage.ets 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {
  2. BasicType,
  3. IBestToast,
  4. reviseImgHeaderBuilder,
  5. takePicture,
  6. Upload,
  7. UserInfo,
  8. userInfo,
  9. YtAvoid,
  10. YtButton,
  11. YTHeader,
  12. YTLog,
  13. yTRequest,
  14. yTRouter,
  15. yTToast
  16. } from 'basic'
  17. @Builder
  18. function settingBuilder() {
  19. NavDestination() {
  20. SettingPage()
  21. }
  22. .hideTitleBar(true)
  23. }
  24. @Component
  25. struct SettingPage {
  26. @StorageProp(YtAvoid.safeTopKey) safeTop: number = 0
  27. @StorageProp(YtAvoid.safeBottomKey) safeBottom: number = 0
  28. @StorageProp(userInfo.KEY) userInfo: UserInfo = new UserInfo()
  29. @State showReviseName: boolean = false
  30. @State showHeaderImgRevise: boolean = false
  31. @State value: string = ''
  32. reviseBuilderArr: Array<BasicType<undefined>> = [
  33. {
  34. text: '头像修改',
  35. src: '',
  36. click: () => {
  37. this.showHeaderImgRevise = true
  38. }
  39. },
  40. {
  41. text: '昵称修改',
  42. click: () => {
  43. this.showReviseName = true
  44. }
  45. }
  46. ]
  47. options: BasicType<undefined>[] = [
  48. {
  49. text: '拍照',
  50. click: async () => {
  51. try {
  52. const fullpath = await takePicture()
  53. this.showHeaderImgRevise = false
  54. yTRouter.router2DelPhotoPage({ src: fullpath, type: 'header' })
  55. } catch (e) {
  56. YTLog.warn(e)
  57. }
  58. }
  59. },
  60. {
  61. text: '从相册中选择',
  62. click: () => {
  63. Upload.selectImage(this.getUIContext().getHostContext()!, (fullPath) => {
  64. this.showHeaderImgRevise = false
  65. yTRouter.router2DelPhotoPage({ src: fullPath, type: 'image' })
  66. })
  67. }
  68. },
  69. {
  70. text: '取消',
  71. click: () => {
  72. this.showHeaderImgRevise = false
  73. }
  74. },
  75. ]
  76. build() {
  77. Column() {
  78. YTHeader({ title: '用户设置' })
  79. Column() {
  80. ForEach(this.reviseBuilderArr, (item: BasicType<undefined>, index) => {
  81. this.reviseBuilder(item)
  82. if (index < this.reviseBuilderArr.length - 1) {
  83. Column() {
  84. Text('')
  85. .height(1)
  86. .width('100%')
  87. .backgroundColor('#0A000000')
  88. }
  89. .padding({ left: 12, right: 8 })
  90. .bindSheet($$this.showHeaderImgRevise, this.changeBuilder, {
  91. height: 204,
  92. backgroundColor: Color.White,
  93. showClose: false
  94. })
  95. }
  96. })
  97. YtButton({
  98. btContent: '退出登录', click: () => {
  99. this.userInfo.logout()
  100. yTRouter.routerBack()
  101. IBestToast.show({ message: '退出登录成功' })
  102. }
  103. })
  104. .margin({ top: 426, bottom: 12 })
  105. YtButton({
  106. btContent: '注销用户',
  107. btFontColor: Color.Red,
  108. bgc: Color.White,
  109. click: () => {
  110. yTToast.doubleConfirm({
  111. text: '警告⚠', message: '确定要注销吗?\n注销后数据可能丢失无法恢复!', click: () => {
  112. userInfo.logout()
  113. yTToast.hide()
  114. yTRouter.pop()
  115. }
  116. })
  117. }
  118. })
  119. }
  120. .bindSheet($$this.showReviseName, this.reviseNameBuilder, {
  121. height: 275,
  122. showClose: false,
  123. backgroundColor: Color.White
  124. })
  125. .padding({
  126. top: 29,
  127. left: 16,
  128. right: 16,
  129. })
  130. }
  131. .padding({ top: this.safeTop, bottom: this.safeBottom })
  132. }
  133. @Builder
  134. reviseBuilder(item: BasicType<undefined>) {
  135. Row() {
  136. Text(item.text)
  137. .fontSize($r('[basic].float.page_text_font_size_12'))
  138. .fontColor('#80000000')
  139. Row() {
  140. if (typeof item.src == 'string') {
  141. Image(this.userInfo.getHeadImg() ? this.userInfo.getHeadImg() : $r('app.media.default_img'))
  142. .height(24)
  143. .width(24)
  144. .borderRadius(12)
  145. } else {
  146. Text(this.userInfo.getName() ?? this.userInfo.getPhoneNumber() ?? this.userInfo.getId()?.toString())
  147. .fontSize($r('[basic].float.page_text_font_size_12'))
  148. .fontColor('#CC000000')
  149. }
  150. Image($r('app.media.right_arrow'))
  151. .width(24)
  152. .width(24)
  153. }
  154. }
  155. .width('100%')
  156. .justifyContent(FlexAlign.SpaceBetween)
  157. .padding({ left: 12, right: 4 })
  158. .height(36)
  159. .onClick(item.click)
  160. }
  161. @Builder
  162. reviseNameBuilder() {
  163. Column() {
  164. Text('昵称')
  165. .width('100%')
  166. .height(36)
  167. .textAlign(TextAlign.Start)
  168. .fontSize(12)
  169. TextInput({ text: $$this.value, placeholder: '请输入昵称' })
  170. .borderRadius(4)
  171. .fontSize(12)
  172. .placeholderFont({ size: 12 })
  173. .placeholderColor('#14000000')
  174. .height(36)
  175. .width('100%')
  176. .backgroundColor(Color.White)
  177. .border({ width: 1, color: '#14000000' })
  178. .margin({ bottom: 25 })
  179. Row({ space: 32 }) {
  180. YtButton({
  181. btContent: '取消',
  182. btHeight: 25,
  183. btWidth: 78,
  184. btBorder: { width: 1, color: '#1A000000' },
  185. btPadding: {
  186. left: 26,
  187. right: 26,
  188. top: 4,
  189. bottom: 4
  190. },
  191. bgc: Color.White,
  192. btFontColor: $r('sys.color.mask_secondary'),
  193. click: () => {
  194. this.showReviseName = false
  195. this.value = ''
  196. }
  197. })
  198. YtButton({
  199. btContent: '完成',
  200. btHeight: 25,
  201. btWidth: 78,
  202. btPadding: {
  203. left: 26,
  204. right: 26,
  205. top: 4,
  206. bottom: 4
  207. },
  208. click: () => {
  209. if (!this.value) {
  210. IBestToast.show({
  211. type: "warning",
  212. message: "昵称不能为空"
  213. })
  214. return
  215. }
  216. yTRequest.changeNickname(this.value, () => {
  217. IBestToast.show({ message: '名称修改成功' })
  218. })
  219. //TODO 发送请求后关闭弹窗
  220. this.showReviseName = false
  221. }
  222. })
  223. }
  224. .justifyContent(FlexAlign.Center)
  225. .width('100%')
  226. }
  227. .padding({ left: 22, right: 22, top: 8 })
  228. }
  229. @Builder
  230. changeBuilder() {
  231. reviseImgHeaderBuilder(this.options)
  232. }
  233. }