bindPhone.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <uni-forms ref="form" :rules="rules" :model="form" 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="number" 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. onLoad: 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. async sendCount() {
  71. const that = this;
  72. let form = that.form;
  73. let user = that.user;
  74. if (form && form.phone) {
  75. let res = await that.$api(`/user/toBindPhone`, 'POST', {
  76. id: user._id,
  77. phone: form.phone
  78. })
  79. if (res.errcode == '0') {
  80. uni.showToast({
  81. title: '发送成功'
  82. })
  83. that.$set(that, `time_count`, 60);
  84. that.timeDown();
  85. } else {
  86. uni.showToast({
  87. title: res.errmsg,
  88. icon: 'none'
  89. })
  90. }
  91. } else {
  92. uni.showToast({
  93. title: '输入错误,请重新输入!',
  94. icon: 'none'
  95. })
  96. }
  97. },
  98. // 倒计时
  99. timeDown() {
  100. const that = this;
  101. var times = setInterval(() => {
  102. that.time_count--;
  103. if (that.time_count <= 0) {
  104. clearInterval(times);
  105. }
  106. }, 1000);
  107. },
  108. // 提交保存
  109. onSubmit(ref) {
  110. const that = this;
  111. let user = that.user;
  112. that.$refs[ref].validate().then(async params => {
  113. params.id = user._id;
  114. const res = await that.$api(`/user/checkBindPhone`, 'POST', params);
  115. if (res.errcode == '0') {
  116. const arr = await that.$api(`/user/${params.id}`, 'POST', params);
  117. if (arr.errcode == '0') {
  118. uni.showToast({
  119. title: '绑定手机号成功',
  120. icon: 'none'
  121. })
  122. uni.navigateBack({
  123. delta: 1
  124. })
  125. } else {
  126. uni.showToast({
  127. title: arr.errmsg,
  128. icon: 'none'
  129. })
  130. }
  131. } else {
  132. uni.showToast({
  133. title: res.errmsg,
  134. icon: 'none'
  135. })
  136. }
  137. })
  138. },
  139. }
  140. }
  141. </script>
  142. <style lang="scss">
  143. .main {
  144. display: flex;
  145. flex-direction: column;
  146. width: 100vw;
  147. height: 100vh;
  148. .one {
  149. padding: 2vw;
  150. .btn {
  151. text-align: center;
  152. button {
  153. margin: 0 2vw 2vw 2vw;
  154. background-color: var(--fFB1Color);
  155. color: var(--fffColor);
  156. }
  157. .name {
  158. color: var(--f85Color);
  159. font-size: var(--font14Size);
  160. }
  161. }
  162. .Tips {
  163. font-size: var(--font12Size);
  164. color: var(--f85Color);
  165. }
  166. }
  167. }
  168. .uni-forms-item {
  169. margin-bottom: 6vw !important;
  170. display: flex;
  171. flex-direction: row;
  172. }
  173. .yzm {
  174. display: flex;
  175. flex-direction: row;
  176. justify-content: space-between;
  177. .l {
  178. flex-grow: 1;
  179. }
  180. .r {
  181. width: 20vw;
  182. button {
  183. width: 100%;
  184. line-height: 2.8;
  185. padding: 0 1vw;
  186. }
  187. }
  188. }
  189. </style>