加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
蓝枫叶 提交于 2020-01-13 09:00 . bugfix
/*
* @Author: 窦子滨<zibin_5257@163.com>
* @Date: 2019-12-25 17:23
*/
package main
import (
"encoding/json"
"io"
"net/http"
"sort"
"strconv"
"strings"
)
func openApi() {
http.HandleFunc("/get_rand", GetRand)
http.HandleFunc("/get_list", GetList)
http.HandleFunc("/init", GetInit)
err := http.ListenAndServe(":666", nil) //设置监听的端口
if err != nil {
log.Error("打开端口出错:%s", err.Error())
}
}
//获取系统初始化数据
func GetInit(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
j := strings.Join(r.Form["callback"], "")
a := returnData{
Data: usersAllList,
Surplus_data: userlist,
Sum: len(usersAllList),
Ssum: len(userlist),
AlreadyNum: len(alreadyList),
}
v, _ := json.Marshal(a)
if j == "" {
io.WriteString(w, string(v))
} else {
io.WriteString(w, j+"("+string(v)+")")
}
}
func GetList(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析参数,默认是不会解析的
kind, _ := strconv.Atoi(strings.Join(r.Form["i"], ""))
j := strings.Join(r.Form["callback"], "")
var data []string
switch kind {
case 2: //已中奖
data = make([]string, len(alreadyList))
copy(data, alreadyList)
case 3: //未中奖
data = make([]string, len(userlist))
copy(data, userlist)
default:
data = make([]string, len(usersAllList))
copy(data, usersAllList)
}
sort.Strings(data)
a := returnListData{
Data: data,
Num: len(data),
}
v, _ := json.Marshal(a)
if j == "" {
io.WriteString(w, string(v))
} else {
io.WriteString(w, j+"("+string(v)+")")
}
}
//随机获取抽奖结果
func GetRand(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析参数,默认是不会解析的
j := strings.Join(r.Form["callback"], "")
status := strings.Join(r.Form["type"], "")
num, _ := strconv.Atoi(strings.Join(r.Form["num"], ""))
number++
log.Info("第%d次抽奖", number)
var list []string
var err error
if status == "1" {
log.Info("本次抽奖方式:%s,参与人数:%d", "全员模式", len(usersAllList), )
list, err = randMulti(num, true)
} else {
log.Info("本次抽奖方式:%s,参与人数:%d", "未中奖模式", len(userlist))
list, err = randMulti(num, false)
}
var v []byte
var a returnRandData
var surplusData = make([]string, len(userlist))
copy(surplusData, userlist)
if err != nil {
a = returnRandData{
Surplus: len(userlist),
Surplus_data: surplusData,
Err: 1,
ErrMsg: err.Error(),
}
v, _ = json.Marshal(a)
log.Error("本次抽奖出错:%s", err.Error())
goto OUTEND
}
log.Info("本次抽奖中奖人数:%d,中奖名单:%s", len(list), strings.Join(list, ","))
a = returnRandData{
Data: list,
Surplus: len(userlist),
Surplus_data: surplusData,
AlreadyNum: len(alreadyList),
Err: 0,
ErrMsg: "",
}
OUTEND:
v, _ = json.Marshal(a)
if j == "" {
io.WriteString(w, string(v))
} else {
io.WriteString(w, j+"("+string(v)+")")
}
log.Info("=========================")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化