change.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <view class="page">
  3. <uni-section :title="$t('user.userInfo')" type="line" />
  4. <uni-card :is-shadow="false">
  5. <view class="avatar-item flex-row align-center">
  6. <view style="color: #dd524d">*</view>
  7. <view style="margin: 10px 0">{{ $t('user.avatar') }}</view>
  8. <image class="avatar" :src="form.avatar ? form.avatar : '/static/common/avata.gif'" @click="changeAvatar" />
  9. </view>
  10. <uni-forms ref="formRef" :model="form" :rules="rules" err-show-type="toast" label-width="80px">
  11. <uni-forms-item :label="$t('user.phone')" name="mobile" required>
  12. <uni-easyinput v-model="form.mobile" :placeholder="$t('operation.PleaseFillIn')" :input-border="false" />
  13. </uni-forms-item>
  14. </uni-forms>
  15. <button type="primary" style="margin-top: 20px; margin-bottom: 10px" @click="submit">{{ $t('operation.save') }}</button>
  16. </uni-card>
  17. <uni-section :title="$t('user.updatePassword')" type="line" />
  18. <uni-card :is-shadow="false" style="margin-top: 10px">
  19. <uni-forms ref="pwdFormRef" :model="pwdForm" :rules="pwdRules" err-show-type="toast" label-width="80px">
  20. <uni-forms-item :label="$t('user.oldPassword')" name="oldPwd" required>
  21. <uni-easyinput v-model="pwdForm.oldPwd" type="password" :placeholder="$t('operation.PleaseFillIn')" :input-border="false" />
  22. </uni-forms-item>
  23. <uni-forms-item :label="$t('user.password')" name="pwd" required>
  24. <uni-easyinput v-model="pwdForm.pwd" type="password" :placeholder="$t('operation.PleaseFillIn')" :input-border="false" />
  25. </uni-forms-item>
  26. <uni-forms-item :label="$t('user.confirmPassword')" name="confirmPwd" required>
  27. <uni-easyinput v-model="pwdForm.confirmPwd" type="password" :placeholder="$t('operation.PleaseFillIn')" :input-border="false" />
  28. </uni-forms-item>
  29. </uni-forms>
  30. <button type="primary" style="margin-top: 20px; margin-bottom: 10px" @click="changePassword">{{ $t('operation.submit') }}</button>
  31. </uni-card>
  32. </view>
  33. </template>
  34. <script setup>
  35. import { onLoad } from '@dcloudio/uni-app'
  36. import { getCurrentInstance, reactive, ref } from "vue";
  37. import { updateAvatar, updateUserInfo, changePassword as change } from "@/api/login";
  38. import $store from '@/store'
  39. const formRef = ref()
  40. const form = reactive({
  41. mobile: '',
  42. })
  43. const pwdFormRef = ref()
  44. const pwdForm = reactive({
  45. oldPwd: '',
  46. pwd: '',
  47. confirmPwd: '',
  48. })
  49. const { appContext } = getCurrentInstance()
  50. const t = appContext.config.globalProperties.$t
  51. const rules = reactive({
  52. mobile: { rules: [{ required: true, errorMessage: t('user.phoneHint') }] }
  53. })
  54. const pwdRules = reactive({
  55. oldPwd: { rules: [{ required: true, errorMessage: t('user.oldPasswordHint') }] },
  56. pwd: { rules: [{ required: true, errorMessage: t('user.passwordHint') }] },
  57. confirmPwd: { rules: [{ required: true, errorMessage: t('user.confirmPasswordHint') }] },
  58. })
  59. const changeAvatar = () => {
  60. uni.chooseImage({
  61. count: 1,
  62. sizeType: ['compressed'],
  63. sourceType: ['album', 'camera'],
  64. success: async (res) => {
  65. const response = await updateAvatar(res.tempFilePaths[0])
  66. if (response.code === 0) {
  67. form.avatar = response.data
  68. toast(t('operation.success'))
  69. uni.$emit('updateUserInfo')
  70. }
  71. },
  72. fail: (error) => {
  73. console.log(error)
  74. }
  75. })
  76. }
  77. const submit = async () => {
  78. try {
  79. const valid = await formRef.value.validate()
  80. if (!valid) return
  81. const code = (await updateUserInfo(form.mobile)).code
  82. if (code === 0) {
  83. toast(t('operation.success'))
  84. uni.$emit('updateUserInfo')
  85. }
  86. } catch (e) {
  87. console.log(e)
  88. }
  89. }
  90. const userStore = $store('user')
  91. const changePassword = async () => {
  92. try {
  93. const valid = await pwdFormRef.value.validate()
  94. if (!valid) return
  95. if (pwdForm.oldPwd === pwdForm.pwd) {
  96. toast(t('user.passwordError1'))
  97. return
  98. } else if (pwdForm.pwd !== pwdForm.confirmPwd) {
  99. toast(t('user.passwordError2'))
  100. return
  101. }
  102. const code = (await change(pwdForm.oldPwd, pwdForm.pwd)).code
  103. if (code === 0) {
  104. toast(t('operation.success'))
  105. await userStore.LogOut()
  106. uni.reLaunch({ url: '/pages/user/login' })
  107. }
  108. } catch (e) {
  109. console.log(e)
  110. }
  111. }
  112. const toast = (msg) => {
  113. uni.showToast({ title: msg, icon: 'none' })
  114. }
  115. const userInfo = ref({})
  116. onLoad((options) => {
  117. const info = JSON.parse(options.info)
  118. userInfo.value = info
  119. form.mobile = info.mobile
  120. form.avatar = info.avatar
  121. })
  122. </script>
  123. <style scoped lang="scss">
  124. :deep(.uni-section) {
  125. background-color: transparent;
  126. .uni-section-header {
  127. padding: 10px 0;
  128. }
  129. }
  130. :deep(.uni-card) {
  131. margin: 0 !important;
  132. padding: 0 !important;
  133. .uni-card__content {
  134. padding: 0 10px !important;
  135. }
  136. }
  137. :deep(.uni-forms-item) {
  138. margin-bottom: 0;
  139. padding: 8px 0;
  140. border-bottom: 1px dashed #CACCCF;
  141. }
  142. .avatar-item {
  143. border-bottom: 1px dashed #CACCCF;
  144. padding: 14px 0;
  145. }
  146. .avatar {
  147. width: 80px;
  148. height: 80px;
  149. border-radius: 50%;
  150. margin-left: 30px;
  151. }
  152. </style>