SettingPage.ets 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {
  2. BasicType,
  3. IBestToast,
  4. reviseImgHeaderBuilder,
  5. takePicture,
  6. Upload,
  7. UserInfo,
  8. YTAvoid,
  9. YtButton,
  10. YTHeader,
  11. YTLog,
  12. YTRequest,
  13. YTRouter,
  14. YTToast
  15. } from 'basic'
  16. @Builder
  17. function settingBuilder() {
  18. NavDestination() {
  19. SettingPage()
  20. }
  21. .hideTitleBar(true)
  22. }
  23. @Component
  24. struct SettingPage {
  25. @StorageProp(YTAvoid.SAFE_TOP_KEY) safeTop: number = 0
  26. @StorageProp(YTAvoid.SAFE_BOTTOM_KEY) safeBottom: number = 0
  27. @StorageProp(UserInfo.KEY) userInfo: UserInfo = UserInfo.getInstance()
  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()
  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: 'image' })
  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.getInstance().doubleConfirm({
  110. text: '警告⚠', message: '确定要注销吗?\n注销后数据可能丢失无法恢复!', click: () => {
  111. UserInfo.getInstance().logout()
  112. YTToast.getInstance().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. top: 29,
  126. left: 16,
  127. right: 16,
  128. })
  129. }
  130. .padding({ top: this.safeTop, 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. YTRequest.changeNickname(this.value, () => {
  222. IBestToast.show({ message: '名称修改成功' })
  223. })
  224. //TODO 发送请求后关闭弹窗
  225. this.showReviseName = false
  226. }
  227. })
  228. }
  229. .justifyContent(FlexAlign.Center)
  230. .width('100%')
  231. }
  232. .padding({ left: 22, right: 22, top: 8 })
  233. }
  234. @Builder
  235. changeBuilder() {
  236. reviseImgHeaderBuilder(this.options)
  237. }
  238. }