index.vue 6.9 KB

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