xuzhiheng преди 4 години
родител
ревизия
31e366efaf
променени са 4 файла, в които са добавени 287 реда и са изтрити 0 реда
  1. 92 0
      src/push/push.go
  2. 90 0
      src/util/netutil.go
  3. BIN
      src/web/res/push/push.xlsx
  4. 105 0
      src/web/templates/push/index.html

+ 92 - 0
src/push/push.go

@@ -0,0 +1,92 @@
+package push
+
+import (
+	"bytes"
+	"encoding/json"
+	"log"
+	qutil "qfw/util"
+	. "util"
+
+	"github.com/go-xweb/xweb"
+	"github.com/tealeg/xlsx"
+)
+
+type Push struct {
+	*xweb.Action
+	index    xweb.Mapper `xweb:"/push/index"`    //首页
+	dataPush xweb.Mapper `xweb:"/push/dataPush"` //数据推送
+}
+
+var fields = []string{"macthkey", "area", "city", "id"}
+
+func (this *Push) Index() {
+	this.Render("/push/index.html")
+}
+
+func (this *Push) DataPush() {
+	appid := this.GetString("appid")
+	getDataType := this.GetString("getDataType")
+	// dataType := this.GetString("dataType")
+	dataArr := []map[string]interface{}{}
+	if getDataType == "0" {
+		query := map[string]interface{}{"appid": appid, "bget": map[string]interface{}{"$ne": 1}}
+		fieldMap := map[string]int{}
+		for _, v := range fields {
+			fieldMap[v] = 1
+		}
+		data, ok := Mgo.Find("usermail", query, nil, fieldMap, false, -1, -1)
+		if ok && data != nil && len(*data) > 0 {
+			dataArr = *data
+		}
+	} else if getDataType == "1" {
+		dataArr = ExcelParse()
+	}
+	log.Println(dataArr)
+	databyte, _ := json.Marshal(dataArr)
+	response, err := HttpPost("url", map[string]string{"Content-Type": ContentTypeJson}, bytes.NewBuffer(databyte))
+	if err == nil {
+		resMap := qutil.ObjToMap(string(response))
+		if *resMap != nil {
+			if qutil.ObjToString((*resMap)["respCode"]) == "00000" {
+				log.Println("成功")
+			} else {
+				log.Println("异常")
+			}
+		}
+	}
+}
+
+func ExcelParse() []map[string]interface{} {
+	filePath := "./web/res/push/push.xlsx"
+	xlFile, _ := xlsx.OpenFile(filePath)
+	//获取行数
+	length := len(xlFile.Sheets[0].Rows)
+	//开辟除表头外的行数的数组内存
+	resourceArr := make([]map[string]interface{}, length-1)
+	//遍历sheet
+	for _, sheet := range xlFile.Sheets {
+		//遍历每一行
+		for rowIndex, row := range sheet.Rows {
+			//跳过第一行表头信息
+			if rowIndex == 0 {
+				continue
+			}
+			dataMap := map[string]interface{}{}
+			for k, v := range fields {
+				dataMap[v] = row.Cells[k].String()
+			}
+			//遍历每一个单元
+			// for cellIndex, cell := range row.Cells {
+			// 	text := cell.String()
+			// 	if text != "" {
+			// 		//如果是每一行的第一个单元格
+			// 		if cellIndex == 0 {
+
+			// 		}
+			// 	}
+			// }
+			resourceArr[rowIndex-1] = dataMap
+		}
+	}
+	return resourceArr
+}

+ 90 - 0
src/util/netutil.go

@@ -0,0 +1,90 @@
+package util
+
+import (
+	"bytes"
+	"io"
+	"io/ioutil"
+	"log"
+	"net"
+	"net/http"
+	"net/url"
+	"strings"
+	"time"
+)
+
+const (
+	ContentTypeJson = "application/json;charset=utf-8"
+	ContentTypeForm = "application/x-www-form-urlencoded;charset=utf-8"
+)
+
+const (
+	MaxIdleCons        int = 100
+	MaxIdleConsPerHost int = 100
+	IdleConnTimeout    int = 2048
+	ConnectTimeOut     int = 30
+	KeepAlive          int = 30
+)
+
+var (
+	httpClient *http.Client
+)
+
+func init() {
+	httpClient = createHttpClient()
+}
+func createHttpClient() *http.Client {
+	client := &http.Client{
+		Transport: &http.Transport{
+			Proxy: http.ProxyFromEnvironment,
+			DialContext: (&net.Dialer{
+				Timeout:   time.Duration(ConnectTimeOut) * time.Second, //TCP连接超时30s
+				KeepAlive: time.Duration(KeepAlive) * time.Second,      //TCP keepalive保活检测定时30s
+			}).DialContext,
+			MaxIdleConns:          MaxIdleCons,
+			MaxIdleConnsPerHost:   MaxIdleConsPerHost,
+			IdleConnTimeout:       time.Duration(IdleConnTimeout) * time.Second, //闲置连接超时2048s
+			ResponseHeaderTimeout: time.Second * 60,
+		},
+	}
+	return client
+}
+
+func HttpPostJson(url string, json string) ([]byte, error) {
+	return HttpPost(url, map[string]string{"Content-Type": ContentTypeJson}, bytes.NewReader([]byte(json)))
+}
+
+func HttpPostForm(url string, header map[string]string, formValues url.Values) ([]byte, error) {
+	header["Content-Type"] = ContentTypeForm
+	return HttpPost(url, header, strings.NewReader(formValues.Encode()))
+}
+
+func HttpPost(url string, header map[string]string, body io.Reader) ([]byte, error) {
+	request, err := http.NewRequest("POST", url, body)
+	if err != nil {
+		return nil, err
+	}
+	for k, v := range header {
+		request.Header.Add(k, v)
+	}
+	response, err := httpClient.Do(request) //前面预处理一些参数,状态,Do执行发送;处理返回结果;Do:发送请求,
+	if err != nil {
+		return nil, err
+	}
+	defer response.Body.Close()
+	replay, err := ioutil.ReadAll(response.Body)
+	if err != nil {
+		log.Println("read reply error:", err)
+		return nil, err
+	}
+	return replay, nil
+}
+
+func RedingJsonDataFromRequestBody(request *http.Request) string {
+	bs, err := ioutil.ReadAll(request.Body)
+	if err != nil {
+		panic(err)
+	}
+	data := string(bs)
+	log.Println("data:", data)
+	return data
+}

