ChangePassword.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. Text, StyleSheet, StatusBar,
  5. } from 'react-native';
  6. import {connect} from 'react-redux'
  7. import {Button, InputItem,} from "@ant-design/react-native";
  8. import ComponentsStyles from "../../components/ComponentsStyles";
  9. import SyncStorage from "sync-storage";
  10. import {createAction} from "../../utils";
  11. @connect(auth => ({...auth}))
  12. class ChangePassword extends Component {
  13. constructor(props) {
  14. super(props);
  15. const credential = SyncStorage.get('credential');
  16. this.state = {
  17. old_password: "",
  18. new_password: "",
  19. new_password_repeat: "",
  20. newPropsText: '',
  21. confirmPropsText: "",
  22. oldPropsText: "",
  23. user_id:credential.user_id
  24. }
  25. }
  26. componentDidMount() {
  27. }
  28. // 原始密码聚焦的时候触发
  29. oldFocus = () => {
  30. if (this.state.newPropsText === "请先输入原始密码") {
  31. this.setState({
  32. newPropsText: ""
  33. })
  34. }
  35. if (this.state.oldPropsText) {
  36. this.setState({
  37. oldPropsText: ""
  38. })
  39. }
  40. }
  41. // 新密码聚焦的时候触发
  42. newFocus = () => {
  43. if (this.state.old_password == "") {
  44. this.setState({
  45. newPropsText: "请先输入原始密码"
  46. })
  47. }
  48. if (this.state.confirmPropsText == '请先输入新密码') {
  49. this.setState({
  50. confirmPropsText: ""
  51. })
  52. }
  53. if (this.state.newPropsText == '请输入新密码') {
  54. this.setState({
  55. newPropsText: ""
  56. })
  57. }
  58. }
  59. // 新密码输入框失去焦点的时候触发
  60. newBlur = () => {
  61. if (this.state.old_password == "") {
  62. this.setState({
  63. newPropsText: "请先输入原始密码"
  64. })
  65. } else if (this.state.old_password == this.state.new_password) {
  66. this.setState({
  67. newPropsText: "新密码与原始密码一致,请重新输入"
  68. })
  69. return false
  70. } else {
  71. this.setState({
  72. newPropsText: ""
  73. })
  74. }
  75. }
  76. // 确认密码聚焦的时候触发
  77. confirmFocus = () => {
  78. if (!this.state.new_password) {
  79. this.setState({
  80. confirmPropsText: '请先输入新密码'
  81. })
  82. }
  83. if (this.state.confirmPropsText == '请输入新密码') {
  84. this.setState({
  85. confirmPropsText: ""
  86. })
  87. }
  88. }
  89. // 确认密码失去焦点的时候触发
  90. confirmBlur = () => {
  91. if (this.state.new_password_repeat && this.state.new_password !== this.state.new_password_repeat) {
  92. this.setState({
  93. confirmPropsText: '两次输入的新密码不一致,请重新输入'
  94. })
  95. return false
  96. } else if (this.state.new_password == this.state.new_password_repeat) {
  97. this.setState({
  98. confirmPropsText: ''
  99. })
  100. return true
  101. }
  102. }
  103. // 确定修改密码的时候执行
  104. confirmHandle = () => {
  105. const {old_password, oldPropsText, new_password, newPropsText, new_password_repeat, confirmPropsText} = this.state;
  106. if (!old_password) {
  107. this.setState({
  108. oldPropsText: "请输入原始密码"
  109. })
  110. }
  111. if (!new_password) {
  112. this.setState({
  113. newPropsText: '请输入新密码'
  114. })
  115. }
  116. if (!new_password_repeat) {
  117. this.setState({
  118. confirmPropsText: '请输入确认密码'
  119. })
  120. }
  121. if (old_password && oldPropsText) {
  122. this.setState({
  123. oldPropsText: ""
  124. })
  125. }
  126. if (new_password && newPropsText) {
  127. this.setState({
  128. newPropsText: ""
  129. })
  130. }
  131. if (new_password_repeat && confirmPropsText === "请输入确认密码") {
  132. this.setState({
  133. confirmPropsText: ""
  134. })
  135. }
  136. if (old_password && new_password && old_password === new_password) {
  137. this.setState({
  138. newPropsText: "新密码与原始密码一致,请重新输入"
  139. })
  140. }
  141. if (new_password && new_password_repeat && new_password !== new_password_repeat) {
  142. this.setState({
  143. confirmPropsText: '两次输入的新密码不一致,请重新输入'
  144. })
  145. }
  146. if (old_password && new_password && new_password_repeat && old_password !== new_password && new_password === new_password_repeat) {
  147. if (confirmPropsText) {
  148. this.setState({
  149. confirmPropsText: ''
  150. })
  151. }
  152. // 执行修改操作
  153. this.props.dispatch({
  154. type: 'auth/changePassword',
  155. payload: this.state,
  156. callback: () => {
  157. this.setState({
  158. replyContent: ''
  159. })
  160. this.props.dispatch(createAction('auth/logout')());
  161. }
  162. });
  163. }
  164. }
  165. render() {
  166. const {loading} = this.props.auth;
  167. const {newPropsText, confirmPropsText, oldPropsText} = this.state;
  168. return (
  169. <View style={{flex: 1,}}>
  170. <StatusBar backgroundColor={'#fff'} barStyle='dark-content'/>
  171. <View style={{margin: 10, borderRadius: 10, backgroundColor: "#ffffff"}}>
  172. {/* 原密码 */}
  173. <InputItem
  174. clear
  175. type="password"
  176. value={this.state.old_password}
  177. onFocus={this.oldFocus}
  178. onChange={value => {
  179. this.setState({
  180. old_password: value
  181. });
  182. }}
  183. placeholder="请输入原始密码"
  184. >
  185. 原始密码
  186. </InputItem>
  187. {
  188. oldPropsText ? <Text
  189. style={{color: "red", marginHorizontal: 15, marginVertical: 10}}>{oldPropsText}</Text> :
  190. <Text style={{display: 'none'}}/>
  191. }
  192. {/* 新密码 */}
  193. <InputItem
  194. clear
  195. type="password"
  196. value={this.state.new_password}
  197. onChange={value => {
  198. this.setState({
  199. new_password: value
  200. });
  201. }}
  202. onFocus={this.newFocus}
  203. onBlur={this.newBlur}
  204. placeholder="请输入新密码"
  205. >
  206. 新密码
  207. </InputItem>
  208. {
  209. newPropsText ? <Text
  210. style={{color: "red", marginHorizontal: 15, marginVertical: 10}}>{newPropsText}</Text> :
  211. <Text style={{display: 'none'}}/>
  212. }
  213. {/* 确认新密码 */}
  214. <InputItem
  215. clear
  216. type="password"
  217. value={this.state.new_password_repeat}
  218. onFocus={this.confirmFocus}
  219. onBlur={this.confirmBlur}
  220. onChange={value => {
  221. this.setState({
  222. new_password_repeat: value
  223. });
  224. }}
  225. placeholder="请确认新密码"
  226. >
  227. 确认密码
  228. </InputItem>
  229. {
  230. confirmPropsText ? <Text
  231. style={{color: "red", marginHorizontal: 15, marginVertical: 10}}>{confirmPropsText}</Text> :
  232. <Text style={{display: 'none'}}/>
  233. }
  234. </View>
  235. <Button
  236. loading={loading}
  237. disabled={loading}
  238. style={ComponentsStyles.button}
  239. onPress={this.confirmHandle}>
  240. <Text style={{color: '#ffffff'}}>确定</Text>
  241. </Button>
  242. </View>
  243. )
  244. }
  245. }
  246. const styles = StyleSheet.create({
  247. btn: {
  248. marginHorizontal: 10,
  249. backgroundColor: '#2b90ea'
  250. }
  251. })
  252. export default ChangePassword