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: #1c2b29"
  24. >{{ item.value }}</view
  25. >
  26. <uni-icons
  27. type="right"
  28. size="12"
  29. color="rgba(28,43,41,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 { requireAuth } from "@/utils/auth";
  47. import { ref } from "vue";
  48. const avatar = ref<string>();
  49. const menu = ref<any[]>([]);
  50. const MENU_TEMPLATE = [
  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. const refresh = () => {
  78. const _menu = [...MENU_TEMPLATE];
  79. fetchProfile().then(() => {
  80. const user = getApp<any>().globalData.user;
  81. if (user) {
  82. _menu[0].value = user.nickname;
  83. _menu[1].value = user.mobilePhone;
  84. _menu[2].value = user.defaultPlateNo;
  85. _menu[3].value = user.vin;
  86. // _menu[4].value = user.card_no;
  87. avatar.value =
  88. user.avatar ||
  89. "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
  90. menu.value = _menu;
  91. }
  92. });
  93. };
  94. const save = (form: Record<string, any>) => {
  95. uni.showLoading({
  96. title: "保存中",
  97. });
  98. return updateProfile(form)
  99. .then((res) => {
  100. uni.hideLoading();
  101. uni.showToast({
  102. icon: "success",
  103. title: "保存成功",
  104. });
  105. refresh();
  106. return res;
  107. })
  108. .catch((err) => {
  109. uni.hideLoading();
  110. uni.showModal({
  111. content: `${err.errMsg},请重试`,
  112. });
  113. });
  114. };
  115. const chooseAvatar = (e: any) => {
  116. if (e.detail.avatarUrl) {
  117. uni.showLoading({
  118. title: "上传中",
  119. });
  120. upload(e.detail.avatarUrl, {
  121. onSuccess: (res) => {
  122. updateProfile({
  123. avatar: res.url,
  124. })
  125. .then(() => {
  126. uni.hideLoading();
  127. uni.showToast({
  128. title: "已更新",
  129. icon: "success",
  130. });
  131. avatar.value = res.url;
  132. })
  133. .catch((err) => {
  134. uni.hideLoading();
  135. uni.showModal({
  136. content: `${err.errMsg},请重试`,
  137. });
  138. });
  139. },
  140. onFail: (err) => {
  141. uni.hideLoading();
  142. uni.showModal({
  143. content: `${err.errMsg},请重试`,
  144. });
  145. },
  146. });
  147. } else {
  148. uni.showModal({
  149. content: `${e.detail.errMsg},请重试`,
  150. });
  151. }
  152. };
  153. const edit = (index: number) => {
  154. const menuItem = menu.value[index];
  155. if (menuItem.disabled) {
  156. return;
  157. }
  158. if (!menuItem.key) {
  159. uni.showToast({
  160. icon: "none",
  161. title: "暂不支持修改",
  162. });
  163. return;
  164. }
  165. if (/车牌/.test(menuItem.title)) {
  166. uni.chooseLicensePlate({
  167. success: (res) => {
  168. save({
  169. defaultPlateNo: res.plateNumber,
  170. });
  171. },
  172. fail: (err) => {},
  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: "#2d9e95",
  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. onLoad(() => {
  220. if (!requireAuth()) {
  221. return;
  222. }
  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 #c9d4d2;
  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(12, 24, 22, 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 var(--divider);
  279. &:last-child {
  280. border-bottom: none;
  281. }
  282. }
  283. </style>