BIN
src/web/res/push/push.xlsx


+ 105 - 0
src/web/templates/push/index.html

@@ -0,0 +1,105 @@
+{{include "com/inc.html"}}
+<!-- Main Header -->
+{{include "com/header.html"}}
+<!-- Left side column. 权限菜单 -->
+{{include "com/menu.html"}}
+<div class="content-wrapper">
+    <!-- Main content -->
+    <section class="content">
+        <div class="tab-content">
+            <div class="nav-tabs-custom">
+                <div class="tab-content">
+                    <div class="tab-pane active" id="tab_1">
+                        <form class="form-horizontal">
+                            <div class="box box-primary">
+                                <div class="box-header with-border">
+                                    <a class="btn btn-primary btn-sm"
+                                       style="float: right;" onclick="saveCuser()">开始推送</a>
+                                </div>
+                                <div class="box-body">
+                                    <div id="pushDiv">
+                                        <div class="box-body">
+                                            <div class="form-group">
+                                                <label class="col-sm-2 control-label"><span
+                                                            style="color:red;">* </span>客户标识</label>
+                                                <div class="col-sm-3">
+                                                    <input type="text" class="form-control" id="appid" placeholder="客户AppId" required>
+                                                </div>
+                                            </div>
+                                            <div class="form-group">
+                                                <label class="col-sm-2 control-label"><span style="color:red;">* </span>数据获取</label>
+                                                <div class="col-sm-3">
+                                                    <select class="form-control" id="getDataSelect">
+                                                        <option value=0>读取数据库</option>
+                                                        <option value=1>读取文件</option>
+                                                    </select>
+                                                </div>
+                                            </div>
+                                            <div class="form-group">
+                                                <label class="col-sm-2 control-label"><span style="color:red;">* </span>数据类型</label>
+                                                <div class="col-sm-3">
+                                                    <select class="form-control" id="dataTypeSelect">
+                                                        <option value=0>招标</option>
+                                                        <option value=1>中标</option>
+                                                    </select>
+                                                </div>
+                                            </div>
+                                            <div class="form-group">
+                                                <label class="col-sm-2 control-label"><span style="color:red;">* </span>推送方式</label>
+                                                <div class="col-sm-3">
+                                                    <select class="form-control" id="pushTypeSelect">
+                                                        <option value=0 code="interface">API</option>
+                                                        <option value=1 code="email">邮箱</option>
+                                                    </select>
+                                                </div>
+                                            </div>
+                                            <div class="form-group">
+                                                <label class="col-sm-2 control-label">邮箱地址</label>
+                                                <div class="col-sm-3">
+                                                    <input type="text" class="form-control" id="email" placeholder="邮箱地址" required>
+                                                </div>
+                                            </div>
+                                        </div>
+                                    </div>
+                                </div>
+                            </div>
+                        </form>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </section>
+    <script>
+        function saveCuser() {
+            var appid = $('#appid').val();
+            var getDataType = $('#getDataSelect option:selected').val();
+            var dataType = $('#dataTypeSelect option:selected').val();
+            var pushType = $('#pushTypeSelect option:selected').val();
+            var email = $('#email').val();
+            var param = {
+                "appid" : appid,
+                "getDataType": getDataType,
+                "dataType": dataType,
+                "email": email
+            }
+            if (pushType == 0) {
+                com.maskShow("正在生成数据...");
+                $.ajax({
+                    url: "/push/dataPush",
+                    type: 'POST',
+                    data: param,
+                    success: function (r) {
+                        com.maskHide();
+                        showTip("数据生成成功", 1000);
+                    },
+                    error: function (r) {
+                        showTip("数据生成失败", 5000);
+                    }
+                })
+            }else if (dataType == 1){
+                alert("尚未开通")
+            }
+        }
+    </script>
+</div>
+{{include "com/footer.html"}}