60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
|
package aliyun
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/alibabacloud-go/docmind-api-20220711/client"
|
||
|
aliyunSdk "github.com/fonchain_enterprise/fonchain-main/pkg/aliyun"
|
||
|
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
|
||
|
"gopkg.in/errgo.v2/fmt/errors"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
func DocStruct(fileUrl, fileName string) (docId string, err error) {
|
||
|
if fileName == "" {
|
||
|
fileName = filepath.Base(fileUrl)
|
||
|
}
|
||
|
request := client.SubmitDocStructureJobRequest{
|
||
|
FileUrl: &fileUrl,
|
||
|
FileName: &fileName,
|
||
|
}
|
||
|
response, err := aliyunSdk.DocClient.SubmitDocStructureJob(&request)
|
||
|
if err != nil {
|
||
|
err = errors.New(e.GetMsg(e.ERROR_ALIYUN_DOC_SUBMIT))
|
||
|
return
|
||
|
}
|
||
|
httpOk := response.StatusCode
|
||
|
if *httpOk != 200 {
|
||
|
err = errors.New(e.GetMsg(e.ERROR_ALIYUN_DOC_SUBMIT))
|
||
|
return
|
||
|
}
|
||
|
docId = *response.Body.Data.Id
|
||
|
fmt.Println(response.Body.Data.Id)
|
||
|
fmt.Println(docId)
|
||
|
return
|
||
|
|
||
|
}
|
||
|
|
||
|
func DocResult(docId string) (complete bool, texts []string, err error) {
|
||
|
// 调用查询接口
|
||
|
request := client.GetDocStructureResultRequest{Id: &docId}
|
||
|
response, err := aliyunSdk.DocClient.GetDocStructureResult(&request)
|
||
|
if err != nil {
|
||
|
err = errors.New(e.GetMsg(e.ERROR_ALIYUN_DOC_PARSE))
|
||
|
return
|
||
|
}
|
||
|
ok := response.Body.Completed
|
||
|
if *ok != true {
|
||
|
return
|
||
|
}
|
||
|
complete = true
|
||
|
var layoutsSlice []map[string]interface{}
|
||
|
layouts := response.Body.Data["layouts"]
|
||
|
layoutsBytes, _ := json.Marshal(layouts)
|
||
|
_ = json.Unmarshal(layoutsBytes, &layoutsSlice)
|
||
|
for _, v := range layoutsSlice {
|
||
|
texts = append(texts, fmt.Sprint(v["text"]))
|
||
|
}
|
||
|
return
|
||
|
}
|