package controllers

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"regexp"

	"SSO/enums"
	"SSO/service"
)

type UeditorController struct {
	BaseController
}

//处理百度富文本配置文件，如果不加上这些处理，无法上传图片
func (this *UeditorController) GetUeditorConfig() {
	op := this.Input().Get("action")

	switch op {
	case "config":
		file, err := os.Open("conf/config.json")
		if err != nil {
			log.Fatal(err)
			os.Exit(1)
		}
		defer file.Close()
		fd, err := ioutil.ReadAll(file)
		src := string(fd)
		re, _ := regexp.Compile("\\/\\*[\\S\\s]+?\\*\\/")
		src = re.ReplaceAllString(src, "")
		tt := []byte(src)
		var r interface{}
		json.Unmarshal(tt, &r)
		this.Data["json"] = r
		this.ServeJSON()
	}
}

func (this *UeditorController) GetQNToken() {
	redisClient := service.RedisClient()
	defer redisClient.Close()

	token, err := redisClient.Get("qn_token").Result()
	if err == nil {
		this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
			"token": token,
		})
		this.ServeJSON()
	} else {
		fmt.Println("redis获取七牛token错误: ", err.Error())
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		this.ServeJSON()
	}
}
