test_lgq/niucloud/public/wap/assets/sms-code.vue_vue_type_script_setup_true_lang.55fd252c.js
2024-01-24 17:36:08 +08:00

2 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{U as e,V as t,W as a,h as s,i as n,w as i,l as c,t as o,r,Y as l,c as u,a7 as h,a8 as d,d as m,a as p,p as g,q as f,E as v,j as x,J as y,K as T,m as _,H as C,F as S,a9 as b,L as k}from"./index-849cf30e.js";import{_ as w}from"./_plugin-vue_export-helper.1b428a4d.js";import{_ as N}from"./u-input.46b72faa.js";import{_ as E}from"./u-modal.081e9589.js";const G=w({name:"u-code",mixins:[t,a,{props:{seconds:{type:[String,Number],default:e.code.seconds},startText:{type:String,default:e.code.startText},changeText:{type:String,default:e.code.changeText},endText:{type:String,default:e.code.endText},keepRunning:{type:Boolean,default:e.code.keepRunning},uniqueKey:{type:String,default:e.code.uniqueKey}}}],data(){return{secNum:this.seconds,timer:null,canGetCode:!0}},mounted(){this.checkKeepRunning()},watch:{seconds:{immediate:!0,handler(e){this.secNum=e}}},methods:{checkKeepRunning(){let e=Number(uni.getStorageSync(this.uniqueKey+"_$uCountDownTimestamp"));if(!e)return this.changeEvent(this.startText);let t=Math.floor(+new Date/1e3);this.keepRunning&&e&&e>t?(this.secNum=e-t,uni.removeStorageSync(this.uniqueKey+"_$uCountDownTimestamp"),this.start()):this.changeEvent(this.startText)},start(){this.timer&&(clearInterval(this.timer),this.timer=null),this.$emit("start"),this.canGetCode=!1,this.changeEvent(this.changeText.replace(/x|X/,this.secNum)),this.setTimeToStorage(),this.timer=setInterval((()=>{--this.secNum?this.changeEvent(this.changeText.replace(/x|X/,this.secNum)):(clearInterval(this.timer),this.timer=null,this.changeEvent(this.endText),this.secNum=this.seconds,this.$emit("end"),this.canGetCode=!0)}),1e3)},reset(){this.canGetCode=!0,clearInterval(this.timer),this.secNum=this.seconds,this.changeEvent(this.endText)},changeEvent(e){this.$emit("change",e)},setTimeToStorage(){if(this.keepRunning&&this.timer&&this.secNum>0&&this.secNum<=this.seconds){let e=Math.floor(+new Date/1e3);uni.setStorage({key:this.uniqueKey+"_$uCountDownTimestamp",data:e+Number(this.secNum)})}}},beforeDestroy(){this.setTimeToStorage(),clearTimeout(this.timer),this.timer=null}},[["render",function(e,t,a,r,l,u){const h=o;return s(),n(h,{class:"u-code"},{default:i((()=>[c(" 此组件功能由js完成无需写html逻辑 ")])),_:1})}],["__scopeId","data-v-198ccd63"]]);function $(e){const t=r("");return{image:t,refresh:async()=>{try{const a=await d();e.captcha_key=a.data.captcha_key,e.captcha_code="",t.value=a.data.img.replace(/\r\n/g,"")}catch(a){}}}}const K=m({__name:"sms-code",props:{mobile:String,type:String,modelValue:{type:String,default:""}},emits:["update:modelValue"],setup(e,{emit:t}){const a=e,n=u({get:()=>a.modelValue,set(e){t("update:modelValue",e)}}),c=r(null),d=function(e){const t=r(l("getSmsCode")),a="X"+l("smsCodeChangeText"),s=u((()=>!e.value||e.value.canGetCode));return{tips:t,seconds:90,canGetCode:s,send:async t=>{if(!s.value)return;e.value.start();let a=!1;return await h(t).then((t=>{1==t.code?a=t.data.key:(e.value.reset(),a=!1)})).catch((t=>{a=!1,e.value.reset()})),a},codeChange:e=>{t.value=e},changeText:a}}(c),m=r(!1),w=p({mobile:"",captcha_code:"",captcha_key:"",type:a.type}),K=$(w),V=async()=>{if(c.value.canGetCode){if(w.mobile=a.mobile,uni.$u.test.isEmpty(w.mobile))return void b({title:l("mobilePlaceholder"),icon:"none"});if(!uni.$u.test.mobile(w.mobile))return void b({title:l("mobileError"),icon:"none"});await K.refresh(),m.value=!0}},R=async()=>{if(uni.$u.test.isEmpty(w.captcha_code))return void b({title:l("captchaPlaceholder"),icon:"none"});const e=await d.send(w);e?(n.value=e,m.value=!1):!1===e&&K.refresh()};return(e,t)=>{const a=o,n=g(f("u-code"),G),r=g(f("u-input"),N),u=k,h=g(f("u-modal"),E);return s(),v(S,null,[x(a,{class:C({"text-primary":_(d).canGetCode.value,"text-gray-300":!_(d).canGetCode.value}),onClick:V},{default:i((()=>[y(T(_(d).tips.value),1)])),_:1},8,["class"]),x(n,{seconds:_(d).seconds,"change-text":_(d).changeText,ref_key:"smsRef",ref:c,onChange:_(d).codeChange},null,8,["seconds","change-text","onChange"]),x(h,{show:m.value,title:_(l)("captchaTitle"),"confirm-text":_(l)("confirm"),"cancel-text":_(l)("cancel"),"show-cancel-button":!0,onCancel:t[2]||(t[2]=e=>m.value=!1),onConfirm:R},{default:i((()=>[x(a,{class:"flex mt-[20rpx]"},{default:i((()=>[x(r,{placeholder:_(l)("captchaPlaceholder"),border:"surround",modelValue:w.captcha_code,"onUpdate:modelValue":t[0]||(t[0]=e=>w.captcha_code=e)},null,8,["placeholder","modelValue"]),x(u,{src:_(K).image.value,class:"h-[76rpx] ml-[20rpx]",mode:"heightFix",onClick:t[1]||(t[1]=e=>_(K).refresh())},null,8,["src"])])),_:1})])),_:1},8,["show","title","confirm-text","cancel-text"])],64)}}});export{K as _,$ as u};