SettingPage.ets 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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(getContext(), (fullpath) => {
  64. this.showHeaderImgRevise = false
  65. IBestToast.showLoading({ message: '上传中...' })
  66. yTRequest.uploadHeadImg(getContext(), fullpath, () => {
  67. yTRequest.refreshUserInfo(() => {
  68. IBestToast.hide()
  69. setTimeout(() => {
  70. IBestToast.show({ message: '头像修改成功' })
  71. }, 100)
  72. })
  73. })
  74. })
  75. }
  76. },
  77. {
  78. text: '取消',
  79. click: () => {
  80. this.showHeaderImgRevise = false
  81. }
  82. },
  83. ]
  84. build() {
  85. Column() {
  86. YTHeader({ title: '用户设置' })
  87. Column() {
  88. ForEach(this.reviseBuilderArr, (item: BasicType<undefined>, index) => {
  89. this.reviseBuilder(item)
  90. if (index < this.reviseBuilderArr.length - 1) {
  91. Column() {
  92. Text('')
  93. .height(1)
  94. .width('100%')
  95. .backgroundColor('#0A000000')
  96. }
  97. .padding({ left: 12, right: 8 })
  98. .bindSheet($$this.showHeaderImgRevise, this.changeBuilder, {
  99. height: 204,
  100. backgroundColor: Color.White,
  101. showClose: false
  102. })
  103. }
  104. })
  105. YtButton({
  106. btContent: '退出登录', click: () => {
  107. this.userInfo.logout()
  108. yTRouter.routerBack()
  109. IBestToast.show({ message: '退出登录成功' })
  110. }
  111. })
  112. .margin({ top: 426, bottom: 12 })
  113. YtButton({
  114. btContent: '注销用户',
  115. btFontColor: Color.Red,
  116. bgc: Color.White,
  117. click: () => {
  118. yTToast.doubleConfirm({
  119. text: '警告⚠', message: '确定要注销吗?\n注销后数据可能丢失无法恢复!', click: () => {
  120. userInfo.logout()
  121. yTToast.hide()
  122. yTRouter.pop()
  123. }
  124. })
  125. }
  126. })
  127. }
  128. .bindSheet($$this.showReviseName, this.reviseNameBuilder, {
  129. height: 275,
  130. showClose: false,
  131. backgroundColor: Color.White
  132. })
  133. .padding({
  134. top: 29,
  135. left: 16,
  136. right: 16,
  137. })
  138. }
  139. .padding({ top: this.safeTop, bottom: this.safeBottom })
  140. }
  141. @Builder
  142. reviseBuilder(item: BasicType<undefined>) {
  143. Row() {
  144. Text(item.text)
  145. .fontSize($r('[basic].float.page_text_font_size_12'))
  146. .fontColor('#80000000')
  147. Row() {
  148. if (typeof item.src == 'string') {
  149. Image(this.userInfo.getHeadImg() ? this.userInfo.getHeadImg() : $r('app.media.default_img'))
  150. .height(24)
  151. .width(24)
  152. .borderRadius(12)
  153. } else {
  154. Text(this.userInfo.getName() ?? this.userInfo.getPhoneNumber() ?? this.userInfo.getId()?.toString())
  155. .fontSize($r('[basic].float.page_text_font_size_12'))
  156. .fontColor('#CC000000')
  157. }
  158. Image($r('app.media.right_arrow'))
  159. .width(24)
  160. .width(24)
  161. }
  162. }
  163. .width('100%')
  164. .justifyContent(FlexAlign.SpaceBetween)
  165. .padding({ left: 12, right: 4 })
  166. .height(36)
  167. .onClick(item.click)
  168. }
  169. @Builder
  170. reviseNameBuilder() {
  171. Column() {
  172. Text('昵称')
  173. .width('100%')
  174. .height(36)
  175. .textAlign(TextAlign.Start)
  176. .fontSize(12)
  177. TextInput({ text: $$this.value, placeholder: '请输入昵称' })
  178. .borderRadius(4)
  179. .fontSize(12)
  180. .placeholderFont({ size: 12 })
  181. .placeholderColor('#14000000')
  182. .height(36)
  183. .width('100%')
  184. .backgroundColor(Color.White)
  185. .border({ width: 1, color: '#14000000' })
  186. .margin({ bottom: 25 })
  187. Row({ space: 32 }) {
  188. YtButton({
  189. btContent: '取消',
  190. btHeight: 25,
  191. btWidth: 78,
  192. btBorder: { width: 1, color: '#1A000000' },
  193. btPadding: {
  194. left: 26,
  195. right: 26,
  196. top: 4,
  197. bottom: 4
  198. },
  199. bgc: Color.White,
  200. btFontColor: $r('sys.color.mask_secondary'),
  201. click: () => {
  202. this.showReviseName = false
  203. this.value = ''
  204. }
  205. })
  206. YtButton({
  207. btContent: '完成',
  208. btHeight: 25,
  209. btWidth: 78,
  210. btPadding: {
  211. left: 26,
  212. right: 26,
  213. top: 4,
  214. bottom: 4
  215. },
  216. click: () => {
  217. if (!this.value) {
  218. IBestToast.show({
  219. type: "warning",
  220. message: "昵称不能为空"
  221. })
  222. return
  223. }
  224. yTRequest.changeNickname(this.value, () => {
  225. IBestToast.show({ message: '名称修改成功' })
  226. })
  227. //TODO 发送请求后关闭弹窗
  228. this.showReviseName = false
  229. }
  230. })
  231. }
  232. .justifyContent(FlexAlign.Center)
  233. .width('100%')
  234. }
  235. .padding({ left: 22, right: 22, top: 8 })
  236. }
  237. @Builder
  238. changeBuilder() {
  239. reviseImgHeaderBuilder(this.options)
  240. }
  241. }