index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view>
  3. <view class="logo">
  4. <image src="../../static/logo.png" mode="aspectFit"></image>
  5. </view>
  6. <view class="form">
  7. <uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="0">
  8. <uni-forms-item name="mobilePhone">
  9. <uni-easyinput type="number" maxlength="11" :clearable="false" v-model="formData.mobilePhone" placeholder="请输入手机号" />
  10. </uni-forms-item>
  11. <uni-forms-item name="code">
  12. <view class="form-code">
  13. <uni-easyinput type="number" :clearable="false" v-model="formData.code" placeholder="请输入验证码" />
  14. <view class="code" :class="{'code-disabled': isCanGetCode}" @click="getCode">
  15. {{codeTips}}
  16. </view>
  17. </view>
  18. </uni-forms-item>
  19. </uni-forms>
  20. <button class="submit" :class="{'submit-disabled': !formData.code || !formData.mobilePhone}" @click="submit">登录</button>
  21. </view>
  22. </view>
  23. </template>
  24. <script lang="ts" setup>
  25. import { reactive, ref, watch } from 'vue'
  26. import { onLoad } from '@dcloudio/uni-app'
  27. import { sendSmsCode, verifyCode, mobilephoneRegister, prefUser } from '@/api/h5.js'
  28. const unitCode = ref('')
  29. const formData = reactive({
  30. code: '',
  31. mobilePhone: '',
  32. })
  33. const rules = reactive({
  34. mobilePhone: {
  35. rules: [{
  36. required: true,
  37. errorMessage: '请输入手机号',
  38. }]
  39. },
  40. code: {
  41. rules: [{
  42. required: true,
  43. errorMessage: '请输入验证码',
  44. }]
  45. },
  46. })
  47. const codeTips = ref("获取验证码")
  48. const isCanGetCode = ref(false)
  49. const countDownNumber = ref(60)
  50. const interval = ref(null)
  51. const countDown = () => {
  52. isCanGetCode.value = true
  53. interval.value = setInterval(() => {
  54. countDownNumber.value = countDownNumber.value - 1
  55. codeTips.value = `${countDownNumber.value}秒重新获取`
  56. }, 1000)
  57. }
  58. watch(() => countDownNumber.value, (value) => {
  59. if (value == 0) {
  60. clearInterval(interval.value)
  61. isCanGetCode.value = false
  62. codeTips.value = "重新获取"
  63. countDownNumber.value = 60
  64. }
  65. })
  66. const getCode = async () => {
  67. if (!formData.mobilePhone) {
  68. uni.showToast({
  69. title: "请输入手机号",
  70. icon: 'none'
  71. })
  72. return
  73. };
  74. if (!isCanGetCode.value) {
  75. uni.showLoading({
  76. title: '正在获取验证码',
  77. mask: true
  78. })
  79. const res = await sendSmsCode({
  80. "mobilePhone": formData.mobilePhone
  81. })
  82. if (res.code === 200) {
  83. uni.hideLoading();
  84. codeTips.value = `${countDownNumber.value}S`
  85. countDown()
  86. uni.showToast({
  87. title: '发送成功',
  88. icon: 'none'
  89. })
  90. }
  91. } else {
  92. uni.showToast({
  93. title: "倒计时结束后再发送",
  94. icon: 'none'
  95. })
  96. }
  97. }
  98. const handlePrefUser = async () => {
  99. const res = await prefUser({
  100. mobilePhone: formData.mobilePhone,
  101. prefUnitCode: unitCode.value
  102. })
  103. if(res.code === 200) {
  104. uni.showToast({
  105. title: res.message,
  106. icon: 'none'
  107. })
  108. if(res.data) {
  109. const { userCode } = res.data
  110. setTimeout(() => {
  111. uni.reLaunch({
  112. url: `/pages/result/result?provinceCode=${userCode?.slice(0, 2)}`
  113. })
  114. }, 800)
  115. }
  116. }
  117. }
  118. const getMobilephoneRegister = async () => {
  119. const res = await mobilephoneRegister({mobilePhone: formData.mobilePhone})
  120. if(res.data) {
  121. const parameter = encodeURIComponent(JSON.stringify({
  122. unitCode: unitCode.value,
  123. mobilePhone: formData.mobilePhone
  124. }))
  125. uni.navigateTo({
  126. url: `/pages/password/password?parameter=${parameter}`
  127. })
  128. }else {
  129. handlePrefUser()
  130. }
  131. }
  132. const form = ref()
  133. const submit = () => {
  134. if(formData.code && formData.mobilePhone) {
  135. form.value.validate().then(async data => {
  136. const res = await verifyCode(data)
  137. if(res.code === 200) {
  138. if(res.data) {
  139. getMobilephoneRegister()
  140. }else {
  141. uni.showToast({
  142. title: "验证码错误",
  143. icon: 'none'
  144. })
  145. }
  146. }
  147. })
  148. }
  149. }
  150. onLoad((options) => {
  151. unitCode.value = options.unitCode
  152. })
  153. </script>
  154. <style lang="scss" scoped>
  155. .form {
  156. padding: 0 40rpx;
  157. :deep(.uni-easyinput__content) {
  158. border: none;
  159. }
  160. :deep(.uni-easyinput__content-input) {
  161. background: #F1F5FE;
  162. height: 112rpx;
  163. border-radius: 24rpx;
  164. font-weight: 500;
  165. font-size: 32rpx;
  166. color: #1D2129;
  167. line-height: 38rpx;
  168. }
  169. :deep(.uni-input-placeholder) {
  170. font-weight: 400;
  171. font-size: 32rpx;
  172. color: #86909C;
  173. line-height: 38rpx;
  174. }
  175. .form-code {
  176. display: flex;
  177. align-items: center;
  178. background: #F1F5FE;
  179. border-top-right-radius: 24rpx;
  180. border-bottom-right-radius: 24rpx;
  181. :deep(.uni-easyinput__content-input){
  182. border-top-right-radius: 0;
  183. border-bottom-right-radius: 0;
  184. }
  185. .code {
  186. font-weight: 400;
  187. font-size: 32rpx;
  188. color: #3682EA;
  189. border-radius: 8rpx;
  190. line-height: 38rpx;
  191. padding: 0 24rpx;
  192. }
  193. .code-disabled {
  194. color: #86909C;
  195. }
  196. }
  197. }
  198. .submit {
  199. height: 112rpx;
  200. background: #3682EA;
  201. border-radius: 24rpx;
  202. font-weight: 500;
  203. font-size: 36rpx;
  204. color: #FFFFFF;
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. margin-top: 128rpx;
  209. }
  210. .submit-disabled {
  211. background: #B0CDFF;
  212. }
  213. </style>