password.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view class="container main">
  3. <view class="one">
  4. <uni-forms ref="baseForm" :rules="rules" :modelValue="form" label-width="80px">
  5. <uni-forms-item label="账号" required name="account">
  6. <uni-easyinput :disabled="true" v-model="form.account" placeholder="请输入账号" />
  7. </uni-forms-item>
  8. <uni-forms-item label="密码" required name="password">
  9. <uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
  10. </uni-forms-item>
  11. <uni-forms-item label="确认密码" required name="ispassword">
  12. <uni-easyinput type="password" v-model="form.ispassword" placeholder="请输入确认密码" />
  13. </uni-forms-item>
  14. </uni-forms>
  15. <view class="button">
  16. <button type="primary" @click="submit('baseForm')">保存</button>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. user: {},
  26. form: {
  27. password: '',
  28. ispassword: ''
  29. },
  30. // 校验规则
  31. rules: {
  32. password: {
  33. rules: [{
  34. required: true,
  35. errorMessage: '密码不能为空'
  36. }]
  37. },
  38. ispassword: {
  39. rules: [{
  40. required: true,
  41. errorMessage: '确认密码不能为空'
  42. },
  43. {
  44. validateFunction: function(rule, value, data, callback) {
  45. if (value != data.password) {
  46. callback('两次输入的密码必须相同')
  47. }
  48. return true
  49. }
  50. }
  51. ]
  52. },
  53. },
  54. }
  55. },
  56. onLoad: async function(e) {
  57. const that = this;
  58. await that.searchToken();
  59. await that.search();
  60. },
  61. onReady() {
  62. // 需要在onReady中设置规则
  63. this.$refs.baseForm.setRules(this.rules)
  64. },
  65. methods: {
  66. // 用户信息
  67. searchToken() {
  68. const that = this;
  69. try {
  70. const res = uni.getStorageSync('token');
  71. if (res) {
  72. const user = that.$jwt(res);
  73. that.$set(that, `user`, user);
  74. }
  75. } catch (e) {}
  76. },
  77. // 查询
  78. async search() {
  79. const that = this;
  80. if (that.user && that.user.id) {
  81. let res;
  82. res = await that.$api(`/user/${that.user.id}`, 'GET', {})
  83. if (res.errcode == '0') {
  84. that.$set(that, `form`, res.data)
  85. } else {
  86. uni.showToast({
  87. title: res.errmsg,
  88. });
  89. }
  90. }
  91. },
  92. // 登录
  93. submit(ref) {
  94. const that = this;
  95. that.$refs[ref].validate().then(async res => {
  96. if (res.password == res.ispassword) {
  97. let arr;
  98. arr = await that.$api(`/login/updatePwd/User`, 'POST', {
  99. id: that.user.id,
  100. password: res.password
  101. })
  102. if (arr.errcode == '0') {
  103. try {
  104. uni.removeStorageSync('token');
  105. } catch (e) {
  106. // error
  107. }
  108. uni.reLaunch({
  109. url: `/pages/home/index`
  110. })
  111. } else {
  112. uni.showToast({
  113. title: arr.errmsg,
  114. });
  115. }
  116. } else {
  117. uni.showToast({
  118. title: `密码不一致`,
  119. icon: 'error'
  120. });
  121. }
  122. }).catch(err => {
  123. console.log('err', err);
  124. })
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss" scoped>
  130. .main {
  131. padding: 3vw;
  132. .button {
  133. margin: 2vw 0 0 0;
  134. text-align: center;
  135. button {
  136. background-color: var(--f3CColor);
  137. font-size: var(--font14Size);
  138. border-radius: 2vw;
  139. }
  140. }
  141. }
  142. </style>