index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <route lang="json5">
  2. {
  3. layout: 'tabbar',
  4. style: {
  5. navigationBarTitleText: '我的',
  6. },
  7. }
  8. </route>
  9. <template>
  10. <view class="profile-container">
  11. {{ JSON.stringify(userStore.userInfo) }}
  12. <!-- 用户信息区域 -->
  13. <view class="user-info-section">
  14. <!-- #ifdef MP-WEIXIN -->
  15. <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  16. <wd-img :src="userStore.userInfo.avatar" width="80px" height="80px" radius="50%"></wd-img>
  17. </button>
  18. <!-- #endif -->
  19. <!-- #ifndef MP-WEIXIN -->
  20. <view class="avatar-wrapper" @click="run">
  21. <wd-img :src="userStore.userInfo.avatar" width="100%" height="100%" radius="50%"></wd-img>
  22. </view>
  23. <!-- #endif -->
  24. <view class="user-details">
  25. <!-- #ifdef MP-WEIXIN -->
  26. <input
  27. type="nickname"
  28. class="weui-input"
  29. placeholder="请输入昵称"
  30. v-model="userStore.userInfo.username"
  31. />
  32. <!-- #endif -->
  33. <!-- #ifndef MP-WEIXIN -->
  34. <view class="username">{{ userStore.userInfo.username }}</view>
  35. <!-- #endif -->
  36. <view class="user-id">ID: {{ userStore.userInfo.id }}</view>
  37. </view>
  38. </view>
  39. <!-- 功能区块 -->
  40. <view class="function-section">
  41. <view class="cell-group">
  42. <view class="group-title">账号管理</view>
  43. <wd-cell title="个人资料" is-link @click="handleProfileInfo">
  44. <template #icon>
  45. <wd-icon name="user" size="20px"></wd-icon>
  46. </template>
  47. </wd-cell>
  48. <wd-cell title="账号安全" is-link @click="handlePassword">
  49. <template #icon>
  50. <wd-icon name="lock-on" size="20px"></wd-icon>
  51. </template>
  52. </wd-cell>
  53. </view>
  54. <view class="cell-group">
  55. <view class="group-title">通用设置</view>
  56. <wd-cell title="消息通知" is-link @click="handleInform">
  57. <template #icon>
  58. <wd-icon name="notification" size="20px"></wd-icon>
  59. </template>
  60. </wd-cell>
  61. <wd-cell title="清理缓存" is-link @click="handleClearCache">
  62. <template #icon>
  63. <wd-icon name="clear" size="20px"></wd-icon>
  64. </template>
  65. </wd-cell>
  66. <wd-cell title="应用更新" is-link @click="handleAppUpdate">
  67. <template #icon>
  68. <wd-icon name="refresh1" size="20px"></wd-icon>
  69. </template>
  70. </wd-cell>
  71. <wd-cell title="关于我们" is-link @click="handleAbout">
  72. <template #icon>
  73. <wd-icon name="info-circle" size="20px"></wd-icon>
  74. </template>
  75. </wd-cell>
  76. </view>
  77. <view class="logout-button-wrapper">
  78. <wd-button type="error" v-if="hasLogin" block @click="handleLogout">退出登录</wd-button>
  79. <wd-button type="primary" v-else block @click="handleLogin">登录</wd-button>
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <script lang="ts" setup>
  85. import { useUserStore } from '@/store'
  86. import { useToast } from 'wot-design-uni'
  87. import { uploadFileUrl, useUpload } from '@/utils/uploadFile'
  88. import { storeToRefs } from 'pinia'
  89. import { IUploadSuccessInfo } from '@/api/login.typings'
  90. const userStore = useUserStore()
  91. const toast = useToast()
  92. const hasLogin = ref(false)
  93. onShow((options) => {
  94. hasLogin.value = !!uni.getStorageSync('token')
  95. console.log('个人中心onShow', hasLogin.value, options)
  96. hasLogin.value && useUserStore().getUserInfo()
  97. })
  98. // #ifndef MP-WEIXIN
  99. // 上传头像
  100. const { run } = useUpload<IUploadSuccessInfo>(
  101. uploadFileUrl.USER_AVATAR,
  102. {},
  103. {
  104. onSuccess: (res) => useUserStore().getUserInfo(),
  105. },
  106. )
  107. // #endif
  108. // 微信小程序下登录
  109. const handleLogin = async () => {
  110. // #ifdef MP-WEIXIN
  111. // 微信登录
  112. await userStore.wxLogin()
  113. hasLogin.value = true
  114. // #endif
  115. // #ifndef MP-WEIXIN
  116. uni.navigateTo({ url: '/pages/login/index' })
  117. // #endif
  118. }
  119. // #ifdef MP-WEIXIN
  120. // 微信小程序下选择头像事件
  121. const onChooseAvatar = (e: any) => {
  122. console.log('选择头像', e.detail)
  123. const { avatarUrl } = e.detail
  124. const { run } = useUpload<IUploadSuccessInfo>(
  125. uploadFileUrl.USER_AVATAR,
  126. {},
  127. {
  128. onSuccess: (res) => useUserStore().getUserInfo(),
  129. },
  130. avatarUrl,
  131. )
  132. run()
  133. }
  134. // #endif
  135. // #ifdef MP-WEIXIN
  136. // 微信小程序下设置用户名
  137. const getUserInfo = (e: any) => {
  138. console.log(e.detail)
  139. }
  140. // #endif
  141. // 个人资料
  142. const handleProfileInfo = () => {
  143. uni.navigateTo({ url: `/pages/mine/info/index` })
  144. }
  145. // 账号安全
  146. const handlePassword = () => {
  147. uni.navigateTo({ url: `/pages/mine/password/index` })
  148. }
  149. // 消息通知
  150. const handleInform = () => {
  151. // uni.navigateTo({ url: `/pages/mine/inform/index` })
  152. toast.success('功能开发中')
  153. }
  154. // 应用更新
  155. const handleAppUpdate = () => {
  156. // #ifdef MP
  157. // #ifndef MP-HARMONY
  158. const updateManager = uni.getUpdateManager()
  159. updateManager.onCheckForUpdate(function (res) {
  160. // 请求完新版本信息的回调
  161. // console.log(res.hasUpdate)
  162. if (res.hasUpdate) {
  163. toast.success('检测到新版本,正在下载中...')
  164. } else {
  165. toast.success('已是最新版本')
  166. }
  167. })
  168. updateManager.onUpdateReady(function (res) {
  169. uni.showModal({
  170. title: '更新提示',
  171. content: '新版本已经准备好,是否重启应用?',
  172. success(res) {
  173. if (res.confirm) {
  174. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  175. updateManager.applyUpdate()
  176. }
  177. },
  178. })
  179. })
  180. updateManager.onUpdateFailed(function (res) {
  181. // 新的版本下载失败
  182. toast.error('新版本下载失败')
  183. })
  184. // #endif
  185. // #endif
  186. // #ifndef MP
  187. toast.success('功能开发中')
  188. // #endif
  189. }
  190. // 关于我们
  191. const handleAbout = () => {
  192. uni.navigateTo({ url: `/pages/mine/about/index` })
  193. }
  194. // 清除缓存
  195. const handleClearCache = () => {
  196. uni.showModal({
  197. title: '清除缓存',
  198. content: '确定要清除所有缓存吗?\n清除后需要重新登录',
  199. success: (res) => {
  200. if (res.confirm) {
  201. try {
  202. // 清除所有缓存
  203. uni.clearStorageSync()
  204. // 清除用户信息并跳转到登录页
  205. useUserStore().logout()
  206. toast.success('清除缓存成功')
  207. } catch (err) {
  208. console.error('清除缓存失败:', err)
  209. toast.error('清除缓存失败')
  210. }
  211. }
  212. },
  213. })
  214. }
  215. // 退出登录
  216. const handleLogout = () => {
  217. uni.showModal({
  218. title: '提示',
  219. content: '确定要退出登录吗?',
  220. success: (res) => {
  221. if (res.confirm) {
  222. // 清空用户信息
  223. useUserStore().logout()
  224. hasLogin.value = false
  225. // 执行退出登录逻辑
  226. toast.success('退出登录成功')
  227. // #ifdef MP-WEIXIN
  228. // 微信小程序,去首页
  229. // uni.reLaunch({ url: '/pages/index/index' })
  230. // #endif
  231. // #ifndef MP-WEIXIN
  232. // 非微信小程序,去登录页
  233. // uni.reLaunch({ url: '/pages/login/index' })
  234. // #endif
  235. }
  236. },
  237. })
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. /* 基础样式 */
  242. .profile-container {
  243. overflow: hidden;
  244. font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
  245. background-color: #f7f8fa;
  246. }
  247. /* 用户信息区域 */
  248. .user-info-section {
  249. display: flex;
  250. align-items: center;
  251. padding: 40rpx;
  252. margin: 30rpx 30rpx 20rpx;
  253. background-color: #fff;
  254. border-radius: 24rpx;
  255. box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.08);
  256. transition: all 0.3s ease;
  257. }
  258. .avatar-wrapper {
  259. width: 160rpx;
  260. height: 160rpx;
  261. margin-right: 40rpx;
  262. overflow: hidden;
  263. border: 4rpx solid #f5f5f5;
  264. border-radius: 50%;
  265. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  266. }
  267. .avatar-button {
  268. height: 160rpx;
  269. padding: 0;
  270. margin-right: 40rpx;
  271. overflow: hidden;
  272. border: 4rpx solid #f5f5f5;
  273. border-radius: 50%;
  274. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  275. }
  276. .user-details {
  277. flex: 1;
  278. }
  279. .username {
  280. margin-bottom: 12rpx;
  281. font-size: 38rpx;
  282. font-weight: 600;
  283. color: #333;
  284. letter-spacing: 0.5rpx;
  285. }
  286. .user-id {
  287. font-size: 28rpx;
  288. color: #666;
  289. }
  290. .user-created {
  291. margin-top: 8rpx;
  292. font-size: 24rpx;
  293. color: #999;
  294. }
  295. /* 功能区块 */
  296. .function-section {
  297. padding: 0 20rpx;
  298. margin-top: 20rpx;
  299. }
  300. .cell-group {
  301. margin-bottom: 20rpx;
  302. overflow: hidden;
  303. background-color: #fff;
  304. border-radius: 16rpx;
  305. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  306. }
  307. .group-title {
  308. padding: 24rpx 30rpx 16rpx;
  309. font-size: 30rpx;
  310. font-weight: 500;
  311. color: #999;
  312. background-color: #fafafa;
  313. }
  314. :deep(.wd-cell) {
  315. border-bottom: 1rpx solid #f5f5f5;
  316. &:last-child {
  317. border-bottom: none;
  318. }
  319. .wd-cell__title {
  320. margin-left: 5px;
  321. font-size: 32rpx;
  322. color: #333;
  323. }
  324. .cell-icon {
  325. margin-right: 20rpx;
  326. font-size: 36rpx;
  327. }
  328. }
  329. /* 退出登录按钮 */
  330. .logout-button-wrapper {
  331. padding: 40rpx 30rpx;
  332. }
  333. :deep(.wd-button--danger) {
  334. height: 88rpx;
  335. font-size: 32rpx;
  336. line-height: 88rpx;
  337. color: #fff;
  338. background-color: #f53f3f;
  339. border-radius: 44rpx;
  340. }
  341. </style>