小程序农机手端

index.jsx 4.6KB

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