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