You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
bililive_webapp/src/request.ts

120 lines
3.0 KiB

import {
addRoomApi,
changeEmailApi,
changePwdApi,
checkAdminApi,
getRoomApi,
sendCodeApi,
telegramBotApi,
testTelegramBotApi
} from "./interface";
// @ts-ignore
import {Message} from 'element3/src/components/Message'
/**
* 发送验证码
* @param userEmail
* @param callback
* @param success
*/
export function sendCode(userEmail: string, callback: (second: number) => void, success: () => void) {
let second = 10
return fetch(new Request(sendCodeApi, {method: 'POST', body: JSON.stringify({userEmail})}))
.then(res => res.json()).then((res: JSONResponse) => {
message(res)
if (res.result === 'OK') {
console.info('发送验证码成功')
callback(second)
success()
const t = setInterval(function () {
if (second === 0) {
clearInterval(t)
} else {
callback(--second)
}
}, 1000)
} else {
second = 0
}
})
}
/**
* 更换邮箱
* @param oldEmail
* @param newEmail
* @param code
*/
export function changeEmail(oldEmail: string, newEmail: string, code: string) {
return fetch(new Request(changeEmailApi, {method: 'POST', body: JSON.stringify({oldEmail, newEmail, code})}))
}
/**
* 更换密码
* @param userEmail
* @param password
*/
export function changePwd(userEmail: string, password: string) {
return fetch(new Request(changePwdApi, {method: 'POST', body: JSON.stringify({userEmail, password})}))
}
/**
* 获取telegram bot
* @param userEmail
*/
export function getTelegramBot(userEmail: string) {
return fetch(new Request(`${telegramBotApi}?userEmail=${userEmail}`))
}
/**
* 测试telegram bot token
* @param token
*/
export function testTelegramBot(token: string) {
return fetch(new Request(testTelegramBotApi(token)))
}
/**
* 绑定telegram bot
* @param userEmail
* @param token
*/
export function bindTelegramBot(userEmail: string, token: string) {
return fetch(new Request(telegramBotApi), {method: 'POST', body: JSON.stringify({userEmail, token})})
}
/**
* 检查是否存在管理员
*/
export function checkAdmin() {
return fetch(new Request(checkAdminApi), {method: 'GET'})
}
export function login(api: string, userEmail: string, password: string) {
return fetch(new Request(api), {method: 'POST', body: JSON.stringify({userEmail, password})})
}
export function addRoom(roomId: string) {
return fetch(new Request(addRoomApi), {method: 'POST', body: JSON.stringify({roomId})})
}
export function loadRoom() {
return fetch(new Request(getRoomApi), {method: 'GET'})
}
export type Result = 'OK' | 'FAIL'
export interface JSONResponse {
result: Result,
body?: any,
message?: string
}
export function message(res: JSONResponse) {
new Message({
showClose: true,
duration: 3000,
message: res.message,
type: res.result === 'OK' ? 'success' : 'error'
})
}