info.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  3. <view v-if="user">
  4. <cu-custom bgImage="/static/bg.png" :isBack="true">
  5. <template v-slot:content>
  6. <text class="text-black">用户信息</text>
  7. </template>
  8. </cu-custom>
  9. <view class="padding">
  10. <uni-forms ref="userForm" :model="user" :rules="rules">
  11. <view class="cu-form-group padding" style="justify-content: center;">
  12. <view class="bg-img">
  13. <image :src="user.avatar" class="cu-avatar round xl logo-xxl" mode="aspectFill"
  14. @tap="chooseImage"></image>
  15. </view>
  16. </view>
  17. <uni-forms-item class="text-bold text-black" label="姓名" name="name">
  18. <uni-easyinput type="text" v-model="user.name" placeholder="请输入姓名" :clearable="false"/>
  19. </uni-forms-item>
  20. <uni-forms-item class="text-bold text-black" label="邮箱" name="email">
  21. <uni-easyinput type="text" v-model="user.email" placeholder="请输入邮箱" :clearable="false"/>
  22. </uni-forms-item>
  23. <uni-forms-item class="text-bold text-black" label="手机" name="mobile">
  24. <uni-easyinput type="text" v-model="user.mobile" placeholder="请输入手机" :clearable="false"/>
  25. </uni-forms-item>
  26. <uni-forms-item class="text-bold text-black" label="性别" name="sex">
  27. <uni-data-select :clear="false"
  28. v-model="user.sex"
  29. :localdata="range"
  30. @change="change"
  31. ></uni-data-select>
  32. </uni-forms-item>
  33. <view class="margin-top text-center">
  34. <button class="cu-btn bg-zblue border lg margin-top" style="width: 100%;" @tap="save">保存</button>
  35. </view>
  36. </uni-forms>
  37. </view>
  38. </view>
  39. </template>
  40. <script setup lang="ts">
  41. import CuCustom from '@/colorui/components/cu-custom.vue'
  42. import UserApi from '@/api/UserApi';
  43. import type User from '@/mode/User';
  44. import {reactive, ref} from "vue";
  45. import {useUserStore} from "@/store/userStore";
  46. import Auth from '@/api/Auth';
  47. import FetchRequest from '@/api/FetchRequest';
  48. import UniForms from "@/uni_modules/uni-forms/components/uni-forms/uni-forms.vue";
  49. import UniFormsItem from "@/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue";
  50. import UniEasyinput from "@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue";
  51. import UniDataSelect from "@/uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
  52. import MessageUtils from "@/utils/MessageUtils";
  53. import VimConfig from "@/config/VimConfig";
  54. const fontValue=ref(Auth.getfontSize());
  55. const userForm = ref();
  56. const range = [
  57. {value: "0", text: "男"},
  58. {value: "1", text: "女"},
  59. {value: "2", text: "未知"},
  60. ]
  61. const rules = {
  62. name: {
  63. rules: [{
  64. required: true,
  65. errorMessage: '请输入姓名',
  66. },
  67. {
  68. minLength: 2,
  69. maxLength: 10,
  70. errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
  71. }
  72. ]
  73. },
  74. // 对email字段进行必填验证
  75. email: {
  76. rules: [{
  77. format: 'email',
  78. errorMessage: '请输入正确的邮箱地址',
  79. },
  80. {
  81. minLength: 3,
  82. maxLength: 64,
  83. errorMessage: '邮箱地址长度在 {minLength} 到 {maxLength} 个字符',
  84. }]
  85. },
  86. // 对mobile字段进行必填验证
  87. mobile: {
  88. rules: [{
  89. validateFunction: function (rule: string, value: string, data: string, callback: Function) {
  90. // const pattern =/^1[3456789]\d{9}$/
  91. // if (!pattern.test(value)) {
  92. // callback('手机号格式不对')
  93. // }
  94. if(value.length<6){
  95. callback('手机号格式不对')
  96. }
  97. return true
  98. }
  99. }]
  100. },
  101. sex: {
  102. rules: [{
  103. required: true,
  104. errorMessage: '请选择性别',
  105. }]
  106. }
  107. }
  108. const userStore = useUserStore();
  109. const user = reactive<User>({
  110. id: "",
  111. name: "",
  112. mobile: "",
  113. email: "",
  114. avatar: "",
  115. deptId: "",
  116. sex: "",
  117. canAddFriend: false,
  118. });
  119. const change = (e: string) => {
  120. user.sex = e
  121. }
  122. UserApi.currentUser()
  123. .then(res => {
  124. console.log('currentUser',res.data)
  125. user.id = res.data.id;
  126. user.name = res.data.name;
  127. user.deptId = res.data.deptId;
  128. user.mobile = res.data.mobile;
  129. user.email = res.data.email;
  130. user.avatar = res.data.avatar === "" ? null : res.data.avatar;
  131. user.sex = res.data.sex;
  132. })
  133. const chooseImage = (e: any) => {
  134. uni.chooseImage({
  135. count: 1, //默认9
  136. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  137. sourceType: ['album'], //从相册选择
  138. success: (res) => {
  139. uni.uploadFile({
  140. url: `${FetchRequest.getHost()}/${VimConfig.uploadType}/upload`, //仅为示例,非真实的接口地址
  141. filePath: res.tempFilePaths[0],
  142. name: 'file',
  143. header: {
  144. "Access-Control-Allow-Origin": "*",
  145. "Authorization": "Bearer " + Auth.getToken(),
  146. },
  147. formData: {
  148. 'type': 'image'
  149. },
  150. success: (res) => {
  151. user.avatar = JSON.parse(res.data).url
  152. },
  153. fail: () => {
  154. MessageUtils.message('上传失败')
  155. },
  156. });
  157. }
  158. });
  159. }
  160. const save = () => {
  161. userForm.value.validate().then(() => {
  162. UserApi.update(user.id, user)
  163. .then(() => {
  164. return UserApi.getUser(user.id);
  165. })
  166. .then((res) => {
  167. userStore.setUser(res.data);
  168. MessageUtils.message('操作成功')
  169. });
  170. }).catch((err: any) => {
  171. console.log('表单错误信息:', err);
  172. })
  173. }
  174. </script>
  175. <style scoped>
  176. .bg-img {
  177. display: flex;
  178. justify-content: center;
  179. }
  180. .avatar {
  181. display: inline-block;
  182. background-color: antiquewhite;
  183. width: 100px;
  184. height: 100px;
  185. }
  186. </style>