SettingPage.ets 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. } from 'basic'
  16. @Builder
  17. function settingBuilder() {
  18. NavDestination() {
  19. SettingPage()
  20. }
  21. .hideTitleBar(true)
  22. }
  23. @Component
  24. struct SettingPage {
  25. @StorageProp(YtAvoid.safeTopKey) safeTop: number = 0
  26. @StorageProp(YtAvoid.safeBottomKey) safeBottom: number = 0
  27. @StorageProp(userInfo.KEY) userInfo: UserInfo = new 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()
  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(getContext(), (fullpath) => {
  63. this.showHeaderImgRevise = false
  64. IBestToast.showLoading({ message: '上传中...' })
  65. yTRequest.uploadHeadImg(getContext(), fullpath, () => {
  66. yTRequest.refreshUserInfo(() => {
  67. IBestToast.hide()
  68. setTimeout(() => {
  69. IBestToast.show({ message: '头像修改成功' })
  70. }, 100)
  71. })
  72. })
  73. })
  74. }
  75. },
  76. {
  77. text: '取消',
  78. click: () => {
  79. this.showHeaderImgRevise = false
  80. }
  81. },
  82. ]
  83. build() {
  84. Column() {
  85. YTHeader({ title: '用户设置' })
  86. Column() {
  87. ForEach(this.reviseBuilderArr, (item: BasicType<undefined>, index) => {
  88. this.reviseBuilder(item)
  89. if (index < this.reviseBuilderArr.length - 1) {
  90. Column() {
  91. Text('')
  92. .height(1)
  93. .width('100%')
  94. .backgroundColor('#0A000000')
  95. }
  96. .padding({ left: 12, right: 8 })
  97. .bindSheet($$this.showHeaderImgRevise, this.changeBuilder, {
  98. height: 204,
  99. backgroundColor: Color.White,
  100. showClose: false
  101. })
  102. }
  103. })
  104. YtButton({
  105. btContent: '退出登录', click: () => {
  106. this.userInfo.logout()
  107. yTRouter.routerBack()
  108. IBestToast.show({ message: '退出登录成功' })
  109. }
  110. })
  111. .margin({ top: 426, bottom: 12 })
  112. YtButton({
  113. btContent: '注销用户',
  114. btFontColor: Color.Red,
  115. bgc: Color.White,
  116. click: () => {
  117. // yTToast.doubleConfirm({
  118. // text: '警告⚠', message: '确定要注销吗?\n注销后数据丢失无法恢复!', click: () => {
  119. // AlertDialog.show({ message: JSON.stringify('点击了确定', null, 2) })
  120. // }
  121. // })
  122. }
  123. })
  124. }
  125. .bindSheet($$this.showReviseName, this.reviseNameBuilder, {
  126. height: 275,
  127. showClose: false,
  128. backgroundColor: Color.White
  129. })
  130. .padding({
  131. top: 29,
  132. left: 16,
  133. right: 16,
  134. })
  135. }
  136. .padding({ top: this.safeTop, bottom: this.safeBottom })
  137. }
  138. @Builder
  139. reviseBuilder(item: BasicType<undefined>) {
  140. Row() {
  141. Text(item.text)
  142. .fontSize($r('[basic].float.page_text_font_size_12'))
  143. .fontColor('#80000000')
  144. Row() {
  145. if (typeof item.src == 'string') {
  146. Image(this.userInfo.getHeadImg() ? this.userInfo.getHeadImg() : $r('app.media.default_img'))
  147. .height(24)
  148. .width(24)
  149. .borderRadius(12)
  150. } else {
  151. Text(this.userInfo.getName() ?? this.userInfo.getPhoneNumber() ?? this.userInfo.getId()?.toString())
  152. .fontSize($r('[basic].float.page_text_font_size_12'))
  153. .fontColor('#CC000000')
  154. }
  155. Image($r('app.media.right_arrow'))
  156. .width(24)
  157. .width(24)
  158. }
  159. }
  160. .width('100%')
  161. .justifyContent(FlexAlign.SpaceBetween)
  162. .padding({ left: 12, right: 4 })
  163. .height(36)
  164. .onClick(item.click)
  165. }
  166. @Builder
  167. reviseNameBuilder() {
  168. Column() {
  169. Text('昵称')
  170. .width('100%')
  171. .height(36)
  172. .textAlign(TextAlign.Start)
  173. .fontSize(12)
  174. TextInput({ text: $$this.value, placeholder: '请输入昵称' })
  175. .borderRadius(4)
  176. .fontSize(12)
  177. .placeholderFont({ size: 12 })
  178. .placeholderColor('#14000000')
  179. .height(36)
  180. .width('100%')
  181. .backgroundColor(Color.White)
  182. .border({ width: 1, color: '#14000000' })
  183. .margin({ bottom: 25 })
  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. }