package yidong

import (
	"errors"
	"fmt"
	"hongze/hongze_open_api/models/tables/open_api_user"
	"hongze/hongze_open_api/utils"
	"strconv"
	"strings"
)

// GetSign 获取签名
func GetSign(activityIdYiDong string, timeInt int) string {
	signStr := fmt.Sprintf(`activity_id=%stime=%s`, activityIdYiDong, strconv.Itoa(timeInt))
	signStr += utils.YIDONG_HONGZE_KEY
	fmt.Print(signStr)
	return strings.ToUpper(utils.MD5(signStr))
}

func CheckAppidAndIp(appid, ip string) (err error) {
	if appid == "" {
		err = errors.New("缺少appid参数")
		return
	}
	openApiUserInfo, tmpErr := open_api_user.GetByAppid(appid)
	if tmpErr != nil {
		if tmpErr.Error() == utils.ErrNoRow() {
			err = errors.New("appid异常，请联系管理员")
		} else {
			err = errors.New("系统异常，请联系管理员")
		}
		return
	}

	if openApiUserInfo == nil {
		err = errors.New("系统异常，请联系管理员")
		return
	}
	//如果有ip限制，那么就添加ip
	//if openApiUserInfo.Ip != "" {
	//	if !strings.Contains(openApiUserInfo.Ip, ip) {
	//		err = errors.New(fmt.Sprintf("无权限访问该接口,ip:%v，请联系管理员", ip))
	//		return
	//	}
	//}
	return
}
