index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <view class="pt-60 pb-20 flex-center">
  3. <uv-navbar title="联系我们" bgColor="#C6171E" leftIconColor="#FFFFFF" :titleStyle="{ color: '#FFFFFF' }" :autoBack="true" :placeholder="true"></uv-navbar>
  4. <button
  5. class="avatar"
  6. open-type="chooseAvatar"
  7. @chooseavatar="chooseAvatar"
  8. >
  9. <image class="avatar_image" :src="avatar" @error="errorHandle"></image>
  10. <view class="avatar_text flex-center">编辑</view>
  11. </button>
  12. </view>
  13. <view class="pl-50 pr-50">
  14. <view
  15. class="menu flex-align-center flex-between"
  16. v-for="(item, index) in state.menuList"
  17. :key="index"
  18. @click="edit(index)"
  19. >
  20. <view class="fs-30">{{ item.title }}</view>
  21. <view class="flex">
  22. <view
  23. :class="['fs-30', 'fw-500', `mr-${item.disabled ? '0' : '20'}`]"
  24. style="color: rgba(0, 0, 0, 0.8)"
  25. >{{ item.value }}
  26. </view
  27. >
  28. <uni-icons
  29. type="right"
  30. size="12"
  31. color="rgba(0,0,0,0.4)"
  32. v-if="!item.disabled"
  33. ></uni-icons>
  34. </view>
  35. </view>
  36. </view>
  37. <style-bottom-view>
  38. <view class="pl-60 pr-60 pb-40">
  39. <style-button type="primary" @click="logoutUser">退出登录</style-button>
  40. </view>
  41. </style-bottom-view>
  42. </template>
  43. <script setup lang="ts">
  44. import {onHide, onShow} from "@dcloudio/uni-app";
  45. import {reactive, ref} from "vue";
  46. import {body, get, upload} from "@/utils/https"
  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 initState = () => ({
  77. user: {},
  78. menuList: []
  79. })
  80. const state = reactive(initState())
  81. const refresh = () => {
  82. const _menu = [...MENU_TEMPLATE];
  83. get(`user/profile`).then(res => {
  84. getApp<any>().globalData.user = res;
  85. })
  86. // fetchProfile().then(() => {
  87. // const user = getApp<any>().globalData.user;
  88. // if (user) {
  89. // _menu[0].value = user.nickname;
  90. // _menu[1].value = user.mobilePhone;
  91. // _menu[2].value = user.defaultPlateNo;
  92. // _menu[3].value = user.vin;
  93. // // _menu[4].value = user.card_no;
  94. // avatar.value =
  95. // user.avatar ||
  96. // "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
  97. // menu.value = _menu;
  98. // }
  99. // });
  100. };
  101. const save = (form: Record<string, any>) => {
  102. uni.showLoading({
  103. title: "保存中",
  104. });
  105. return body(`user/updateProfile`, form)
  106. .then((res) => {
  107. uni.hideLoading();
  108. uni.showToast({
  109. icon: "success",
  110. title: "保存成功",
  111. });
  112. refresh();
  113. return res;
  114. })
  115. .catch((err) => {
  116. uni.hideLoading();
  117. uni.showModal({
  118. content: `${err.errMsg},请重试`,
  119. });
  120. });
  121. };
  122. const chooseAvatar = (e: any) => {
  123. if (e.detail.avatarUrl) {
  124. uni.showLoading({
  125. title: "上传中",
  126. });
  127. let params = {
  128. url: `file/upload`,
  129. filePath: e.detail.avatarUrl,
  130. success: (res) => {
  131. body(`user/updateAvatar`, {
  132. avatar: res.url,
  133. })
  134. .then(() => {
  135. uni.hideLoading();
  136. uni.showToast({
  137. title: "已更新",
  138. icon: "success",
  139. });
  140. avatar.value = res.url;
  141. })
  142. .catch((err) => {
  143. uni.hideLoading();
  144. uni.showModal({
  145. content: `${err.errMsg},请重试`,
  146. });
  147. });
  148. }
  149. }
  150. upload(params)
  151. } else {
  152. uni.showModal({
  153. content: `${e.detail.errMsg},请重试`,
  154. });
  155. }
  156. };
  157. const edit = (index: number) => {
  158. const menuItem = menu.value[index];
  159. if (menuItem.disabled) {
  160. return;
  161. }
  162. if (!menuItem.key) {
  163. uni.showToast({
  164. icon: "none",
  165. title: "暂不支持修改",
  166. });
  167. return;
  168. }
  169. if (/车牌/.test(menuItem.title)) {
  170. uni.chooseLicensePlate({
  171. success: (res) => {
  172. save({
  173. defaultPlateNo: res.plateNumber,
  174. });
  175. },
  176. fail: (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: "#C6171E",
  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. };
  225. onShow(() => {
  226. if (getApp<any>().globalData.lastData.profile) {
  227. const {key, value} = getApp<any>().globalData.lastData.profile;
  228. save({
  229. [key]: value,
  230. }).then(() => {
  231. getApp<any>().globalData.lastData.profile = undefined;
  232. });
  233. }
  234. });
  235. onHide(() => {
  236. Object.assign(state, initState());
  237. })
  238. </script>
  239. <style lang="scss" scoped>
  240. .avatar {
  241. position: relative;
  242. height: 116rpx !important;
  243. width: 116rpx !important;
  244. border-radius: 50%;
  245. border: 2rpx solid $uni-border-color;
  246. overflow: hidden;
  247. &_image {
  248. position: absolute;
  249. width: 100%;
  250. height: 100%;
  251. left: 0;
  252. top: 0;
  253. border-radius: 50%;
  254. }
  255. &_text {
  256. position: absolute;
  257. bottom: 0;
  258. left: 0;
  259. width: 100%;
  260. height: 40rpx;
  261. background: rgba(0, 0, 0, 0.5);
  262. color: $uni-text-color-inverse;
  263. font-size: 24rpx;
  264. }
  265. }
  266. .menu {
  267. background-color: $uni-bg-color-card;
  268. height: 120rpx;
  269. border-bottom: 1rpx solid $uni-border-color-light;
  270. &:last-child {
  271. border-bottom: none;
  272. }
  273. }
  274. </style>