index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="container">
  3. <!-- 页面头部 -->
  4. <view class="header">
  5. <view class="title">
  6. <text>绑定您的手机号</text>
  7. </view>
  8. <view class="sub-title">
  9. <text>为了更好的服务您,请绑定手机号</text>
  10. </view>
  11. </view>
  12. <!-- 表单 -->
  13. <view class="submit-form">
  14. <!-- 手机号 -->
  15. <view class="form-item">
  16. <input class="form-item--input" type="number" v-model="mobile" maxlength="11" placeholder="请输入手机号码" />
  17. </view>
  18. <!-- 图形验证码 -->
  19. <view class="form-item">
  20. <input class="form-item--input" type="text" v-model="captchaCode" maxlength="5" placeholder="请输入图形验证码" />
  21. <view class="form-item--parts">
  22. <view class="captcha" @click="getCaptcha()">
  23. <image class="image" :src="captcha.base64"></image>
  24. </view>
  25. </view>
  26. </view>
  27. <!-- 短信验证码 -->
  28. <view class="form-item">
  29. <input class="form-item--input" type="number" v-model="smsCode" maxlength="6" placeholder="请输入短信验证码" />
  30. <view class="form-item--parts">
  31. <view class="captcha-sms" @click="handelSmsCaptcha()">
  32. <text v-if="!smsState" class="activate">获取验证码</text>
  33. <text v-else class="un-activate">重新发送({{ times }})秒</text>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 确认绑定 -->
  38. <view class="submit-button" @click="handleSubmit()">
  39. <text>确认绑定</text>
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import store from '@/store'
  46. import * as UserApi from '@/api/user'
  47. import * as CaptchaApi from '@/api/captcha'
  48. import * as Verify from '@/utils/verify'
  49. // 倒计时时长(秒)
  50. const times = 60
  51. // 表单验证场景
  52. const GET_CAPTCHA = 10
  53. const FORM_SUBMIT = 20
  54. export default {
  55. data() {
  56. return {
  57. // 正在加载
  58. isLoading: false,
  59. // 图形验证码信息
  60. captcha: {},
  61. // 短信验证码发送状态
  62. smsState: false,
  63. // 倒计时
  64. times,
  65. // 手机号
  66. mobile: '',
  67. // 图形验证码
  68. captchaCode: '',
  69. // 短信验证码
  70. smsCode: ''
  71. }
  72. },
  73. /**
  74. * 生命周期函数--监听页面加载
  75. */
  76. created() {
  77. // 获取图形验证码
  78. this.getCaptcha()
  79. },
  80. methods: {
  81. // 获取图形验证码
  82. getCaptcha() {
  83. const app = this
  84. CaptchaApi.image().then(result => app.captcha = result.data)
  85. },
  86. // 点击发送短信验证码
  87. handelSmsCaptcha() {
  88. const app = this
  89. if (!app.isLoading && !app.smsState && app.formValidation(GET_CAPTCHA)) {
  90. app.sendSmsCaptcha()
  91. app.getCaptcha()
  92. }
  93. },
  94. // 表单验证
  95. formValidation(scene = GET_CAPTCHA) {
  96. const app = this
  97. // 验证获取短信验证码
  98. if (scene === GET_CAPTCHA) {
  99. if (!app.validteMobile(app.mobile) || !app.validteCaptchaCode(app.captchaCode)) {
  100. return false
  101. }
  102. }
  103. // 验证表单提交
  104. if (scene === FORM_SUBMIT) {
  105. if (!app.validteMobile(app.mobile) || !app.validteSmsCode(app.smsCode)) {
  106. return false
  107. }
  108. }
  109. return true
  110. },
  111. // 验证手机号
  112. validteMobile(str) {
  113. if (Verify.isEmpty(str)) {
  114. this.$toast('请先输入手机号')
  115. return false
  116. }
  117. if (!Verify.isMobile(str)) {
  118. this.$toast('请输入正确格式的手机号')
  119. return false
  120. }
  121. return true
  122. },
  123. // 验证图形验证码
  124. validteCaptchaCode(str) {
  125. if (Verify.isEmpty(str)) {
  126. this.$toast('请先输入图形验证码')
  127. return false
  128. }
  129. return true
  130. },
  131. // 验证短信验证码
  132. validteSmsCode(str) {
  133. if (Verify.isEmpty(str)) {
  134. this.$toast('请先输入短信验证码')
  135. return false
  136. }
  137. return true
  138. },
  139. // 请求发送短信验证码接口
  140. sendSmsCaptcha() {
  141. const app = this
  142. app.isLoading = true
  143. CaptchaApi.sendSmsCaptcha({
  144. form: {
  145. captchaKey: app.captcha.key,
  146. captchaCode: app.captchaCode,
  147. mobile: app.mobile
  148. }
  149. })
  150. .then(result => {
  151. // 显示发送成功
  152. app.$toast(result.message)
  153. // 执行定时器
  154. app.timer()
  155. })
  156. .finally(() => app.isLoading = false)
  157. },
  158. // 执行定时器
  159. timer() {
  160. const app = this
  161. app.smsState = true
  162. const inter = setInterval(() => {
  163. app.times = app.times - 1
  164. if (app.times <= 0) {
  165. app.smsState = false
  166. app.times = times
  167. clearInterval(inter)
  168. }
  169. }, 1000)
  170. },
  171. // 点击提交
  172. handleSubmit() {
  173. const app = this
  174. if (!app.isLoading && app.formValidation(FORM_SUBMIT)) {
  175. app.onSubmitEvent()
  176. }
  177. },
  178. // 确认提交事件
  179. onSubmitEvent() {
  180. const app = this
  181. app.isLoading = true
  182. UserApi.bindMobile({ form: { smsCode: app.smsCode, mobile: app.mobile } })
  183. .then(result => {
  184. // 显示操作成功
  185. app.$toast(result.message)
  186. // 跳转回原页面
  187. setTimeout(() => {
  188. app.onNavigateBack(1)
  189. }, 2000)
  190. })
  191. .finally(() => app.isLoading = false)
  192. },
  193. /**
  194. * 提交成功-跳转回原页面
  195. */
  196. onNavigateBack(delta) {
  197. uni.navigateBack({
  198. delta: Number(delta || 1)
  199. })
  200. }
  201. }
  202. }
  203. </script>
  204. <style lang="scss" scoped>
  205. .container {
  206. padding: 100rpx 60rpx;
  207. min-height: 100vh;
  208. background-color: #fff;
  209. }
  210. // 页面头部
  211. .header {
  212. margin-bottom: 50rpx;
  213. .title {
  214. color: #191919;
  215. font-size: 50rpx;
  216. }
  217. .sub-title {
  218. margin-top: 20rpx;
  219. color: #b3b3b3;
  220. font-size: 25rpx;
  221. }
  222. }
  223. // 输入框元素
  224. .form-item {
  225. display: flex;
  226. padding: 18rpx;
  227. border-bottom: 1rpx solid #f3f1f2;
  228. margin-bottom: 25rpx;
  229. height: 96rpx;
  230. &--input {
  231. font-size: 26rpx;
  232. letter-spacing: 1rpx;
  233. flex: 1;
  234. height: 100%;
  235. }
  236. &--parts {
  237. min-width: 100rpx;
  238. height: 100%;
  239. }
  240. // 图形验证码
  241. .captcha {
  242. height: 100%;
  243. .image {
  244. display: block;
  245. width: 192rpx;
  246. height: 100%;
  247. }
  248. }
  249. // 短信验证码
  250. .captcha-sms {
  251. font-size: 22rpx;
  252. line-height: 50rpx;
  253. padding-right: 20rpx;
  254. .activate {
  255. color: #cea26a;
  256. }
  257. .un-activate {
  258. color: #9e9e9e;
  259. }
  260. }
  261. }
  262. // 提交按钮
  263. .submit-button {
  264. width: 100%;
  265. height: 86rpx;
  266. margin-top: 70rpx;
  267. // background: #cea26a;
  268. background: linear-gradient(to right, #ecb53c, #ff9211);
  269. text-align: center;
  270. line-height: 86rpx;
  271. color: #fff;
  272. border-radius: 80rpx;
  273. box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.1);
  274. letter-spacing: 5rpx;
  275. }
  276. </style>