verification_phone.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <template>
  2. <view class="code-box">
  3. <hx-navbar left-text="" defaultBackUrl="update_phone" transparent="hidden" >
  4. <view slot="right" class="navRight">
  5. <button class="nextBtn" size="mini" @click="formSubmit">完成</button>
  6. </view>
  7. </hx-navbar>
  8. <view class="mRL">
  9. <view class="title">
  10. 输入短信验证码
  11. </view>
  12. <view class="content">
  13. 验证码已发送至{{mobile}},请在下方输入框内输入4位数字验证码。
  14. </view>
  15. <view class="flex-box margin-top">
  16. <input
  17. type="number"
  18. focus="true"
  19. :maxlength="maxlength"
  20. class="hide-input"
  21. @input="getVal"
  22. />
  23. <block v-for="(item, index) in ranges" :key="index">
  24. <view :class="['item', { active: codeIndex === item, middle: type === 'middle', bottom: type === 'bottom', box: type === 'box' }]">
  25. <view class="line" v-if="type !== 'middle'"></view>
  26. <view v-if="type === 'middle' && codeIndex <= item" class="bottom-line"></view>
  27. <block v-if="isPwd && codeArr.length >= item">
  28. <text class="dot">.</text>
  29. </block>
  30. <block v-else> {{ codeArr[index] ? codeArr[index] : ''}}</block>
  31. </view>
  32. </block>
  33. </view>
  34. <view class="timing-box margin-top">
  35. <block v-if="timing>0">
  36. <block v-if="postStatus"><text>正在发送短信验证...</text></block>
  37. <block v-else><text>{{timing}}秒后重新发送</text></block>
  38. </block>
  39. <block v-else>
  40. <view class="color-green" @click="resend">
  41. 重新发送
  42. </view>
  43. </block>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. watch: {
  51. maxlength: {
  52. immediate: true,
  53. handler: function(newV) {
  54. if (newV === 6) {
  55. this.ranges = [1, 2, 3, 4, 5, 6]
  56. } else {
  57. this.ranges = [1, 2, 3, 4]
  58. }
  59. }
  60. }
  61. },
  62. data() {
  63. return {
  64. codeIndex: 1,
  65. mobile: '',
  66. codeArr: [],
  67. ranges: [1, 2, 3, 4],
  68. maxlength:4,
  69. isPwd: false,
  70. type: "bottom",
  71. timing: 0,
  72. tNumber: 90,
  73. postStatus: false
  74. }
  75. },
  76. onLoad(option) {
  77. let reg =/^1[3-9]\d{9}$/;
  78. let that = this;
  79. if(!reg.exec(option.mobile)){
  80. uni.showToast({
  81. icon:'none',
  82. title: '手机号格式不正确!',
  83. duration: 2000
  84. })
  85. }else{
  86. this.mobile = option.mobile;
  87. that.resend();
  88. }
  89. },
  90. methods: {
  91. getVal(e) {
  92. let { value } = e.detail
  93. let arr = value.split('')
  94. this.codeIndex = arr.length + 1
  95. this.codeArr = arr
  96. if (this.codeIndex > Number(this.maxlength)) {
  97. //验证码输入完成以后的操作
  98. let code =this.codeArr.join('');
  99. console.log(code);
  100. this.updatePhone(code);
  101. }
  102. },
  103. //验证手机号
  104. updatePhone(c){
  105. let that = this;
  106. uni.showLoading({mask:true});
  107. this.$api.user.updatePhone({
  108. phone: that.mobile,
  109. code: c
  110. }).then((res)=>{
  111. uni.hideLoading();
  112. if(res.data.code == '0'){
  113. uni.showToast({
  114. title: '完成!',
  115. icon: 'success',
  116. duration: 2000,
  117. success:function(){
  118. //完成后返回至安全设置
  119. if(getCurrentPages() >= 3){
  120. uni.navigateBack({
  121. delta: 3
  122. });
  123. }else{
  124. uni.reLaunch({
  125. url: 'security'
  126. });
  127. }
  128. }
  129. });
  130. }
  131. }).catch((err)=>{
  132. uni.hideLoading();
  133. })
  134. },
  135. //重新发送验证码
  136. resend(){
  137. let that = this;
  138. if(!that.mobile){
  139. uni.showToast({
  140. icon:'none',
  141. title: '手机号格式不正确!',
  142. duration: 2000
  143. })
  144. return;
  145. }
  146. uni.showLoading({mask:true});
  147. this.postStatus = true
  148. this.$api.code.updatePhone({
  149. phone: that.mobile
  150. }).then((res)=>{
  151. uni.hideLoading();
  152. that.postStatus = false;
  153. if(res.data.code != '0'){
  154. uni.showToast({
  155. title: '获取验证码失败!',
  156. duration: 2000
  157. });
  158. }
  159. }).catch((e)=>{
  160. that.postStatus = false
  161. uni.hideLoading();
  162. that.timing = 0
  163. console.log("catch",e);
  164. })
  165. that.timing = that.tNumber;
  166. let t = setInterval(function(){
  167. that.timing = that.timing - 1
  168. if(that.timing <= 0){
  169. clearTimeout(t);
  170. }
  171. },1000)
  172. }
  173. }
  174. }
  175. </script>
  176. <style scoped>
  177. @keyframes twinkling {
  178. 0% {
  179. opacity: 0.2;
  180. }
  181. 50% {
  182. opacity: 0.5;
  183. }
  184. 100% {
  185. opacity: 0.2;
  186. }
  187. }
  188. .navRight{
  189. height: 100%;
  190. display: flex;
  191. justify-content: center;
  192. align-items: center;
  193. margin-right: 10upx;
  194. }
  195. .nextBtn{
  196. background: #07c160;
  197. color: #ffffff;
  198. border-color: #07c160;
  199. }
  200. .nextBtn:after{
  201. border-color: #07c160;
  202. }
  203. .color-green{
  204. color: #00C777;
  205. }
  206. .mRL{
  207. margin: 30upx;
  208. }
  209. .margin-top{
  210. margin-top: 30upx;
  211. }
  212. .title {
  213. font-size: 50upx;
  214. color: #555555;
  215. margin-bottom: 24upx;
  216. }
  217. .content {
  218. font-size: 32upx;
  219. color: #999999;
  220. }
  221. .timing-box{
  222. color: #aaaaaa;
  223. text-align: center;
  224. }
  225. .code-box {
  226. text-align: left;
  227. }
  228. .flex-box {
  229. display: flex;
  230. justify-content: center;
  231. flex-wrap: wrap;
  232. position: relative;
  233. text-align: center;
  234. }
  235. .flex-box .hide-input {
  236. position: absolute;
  237. top: 0;
  238. left: -100%;
  239. width: 200%;
  240. height: 100%;
  241. text-align: left;
  242. z-index: 9;
  243. opacity: 1;
  244. }
  245. .flex-box .item {
  246. position: relative;
  247. width: 100upx;
  248. height: 100upx;
  249. margin-right: 18upx;
  250. font-size: 70upx;
  251. color: #555555;
  252. line-height: 100upx;
  253. }
  254. .flex-box .item:last-child {
  255. margin-right: 0;
  256. }
  257. .flex-box .middle {
  258. border: none;
  259. }
  260. .flex-box .box {
  261. box-sizing: border-box;
  262. border: 2upx solid #cccccc;
  263. border-radius: 6upx;
  264. }
  265. .flex-box .bottom {
  266. box-sizing: border-box;
  267. border-bottom: 4upx solid #d2d2d2;
  268. }
  269. .flex-box .active {
  270. border-color: #00C777;
  271. }
  272. .flex-box .active .line {
  273. display: block;
  274. }
  275. .flex-box .line {
  276. display: none;
  277. position: absolute;
  278. left: 50%;
  279. top: 50%;
  280. transform: translate(-50%, -50%);
  281. width: 2upx;
  282. height: 40upx;
  283. background: #333333;
  284. animation: twinkling 1s infinite ease;
  285. }
  286. .flex-box .dot{
  287. font-size: 80upx;
  288. line-height: 40upx;
  289. }
  290. .flex-box .bottom-line {
  291. height: 4px;
  292. background: #000000;
  293. width: 80%;
  294. position: absolute;
  295. border-radius: 2px;
  296. top: 50%;
  297. left: 50%;
  298. transform: translate(-50%, -50%);
  299. }
  300. </style>