import {showToast} from './toast.js' // 接口api域名 import { host } from './url.js' // 请求状态 let loading = false // 解析url参数 const urlParams = new URLSearchParams(window.location.search) const params = { deviceId: '' } for (let [key, value] of urlParams) { params[key] = value } const { deviceId } = params console.log('urlParams=',window.location.search) console.log('deviceId=',deviceId) // 发送验证码 document.querySelector('.valid-btn').addEventListener('click', async () => { const { value } = document.querySelector('.phone-input') if (!value || !validPhone(value)) { return showToast('请输入正确的手机号') } if (loading) { return; } loading = true try { const res = await (await fetch(`../h5/sendPhoneCode?phoneNumber=${value}`)).json() const { code, message } = res if (code !== 200) { return showToast(message) } showToast('验证码已发送') } catch (error) { console.log(error) showToast('网络请求失败') } finally { loading = false } }) // 点击登录 document.querySelector('.login-btn').addEventListener('click', async () => { const { value: validCode } = document.querySelector('.valid-input') const { value: phoneNumber } = document.querySelector('.phone-input') try { const res = await fetch(`../h5/getCombos?deviceId=${deviceId}&phoneNumber=${phoneNumber}&code=${validCode}`).then(response => { const token = response.headers.get('Authorization') if (token) { sessionStorage.setItem('token', token) } return response.json() }) const {code, message, data} = res if (code !== 200) { return showToast(message) } sessionStorage.setItem('combo-list', JSON.stringify(data)) window.location.href = 'combo.html' } catch (error) { console.log(error) } }) const validPhone = (phone) => { return /^(?:(?:\+|00)86)?1\d{10}$/.test(phone) }