editPassword.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="box">
  3. <view class="box-tips">
  4. <h2>
  5. {{verificationTitle[validateFlage==false ? 0 : 1].title}}
  6. </h2>
  7. <view class="verification">{{verificationTitle[step].desc}}</view>
  8. </view>
  9. <u-form :model="codeForm" class="form" ref="validateCodeForm">
  10. <view v-if="!validateFlage">
  11. <u-form-item label-width="120" label="手机号" prop="mobile">
  12. <u-input v-model="codeForm.mobile" placeholder="请输入您的手机号" />
  13. </u-form-item>
  14. <u-form-item class="sendCode" label-width="120" prop="code" label="验证码">
  15. <u-input v-model="codeForm.code" placeholder="请输入验证码" />
  16. <u-verification-code unique-key="page-edit" :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
  17. <view @tap="getCode" class="text-tips">{{ tips }}</view>
  18. </u-form-item>
  19. <view class="submit" @click="validatePhone">验证</view>
  20. <myVerification keep-running @send="verification" class="verification" ref="verification" business="LOGIN" />
  21. </view>
  22. <view v-if="validateFlage">
  23. <u-form-item label-width="120" label="旧密码">
  24. <u-input type="password" v-model="password" placeholder="请输入您的旧密码" />
  25. </u-form-item>
  26. <u-form-item label-width="120" label="新密码">
  27. <u-input type="password" v-model="newPassword" placeholder="请输入您的新密码" />
  28. </u-form-item>
  29. <view class="submit" @click="updatePassword">修改密码</view>
  30. </view>
  31. </u-form>
  32. </view>
  33. </template>
  34. <script>
  35. import { sendMobile, resetByMobile, modifyPass } from "@/api/login";
  36. import storage from "@/utils/storage.js";
  37. import { md5 } from "@/utils/md5.js"; // md5
  38. import myVerification from "@/components/verification/verification.vue"; //验证
  39. import uuid from "@/utils/uuid.modified.js";
  40. export default {
  41. components: {
  42. myVerification,
  43. },
  44. data() {
  45. return {
  46. uuid,
  47. validateFlage: false, //是否进行了手机号验证
  48. verificationTitle: [
  49. {
  50. title: "安全验证",
  51. desc: "请输入当前手机号进行安全验证",
  52. },
  53. {
  54. title: "修改密码",
  55. desc: "请输入新密码",
  56. },
  57. ],
  58. step: 0, //当前验证步骤
  59. flage: false, //是否验证码验证
  60. codeForm: {
  61. mobile: "", //手机号
  62. code: "", //验证码
  63. },
  64. newPassword: "", //新密码
  65. password: "", //密码
  66. tips: "", //提示
  67. seconds: 60, // 60s等待时间
  68. // 验证码登录校验
  69. codeRules: {
  70. mobile: [
  71. {
  72. validator: (rule, value, callback) => {
  73. return this.$u.test.mobile(value);
  74. },
  75. message: "手机号码不正确",
  76. trigger: ["blur"],
  77. },
  78. ],
  79. code: [
  80. {
  81. min: 4,
  82. max: 6,
  83. required: true,
  84. message: "请输入验证码",
  85. trigger: ["blur"],
  86. },
  87. ],
  88. },
  89. };
  90. },
  91. onReady() {
  92. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  93. this.$refs.validateCodeForm.setRules(this.codeRules);
  94. },
  95. watch: {
  96. flage(val) {
  97. if (val) {
  98. if (this.$refs.uCode.canGetCode) {
  99. uni.showLoading({
  100. title: "正在获取验证码",
  101. });
  102. sendMobile(this.codeForm.mobile).then((res) => {
  103. uni.hideLoading();
  104. // 这里此提示会被this.start()方法中的提示覆盖
  105. if (res.data.code == 200) {
  106. this.$refs.uCode.start();
  107. } else {
  108. uni.showToast({
  109. title: res.data.message,
  110. duration: 2000,
  111. icon: "none",
  112. });
  113. }
  114. });
  115. } else {
  116. this.$u.toast("请倒计时结束后再发送");
  117. }
  118. }
  119. },
  120. },
  121. methods: {
  122. // 修改密码
  123. updatePassword() {
  124. modifyPass({
  125. newPassword: md5(this.newPassword),
  126. password: md5(this.password),
  127. }).then((res) => {
  128. if (res.data.success) {
  129. uni.showToast({
  130. title: "修改成功!",
  131. duration: 2000,
  132. icon: "none",
  133. });
  134. setTimeout(() => {
  135. uni.navigateBack({
  136. delta: 1,
  137. });
  138. }, 1000);
  139. }
  140. });
  141. },
  142. // 验证码验证
  143. verification(val) {
  144. this.flage = val == this.$store.state.verificationKey ? true : false;
  145. },
  146. // 验证手机号
  147. validatePhone() {
  148. this.$refs.validateCodeForm.validate((valid) => {
  149. if (valid) {
  150. resetByMobile(this.codeForm).then((res) => {
  151. if (res.data.success) {
  152. storage.setAccessToken(res.data.result.accessToken);
  153. storage.setRefreshToken(res.data.result.refreshToken);
  154. // 登录成功
  155. uni.showToast({
  156. title: "验证成功!",
  157. icon: "none",
  158. });
  159. }
  160. });
  161. }
  162. });
  163. },
  164. codeChange(text) {
  165. this.tips = text;
  166. },
  167. end() {},
  168. /**判断是否是当前用户的手机号 */
  169. isUserPhone() {
  170. let flage = false;
  171. let user = this.$options.filters.isLogin();
  172. if (user.mobile != this.codeForm.mobile) {
  173. uni.showToast({
  174. title: "请输入当前绑定手机号",
  175. icon: "none",
  176. });
  177. flage = false;
  178. } else {
  179. flage = true;
  180. }
  181. return flage;
  182. },
  183. /**获取验证码 */
  184. getCode() {
  185. if (this.isUserPhone()) {
  186. if (this.tips == "重新获取") {
  187. this.flage = true;
  188. }
  189. if (!this.$u.test.mobile(this.codeForm.mobile)) {
  190. uni.showToast({
  191. title: "请输入正确手机号",
  192. icon: "none",
  193. });
  194. return false;
  195. }
  196. if (!this.flage) {
  197. this.$refs.verification.hide();
  198. return false;
  199. }
  200. }
  201. },
  202. start() {
  203. this.$u.toast("验证码已发送");
  204. this.flage = false;
  205. },
  206. },
  207. };
  208. </script>
  209. <style lang="scss" scoped>
  210. @import url("../../../passport/login.scss");
  211. .u-form-item {
  212. margin: 40rpx 0;
  213. }
  214. .sendCode {
  215. /deep/ .u-form-item--right__content__slot {
  216. display: flex;
  217. }
  218. }
  219. page {
  220. background: #fff;
  221. }
  222. .box {
  223. padding: 80rpx 0;
  224. border-radius: 20rpx;
  225. }
  226. .box-tips {
  227. margin: 0 72rpx;
  228. }
  229. .verification {
  230. font-size: 24rpx;
  231. color: #999;
  232. margin-top: 10rpx;
  233. }
  234. </style>