车间能级提升-智能设备管理系统
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<route lang="json5" type="page">
{
  layout: 'default',
  needLogin: true,
  "style": {
    "navigationBarTitleText": "修改密码"
  }
}
</route>
 
<template>
  <view class="container">
    <wd-form ref="formRef" :model="form">
      <wd-cell-group custom-class="cell-group" border>
        <wd-input
          label="旧密码"
          label-width="100px"
          clearable
          show-word-limit
          prop="oldPassword"
          show-password
          v-model="form.oldPassword"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '旧密码不能为空' }]"
        />
        <wd-input
          label="新密码"
          label-width="100px"
          clearable
          show-word-limit
          prop="newPassword"
          show-password
          v-model="form.newPassword"
          placeholder="请输入新密码"
          :rules="[
            { required: true, message: '新密码不能为空' },
            { validator: validatePassword },
          ]"
        />
        <wd-input
          label="确认新密码"
          label-width="100px"
          clearable
          show-word-limit
          prop="confirmNewPassword"
          show-password
          v-model="form.confirmNewPassword"
          placeholder="请再次输入新密码"
          :rules="[
            { required: true, message: '确认新密码不能为空' },
            { validator: validatePassword },
            {
              validator: (value) => value === form.newPassword,
              message: '两次输入的密码不一致'
            }
          ]"
        />
      </wd-cell-group>
      <view class="footer">
        <wd-button type="primary" class="mt-6" size="large" block @click="submit">提交</wd-button>
      </view>
    </wd-form>
  </view>
</template>
 
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { updatePassword } from '@/service/app/user'
import { useUserStore, useAccessStore, useSystemConfigStore } from '@/store'
 
 
const userStore = useUserStore()
const accessStore = useAccessStore()
const configStore = useSystemConfigStore()
 
const form = reactive({
  oldPassword: '',
  newPassword: '',
  confirmNewPassword: ''
})
const formRef = ref()
 
const validatePassword = (value) => {
  if (value.length < 6) {
    return Promise.reject('密码长度不能少于6位')
  }
  if (value.length > 20) {
    return Promise.reject('密码长度不能超过20位')
  }
  return Promise.resolve()
}
 
const submit = () => {
 
  formRef.value.validate().then(({ valid, errors }) => {
    if (valid) {
      updatePassword(form.oldPassword, form.newPassword)
        .then((response) => {
          if (response.code === 200) {
            uni.showToast({
              title: '修改密码成功',
              icon: 'success',
            })
            userStore.clearUserInfo()
            accessStore.clearAccessInfo()
            configStore.clearConfigInfo()
            uni.reLaunch({
              url: '/pages/login/index',
            })
          } else {
            uni.showToast({
              title: response.msg || '修改密码失败',
              icon: 'error',
            })
          }
        })
        .catch((error) => {
          uni.showToast({
            title: error.msg || '修改密码失败',
            icon: 'error',
          })
        })
    }
  })
}
</script>
 
<style lang="scss" scoped>
.footer {
  margin: 20px;
}
</style>