bindEmail.vue 3.8 KB

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