33744e3d3e2f3b78332e5a895f1ad397a451f199.svn-base 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div>
  3. <a-form-model ref="form" :model="model" :rules="validatorRules">
  4. <a-form-model-item required prop="mobile">
  5. <a-input v-model="model.mobile" size="large" type="text" placeholder="请输入手机号">
  6. <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  7. </a-input>
  8. </a-form-model-item>
  9. <a-row :gutter="16">
  10. <a-col class="gutter-row" :span="16">
  11. <a-form-model-item required prop="captcha">
  12. <a-input v-model="model.captcha" size="large" type="text" placeholder="请输入验证码">
  13. <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  14. </a-input>
  15. </a-form-model-item>
  16. </a-col>
  17. <a-col class="gutter-row" :span="8">
  18. <a-button
  19. class="getCaptcha"
  20. tabindex="-1"
  21. :disabled="state.smsSendBtn"
  22. @click.stop.prevent="getCaptcha"
  23. v-text="!state.smsSendBtn && '获取验证码' || (state.time+' s')"></a-button>
  24. </a-col>
  25. </a-row>
  26. </a-form-model>
  27. </div>
  28. </template>
  29. <script>
  30. import { postAction } from '@/api/manage'
  31. import { mapActions } from 'vuex'
  32. export default {
  33. name: 'LoginPhone',
  34. data(){
  35. return {
  36. model:{
  37. mobile: '',
  38. captcha: ''
  39. },
  40. //手机号登录用
  41. state: {
  42. time: 60,
  43. smsSendBtn: false,
  44. },
  45. validatorRules:{
  46. mobile: [
  47. { required: true, message: '请输入手机号码!' },
  48. { validator: this.validateMobile }
  49. ],
  50. captcha: [{
  51. required: true, message: '请输入验证码!'
  52. }]
  53. }
  54. }
  55. },
  56. methods:{
  57. ...mapActions(['PhoneLogin']),
  58. handleLogin(rememberMe){
  59. this.validateFields([ 'mobile', 'captcha' ], (err) => {
  60. if (!err) {
  61. let loginParams = {
  62. mobile: this.model.mobile,
  63. captcha: this.model.captcha,
  64. remember_me: rememberMe
  65. }
  66. console.log("登录参数", loginParams)
  67. this.PhoneLogin(loginParams).then((res) => {
  68. this.$emit('success', res.result)
  69. }).catch((err) => {
  70. this.$emit('fail', err)
  71. });
  72. }else{
  73. this.$emit('validateFail')
  74. }
  75. })
  76. },
  77. // 校验手机号
  78. validateMobile(rule,value,callback){
  79. if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)){
  80. callback();
  81. }else{
  82. callback("您的手机号码格式不正确!");
  83. }
  84. },
  85. //获取验证码
  86. getCaptcha (e) {
  87. e.preventDefault();
  88. let that = this;
  89. that.validateFields([ 'mobile' ], (err) => {
  90. if (!err) {
  91. that.state.smsSendBtn = true;
  92. let interval = window.setInterval(() => {
  93. if (that.state.time-- <= 0) {
  94. that.state.time = 60;
  95. that.state.smsSendBtn = false;
  96. window.clearInterval(interval);
  97. }
  98. }, 1000);
  99. const hide = that.$message.loading('验证码发送中..', 0);
  100. let smsParams = {};
  101. smsParams.mobile=that.model.mobile;
  102. smsParams.smsmode="0";
  103. postAction("/sys/sms",smsParams)
  104. .then(res => {
  105. if(!res.success){
  106. setTimeout(hide, 0);
  107. that.cmsFailed(res.message);
  108. }
  109. console.log(res);
  110. setTimeout(hide, 500);
  111. })
  112. .catch(err => {
  113. setTimeout(hide, 1);
  114. clearInterval(interval);
  115. that.state.time = 60;
  116. that.state.smsSendBtn = false;
  117. that.requestFailed(err);
  118. });
  119. }
  120. }
  121. );
  122. },
  123. cmsFailed(err){
  124. this.$notification[ 'error' ]({
  125. message: '获取验证码失败',
  126. description:err,
  127. duration: 4,
  128. });
  129. },
  130. /**
  131. * 验证字段
  132. * @param arr
  133. * @param callback
  134. */
  135. validateFields(arr, callback){
  136. let promiseArray = []
  137. for(let item of arr){
  138. let p = new Promise((resolve, reject) => {
  139. this.$refs['form'].validateField(item, (err)=>{
  140. if(!err){
  141. resolve();
  142. }else{
  143. reject(err);
  144. }
  145. })
  146. });
  147. promiseArray.push(p)
  148. }
  149. Promise.all(promiseArray).then(()=>{
  150. callback()
  151. }).catch(err=>{
  152. callback(err)
  153. })
  154. },
  155. }
  156. }
  157. </script>
  158. <style scoped>
  159. .getCaptcha{
  160. display: block;
  161. width: 100%;
  162. height: 40px;
  163. }
  164. </style>