bind-phone.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <view class="bind-phone">
  3. <custom-nav ref="ltm" :title="title" />
  4. <view class="content">
  5. <view class="form">
  6. <navigator url="../phone-area/phone-area" class="phone-area">
  7. <text class="text">国家/地区</text>
  8. <text class="area">{{ areaName }}({{ areaCode === '86' ? '+' : '' }}{{ areaCode }})</text>
  9. <text class="icon cuIcon-right"></text>
  10. </navigator>
  11. <view class="item">
  12. <text class="icon cuIcon-mobilefill"></text>
  13. <input maxlength="11" type="number" v-model="phone" placeholder="请输入手机号" />
  14. <text v-if="phone" class="cuIcon-roundclosefill" @tap="clearPhone"></text>
  15. </view>
  16. <view class="item">
  17. <text class="icon cuIcon-markfill"></text>
  18. <input maxlength="6" type="number" v-model="verifycode" placeholder="请输入6位验证码" />
  19. <text v-if="verifycode" class="cuIcon-roundclosefill" @tap="clearVerify"></text>
  20. <view class="countdown" @tap="getCode">
  21. 发送验证码 <text v-if="countDown">{{ ' ' + countDown }}</text>
  22. </view>
  23. </view>
  24. <view v-if="isREG" class="item">
  25. <text class="icon cuIcon-medalfill"></text>
  26. <input maxlength="6" type="text" v-model="invitecode" placeholder="请输入6位邀请码" />
  27. <text v-if="invitecode" class="cuIcon-roundclosefill" @tap="clearInvite"></text>
  28. </view>
  29. </view>
  30. <view class="big-btn bg" @tap="bind">立即绑定</view>
  31. <view class="aggree">注册即代表您同意<navigator url="../user-agreement/user-agreement" class="basecolor">《大卫博士会员隐私协议》</navigator></view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { _API_GetVerifyCode, _API_Reg, _API_Login } from '@/apis/verify.js'
  37. export default {
  38. data() {
  39. return {
  40. title: '手机绑定',
  41. phone: '', // 手机号
  42. verifycode: '', // 验证码
  43. invitecode: '', // 邀请码
  44. countDown: 0, // 倒计时
  45. isREG: false, // 是否是注册
  46. areaName: '中国大陆', // 手机号地区名称
  47. areaCode: '86', // 手机号地区代码
  48. wx: {} // 要绑定的微信信息
  49. }
  50. },
  51. created() {
  52. uni.$on('CHOOSEPHONECODE', (name, code) => { // 监听 chooseArea 事件更新
  53. this.areaName = name // 修改选择的手机号地区名称
  54. this.areaCode = code // 修改选择的手机号地区代码
  55. })
  56. },
  57. onLoad(opt) {
  58. this.wx = JSON.parse(opt.wx) // 获取微信信息
  59. },
  60. methods: {
  61. clearPhone() { setTimeout(() => this.phone = '') }, // 清空手机号
  62. clearVerify() { setTimeout(() => this.verifycode = '') }, // 清空验证码
  63. clearInvite() { setTimeout(() => this.invitecode = '') }, // 清空邀请码
  64. getCode() { // 点击获取手机验证码
  65. if (this.phone.length) { // 手机号已经输入
  66. if (this.countDown) { // 如果正在倒计时
  67. this.$refs.ltm.toast('验证码已发送,请稍后重试')
  68. } else {
  69. this.$refs.ltm.loading() // 显示 loading
  70. _API_GetVerifyCode({ mobile: this.phone, code: this.areaCode }).then(res => { // 发送网络请求
  71. if (res.code === 200) {
  72. if (res.data.status == 0) { // 登录 // 0、 1 代表用户是登录 或 注册
  73. this.isREG = false // 隐藏邀请码框
  74. } else {
  75. this.isREG = true // 显示邀请码框
  76. }
  77. this.$refs.ltm.toast('验证码发送成功') // 提示邀请码发送成功
  78. this.countDown = 120 // 倒计时时长
  79. this.timer = setInterval(() => { // 开始倒计时
  80. this.countDown --
  81. if (this.countDown <= 0) { // 倒计时结束清除倒计时
  82. this.countDown = 0
  83. clearInterval(this.timer)
  84. }
  85. }, 1000)
  86. } else if (res.code === 300) { // code 300 表示禁止获取验证码,禁止登陆
  87. this.$refs.ltm.toast('获取验证码失败')
  88. }
  89. })
  90. }
  91. } else {
  92. this.$refs.ltm.toast('手机号格式不正确,请重新输入')
  93. }
  94. },
  95. bind() { // 绑定手机
  96. if (!this.phone.length) { this.$refs.ltm.toast('手机号格式不正确,请重新输入'); return }
  97. if (!(this.verifycode.length == 6)) { this.$refs.ltm.toast('验证码格式不正确,请重新输入'); return }
  98. if (this.isREG && this.invitecode.length != 6) { this.$refs.ltm.toast('请输入正确的邀请码'); return }
  99. this.$refs.ltm.loading() // 显示loading
  100. if (this.isREG) { // 如果是注册
  101. _API_Reg({ mobile: this.phone, verify_code: this.verifycode, recom_code: this.invitecode, code: this.areaCode, ...this.wx }).then(res => {
  102. if (res.code === 200) { // 注册成功
  103. this.$store.commit('app/LOGIN', res.data.token) // 保存 token
  104. uni.reLaunch({ url: '/pages/index-index/index-index' }) // 跳转到首页
  105. } else if (res.code === 300) { // 验证码不正确
  106. this.$refs.ltm.toast('验证码错误')
  107. } else if (res.code === 400) { // 邀请码不存在
  108. this.$refs.ltm.toast('邀请码不存在,请联系邀请人确认邀请码是否正确')
  109. }
  110. })
  111. } else { // 如果是登陆
  112. _API_Login({ mobile: this.phone, verify_code: this.verifycode, ...this.wx }).then(res => {
  113. if (res.code === 200) { // 登录成功
  114. this.$store.commit('app/LOGIN', res.data.token) // 保存 token
  115. uni.reLaunch({ url: '/pages/index-index/index-index' }) // 跳转到首页
  116. } else if (res.code === 300) { // 验证码不正确
  117. this.$refs.ltm.toast('验证码错误')
  118. } else if (res.code === 300) { // 验证码不正确
  119. this.$refs.ltm.toast('验证码失效')
  120. }
  121. })
  122. }
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss">
  128. .bind-phone {
  129. @include page();
  130. .content {
  131. height: 100%;
  132. background: #FFFFFF;
  133. padding: 30rpx 60rpx;
  134. box-sizing: border-box;
  135. .form {
  136. .phone-area {
  137. @include flex();
  138. justify-content: space-between;
  139. .text {
  140. font-size: 32rpx;
  141. }
  142. .area {
  143. color: #42b983;
  144. }
  145. }
  146. .item {
  147. @include flex();
  148. height: 110rpx;
  149. padding-top: 20rpx;
  150. box-sizing: border-box;
  151. color: $app-sec-text-color;
  152. border-bottom: 1rpx solid #B2B2B2;
  153. .icon, .cuIcon-roundclosefill {
  154. margin: 0 20rpx;
  155. font-size: 36rpx;
  156. }
  157. input {
  158. flex: 1;
  159. height: 84rpx;
  160. margin-left: 15rpx;
  161. }
  162. .countdown {
  163. @include flex();
  164. height: 50rpx;
  165. font-size: 24rpx;
  166. padding: 0 20rpx;
  167. background: #FFEFF0;
  168. margin-right: 20rpx;
  169. border-radius: 33rpx;
  170. color: $app-base-color;
  171. text {
  172. margin-left: 8rpx;
  173. }
  174. }
  175. }
  176. }
  177. .aggree {
  178. margin-top: 30rpx;
  179. text-align: center;
  180. font-size: 22rpx;
  181. navigator {
  182. display: inline-block;
  183. }
  184. }
  185. }
  186. }
  187. </style>