index.vue 9.1 KB

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