bindPhone.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
  6. <uni-forms-item label="手机号" name="phone">
  7. <uni-easyinput type="number" v-model="form.phone" placeholder="请输入您的手机号" />
  8. </uni-forms-item>
  9. <uni-forms-item label="手机验证码" name="code">
  10. <view class="yzm">
  11. <view class="l">
  12. <uni-easyinput type="text" v-model="form.code" placeholder="请输入您的手机验证码" />
  13. </view>
  14. <view class="r">
  15. <button type="default" size="mini" @tap="sendCount">{{time_count==0?'发送验证码':time_count}}</button>
  16. </view>
  17. </view>
  18. </uni-forms-item>
  19. </uni-forms>
  20. <view class="btn">
  21. <button type="primary" @click="onSubmit('form')" size="small">立即绑定</button>
  22. </view>
  23. <view class="Tips">提示:为了您的账号安全,请及时绑定手机号</view>
  24. </view>
  25. </view>
  26. </mobile-frame>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. user: {},
  33. form: {},
  34. rules: {
  35. phone: {
  36. rules: [{
  37. required: true,
  38. errorMessage: '请输入手机号',
  39. }]
  40. },
  41. code: {
  42. rules: [{
  43. required: true,
  44. errorMessage: '请输入手机验证码',
  45. }]
  46. },
  47. },
  48. // 倒计时
  49. time_count: 0
  50. };
  51. },
  52. onShow: function() {
  53. const that = this;
  54. that.watchLogin();
  55. },
  56. methods: {
  57. watchLogin() {
  58. const that = this;
  59. uni.getStorage({
  60. key: 'token',
  61. success: function(res) {
  62. let user = that.$jwt(res.data);
  63. if (user) that.$set(that, `user`, user);
  64. },
  65. fail: function(err) {
  66. console.log(err);
  67. }
  68. })
  69. },
  70. sendCount() {
  71. const that = this;
  72. let form = that.form;
  73. if (form && form.phone) {
  74. let time_count = 60;
  75. that.$set(that, `time_count`, time_count);
  76. that.timeDown();
  77. } else {
  78. uni.showToast({
  79. title: '输入错误,请重新输入!',
  80. icon: 'none'
  81. })
  82. }
  83. },
  84. // 倒计时
  85. timeDown() {
  86. const that = this;
  87. var times = setInterval(() => {
  88. that.time_count--;
  89. if (that.time_count <= 0) {
  90. clearInterval(times);
  91. }
  92. }, 1000);
  93. },
  94. // 提交保存
  95. onSubmit(ref) {
  96. const that = this;
  97. let user = that.user;
  98. that.$refs[ref].validate().then(async params => {
  99. let code = '1234';
  100. if (params.code == code) {
  101. let res = await that.$api(`/user/${user._id}`, 'POST', params);
  102. if (res.errcode == '0') {
  103. uni.showToast({
  104. title: '手机号绑定成功',
  105. icon: 'none'
  106. })
  107. uni.navigateBack({
  108. delta: 1
  109. })
  110. } else {
  111. uni.showToast({
  112. title: res.errms,
  113. icon: 'none'
  114. })
  115. }
  116. } else {
  117. uni.showToast({
  118. title: '验证码输入错误!',
  119. icon: 'none'
  120. })
  121. }
  122. })
  123. },
  124. }
  125. }
  126. </script>
  127. <style lang="scss">
  128. .main {
  129. display: flex;
  130. flex-direction: column;
  131. width: 100vw;
  132. height: 100vh;
  133. .one {
  134. padding: 2vw;
  135. .btn {
  136. text-align: center;
  137. button {
  138. margin: 0 2vw 2vw 2vw;
  139. background-color: var(--ff0Color);
  140. color: var(--fffColor);
  141. }
  142. .name {
  143. color: var(--f85Color);
  144. font-size: var(--font14Size);
  145. }
  146. }
  147. .Tips {
  148. font-size: var(--font12Size);
  149. color: var(--f85Color);
  150. }
  151. }
  152. }
  153. .uni-forms-item {
  154. margin-bottom: 6vw !important;
  155. display: flex;
  156. flex-direction: row;
  157. }
  158. .yzm {
  159. display: flex;
  160. flex-direction: row;
  161. justify-content: space-between;
  162. .l {
  163. flex-grow: 1;
  164. }
  165. .r {
  166. width: 20vw;
  167. button {
  168. width: 100%;
  169. line-height: 2.8;
  170. padding: 0 1vw;
  171. }
  172. }
  173. }
  174. </style>