profile.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 MENU_TEMPLATE = [
  50. {
  51. title: "昵称",
  52. key: "nickname",
  53. value: "",
  54. },
  55. {
  56. title: "电话",
  57. key: "",
  58. disabled: true,
  59. },
  60. {
  61. title: "车牌号",
  62. key: "defaultPlateNo",
  63. value: "",
  64. },
  65. {
  66. title: "VIN码",
  67. key: "vin",
  68. value: "",
  69. },
  70. // {
  71. // title: "充电卡",
  72. // key: "",
  73. // value: "",
  74. // },
  75. ];
  76. const refresh = () => {
  77. const _menu = [...MENU_TEMPLATE];
  78. fetchProfile().then(() => {
  79. const user = getApp<any>().globalData.user;
  80. if (user) {
  81. _menu[0].value = user.nickname;
  82. _menu[1].value = user.mobilePhone;
  83. _menu[2].value = user.defaultPlateNo;
  84. _menu[3].value = user.vin;
  85. // _menu[4].value = user.card_no;
  86. avatar.value =
  87. user.avatar ||
  88. "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
  89. menu.value = _menu;
  90. }
  91. });
  92. };
  93. const save = (form: Record<string, any>) => {
  94. uni.showLoading({
  95. title: "保存中",
  96. });
  97. return updateProfile(form)
  98. .then((res) => {
  99. uni.hideLoading();
  100. uni.showToast({
  101. icon: "success",
  102. title: "保存成功",
  103. });
  104. refresh();
  105. return res;
  106. })
  107. .catch((err) => {
  108. uni.hideLoading();
  109. uni.showModal({
  110. content: `${err.errMsg},请重试`,
  111. });
  112. });
  113. };
  114. const chooseAvatar = (e: any) => {
  115. if (e.detail.avatarUrl) {
  116. uni.showLoading({
  117. title: "上传中",
  118. });
  119. upload(e.detail.avatarUrl, {
  120. onSuccess: (res) => {
  121. updateProfile({
  122. avatar: res.url,
  123. })
  124. .then(() => {
  125. uni.hideLoading();
  126. uni.showToast({
  127. title: "已更新",
  128. icon: "success",
  129. });
  130. avatar.value = res.url;
  131. })
  132. .catch((err) => {
  133. uni.hideLoading();
  134. uni.showModal({
  135. content: `${err.errMsg},请重试`,
  136. });
  137. });
  138. },
  139. onFail: (err) => {
  140. uni.hideLoading();
  141. uni.showModal({
  142. content: `${err.errMsg},请重试`,
  143. });
  144. },
  145. });
  146. } else {
  147. uni.showModal({
  148. content: `${e.detail.errMsg},请重试`,
  149. });
  150. }
  151. };
  152. const edit = (index: number) => {
  153. const menuItem = menu.value[index];
  154. if (menuItem.disabled) {
  155. return;
  156. }
  157. if (!menuItem.key) {
  158. uni.showToast({
  159. icon: "none",
  160. title: "暂不支持修改",
  161. });
  162. return;
  163. }
  164. if (/车牌/.test(menuItem.title)) {
  165. uni.chooseLicensePlate({
  166. success: (res) => {
  167. save({
  168. defaultPlateNo: res.plateNumber,
  169. });
  170. },
  171. fail: (err) => {
  172. console.log(err);
  173. },
  174. });
  175. return;
  176. }
  177. uni.navigateTo({
  178. url: `/pages-user/profile-edit/profile-edit?key=${menuItem.key}&title=${
  179. menuItem.title
  180. }${menuItem.value ? `&value=${encodeURIComponent(menuItem.value)}` : ""}`,
  181. });
  182. };
  183. const logoutUser = () => {
  184. uni.showModal({
  185. title: "温馨提示",
  186. content: "确定退出登录吗?",
  187. confirmColor: "#2d9e95",
  188. confirmText: "确定退出",
  189. cancelText: "手滑了",
  190. success: (res) => {
  191. if (res.confirm) {
  192. uni.showLoading({
  193. title: "退出中",
  194. });
  195. logout()
  196. .then(() => {
  197. uni.hideLoading();
  198. uni.showToast({
  199. icon: "success",
  200. title: "已退出",
  201. });
  202. clearToken();
  203. setTimeout(() => {
  204. uni.reLaunch({
  205. url: "/pages/map/map",
  206. });
  207. }, 1500);
  208. })
  209. .catch((err) => {
  210. uni.hideLoading();
  211. uni.showModal({
  212. content: `${err.errMsg},请重试`,
  213. });
  214. });
  215. }
  216. },
  217. });
  218. };
  219. const errorHandle = (e: any) => {
  220. console.log(e);
  221. };
  222. onLoad(() => {
  223. if (getApp<any>().globalData.user) {
  224. const user = getApp<any>().globalData.user;
  225. const _menu = [...MENU_TEMPLATE];
  226. _menu[0].value = user.nickname;
  227. _menu[1].value = user.mobilePhone;
  228. _menu[2].value = user.defaultPlateNo;
  229. _menu[3].value = user.vin;
  230. // _menu[4].value = user.card_no;
  231. avatar.value =
  232. user.avatar ||
  233. "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
  234. menu.value = _menu;
  235. }
  236. });
  237. onShow(() => {
  238. if (getApp<any>().globalData.lastData.profile) {
  239. const { key, value } = getApp<any>().globalData.lastData.profile;
  240. save({
  241. [key]: value,
  242. }).then(() => {
  243. getApp<any>().globalData.lastData.profile = undefined;
  244. });
  245. }
  246. });
  247. </script>
  248. <style lang="scss">
  249. .avatar {
  250. position: relative;
  251. height: 116rpx !important;
  252. width: 116rpx !important;
  253. border-radius: 50%;
  254. border: 2rpx solid rgba(0, 0, 0, 0.15);
  255. overflow: hidden;
  256. &_image {
  257. position: absolute;
  258. width: 100%;
  259. height: 100%;
  260. left: 0;
  261. top: 0;
  262. border-radius: 50%;
  263. }
  264. &_text {
  265. position: absolute;
  266. bottom: 0;
  267. left: 0;
  268. width: 100%;
  269. height: 40rpx;
  270. background: rgba(0, 0, 0, 0.5);
  271. color: #fff;
  272. font-size: 24rpx;
  273. }
  274. }
  275. .menu {
  276. background-color: #fff;
  277. height: 120rpx;
  278. border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
  279. &:last-child {
  280. border-bottom: none;
  281. }
  282. }
  283. </style>