verif.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <view>
  3. <u-code ref="uCode" @change="codeChange"></u-code>
  4. <view class="tips" @tap="getCode">{{ tips }}</view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. phone:{
  11. type: [String,Number],
  12. default: ''
  13. },
  14. flag:{
  15. type: [String,Number],
  16. default: 1
  17. }
  18. },
  19. data() {
  20. return {
  21. tips:'获取验证码'
  22. };
  23. },
  24. methods: {
  25. codeChange(text) {
  26. this.tips = text;
  27. },
  28. getCode() {
  29. console.log(Boolean(this.phone))
  30. let phoneReg = /^(1[3-9])\d{9}$/;
  31. if (!phoneReg.test(this.phone)) {
  32. return this.$api.toast('手机号码不合法!');
  33. }
  34. if (!this.phone) {
  35. return this.$api.toast('请输入手机号');
  36. }
  37. if (this.$refs.uCode.canGetCode) {
  38. // 模拟向后端请求验证码
  39. uni.showLoading({
  40. title: '正在获取验证码'
  41. });
  42. this.$api
  43. .request('common/sendSms', {
  44. phone: this.phone,
  45. flag: 3 ,// 1
  46. })
  47. .then(data => {
  48. if (data.code == '200') {
  49. uni.hideLoading();
  50. // 这里此提示会被this.start()方法中的提示覆盖
  51. // this.$u.toast('验证码已发送');
  52. // 通知验证码组件内部开始倒计时
  53. this.$refs.uCode.start();
  54. } else if (data.code == '204') {
  55. this.$api.toast(data.msg);
  56. }
  57. })
  58. .catch(function(error) {
  59. this.$api.toast(error);
  60. });
  61. } else {
  62. this.$u.toast('倒计时结束后再发送');
  63. }
  64. }
  65. }
  66. };
  67. </script>
  68. <style lang="scss">
  69. //yanzhengma
  70. .u-hairline-border[data-v-3bf2dba7]:after {
  71. border: 0;
  72. }
  73. .tips {
  74. padding: 0;
  75. color: #0BB291;
  76. font-size: 28rpx;
  77. line-height: 40rpx;
  78. height: 40rpx;
  79. }
  80. </style>