import * as vscode from "vscode" ; import { createAnswer } from "../service" ; import { getDayId } from "../utils/getDayId" ; import { login } from "./login" ; export async function postAnswer ( document , content , context ) { if ( ! context . globalState . get ( "login" ) ) { vscode . window . showWarningMessage ( "请先点击此处登录" , "登录" ) . then ( ( result ) => { if ( result === "登录" ) { login ( context ) ; } } ) ; return ; } const { account } = await vscode . authentication . getSession ( "github" , [ "user:email" ] , { createIfNone : true } ) ; const { fileName } = document ; const dayId = getDayId ( fileName ) ; if ( ! + dayId ) { vscode . window . showErrorMessage ( "问题id不存在" ) ; return ; } const answer = pickUserAnswer ( content ) ; if ( ! answer ) { vscode . window . showErrorMessage ( "答案不能为空" ) ; return ; } createAnswer ( dayId , answer , Number ( account . id ) ) . then ( ( res ) => { if ( res . success ) { vscode . window . showInformationMessage ( "提交成功" ) ; } else { vscode . window . showErrorMessage ( res . errorMsg || "提交失败" ) ; } } ) . catch ( ( e ) => { vscode . window . showErrorMessage ( "网络异常" ) ; console . log ( e ) ; } ) ; } function pickUserAnswer ( content ) { const regex = /\*\[interview\]: start(.*?)\*\[interview\]: end/s ; const regexJs = /\/\/ @interview start(.*?)\/\/ @interview end/s ; if ( regex . exec ( content ) ) { const [ , answer = "" ] = regex . exec ( content ) || [ ] ; return answer . trim ( ) ; } else if ( regexJs . exec ( content ) ) { const [ , answer = "" ] = regexJs . exec ( content ) || [ ] ; return answer . trim ( ) ; } return "" ; }