edit.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="container">
  3. <view class="example">
  4. <uni-forms ref="form" :model="user" labelWidth="80px">
  5. <uni-forms-item label="用户昵称" name="nickName">
  6. <uni-easyinput v-model="user.nickName" placeholder="请输入昵称" />
  7. </uni-forms-item>
  8. <uni-forms-item label="手机号码" name="phonenumber">
  9. <uni-easyinput v-model="user.phonenumber" placeholder="请输入手机号码" />
  10. </uni-forms-item>
  11. <uni-forms-item label="邮箱" name="email">
  12. <uni-easyinput v-model="user.email" placeholder="请输入邮箱" />
  13. </uni-forms-item>
  14. <uni-forms-item label="性别" name="sex" required>
  15. <uni-data-checkbox v-model="user.sex" :localdata="sexs" />
  16. </uni-forms-item>
  17. </uni-forms>
  18. <button type="primary" @click="submit">提交</button>
  19. </view>
  20. </view>
  21. </template>
  22. <script setup>
  23. import { getUserProfile } from "@/api/system/user";
  24. import { updateUserProfile } from "@/api/system/user";
  25. import { onReady, onLoad, onShow, onNavigationBarButtonTap } from "@dcloudio/uni-app";
  26. import { ref, onMounted, inject, shallowRef, reactive, getCurrentInstance, toRefs } from "vue";
  27. const data = reactive({
  28. user: {
  29. nickName: "",
  30. phonenumber: "",
  31. email: "",
  32. sex: "",
  33. },
  34. sexs: [
  35. {
  36. text: "男",
  37. value: "0",
  38. },
  39. {
  40. text: "女",
  41. value: "1",
  42. },
  43. ],
  44. rules: {
  45. nickName: {
  46. rules: [
  47. {
  48. required: true,
  49. errorMessage: "用户昵称不能为空",
  50. },
  51. ],
  52. },
  53. phonenumber: {
  54. rules: [
  55. {
  56. required: true,
  57. errorMessage: "手机号码不能为空",
  58. },
  59. {
  60. pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
  61. errorMessage: "请输入正确的手机号码",
  62. },
  63. ],
  64. },
  65. email: {
  66. rules: [
  67. {
  68. required: true,
  69. errorMessage: "邮箱地址不能为空",
  70. },
  71. {
  72. format: "email",
  73. errorMessage: "请输入正确的邮箱地址",
  74. },
  75. ],
  76. },
  77. },
  78. });
  79. const { user, sexs, rules } = toRefs(data);
  80. const { proxy } = getCurrentInstance();
  81. const form = ref(null);
  82. function getUser() {
  83. getUserProfile().then((response) => {
  84. console.log(response);
  85. user.value = response.data.user;
  86. });
  87. }
  88. function submit(ref) {
  89. form.value.validate().then((res) => {
  90. updateUserProfile(user.value).then((response) => {
  91. proxy.$modal.msgSuccess("修改成功,重新登录后生效!");
  92. proxy.$tab.reLaunch("pages/mine/index");
  93. });
  94. });
  95. }
  96. onLoad((options) => {
  97. getUser();
  98. });
  99. onReady(() => {
  100. form.value.setRules(rules.value);
  101. });
  102. </script>
  103. <style>
  104. page {
  105. background-color: #ffffff;
  106. }
  107. </style>
  108. <style lang="scss">
  109. .example {
  110. padding: 15px;
  111. background-color: #fff;
  112. }
  113. .segmented-control {
  114. margin-bottom: 15px;
  115. }
  116. .button-group {
  117. margin-top: 15px;
  118. display: flex;
  119. justify-content: space-around;
  120. }
  121. .form-item {
  122. display: flex;
  123. align-items: center;
  124. flex: 1;
  125. }
  126. .button {
  127. display: flex;
  128. align-items: center;
  129. height: 35px;
  130. line-height: 35px;
  131. margin-left: 10px;
  132. }
  133. </style>