index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <template>
  2. <div class="login">
  3. <van-nav-bar title="自然人开票" />
  4. <div class="login-content">
  5. <div class="text">感谢您参加{{ pubName }}项目,请您登录系统开具发票。</div>
  6. <div class="bold-text">已注册电子税务局</div>
  7. <div class="form">
  8. <!-- 手机号输入 -->
  9. <van-cell-group inset class="grop">
  10. <van-field
  11. class="input"
  12. v-model="formData.mobile"
  13. placeholder="请输入手机号"
  14. clearable
  15. type="tel"
  16. maxlength="30"
  17. @update:model-value="onInputMobile"
  18. />
  19. </van-cell-group>
  20. <!-- 验证码输入 + 按钮 -->
  21. <van-cell-group inset class="grop">
  22. <van-field
  23. class="input code-input"
  24. v-model="formData.code"
  25. placeholder="请输入验证码"
  26. clearable
  27. type="tel"
  28. maxlength="6"
  29. @update:model-value="onInputCode"
  30. />
  31. <button class="code-btn" :disabled="isCounting || !formData.mobile" @click="sendCode">
  32. {{ isCounting ? countDown + 's后重发' : '获取验证码' }}
  33. </button>
  34. </van-cell-group>
  35. <!-- 同意协议 -->
  36. <div class="agree">
  37. <label>
  38. <van-checkbox v-model="agree" checked-color="#fe783d" shape="square" icon-size="4vw">
  39. 我已阅读并同意
  40. <a @click.self.stop="toAgreement('auth')">《授权协议》</a>
  41. <a @click.self.stop="toAgreement('privacy')">《隐私权政策》</a>
  42. </van-checkbox>
  43. </label>
  44. </div>
  45. <!-- 登录按钮 -->
  46. <div class="login-btn btn" @click="loginBtn">登录</div>
  47. </div>
  48. <div class="bold-text">未注册电子税务局</div>
  49. <div class="register-btn btn" @click="registerUrl">立即注册</div>
  50. <div class="footer">技术支持:要易云(北京)科技有限公司</div>
  51. <!-- 失败弹窗 -->
  52. <ModernDialog
  53. v-model:show="showDialog"
  54. title="提示"
  55. :message="codeMessage"
  56. cancelText="返回继续登录"
  57. @cancel="onCancel"
  58. />
  59. <!-- 注册弹窗 -->
  60. <ModernDialog
  61. v-model:show="loginDialog"
  62. title="提示"
  63. :message="loginMessage"
  64. cancelText="返回"
  65. @cancel="loginCancel"
  66. confirmText="立即注册"
  67. @confirm="loginConfirm"
  68. />
  69. </div>
  70. </div>
  71. </template>
  72. <script setup lang="ts">
  73. import { ref, reactive, onBeforeMount } from 'vue'
  74. import { useRouter, useRoute } from 'vue-router'
  75. import { showFailToast, showSuccessToast, showToast, showLoadingToast } from 'vant'
  76. import {
  77. getPubNameApi,
  78. sendEtsSmsApi,
  79. loginEtssmsApi,
  80. registerUrlApi,
  81. } from '@/services/modules/login'
  82. import { getStatusApi } from '@/services/modules/invoiceInformation'
  83. import type {
  84. SendTtsSmsRequest,
  85. LoginEtssmsRequest,
  86. RegisterUrlRequest,
  87. } from '@/services/modules/login/type.d'
  88. import { useUserStore } from '@/stores/modules/user'
  89. import { useDebounceFn } from '@/utils/util'
  90. import { REGISTER_URL_KEY } from '@/constants/storage'
  91. import ModernDialog from '@/components/ModernDialog.vue'
  92. const userStore = useUserStore()
  93. const router = useRouter()
  94. const route = useRoute()
  95. /* 表单数据 */
  96. const formData = reactive<LoginEtssmsRequest>({
  97. mobile: '',
  98. code: '',
  99. pushRecordId: '',
  100. })
  101. const sendEtsSms = reactive<SendTtsSmsRequest>({
  102. mobile: '',
  103. pushRecordId: '',
  104. })
  105. /* 限制手机号只能输入数字,最多 30 位 */
  106. const onInputMobile = (val: string) => {
  107. formData.mobile = val.replace(/\D/g, '').slice(0, 30)
  108. }
  109. /* 限制验证码只能输入数字,最多 6 位 */
  110. const onInputCode = (val: string) => {
  111. formData.code = val.replace(/\D/g, '').slice(0, 6)
  112. }
  113. /* 是否同意协议 */
  114. const agree = ref(false)
  115. /* 验证码按钮逻辑 */
  116. const isCounting = ref(false)
  117. const countDown = ref(60)
  118. let timer: number | undefined
  119. const sending = ref(false)
  120. const showDialog = ref(false)
  121. const codeMessage = ref()
  122. const onCancel = () => {
  123. showDialog.value = false
  124. }
  125. /* 发送验证码 */
  126. const sendCode = async () => {
  127. if (sending.value) return
  128. if (!/^1\d{10}$/.test(formData.mobile)) {
  129. showToast('请输入正确的手机号')
  130. return
  131. }
  132. sending.value = true
  133. const toast = showLoadingToast({ message: '发送中…', forbidClick: true, duration: 3000 })
  134. sendEtsSms.mobile = formData.mobile
  135. try {
  136. const res = await sendEtsSmsApi(sendEtsSms)
  137. if (res.code === 0 && res.data) {
  138. showSuccessToast('发送成功')
  139. startCountDown()
  140. } else {
  141. codeMessage.value = res.msg
  142. showDialog.value = true
  143. }
  144. toast.close()
  145. } catch (err: any) {
  146. toast.close()
  147. const { code, message } = err
  148. if (code === 401) {
  149. loginMessage.value = message
  150. loginDialog.value = true
  151. return
  152. }
  153. showFailToast(err?.message || '获取验证码失败')
  154. } finally {
  155. sending.value = false
  156. }
  157. }
  158. /* 倒计时逻辑 */
  159. const startCountDown = () => {
  160. isCounting.value = true
  161. countDown.value = 60
  162. timer = window.setInterval(() => {
  163. countDown.value--
  164. if (countDown.value <= 0) {
  165. isCounting.value = false
  166. clearInterval(timer)
  167. }
  168. }, 1000)
  169. }
  170. const loginDialog = ref(false)
  171. const loginMessage = ref('')
  172. const loginCancel = () => {
  173. loginDialog.value = false
  174. }
  175. const loginConfirm = () => {
  176. registerUrl()
  177. }
  178. /* 登录逻辑 */
  179. const loginBtn = useDebounceFn(async () => {
  180. if (!formData.mobile) {
  181. showFailToast('请输入手机号')
  182. return
  183. }
  184. if (!formData.code) {
  185. showFailToast('验证码不能为空')
  186. return
  187. }
  188. if (!agree.value) {
  189. showFailToast('请勾选阅读并同意')
  190. return
  191. }
  192. const toast = showLoadingToast({
  193. message: '登录中…',
  194. forbidClick: true,
  195. duration: 0,
  196. })
  197. // 已提交状态映射
  198. const isSubmitStatusMap = ['PENDING']
  199. const isSubmitEventStatusMap = [
  200. 'SUBMITTED',
  201. 'PENDING_PAYMENT',
  202. 'PAID',
  203. 'FINAL_INVOICE_SUBMITTED',
  204. 'INVOICE_DOWNLOADABLE',
  205. ]
  206. try {
  207. const res: any = await loginEtssmsApi(formData)
  208. if (res?.access_token) {
  209. toast.close()
  210. userStore.setAccessToken(res.access_token)
  211. const statusRes = await getStatusApi({ pushRecordId: formData.pushRecordId })
  212. // 修改认证下的状态内容,如果已经提交,跳转到成功页
  213. if (statusRes.code == 0) {
  214. const { invoiceStatus, eventStatus } = statusRes.data
  215. const isInvoiceSubmitted = invoiceStatus && isSubmitStatusMap.includes(invoiceStatus)
  216. const isEventSubmitted = eventStatus && isSubmitEventStatusMap.includes(eventStatus)
  217. if (isInvoiceSubmitted && isEventSubmitted) {
  218. return router.replace({
  219. path: '/login-success',
  220. })
  221. }
  222. }
  223. router.replace({ path: '/invoice-information' })
  224. } else if (res.code == 401) {
  225. loginMessage.value = res.msg
  226. loginDialog.value = true
  227. }
  228. } catch (err: any) {
  229. toast.close()
  230. const { code, message } = err || {}
  231. if (code && message) {
  232. showFailToast(message)
  233. }
  234. }
  235. }, 800)
  236. // 获取注册链接
  237. const registerUrlParams = ref<RegisterUrlRequest>({
  238. areaId: '',
  239. returnUrl: '',
  240. pushRecordId: '',
  241. })
  242. const registerUrl = useDebounceFn(async () => {
  243. try {
  244. const res = await registerUrlApi(registerUrlParams.value)
  245. if (res.code === 0 && res.data.url) {
  246. // 保存一次性链接
  247. sessionStorage.setItem(REGISTER_URL_KEY, res.data.url as string)
  248. return router.push({ path: 'register' })
  249. }
  250. showFailToast('获取注册链接失败')
  251. } catch (err: any) {
  252. showFailToast(err?.message || '获取注册链接失败')
  253. }
  254. }, 800)
  255. // 项目名称
  256. const pubName = ref('')
  257. const getPubName = async () => {
  258. try {
  259. const res = await getPubNameApi({
  260. pushRecordId: sendEtsSms.pushRecordId,
  261. })
  262. if (res.code === 0) {
  263. pubName.value = res.data.projectName
  264. registerUrlParams.value.areaId = res.data.cityCode
  265. userStore.setAreaCode(res.data.cityCode)
  266. registerUrlParams.value.pushRecordId = sendEtsSms.pushRecordId
  267. }
  268. } catch (err) {
  269. console.log('err', err)
  270. }
  271. }
  272. /* 协议跳转 */
  273. const toAgreement = (type: string) => {
  274. router.push({
  275. path: '/agreement',
  276. query: { type },
  277. })
  278. }
  279. onBeforeMount(() => {
  280. userStore.LogOut()
  281. registerUrlParams.value.returnUrl = `${location.origin}${route.fullPath}`
  282. const pushRecordId = route?.query.pushRecordId || ''
  283. if (pushRecordId) {
  284. sendEtsSms.pushRecordId = pushRecordId as string
  285. userStore.setPushRecordId(pushRecordId as string)
  286. formData.pushRecordId = pushRecordId as string
  287. getPubName()
  288. } else {
  289. showToast('登录链接存在不完整或者输入错误,请重新复制链接登录')
  290. }
  291. })
  292. </script>
  293. <style lang="scss" scoped>
  294. .login {
  295. min-height: 100vh;
  296. display: flex;
  297. flex-direction: column;
  298. background: linear-gradient(180deg, #ffffff 0%, #f5f7fb 100%);
  299. overflow-x: hidden;
  300. .van-nav-bar {
  301. background-color: #fff;
  302. border-bottom: 1px solid #f2f2f2;
  303. font-weight: 600;
  304. }
  305. .login-content {
  306. flex: 1;
  307. padding: 6vw 6vw 12vw;
  308. display: flex;
  309. flex-direction: column;
  310. align-items: center;
  311. .text {
  312. width: 90%;
  313. font-size: 4vw;
  314. text-align: center;
  315. line-height: 6vw;
  316. color: #333;
  317. margin-top: 8vw;
  318. margin-bottom: 6vw;
  319. }
  320. .bold-text {
  321. width: 100%;
  322. font-weight: 700;
  323. font-size: 4.5vw;
  324. text-align: left;
  325. color: #222;
  326. margin: 4vw 0 2vw;
  327. }
  328. .form {
  329. width: 96%;
  330. margin-top: 20px;
  331. background: #fff;
  332. border-radius: 3vw;
  333. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  334. padding: 6vw 4vw;
  335. margin-bottom: 8vw;
  336. .grop {
  337. margin-top: 3vw;
  338. display: flex;
  339. align-items: center;
  340. justify-content: space-between;
  341. }
  342. .input {
  343. width: 100%;
  344. border-radius: 6vw;
  345. border: 1px solid #e5e5e5;
  346. padding-left: 5vw;
  347. font-size: 3.8vw;
  348. background: #fff;
  349. transition: 0.3s;
  350. &:focus {
  351. border-color: #fe783d;
  352. box-shadow: 0 0 8px rgba(254, 120, 61, 0.3);
  353. }
  354. }
  355. .code-input {
  356. width: 55%;
  357. }
  358. .code-btn {
  359. background: linear-gradient(90deg, #fe783d 0%, #ff994f 100%);
  360. border: none;
  361. border-radius: 25px;
  362. height: 9vw;
  363. width: 30%;
  364. color: #fff;
  365. font-weight: 600;
  366. font-size: 3.4vw;
  367. cursor: pointer;
  368. transition: all 0.3s;
  369. &:active {
  370. opacity: 0.85;
  371. }
  372. &:disabled {
  373. background: #ccc;
  374. cursor: not-allowed;
  375. }
  376. }
  377. .agree {
  378. text-align: left;
  379. font-size: 3vw;
  380. line-height: 5vw;
  381. margin: 4vw 0;
  382. color: #666;
  383. padding-left: 36px;
  384. .custom-checkbox {
  385. accent-color: #fe783d;
  386. }
  387. label {
  388. display: flex;
  389. align-items: center;
  390. flex-wrap: wrap;
  391. input {
  392. width: 3.5vw;
  393. height: 3.5vw;
  394. margin-right: 6px;
  395. }
  396. }
  397. a {
  398. color: #fe783d;
  399. text-decoration: none;
  400. margin-left: 2px;
  401. }
  402. }
  403. }
  404. .btn {
  405. box-sizing: border-box;
  406. width: 80%;
  407. height: 12vw;
  408. border-radius: 6vw;
  409. color: #fff;
  410. font-size: 4vw;
  411. line-height: 12vw;
  412. text-align: center;
  413. font-weight: 600;
  414. cursor: pointer;
  415. transition: all 0.3s ease;
  416. margin: 4vw 0;
  417. &:active {
  418. transform: scale(0.98);
  419. }
  420. }
  421. .login-btn {
  422. margin: 0 auto;
  423. background: linear-gradient(90deg, #ff8036 0%, #ffa253 100%);
  424. box-shadow: 0 4px 12px rgba(255, 128, 54, 0.3);
  425. }
  426. .register-btn {
  427. background: linear-gradient(90deg, #0072f8 0%, #3a9fff 100%);
  428. box-shadow: 0 4px 12px rgba(0, 114, 248, 0.25);
  429. }
  430. .footer {
  431. width: 100%;
  432. text-align: center;
  433. color: #999;
  434. font-size: 3vw;
  435. margin-top: auto;
  436. padding-bottom: 4vh;
  437. }
  438. }
  439. }
  440. </style>