Example #1
0
func Load(file string, isGBK bool) (cfg *Config, err error) {
	var data []byte
	var pair [][]byte
	var n int

	if data, err = ioutil.ReadFile(file); err != nil {
		return
	}
	//读取GBK类型文件
	if isGBK {
		decode := mahonia.NewDecoder("GBK")
		encode := mahonia.NewEncoder("UTF-8")
		r := decode.NewReader(bytes.NewReader(data))
		buf := bytes.NewBuffer([]byte(""))
		w := encode.NewWriter(buf)
		io.Copy(w, r)
		data = buf.Bytes()
	}
	cfg = &Config{}
	cfg.Sections = make(map[string]*Section)

	section := "_"
	addSection(cfg, section)

	for _, line := range bytes.Split(data, strNewline) {
		if line = bytes.TrimSpace(line); len(line) == 0 {
			continue
		}

		pair = bytes.Split(line, strPairSeparator)

		for i, _ := range pair {
			pair[i] = bytes.TrimSpace(pair[i])
		}

		switch {
		case len(pair) == 1 && len(pair[0]) > 1:
			switch {
			case pair[0][0] == ';' || pair[0][0] == '#':
				cfg.Sections[section].AddComment(string(pair[0][1:]))
			case pair[0][0] == '[' && pair[0][len(pair[0])-1] == ']':
				section = string(pair[0][1 : len(pair[0])-1])
				addSection(cfg, section)
			}
		case len(pair) == 2:
			if n = bytes.Index(pair[1], strComment); n != -1 {
				cfg.Sections[section].AddComment(string(pair[1][n+1:]))
				pair[1] = pair[1][0:n]
			}
			addPair(cfg, section, pair)
		}
	}

	return
}
//绘制等值线服务
func CallCenterWebApi(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		content := r.FormValue("content")
		telephones := r.FormValue("telephones")
		tels := strings.Split(telephones, ",")
		var data []byte
		buf := bytes.NewBuffer(data)
		buf.WriteString(fmt.Sprintf("%s,%s,%s,%s,%s,%s", "序号", "电话号码", "允许外呼通道", "月", "日", "积分"))
		//回车换行符
		buf.WriteByte('\r')
		buf.WriteByte('\n')
		for k, v := range tels {
			buf.WriteString(fmt.Sprintf("%d,%s,%s,%s,%s,%s", k+1, v, "", "1", "1", "100"))
			buf.WriteByte('\r')
			buf.WriteByte('\n')
		}
		//编码转换,utf-8转换到GBK
		encode := mahonia.NewEncoder("GBK")
		encodeData := encode.ConvertString(string(buf.Bytes()))
		result := []byte(encodeData)

		t := time.Now().Add(60 * time.Second)
		id := fmt.Sprintf("2-%s-%s", t.Format("20060102"), t.Format("150405.000"))
		id = strings.Replace(id, ".", "", 1)
		ioutil.WriteFile(fmt.Sprintf(`CallTask\%s.csv`, id), result, 0600)
		conn, err := odbc.Connect(fmt.Sprintf("DSN=%s;UID=%s;PWD=%s", config.Server, config.UserName, config.Password))
		if err != nil {
			fmt.Println(err)
			fmt.Fprint(w, err)
		}
		stmt, err := conn.ExecDirect(fmt.Sprintf("INSERT INTO YZ_GroupCall(Name,TheDate,TheTime,CallType,Content,Status1,Status2,TelAtt) VALUES('%s','%s','%s','%s','%s','%s','%s','%s.csv')", t.Format("200601021504.000"), t.Format("2006-01-02"), t.Format("15:04"), "2", content, "1", "0", id))
		if err != nil {
			fmt.Println(err)
			fmt.Fprint(w, err)
		}
		stmt.Close()
		conn.Close()
		fmt.Fprint(w, "ok")
	}
}
Example #3
0
func Save(file string, isGBK bool, cfg *Config) (err error) {
	var data []byte
	buf := bytes.NewBuffer(data)

	// global section _ goes first
	if s, ok := cfg.Sections["_"]; ok {
		writeSection(buf, s)
	}

	for k, v := range cfg.Sections {
		if k == "_" {
			continue
		}
		writeSection(buf, v)
	}
	result := buf.Bytes()
	if isGBK {
		encode := mahonia.NewEncoder("GBK")
		encodeData := encode.ConvertString(string(result))
		result = []byte(encodeData)
	}
	return ioutil.WriteFile(file, result, 0600)
}