SettingPage.ets 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {
  2. BasicType,
  3. IBestToast,
  4. reviseImgHeaderBuilder,
  5. userInfo,
  6. UserInfo,
  7. YTAvoid,
  8. YTButton,
  9. YTHeader,
  10. YTLog,
  11. YTPhotoHelper,
  12. yTRouter,
  13. yTToast,
  14. YTUserRequest
  15. } from 'basic'
  16. @Builder
  17. function settingBuilder() {
  18. NavDestination() {
  19. SettingPage()
  20. }
  21. .hideTitleBar(true)
  22. }
  23. @Component
  24. struct SettingPage {
  25. @State showReviseName: boolean = false
  26. @State showHeaderImgRevise: boolean = false
  27. @StorageProp(YTAvoid.SAFE_BOTTOM_KEY) private safeBottom: number = 0
  28. @StorageProp(UserInfo.KEY) private userInfo: UserInfo = userInfo
  29. @State private value: string = ''
  30. private yTPhotoHelper = new YTPhotoHelper(this.getUIContext().getHostContext()!)
  31. private 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. private options: BasicType<undefined>[] = [
  47. {
  48. text: '拍照',
  49. click: async () => {
  50. try {
  51. const fullpath = await this.yTPhotoHelper.takePicture()
  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. this.yTPhotoHelper.selectImage(
  63. (fullPath) => {
  64. this.showHeaderImgRevise = false
  65. yTRouter.router2DelPhotoPage({ src: fullPath, type: 'header' })
  66. })
  67. }
  68. },
  69. {
  70. text: '取消',
  71. click: () => {
  72. this.showHeaderImgRevise = false
  73. }
  74. },
  75. ]
  76. build() {
  77. Column() {
  78. YTHeader({ defaultStyle: { 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.routerBack()
  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. left: 16,
  127. right: 16,
  128. })
  129. }
  130. .padding({ bottom: this.safeBottom })
  131. }
  132. @Builder
  133. reviseBuilder(item: BasicType<undefined>) {
  134. Row() {
  135. Text(item.text)
  136. .fontSize($r('[basic].float.page_text_font_size_12'))
  137. .fontColor('#80000000')
  138. Row() {
  139. if (typeof item.src == 'string') {
  140. Image(this.userInfo.getHeadImg() ? this.userInfo.getHeadImg() : $r('app.media.default_img'))
  141. .height(24)
  142. .width(24)
  143. .borderRadius(12)
  144. } else {
  145. Text(this.userInfo.getName() ?? this.userInfo.getPhoneNumber() ?? this.userInfo.getId()?.toString())
  146. .fontSize($r('[basic].float.page_text_font_size_12'))
  147. .fontColor('#CC000000')
  148. }
  149. Image($r('app.media.right_arrow'))
  150. .width(24)
  151. .width(24)
  152. }
  153. }
  154. .width('100%')
  155. .justifyContent(FlexAlign.SpaceBetween)
  156. .padding({ left: 12, right: 4 })
  157. .height(36)
  158. .onClick(item.click)
  159. }
  160. @Builder
  161. reviseNameBuilder() {
  162. Column() {
  163. Text('昵称')
  164. .width('100%')
  165. .height(36)
  166. .textAlign(TextAlign.Start)
  167. .fontSize(12)
  168. TextInput({ text: $$this.value, placeholder: '请输入昵称' })
  169. .borderRadius(4)
  170. .fontSize(12)
  171. .placeholderFont({ size: 12 })
  172. .placeholderColor('#14000000')
  173. .height(36)
  174. .width('100%')
  175. .backgroundColor(Color.White)
  176. .border({ width: 1, color: '#14000000' })
  177. .margin({ bottom: 25 })
  178. .onChange((value) => {
  179. if (value.length > 7) {
  180. this.value = value.slice(0, 7)
  181. IBestToast.show({ type: "warning", message: '用户名称最大为7位' })
  182. }
  183. })
  184. Row({ space: 32 }) {
  185. YTButton({
  186. btContent: '取消',
  187. btHeight: 25,
  188. btWidth: 78,
  189. btBorder: { width: 1, color: '#1A000000' },
  190. btPadding: {
  191. left: 26,
  192. right: 26,
  193. top: 4,
  194. bottom: 4
  195. },
  196. bgc: Color.White,
  197. btFontColor: $r('sys.color.mask_secondary'),
  198. click: () => {
  199. this.showReviseName = false
  200. this.value = ''
  201. }
  202. })
  203. YTButton({
  204. btContent: '完成',
  205. btHeight: 25,
  206. btWidth: 78,
  207. btPadding: {
  208. left: 26,
  209. right: 26,
  210. top: 4,
  211. bottom: 4
  212. },
  213. click: () => {
  214. if (!this.value) {
  215. IBestToast.show({
  216. type: "warning",
  217. message: "昵称不能为空"
  218. })
  219. return
  220. }
  221. YTUserRequest.changeNickname(this.value, () => {
  222. IBestToast.show({ message: '名称修改成功' })
  223. this.showReviseName = false
  224. })
  225. }
  226. })
  227. }
  228. .justifyContent(FlexAlign.Center)
  229. .width('100%')
  230. }
  231. .padding({ left: 22, right: 22, top: 8 })
  232. }
  233. @Builder
  234. changeBuilder() {
  235. reviseImgHeaderBuilder(this.options)
  236. }
  237. }