小程序农机手端

index.jsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import Taro from "@tarojs/taro"
  2. import { useState } from "react"
  3. import { useModel } from '@/store'
  4. import { View, Image, Input, Text, Button } from "@tarojs/components"
  5. import CustomNav from "@/components/CustomNav"
  6. import MyButton from "@/components/MyButton"
  7. import {updatePhone, setQcode } from '@/services/login'
  8. import withLayout from '@/layouts'
  9. import bgi from '@/assets/login/loginImg.png'
  10. import vCode from '@/assets/login/VerificationCode.png'
  11. import checkedImg from '@/assets/login/checkedImg.png'
  12. import unChecked from '@/assets/login/unChecked.png'
  13. import './style.less'
  14. export default withLayout((props) => {
  15. const { router } = props
  16. let { tab } = router.params
  17. const { person,setPerson } = useModel('person')
  18. const [agreement,setAgreement]=useState(false)
  19. const [phone,setPhone]=useState()
  20. const [qCode,setqCode]=useState()
  21. const [qCodeBtn,setqCodeBtn]=useState('获取验证码')
  22. const [dsb,setDsb]=useState(false)
  23. const handlePhone=(e)=>{
  24. if (e.detail.value) {
  25. setPhone(e.detail.value)
  26. }
  27. }
  28. const changeqCode=()=>{
  29. if (rulePhone(phone)) {
  30. setQcode({phone:phone}).then((res)=>{
  31. Taro.showToast({
  32. title: '发送成功,请注意查收',
  33. icon: 'none',
  34. duration: 2000
  35. })
  36. }).catch(() => {
  37. Taro.showToast({
  38. title: '网络异常, 请刷新小程序重试',
  39. icon: 'none',
  40. })
  41. })
  42. let time=60
  43. setDsb(true)
  44. // 调接口
  45. let timer = setInterval(() => {
  46. time-- //倒计时递减
  47. setqCodeBtn(`${time}s后重新发送`)
  48. if (time === 0) {
  49. setDsb(false)
  50. setqCodeBtn('重新发送')
  51. time=60
  52. clearInterval(timer)
  53. }
  54. }, 1000)
  55. }
  56. }
  57. const rulePhone=(val)=>{
  58. if (!/^1[0-9]{10}$/.test(val)) {
  59. Taro.showToast({
  60. title: '请输入正确的11位手机号',
  61. icon: 'none',
  62. duration: 2000
  63. })
  64. return false
  65. } else return true
  66. }
  67. const handleqCode=(e)=>{
  68. if (e.detail.value) {
  69. setqCode(e.detail.value)
  70. }
  71. }
  72. const onLogin = () => {
  73. if (rulePhone(phone)&&qCode) {
  74. if (!agreement) {
  75. Taro.showToast({
  76. title: '请确认下方协议',
  77. icon: 'none',
  78. duration: 2000
  79. })
  80. return false
  81. }
  82. // 登录
  83. updatePhone(person.personId,{phone:phone,captcha:qCode}).then((res)=>{
  84. setPerson({...person,phone:phone})
  85. Taro.showToast({
  86. title: '登录成功',
  87. icon: 'none',
  88. duration: 1000
  89. })
  90. if (tab) {
  91. setTimeout(()=>{
  92. Taro.redirectTo({ url: `/pages/index/index?tab=${tab}` });
  93. },1000)
  94. }
  95. }).catch((e) => {
  96. console.log(e.message)
  97. Taro.showToast({
  98. title: '手机号或者验证码不正确',
  99. icon: 'none',
  100. })
  101. })
  102. } else {
  103. Taro.showToast({
  104. title: '请输入正确的手机号和验证码',
  105. icon: 'none',
  106. duration: 2000
  107. })
  108. }
  109. }
  110. return (
  111. <View className='page-index'>
  112. <View className='index-navbar'>
  113. <CustomNav home title='登录' />
  114. </View>
  115. <Image src={bgi} className='loginBgi' />
  116. <View className='index-container loginContent'>
  117. <View className='title1'>您好!</View>
  118. <View className='title2'>欢迎进入农机手端小程序!</View>
  119. <View className='loginCell'>
  120. <View className='loginHeader'>+86</View>
  121. <Input type='number' maxlength='11' placeholder='请输入手机号' value={phone} onInput={handlePhone} />
  122. <Button className={['loginAction',dsb?'qCodeActive':'']} disabled={dsb} onClick={dsb?'':changeqCode}>{qCodeBtn}</Button>
  123. </View>
  124. <View className='loginCell'>
  125. <View className='loginHeader'>
  126. <Image src={vCode} className='headerImg' />
  127. </View>
  128. <Input type='text' value={qCode} onInput={handleqCode} placeholder='请输入验证码' />
  129. </View>
  130. <View className='footer'>
  131. <MyButton value='登录' onClick={onLogin} />
  132. <View className='agreement' onClick={()=>setAgreement(!agreement)}>
  133. <Image src={agreement?checkedImg:unChecked} className='footerCheckbox' />
  134. 请认真查看<Text>文本协议/隐私政策</Text>,确认之后选择此项!
  135. </View>
  136. </View>
  137. </View>
  138. </View>
  139. )
  140. })