profile.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="pt-60 pb-20 flex-center">
  3. <button
  4. class="avatar"
  5. open-type="chooseAvatar"
  6. @chooseavatar="chooseAvatar"
  7. >
  8. <image class="avatar_image" :src="avatar" @error="errorHandle"></image>
  9. <view class="avatar_text flex-center">编辑</view>
  10. </button>
  11. </view>
  12. <view class="pl-50 pr-50">
  13. <view
  14. class="menu flex-align-center flex-between"
  15. v-for="(item, index) in menu"
  16. :key="index"
  17. @click="edit(index)"
  18. >
  19. <view class="fs-30">{{ item.title }}</view>
  20. <view class="flex">
  21. <view
  22. :class="['fs-30', 'fw-500', `mr-${item.disabled ? '0' : '20'}`]"
  23. style="color: rgba(0, 0, 0, 0.8)"
  24. >{{ item.value }}</view
  25. >
  26. <uni-icons
  27. type="right"
  28. size="12"
  29. color="rgba(0,0,0,0.4)"
  30. v-if="!item.disabled"
  31. ></uni-icons>
  32. </view>
  33. </view>
  34. </view>
  35. <style-bottom-view>
  36. <view class="pl-60 pr-60 pb-40">
  37. <style-button type="primary" @click="logoutUser">退出登录</style-button>
  38. </view>
  39. </style-bottom-view>
  40. </template>
  41. <script setup lang="ts">
  42. import { clearToken } from "../../api/auth";
  43. import { fetchProfile, updateProfile, logout } from "../../api/user";
  44. import { upload } from "../../utils/uploader";
  45. import { onLoad, onShow } from "@dcloudio/uni-app";
  46. import { ref } from "vue";
  47. const avatar = ref<string>();
  48. const menu = ref<any[]>([]);
  49. const refresh = () => {
  50. const _menu = [
  51. {
  52. title: "昵称",
  53. key: "nickname",
  54. value: "",
  55. },
  56. {
  57. title: "电话",
  58. key: "",
  59. disabled: true,
  60. },
  61. {
  62. title: "车牌号",
  63. key: "defaultPlateNo",
  64. value: "",
  65. },
  66. {
  67. title: "VIN码",
  68. key: "vin",
  69. value: "",
  70. },
  71. // {
  72. // title: "充电卡",
  73. // key: "",
  74. // value: "",
  75. // },
  76. ];
  77. fetchProfile().then(() => {
  78. const user = getApp<any>().globalData.user;
  79. if (user) {
  80. _menu[0].value = user.nickname;
  81. _menu[1].value = user.mobilePhone;
  82. _menu[2].value = user.defaultPlateNo;
  83. _menu[3].value = user.vin;
  84. // _menu[4].value = user.card_no;
  85. avatar.value =
  86. user.avatar ||
  87. "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
  88. menu.value = _menu;
  89. }
  90. });
  91. };
  92. const save = (form: Record<string, any>) => {
  93. uni.showLoading({
  94. title: "保存中",
  95. });
  96. return updateProfile(form)
  97. .then((res) => {
  98. uni.hideLoading();
  99. uni.showToast({
  100. icon: "success",
  101. title: "保存成功",
  102. });
  103. refresh();
  104. return res;
  105. })
  106. .catch((err) => {
  107. uni.hideLoading();
  108. uni.showModal({
  109. content: `${err.errMsg},请重试`,
  110. });
  111. });
  112. };
  113. const chooseAvatar = (e: any) => {
  114. if (e.detail.avatarUrl) {
  115. uni.showLoading({
  116. title: "上传中",
  117. });
  118. upload(e.detail.avatarUrl, {
  119. onSuccess: (res) => {
  120. updateProfile({
  121. avatar: res.url,
  122. })
  123. .then(() => {
  124. uni.hideLoading();
  125. uni.showToast({
  126. title: '已更新',
  127. icon: 'success'
  128. });
  129. avatar.value = res.url;
  130. })
  131. .catch((err) => {
  132. uni.hideLoading();
  133. uni.showModal({
  134. content: `${err.errMsg},请重试`,
  135. });
  136. });
  137. },
  138. onFail: (err) => {
  139. uni.hideLoading();
  140. uni.showModal({
  141. content: `${err.errMsg},请重试`,
  142. });
  143. },
  144. });
  145. } else {
  146. uni.showModal({
  147. content: `${e.detail.errMsg},请重试`,
  148. });
  149. }
  150. };
  151. const edit = (index: number) => {
  152. const menuItem = menu.value[index];
  153. if (menuItem.disabled) {
  154. return;
  155. }
  156. if (!menuItem.key) {
  157. uni.showToast({
  158. icon: "none",
  159. title: "暂不支持修改",
  160. });
  161. return;
  162. }
  163. if (/车牌/.test(menuItem.title)) {
  164. uni.chooseLicensePlate({
  165. success: (res) => {
  166. save({
  167. defaultPlateNo: res.plateNumber,
  168. });
  169. },
  170. fail: (err) => {
  171. console.log(err);
  172. },
  173. });
  174. return;
  175. }
  176. uni.navigateTo({
  177. url: `/pages-user/profile-edit/profile-edit?key=${menuItem.key}&title=${
  178. menuItem.title
  179. }${menuItem.value ? `&value=${encodeURIComponent(menuItem.value)}` : ""}`,
  180. });
  181. };
  182. const logoutUser = () => {
  183. uni.showModal({
  184. title: "温馨提示",
  185. content: "确定退出登录吗?",
  186. confirmColor: "#347DFF",
  187. confirmText: "确定退出",
  188. cancelText: "手滑了",
  189. success: (res) => {
  190. if (res.confirm) {
  191. uni.showLoading({
  192. title: "退出中",
  193. });
  194. logout()
  195. .then(() => {
  196. uni.hideLoading();
  197. uni.showToast({
  198. icon: "success",
  199. title: "已退出",
  200. });
  201. clearToken();
  202. setTimeout(() => {
  203. uni.reLaunch({
  204. url: "/pages/map/map",
  205. });
  206. }, 1500);
  207. })
  208. .catch((err) => {
  209. uni.hideLoading();
  210. uni.showModal({
  211. content: `${err.errMsg},请重试`,
  212. });
  213. });
  214. }
  215. },
  216. });
  217. };
  218. const errorHandle = (e: any) => {
  219. console.log(e);
  220. };
  221. onLoad(() => {
  222. refresh();
  223. });
  224. onShow(() => {
  225. if (getApp<any>().globalData.lastData.profile) {
  226. const { key, value } = getApp<any>().globalData.lastData.profile;
  227. save({
  228. [key]: value,
  229. }).then(() => {
  230. getApp<any>().globalData.lastData.profile = undefined;
  231. });
  232. }
  233. });
  234. </script>
  235. <style lang="scss">
  236. .avatar {
  237. position: relative;
  238. height: 116rpx !important;
  239. width: 116rpx !important;
  240. border-radius: 50%;
  241. border: 2rpx solid rgba(0, 0, 0, 0.15);
  242. overflow: hidden;
  243. &_image {
  244. position: absolute;
  245. width: 100%;
  246. height: 100%;
  247. left: 0;
  248. top: 0;
  249. border-radius: 50%;
  250. }
  251. &_text {
  252. position: absolute;
  253. bottom: 0;
  254. left: 0;
  255. width: 100%;
  256. height: 40rpx;
  257. background: rgba(0, 0, 0, 0.5);
  258. color: #fff;
  259. font-size: 24rpx;
  260. }
  261. }
  262. .menu {
  263. background-color: #fff;
  264. height: 120rpx;
  265. border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
  266. &:last-child {
  267. border-bottom: none;
  268. }
  269. }
  270. </style>