init
This commit is contained in:
BIN
api/.DS_Store
vendored
Normal file
BIN
api/.DS_Store
vendored
Normal file
Binary file not shown.
165
api/v1/base.go
Executable file
165
api/v1/base.go
Executable file
@@ -0,0 +1,165 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
"video/response"
|
||||
"video/utils/code"
|
||||
"video/utils/exception"
|
||||
)
|
||||
|
||||
type Base struct{}
|
||||
|
||||
type CallUserNpBody struct {
|
||||
EnCodeStr string `json:"EnCodeStr"`
|
||||
}
|
||||
|
||||
func (r *Base) CallUserNp(c *gin.Context) {
|
||||
var body CallUserNpBody
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, exception.E{Code: code.SERVER_ERROR, Msg: "接收加密参数失败", Err: err})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := runJSFunction("script/sign.js", "userNp", body.EnCodeStr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.JSON(http.StatusForbidden, exception.E{Code: code.SERVER_ERROR, Msg: "处理失败", Err: err})
|
||||
return
|
||||
}
|
||||
|
||||
response.SuccessData(result, c)
|
||||
}
|
||||
|
||||
type CallUserPbBody struct {
|
||||
DeCodeStr string `json:"DeCodeStr"`
|
||||
}
|
||||
|
||||
func (r *Base) CallUserPb(c *gin.Context) {
|
||||
var body CallUserPbBody
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, exception.E{Code: code.SERVER_ERROR, Msg: "接收解密参数失败", Err: err})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := runJSFunction("script/sign.js", "userPB", body.DeCodeStr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.JSON(http.StatusForbidden, exception.E{Code: code.SERVER_ERROR, Msg: "处理失败", Err: err})
|
||||
return
|
||||
}
|
||||
// 将结果转为 JSON 字符串
|
||||
jsonBytes, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, exception.E{Code: code.SERVER_ERROR, Msg: "结果转JSON失败", Err: err})
|
||||
return
|
||||
}
|
||||
|
||||
// 再转为 Base64 字符串
|
||||
encoded := base64.StdEncoding.EncodeToString(jsonBytes)
|
||||
response.SuccessData(encoded, c)
|
||||
}
|
||||
|
||||
func runJSFunction(scriptPath string, funcName string, args ...interface{}) (interface{}, error) {
|
||||
script, err := os.ReadFile(scriptPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取脚本失败: %w", err)
|
||||
}
|
||||
|
||||
vm := goja.New()
|
||||
|
||||
|
||||
|
||||
|
||||
// 注入 URLSearchParams Polyfill
|
||||
polyfill := `
|
||||
function URLSearchParams(query) {
|
||||
this.params = {};
|
||||
if (typeof query === 'string') {
|
||||
query.replace(/^\?/, '').split('&').forEach(function(pair) {
|
||||
var parts = pair.split('=');
|
||||
var k = parts[0];
|
||||
var v = parts[1];
|
||||
if (k) this.params[decodeURIComponent(k)] = decodeURIComponent(v || '');
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
|
||||
URLSearchParams.prototype.get = function(key) {
|
||||
return this.params[key] || null;
|
||||
};
|
||||
|
||||
URLSearchParams.prototype.set = function(key, value) {
|
||||
this.params[key] = value;
|
||||
};
|
||||
|
||||
URLSearchParams.prototype.toString = function() {
|
||||
var entries = [];
|
||||
for (var key in this.params) {
|
||||
if (this.params.hasOwnProperty(key)) {
|
||||
entries.push([key, this.params[key]]);
|
||||
}
|
||||
}
|
||||
var queryString = '';
|
||||
for (var i = 0; i < entries.length; i++) {
|
||||
var pair = entries[i];
|
||||
queryString += encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1]);
|
||||
if (i < entries.length - 1) {
|
||||
queryString += '&';
|
||||
}
|
||||
}
|
||||
return queryString;
|
||||
};
|
||||
|
||||
globalThis.URLSearchParams = URLSearchParams;
|
||||
`
|
||||
|
||||
// 先注入 polyfill
|
||||
_, err = vm.RunString(polyfill)
|
||||
if err != nil {
|
||||
//log.Fatal("polyfill error:", err)
|
||||
return nil, fmt.Errorf("注入 URLSearchParams 失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
err = vm.Set("console", map[string]func(...interface{}){
|
||||
"log": func(args ...interface{}) {
|
||||
fmt.Println(args...)
|
||||
},
|
||||
"error":func(args ...interface{}) {
|
||||
fmt.Println(args...)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("注入 console.log 失败: %w", err)
|
||||
}
|
||||
|
||||
if _, err := vm.RunString(string(script)); err != nil {
|
||||
return nil, fmt.Errorf("执行 JS 脚本失败: %w", err)
|
||||
}
|
||||
|
||||
fn, ok := goja.AssertFunction(vm.Get(funcName))
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("找不到函数: %s", funcName)
|
||||
}
|
||||
|
||||
// 转换参数为 goja.Value
|
||||
var gojaArgs []goja.Value
|
||||
for _, arg := range args {
|
||||
gojaArgs = append(gojaArgs, vm.ToValue(arg))
|
||||
}
|
||||
|
||||
result, err := fn(goja.Undefined(), gojaArgs...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("调用函数失败: %w", err)
|
||||
}
|
||||
|
||||
return result.Export(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user