guangchao.xu | 50e4238 | 2021-01-04 17:53:47 +0800 | [diff] [blame] | 1 | <template> |
| 2 | <view class="bindEmail"> |
| 3 | <view class="bindEmail-field"> |
| 4 | <u-field v-model="email" label="邮箱" placeholder="请输入您的邮箱号" placeholder-style="color:#999999;font-family:PingFang-SC-Regular" |
| 5 | label-width="160" clear-size="40"></u-field> |
| 6 | <u-field v-model="code" label="验证码" placeholder="请输入验证码" clear-size="40" placeholder-style="color:#999999" |
| 7 | label-width="160" maxlength="6" type="number"> |
| 8 | <u-button size="mini" slot="right" type="primary" @click="isHaveEmail?getcode('unbind'):getcode('bind')" shape="circle" |
| 9 | :custom-style="btn" :disabled="isChecked">{{codeText}}</u-button> |
| 10 | </u-field> |
| 11 | </view> |
| 12 | <u-button class="bindEmail-btn" @click="isHaveEmail?unbind():bind()" :custom-style="isHaveEmail?cancel:confirm">{{isHaveEmail?'解除绑定':'绑定邮箱'}}</u-button> |
| 13 | </view> |
| 14 | </template> |
| 15 | |
| 16 | <script> |
| 17 | export default { |
| 18 | data() { |
| 19 | return { |
| 20 | email: '', |
| 21 | confirm: { |
| 22 | width: '600rpx', |
| 23 | backgroundColor: '#2FA8E1', |
| 24 | color: '#FFFFFF', |
| 25 | fontFamily: "PingFang-SC-Medium", |
| 26 | fontSize: '30rpx', |
| 27 | marginTop: '100rpx', |
| 28 | padding: '50rpx 0' |
| 29 | }, |
| 30 | cancel: { |
| 31 | width: '600rpx', |
| 32 | backgroundColor: '#fa3534', |
| 33 | color: '#FFFFFF', |
| 34 | fontFamily: "PingFang-SC-Medium", |
| 35 | fontSize: '30rpx', |
| 36 | marginTop: '100rpx', |
| 37 | padding: '50rpx 0' |
| 38 | }, |
| 39 | isChecked: false, |
| 40 | codeText: '获取验证码', |
| 41 | btn: { |
| 42 | color: '#2FA8E1', |
| 43 | backgroundColor: '#FFFFFF', |
| 44 | borderColor: '#2FA8E1', |
| 45 | borderWidth: '1rpx', |
| 46 | borderStyle: 'solid', |
| 47 | fontFamily: "PingFang-SC-Regular" |
| 48 | }, |
| 49 | code: '', |
| 50 | isHaveEmail: false |
| 51 | } |
| 52 | }, |
| 53 | onLoad() { |
| 54 | let isHaveEmail = !!uni.getStorageSync('email') |
| 55 | this.email = uni.getStorageSync('email') |
| 56 | this.isHaveEmail = isHaveEmail |
| 57 | }, |
| 58 | methods: { |
| 59 | bind() { |
| 60 | let that = this |
| 61 | let email = that.email |
| 62 | let code = that.code |
| 63 | let params = { |
| 64 | email, |
| 65 | code |
| 66 | } |
| 67 | if (!code || !email) { |
| 68 | uni.showToast({ |
| 69 | title: '请输入验证码', |
| 70 | icon: 'none', |
| 71 | }) |
| 72 | } |
| 73 | that.$u.post('/v1/email/bind', params).then(res => { |
| 74 | uni.showToast({ |
| 75 | title: "绑定成功", |
| 76 | icon: "none", |
| 77 | duration: 800, |
| 78 | complete(res) { |
guangchao.xu | c43cf97 | 2021-01-18 13:37:55 +0800 | [diff] [blame] | 79 | uni.setStorageSync('email', email) |
guangchao.xu | 50e4238 | 2021-01-04 17:53:47 +0800 | [diff] [blame] | 80 | setTimeout(() => { |
| 81 | uni.navigateBack({ |
| 82 | delta: 1 |
| 83 | }) |
| 84 | }, 1500) |
| 85 | } |
| 86 | }) |
| 87 | }) |
| 88 | }, |
| 89 | unbind() { |
| 90 | let that = this |
| 91 | let code = that.code |
| 92 | let params = { |
| 93 | code |
| 94 | } |
| 95 | if (!code) { |
| 96 | uni.showToast({ |
| 97 | title: '请输入验证码', |
| 98 | icon: 'none', |
| 99 | }) |
| 100 | } |
| 101 | that.$u.post('/v1/email/unbind', params).then(res => { |
| 102 | uni.showToast({ |
| 103 | title: "解绑成功", |
| 104 | icon: "none", |
| 105 | duration: 800, |
| 106 | complete(res) { |
| 107 | uni.setStorageSync('email', '') |
| 108 | setTimeout(() => { |
| 109 | uni.navigateBack({ |
| 110 | delta: 1 |
| 111 | }) |
| 112 | }, 1500) |
| 113 | } |
| 114 | }) |
| 115 | }) |
| 116 | }, |
| 117 | getcode(type) { |
| 118 | let that = this |
| 119 | let reg = /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/; |
| 120 | let email = that.email |
| 121 | if (!reg.test(email)) { |
| 122 | uni.showToast({ |
| 123 | title: '请输入正确的邮箱号', |
| 124 | icon: 'none', |
| 125 | }) |
| 126 | return |
| 127 | } |
| 128 | let params = { |
| 129 | email, |
| 130 | type |
| 131 | } |
| 132 | that.$u.post('/v1/email/send', params).then(res => { |
| 133 | uni.showToast({ |
| 134 | title: '发送成功,请去邮箱中获取验证码', |
| 135 | icon: "none", |
| 136 | duration: 800 |
| 137 | }) |
| 138 | let i = 120 |
| 139 | that.isChecked = true |
| 140 | that.timer = setInterval(() => { |
| 141 | if (i != 0) { |
| 142 | i-- |
| 143 | that.codeText = i + "s" |
| 144 | } else { |
| 145 | clearInterval(that.timer) |
| 146 | that.codeText = "重新获取" |
| 147 | that.isChecked = false |
| 148 | } |
| 149 | }, 1000) |
| 150 | }) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | </script> |
| 155 | |
| 156 | <style lang="scss" scoped> |
| 157 | .bindEmail { |
| 158 | &-field { |
| 159 | margin-top: 30rpx; |
| 160 | background-color: #FFFFFF; |
| 161 | } |
| 162 | } |
| 163 | </style> |