index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <route lang="json5" type="page">
  2. {
  3. layout: 'default',
  4. style: {
  5. navigationBarTitleText: '我的',
  6. },
  7. }
  8. </route>
  9. <template>
  10. <view class="main overflow-hidden pt-2 px4" :style="{ marginTop: safeAreaInsets?.top + 'px' }">
  11. <view class="head">
  12. <!-- <wd-upload
  13. accept="image"
  14. :file-list="fileList"
  15. :max-count="1"
  16. :upload-method="uploadImage"
  17. @change="handleChange"
  18. >
  19. <wd-img
  20. v-if="userInfo.avatar"
  21. custom-class="img-btn"
  22. :width="40"
  23. :height="40"
  24. :src="userInfo.avatar"
  25. />
  26. <wd-button v-else custom-class="gray-btn" type="icon" icon="photo"></wd-button>
  27. </wd-upload> -->
  28. <wd-img
  29. v-if="userInfo.avatar"
  30. custom-class="img-btn"
  31. :width="40"
  32. :height="40"
  33. :src="userInfo.avatar"
  34. />
  35. <view class="head-name">{{ userInfo.name }}</view>
  36. </view>
  37. <view class="main-content mt6">
  38. <view class="content-item line-b" @click="toAddUserHub">
  39. <view>添加打印助手</view>
  40. <wd-icon name="arrow-right" size="20px" color="#808080"></wd-icon>
  41. </view>
  42. <view class="content-item" @click="handleLogout">
  43. <view>退出登录</view>
  44. <wd-icon name="arrow-right" size="20px" color="#808080"></wd-icon>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script lang="ts" setup>
  50. import { getEnvBaseUrl } from '@/utils'
  51. import { logout } from '@/service/api/index'
  52. import { useUserStore } from '@/store'
  53. import { useToast } from 'wot-design-uni'
  54. import type { UploadMethod, UploadFile } from 'wot-design-uni/components/wd-upload/types'
  55. // 请求基准地址
  56. const baseUrl = getEnvBaseUrl()
  57. const toast = useToast()
  58. // 获取屏幕边界到安全区域距离
  59. const { safeAreaInsets } = uni.getSystemInfoSync()
  60. const userInfo = reactive({
  61. avatar: '',
  62. name: '',
  63. })
  64. const fileList = ref([])
  65. const uploadImage: UploadMethod = (file, formData, options) => {
  66. uni.uploadFile({
  67. url: `${baseUrl}/sys/user/avatar`,
  68. filePath: file.url,
  69. name: 'file', // 这里根据后端需要的字段来定义
  70. fileType: options.fileType,
  71. formData, // 如果需要额外的 formData
  72. success: async (res) => {
  73. if (res.statusCode === options.statusCode) {
  74. toast.success('头像上传成功')
  75. await useUserStore().initUserInfo()
  76. initFn()
  77. } else {
  78. const dataObj: any = JSON.parse(res.data)
  79. toast.error({ msg: dataObj.message || '头像上传失败', duration: 4000 })
  80. }
  81. },
  82. fail: (error) => {
  83. console.error('上传失败', error)
  84. },
  85. })
  86. }
  87. function handleChange({ fileList }) {
  88. fileList.value = fileList
  89. }
  90. function toAddUserHub() {
  91. uni.navigateTo({
  92. url: `/pages/assistant/detail?type=add`,
  93. })
  94. }
  95. function handleLogout() {
  96. // logout().then((res) => {
  97. // if (res.code === 0) {
  98. // useUserStore().setToken('')
  99. // useUserStore().setAvatar('')
  100. // useUserStore().setUserInfo({})
  101. // toast.success('退出登录成功')
  102. // toLogin()
  103. // }
  104. // })
  105. useUserStore().clearUserInfo()
  106. toast.success('退出登录成功')
  107. uni.reLaunch({
  108. url: `/pages/login/index`
  109. })
  110. }
  111. function initFn() {
  112. // userInfo.name = useUserStore().userInfo.name
  113. // userInfo.avatar = useUserStore().userInfo.avatar
  114. userInfo.name = "微信用户"
  115. userInfo.avatar = "/static/images/avatar.png"
  116. }
  117. onLoad(() => {
  118. initFn()
  119. })
  120. </script>
  121. <style lang="scss" scoped>
  122. .head {
  123. display: flex;
  124. align-items: center;
  125. .head-name {
  126. @apply pl-3;
  127. }
  128. }
  129. .main-content {
  130. padding: 20rpx;
  131. background-color: white;
  132. border-radius: 16rpx;
  133. }
  134. .content-item {
  135. display: flex;
  136. align-items: start;
  137. justify-content: space-between;
  138. padding: 30rpx 0;
  139. }
  140. .line-b {
  141. border-bottom: 1px solid #eee;
  142. }
  143. :deep(.head .wd-upload__evoke) {
  144. width: 80rpx;
  145. height: 80rpx;
  146. margin-bottom: 0;
  147. border-radius: 100%;
  148. }
  149. :deep(.wd-upload) {
  150. display: flex;
  151. }
  152. :deep(.wd-upload__evoke-num) {
  153. display: none;
  154. }
  155. :deep(.wd-icon) {
  156. font-size: 50rpx;
  157. }
  158. :deep(.img-btn .wd-img__image) {
  159. width: 80rpx;
  160. height: 80rpx;
  161. border-radius: 40rpx;
  162. }
  163. :deep(.wd-upload__preview) {
  164. display: none !important;
  165. }
  166. :deep(.gray-btn.wd-button) {
  167. width: 80rpx;
  168. height: 80rpx;
  169. background-color: #ddd;
  170. border-radius: 40rpx;
  171. }
  172. :deep(.form-ipt) {
  173. padding-right: 0 !important;
  174. padding-left: 0 !important;
  175. }
  176. </style>