SettingPage.ets 6.2 KB

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