123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <template>
- <view class="vk-data-verification-code">
- <view @click="sendSmsCode" :style="customStyle">
- <slot :tips="tips" :sec-num="secNum">{{ tips }}</slot>
- </view>
- </view>
- </template>
- <script>
- const localeObj = {
- "zh-Hans":{
- "startText":"获取验证码",
- "changeText":"X秒重新获取",
- "endText":"重新获取",
- "tryAgainInSeconds":"秒后再重试",
- "pleaseEnterTheCorrectMobileNumber":"请输入正确的手机号码",
- "sending":"发送中...",
- "verificationCodeSent":"验证码已发送",
- "triggerDayLevelFlowControl":"触发天级流控",
- "pleaseTryAgainTomorrow":"短信发送频繁,请明日再试!",
- "pleaseTryAgainIn1Hour":"短信发送频繁,请过1小时后再试!",
- "triggerMinuteLevelFlowControl":"触发分钟级流控",
- "pleaseTryAgainLater":"短信发送频繁,请稍后再试!",
- },
- "zh-Hant":{
- "startText":"獲取驗證碼",
- "changeText":"X秒重新獲取",
- "endText":"重新獲取",
- "tryAgainInSeconds":"秒後再重試",
- "pleaseEnterTheCorrectMobileNumber":"請輸入正確的手機號碼",
- "sending":"發送中...",
- "verificationCodeSent":"驗證碼已發送",
- "triggerDayLevelFlowControl":"觸發天級流控",
- "pleaseTryAgainTomorrow":"短信發送頻繁,請明日再試!",
- "pleaseTryAgainIn1Hour":"短信發送頻繁,請過1小時後再試!",
- "triggerMinuteLevelFlowControl":"觸發分鐘級流控",
- "pleaseTryAgainLater":"短信發送頻繁,請稍後再試!",
- },
- "en":{
- "startText":"Get code",
- "changeText":"X second reacquire",
- "endText":"Reacquire",
- "tryAgainInSeconds":"Try again in seconds",
- "pleaseEnterTheCorrectMobileNumber":"Please enter the correct mobile number",
- "sending":"Sending...",
- "verificationCodeSent":"Verification code sent",
- "triggerDayLevelFlowControl":"Trigger day level flow control",
- "pleaseTryAgainTomorrow":"SMS sent frequently, please try again tomorrow!",
- "pleaseTryAgainIn1Hour":"SMS sent frequently, please try again in 1 hour",
- "triggerMinuteLevelFlowControl":"Trigger minute level flow control",
- "pleaseTryAgainLater":"SMS sent frequently, please try again later",
- }
- };
- export default {
- name: 'vk-data-verification-code',
- emits: ['start', 'end', 'change','send','codeChange'],
- props: {
-
- mode: {
- type: String,
- default: 'mobile'
- },
-
- mobile: {
- type: String
- },
-
- type: {
- type: String,
- default: 'login'
- },
- customStyle: {
- type: [String,Object]
- },
-
- seconds: {
- type: [String, Number],
- default: 60
- },
-
- startText: {
- type: String,
- default: ''
- },
-
- changeText: {
- type: String,
- default: ''
- },
-
- endText: {
- type: String,
- default: ''
- },
-
- keepRunning: {
- type: Boolean,
- default: false
- },
-
- uniqueKey: {
- type: String,
- default: ''
- }
- },
- data() {
- return {
- secNum: this.seconds,
- timer: null,
- canGetCode: true,
- tips: '',
- locale:{},
- };
- },
- created(){
- if (typeof vk !== "undefined") {
- let locale = vk.pubfn.getLocale();
- this.locale = localeObj[locale];
- } else {
- this.locale = localeObj["zh-Hans"];
- }
- },
- mounted() {
- this.checkKeepRunning();
- },
- watch: {
- seconds: {
- immediate: true,
- handler(n) {
- this.secNum = n;
- }
- }
- },
-
- computed:{
- startTextCom(){
- return this.startText || this.locale.startText || "获取验证码";
- },
- changeTextCom(){
- return this.changeText || this.locale.changeText || "X秒重新获取";
- },
- endTextCom(){
- return this.endText || this.locale.endText || "重新获取";
- }
- },
- methods: {
- checkKeepRunning() {
-
- let lastTimestamp = Number(uni.getStorageSync(this.uniqueKey + '_$uCountDownTimestamp'));
- if (!lastTimestamp) return this.changeEvent(this.startTextCom);
-
- let nowTimestamp = Math.floor(+new Date() / 1000);
-
- if (this.keepRunning && lastTimestamp && lastTimestamp > nowTimestamp) {
-
- this.secNum = lastTimestamp - nowTimestamp;
-
- uni.removeStorageSync(this.uniqueKey + '_$uCountDownTimestamp');
-
- this.start();
- } else {
-
- this.changeEvent(this.startTextCom);
- }
- },
-
- start() {
-
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- this.$emit('start');
- this.canGetCode = false;
-
- this.changeEvent(this.changeTextCom.replace(/x|X/, this.secNum));
- this.setTimeToStorage();
- this.timer = setInterval(() => {
- if (--this.secNum) {
-
- this.changeEvent(this.changeTextCom.replace(/x|X/, this.secNum));
- } else {
- clearInterval(this.timer);
- this.timer = null;
- this.changeEvent(this.endTextCom);
- this.secNum = this.seconds;
- this.$emit('end');
- this.canGetCode = true;
- }
- }, 1000);
- },
-
- reset(text) {
- this.canGetCode = true;
- clearInterval(this.timer);
- this.secNum = this.seconds;
- this.changeEvent(text || this.endTextCom);
- },
- changeEvent(text) {
- this.codeChange(text);
- this.$emit('change', text);
- },
-
- setTimeToStorage() {
- if (!this.keepRunning || !this.timer) return;
-
-
- if (this.secNum > 0 && this.secNum <= this.seconds) {
-
- let nowTimestamp = Math.floor(+new Date() / 1000);
-
- uni.setStorage({
- key: this.uniqueKey + '_$uCountDownTimestamp',
- data: nowTimestamp + Number(this.secNum)
- });
- }
- },
-
- codeChange(text) {
- this.tips = text;
- this.$emit('codeChange', text);
- },
-
- sendSmsCode() {
- let that = this;
- let vk = uni.vk;
- if (!that.canGetCode) {
- vk.toast(`${that.secNum}${that.locale.tryAgainInSeconds}`, 'none');
- return;
- }
- if (that.mode === "custom") {
- that.$emit("send", { type: that.type });
- return;
- }
- let mobile = that.mobile;
- let type = that.type;
- if (!vk.pubfn.test(mobile, 'mobile')) {
- vk.toast(that.locale.pleaseEnterTheCorrectMobileNumber, 'none');
- return;
- }
- that.tips = that.locale.sending;
- vk.userCenter.sendSmsCode({
- needAlert: false,
- data: {
- mobile,
- type
- },
- success: function(data) {
- vk.toast(that.locale.verificationCodeSent);
- that.start();
- },
- fail: function(err) {
- that.tips = that.startTextCom;
- if (err.errMsg && err.errMsg.indexOf('触发天级流控') > -1) {
- vk.alert(that.locale.pleaseTryAgainTomorrow);
- } else if (err.errMsg && err.errMsg.indexOf('触发小时级流控') > -1) {
- vk.alert(that.locale.pleaseTryAgainIn1Hour);
- } else if (err.errMsg && err.errMsg.indexOf('触发分钟级流控') > -1) {
- vk.alert(that.locale.pleaseTryAgainLater);
- } else if (err.msg) {
- vk.alert(err.msg);
- } else {
- vk.alert(that.locale.pleaseTryAgainLater);
- }
- }
- });
- }
- },
-
-
- beforeDestroy() {
- this.setTimeToStorage();
- clearTimeout(this.timer);
- this.timer = null;
- },
-
-
- beforeUnmount() {
- this.setTimeToStorage();
- clearTimeout(this.timer);
- this.timer = null;
- }
-
- };
- </script>
- <style lang="scss" scoped></style>
|