package app

import (
	. "../../../hotime"
	. "../../../hotime/common"
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"encoding/hex"
	"encoding/pem"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"
)

// Project 管理端项目
var Project = Proj{
	//"user": UserCtr,
	"category":       categoryCtr,
	"ctg_order_date": ctg_order_dateCtr,
	"order":          orderCtr,
	"user":           userCtr,
	"sms":            Sms,
}

//生成随机码的4位随机数
func getCode() string {
	//res := ""
	//for i := 0; i < 4; i++ {
	res := ObjToStr(RandX(1000, 9999))
	//}
	return res
}

func tencentSendYzm(umobile, code string) error {

	random := RandX(999999, 9999999)
	url := "https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=1400235813&random=" + ObjToStr(random)
	fmt.Println("URL:>", url)

	h := sha256.New()
	h.Write([]byte(`appkey=d511de15e5ccb43fc171772dbb8b599f&random=` + ObjToStr(random) + `&time=` + ObjToStr(time.Now().Unix()) + `&mobile=` + umobile))
	bs := h.Sum(nil)
	s256 := hex.EncodeToString(bs)

	//json序列化
	post := `{
    "ext": "",
    "extend": "",
    "params": [
        "` + code + `"
    ],
    "sig": "` + s256 + `",
    "sign": "乐呵呵旅游网",
    "tel": {
        "mobile": "` + umobile + `",
        "nationcode": "86"
    },
    "time": ` + ObjToStr(time.Now().Unix()) + `,
    "tpl_id": 378916
}`

	fmt.Println(url, "post", post)

	var jsonStr = []byte(post)
	fmt.Println("jsonStr", jsonStr)
	fmt.Println("new_str", bytes.NewBuffer(jsonStr))

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	// req.Header.Set("X-Custom-Header", "myvalue")
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	fmt.Println("response Status:", resp.Status)
	fmt.Println("response Headers:", resp.Header)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("response Body:", string(body))
	return nil
}

var privateKey = `-----BEGIN RSA Private Key-----
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJJuFUH/4m9H5hCCzxtd9BxpjWlG9gbejqiJpV0XJKaU1V7xDBJasswxPY7Zc15RoxWClPoKPwKrbWKm49dgBJebJq5xd4sLCSbboxRkKxpRiJHMZ4LJjYa5h9Ei9RyfoUzqGHqH4UrDy3m3IwPiP19cIBqoU50shyQf92ZpcGZhAgMBAAECgYEAiadU8pODoUs82x6tZbPALQmJN4PO+wwznfqv6sA74yGdKECAMazz0oMjtGt1SiCCqFD2jcweCftvvELZg3mvNg1V0vRQRD1ZCA8HDp8DXm20d11K3+RX39tR4KgyyM3HsSEhkUDujMxKIpYjyiB5iEtV7Ja9bZ2fROszq+mUIqUCQQDQQf6vWRMLBqfnDcU77vuDGOhXbjkF2ytLxLW3fbKaW3GWvC3n93zPM+mcvWSXgkl448+jFjpMktm1Vn+w+YX3AkEAs/+bbRbod6AcVbLu8C5E44qDRoRpu+LF7Cphp8tlSAIRjm2yGP5acMWGRUtH9MF2QJYPF0PgDzdmUSVqWnCAZwJBALnSuRri4wAKn1SmT+ALfLZcSiyBODZGeppv2ijw6qWahH8YR+ncRaxoyMFHqPMbmM1akJIXqktbGREaLnPOIb8CQQCdJycJaL3Qa98xR4dr9cm5rF6PO96g5w6M8jfO6ztjUkMHymh7f99wpFRlvaN2Y06edyV315ARWPohEPy5N44zAkBlLuDHLm1TkTTAfdlL5r2OcdjpaJYloTdn05Mp3+J+w1zTX8k6Mz8lFZtLUcoMeTfQ9rm/+u2KwxS8NljtSZWH
-----END RSA Private Key-----
`

func RSA_Decrypt(cipherTextBase64 string) string {
	cipherText, _ := base64.StdEncoding.DecodeString(cipherTextBase64)
	buf := []byte(privateKey)
	//pem解码
	block, _ := pem.Decode(buf)
	//X509解码
	private, err := x509.ParsePKCS8PrivateKey(block.Bytes)

	if err != nil {
		return ""
	}
	//对密文进行解密
	//plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey,cipherText)

	v, err := rsa.DecryptPKCS1v15(rand.Reader, private.(*rsa.PrivateKey), cipherText)
	if err != nil {
		return ""
	}
	//返回明文
	v1, err1 := url.QueryUnescape(string(v))
	if err1 != nil {
		return ""
	}
	return v1
}
