index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="content">
  3. <view class="one">
  4. <form @submit="formSubmit">
  5. <view class="value other" style="display: none;">
  6. <view class="title">id</view>
  7. <view class="label">
  8. <input name="id" class="input" :value="form.id" placeholder="请输入id" />
  9. </view>
  10. </view>
  11. <view class="value icon">
  12. <view class="title">头像</view>
  13. <view class="label">
  14. <u-avatar :text="formatName(form.nickname||'证')" shape="square" fontSize="20"
  15. randomBgColor></u-avatar>
  16. </view>
  17. </view>
  18. <view class="remark">
  19. 实名信息(仅用于咨询、免费估值功能)
  20. </view>
  21. <view class="value other">
  22. <view class="title">姓名</view>
  23. <view class="label">
  24. <input name="name" class="input" :value="form.name" placeholder="请输入真实姓名" />
  25. </view>
  26. </view>
  27. <view class="value other margin">
  28. <view class="title">昵称</view>
  29. <view class="label">
  30. <input name="nickname" class="input" :value="form.nickname" placeholder="请输入昵称" />
  31. </view>
  32. </view>
  33. <view class="value other">
  34. <view class="title">身份证号码</view>
  35. <view class="label">
  36. <input name="card" class="input" :value="form.card" placeholder="请输入身份证号码" />
  37. </view>
  38. </view>
  39. <view class="value other">
  40. <view class="title">手机号</view>
  41. <view class="label">
  42. <input name="phone" class="input" :value="form.phone" placeholder="请输入手机号" />
  43. </view>
  44. </view>
  45. <view class="button">
  46. <button type="warn" size="mini" form-type="submit">保存</button>
  47. </view>
  48. </form>
  49. </view>
  50. </view>
  51. </template>
  52. <script setup lang="ts">
  53. import { getCurrentInstance, computed, ref } from 'vue';
  54. //该依赖已内置不需要单独安装
  55. import { onLoad } from "@dcloudio/uni-app";
  56. // 请求接口
  57. const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
  58. // openid
  59. const openid = computed(() => {
  60. return uni.getStorageSync('openid');
  61. })
  62. // 基本信息
  63. const config = ref({ logo: '' });
  64. // 用户信息
  65. const form = ref({});
  66. // 字典表
  67. const sexList = ref([]);
  68. onLoad(async () => {
  69. await searchConfig();
  70. await search();
  71. })
  72. // config信息
  73. const searchConfig = async () => {
  74. config.value = uni.getStorageSync('config');
  75. };
  76. // 查询
  77. const search = async () => {
  78. const res = await $api(`login/wxapp/${openid.value}`, 'POST', {});
  79. if (res.errcode == '0') {
  80. form.value = res.data
  81. uni.setStorageSync('user', res.data);
  82. }
  83. };
  84. // 保存
  85. const formSubmit = async (e) => {
  86. if (form.value._id) {
  87. let data = e.detail.value;
  88. data = delEmptyQueryNodes(data);
  89. const res = await $api(`user/${form.value._id}`, 'POST', data);
  90. if (res.errcode == '0') search();
  91. } else {
  92. uni.navigateTo({
  93. url: `/pagesHome/login/index`
  94. })
  95. }
  96. };
  97. // 去除obj里为null的数据
  98. const delEmptyQueryNodes = (obj = {}) => {
  99. Object.keys(obj).forEach((key) => {
  100. let value = obj[key];
  101. value && typeof value === 'object' && delEmptyQueryNodes(value);
  102. (value === '' || value === null || value === undefined || value.length === 0 || Object.keys(value).length === 0) && delete obj[key];
  103. });
  104. return obj;
  105. };
  106. const formatName = (str) => {
  107. if (str) return str.substr(0, 1) + new Array(str.length).join('');
  108. };
  109. </script>
  110. <style lang="scss" scoped>
  111. .content {
  112. display: flex;
  113. flex-direction: column;
  114. width: 100vw;
  115. height: 100vh;
  116. background-color: var(--footColor);
  117. .one {
  118. .icon {
  119. padding: 2vw;
  120. }
  121. .margin {
  122. margin: 3vw 0 0 0;
  123. }
  124. .other {
  125. padding: 3vw 2vw;
  126. border-bottom: 1px solid var(--footColor);
  127. }
  128. .value {
  129. display: flex;
  130. justify-content: space-between;
  131. align-items: center;
  132. background-color: var(--mainColor);
  133. .label {
  134. .input {
  135. text-align: right;
  136. }
  137. .image {
  138. width: 15vw;
  139. height: 15vw;
  140. border-radius: 20vw;
  141. }
  142. }
  143. }
  144. .remark {
  145. padding: 4vw 2vw;
  146. text-align: center;
  147. color: var(--f99Color);
  148. font-size: var(--font18Size);
  149. }
  150. .button {
  151. margin: 2vw 0 0 0;
  152. text-align: center;
  153. button {
  154. background-color: var(--fFFColor);
  155. font-size: var(--font14Size);
  156. border-radius: 2vw;
  157. }
  158. }
  159. }
  160. }
  161. </style>