package front import ( "encoding/json" "errors" "fmt" "jfw/config" "jfw/jyutil" "qfw/util/dataexport" "strconv" "github.com/go-xweb/xweb" // "jfw/pay" "jfw/public" "jfw/wx" "log" "net/url" "qfw/util" "qfw/util/redis" "strings" "time" "go.mongodb.org/mongo-driver/bson" ) type WsDataExport struct { *xweb.Action searchExport xweb.Mapper `xweb:"/front/wx_dataExport/searchExport"` //微信数据导出 toPreview xweb.Mapper `xweb:"/front/wx_dataExport/toPreview/(.*)"` //微信数据导出-预览页面 getPreview xweb.Mapper `xweb:"/front/wx_dataExport/getPreview"` //微信数据导出-预览数据 submitOrder xweb.Mapper `xweb:"/front/wx_dataExport/submitOrder"` //微信数据导出-提交订单页面 wxToOrderDetail xweb.Mapper `xweb:"/front/wx_dataExport/wxToOrderDetail"` //微信数据导出-订单详情 } func init() { xweb.AddAction(&WsDataExport{}) } func (w *WsDataExport) WxToOrderDetail() error { myOpenid := "" if openid := w.GetSession("s_m_openid"); openid != nil { myOpenid = openid.(string) } else { checkIsSubscribeFlag := true if w.GetString("state") == "wx" { //微信跳回来的 code := w.GetString("code") if code != "" { openid := jyutil.Getopenid(code) if openid != "" { isSubscribe := true if !checkIsSubscribeFlag { checkIsSubscribeFlag = true isSubscribe = CheckUserIsSubscribe(openid) } if isSubscribe { FindUserAndCreateSess(openid, w.Session()) } } } } else { if public.CheckWxBrowser(w.Request) { //所有参数都不再使用,跳到微信验证用户 return w.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(w.Site()+w.Url()), "wx"), 302) } } userid := util.ObjToString(w.GetSession("userId")) myOpenid, _ = w.Session().Get("s_m_openid").(string) if userid == "" { return errors.New("未登录") } } myUserId, _ := w.Session().Get("userId").(string) orderCode := w.GetString("orderCode") //分享 w.T["signature"] = wx.SignJSSDK(w.Site() + w.Url()) w.T["openid"] = se.EncodeString(myOpenid) mynickname, _ := w.Session().Get("s_nickname").(string) myavatar, _ := w.Session().Get("s_avatar").(string) w.T["nickname"] = mynickname w.T["avatar"] = myavatar //----------------------- orderDetail := map[string]interface{}{} filter := dataexport.SieveCondition{} queryMap := map[string]interface{}{ "order_code": orderCode, "user_id": myUserId, } if orderCode != "" { orderDetail = *public.Mysql.FindOne(tableName_order, queryMap, "", "") } orderDetail["order_code"] = orderCode if orderDetail["pay_money"] != nil { orderDetail["pay_money"] = float64(orderDetail["pay_money"].(int64)) / 100 } if orderDetail["filter"] != nil { err := json.Unmarshal([]byte(orderDetail["filter"].(string)), &filter) if err == nil { publishtime := filter.PublishTime if publishtime != "" { timeArr := strings.Split(publishtime, "_") if len(timeArr) == 2 { start, err := strconv.ParseInt(timeArr[0], 10, 64) end, erro := strconv.ParseInt(timeArr[1], 10, 64) if err == nil && erro == nil { filter.PublishTime = util.FormatDateByInt64(&start, layout_date) + "-" + util.FormatDateByInt64(&end, layout_date) } else if err == nil && erro != nil { filter.PublishTime = util.FormatDateByInt64(&start, layout_date) + "-" } else if err != nil && erro == nil { filter.PublishTime = "-" + util.FormatDateByInt64(&end, layout_date) } } } filter.MinPrice = public.GetPriceDes_SieveCondition(filter.MinPrice, filter.MaxPrice) orderDetail["filter"] = filter } else { log.Println("筛选条件-关键词-结构体反序列化-错误", err) } } if orderDetail["applybill_type"] != nil && orderDetail["applybill_type"].(int64) == 0 { orderDetail["applybill_type"] = "个人" } else if orderDetail["applybill_type"] != nil && orderDetail["applybill_type"].(int64) == 1 { orderDetail["applybill_type"] = "单位" } else { orderDetail["applybill_type"] = "-" } if orderDetail["applybill_status"] != nil && orderDetail["applybill_status"].(int64) == 1 { orderDetail["applybill_status"] = "T" //已申请 } else { orderDetail["applybill_status"] = "F" //未申请 } if orderDetail["pay_time"] != nil { pay_time := strings.Replace(orderDetail["pay_time"].(string), "-", ".", -1) orderDetail["pay_time"] = pay_time } if orderDetail["create_time"] != nil { create_time := strings.Replace(orderDetail["create_time"].(string), "-", ".", -1) orderDetail["create_time"] = create_time } orderMoney := orderDetail["order_money"] if orderMoney != nil { orderDetail["order_money"] = float64(orderMoney.(int64)) / 100 } //加密 orderStatus := util.IntAll((orderDetail)["order_status"]) orderDetail["order_status"] = orderStatus if orderStatus == 1 { orderDetail["transaction_id"] = func() string { table := "" payway := util.ObjToString(orderDetail["pay_way"]) if strings.Contains(payway, "wx") { payway = "微信" table = "weixin_pay" } else if strings.Contains(payway, "ali") { payway = "支付宝" table = "ali_pay" } else { return "" } orderDetail["pay_way"] = payway wxPayMap := map[string]interface{}{} wxPayMap["out_trade_no"] = orderDetail["out_trade_no"] wxpay := public.Mysql.FindOne(table, wxPayMap, "", "") if wxpay == nil { return "" } return util.ObjToString((*wxpay)["transaction_id"]) }() } w.T["o"] = orderDetail w.Render("/weixin/dataExport/dataExport_toOrderDetail.html", &w.T) return nil } // func (w *WsDataExport) GetPreview() error { userId := util.ObjToString(w.GetSession("userId")) if userId == "" { return errors.New("未登录") } _res := make(map[string]interface{}) //数据预览每天限制50次 incurKey := fmt.Sprintf("PreviewData_%s_%d", w.GetSession("userId"), time.Now().Day()) times := util.IntAll(redis.Get("other", incurKey)) if times >= 50 { _res["msg"] = "超出预览次数" w.ServeJson(map[string]interface{}{ "res": _res, }) return nil } if times == 0 { redis.Put("other", incurKey, 1, 24*60*60) } else { redis.Incr("other", incurKey) } _id := util.SE.Decode4Hex(w.GetString("_id")) dataType := w.GetString("dataType") //从500条数据中筛选字段最全五条 scd := dataexport.GetSqlObjFromId(public.MQFW, _id) kws := scd.Keyword res, err := dataexport.GetDataExportSearchResult(public.DbConf.Elasticsearch.Main.Address, scd, dataType, -1) if res == nil || err != nil { log.Println("PreviewData查询出错") return w.Render("/pc/dataExport_noDataErr.html", &w.T) } //格式化字段 res_screen := dataexport.ScreenData(res, dataType, 20, kws) list := dataexport.FormatExportData(public.Mgo_Ent, &res_screen, config.Sysconfig["webdomain"].(string), dataType, true) _res["data"] = subUrl(list, dataType) _res["dataType"] = dataType w.ServeJson(map[string]interface{}{ "res": _res, }) return nil } //预览 func (w *WsDataExport) ToPreview(_id string) error { defer util.Catch() checkIsSubscribeFlag := true openid, _ := w.Session().Get("s_m_openid").(string) userid, _ := w.Session().Get("userId").(string) if userid == "" { if w.GetString("state") == "wx" { //微信跳回来的 code := w.GetString("code") if code != "" { openid = jyutil.Getopenid(code) if openid != "" { isSubscribe := true if !checkIsSubscribeFlag { checkIsSubscribeFlag = true isSubscribe = CheckUserIsSubscribe(openid) } if isSubscribe { FindUserAndCreateSess(openid, w.Session()) } } } } else { if public.CheckWxBrowser(w.Request) { //所有参数都不再使用,跳到微信验证用户 return w.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(w.Site()+w.Url()), "wx"), 302) } } userid = util.ObjToString(w.GetSession("userId")) } w.T["id"] = _id w.T["dataType"] = w.GetString("dataType") w.T["ttf"] = public.GetFontVersion() + "_" + public.PC return w.Render("/weixin/dataExport/dataExport_previewData.html") } //创建订单页面 func (w *WsDataExport) SubmitOrder() error { defer util.Catch() checkIsSubscribeFlag := true id := util.SE.Decode4Hex(w.GetString("id")) openid := util.ObjToString(w.GetSession("s_m_openid")) userId := util.ObjToString(w.GetSession("userId")) if userId == "" { if w.GetString("state") == "wx" { //微信跳回来的 code := w.GetString("code") if code != "" { openid = jyutil.Getopenid(code) if openid != "" { isSubscribe := true if !checkIsSubscribeFlag { checkIsSubscribeFlag = true isSubscribe = CheckUserIsSubscribe(openid) } if isSubscribe { FindUserAndCreateSess(openid, w.Session()) } } } } else { if public.CheckWxBrowser(w.Request) { //所有参数都不再使用,跳到微信验证用户 return w.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(w.Site()+w.Url()), "wx"), 302) } } userId = util.ObjToString(w.GetSession("userId")) if userId == "" { return errors.New("未登录") } } msgCount := dataexport.GetDataExportSearchCountByScdId(public.MQFW, public.DbConf.Elasticsearch.Main.Address, id) if msgCount > public.ExConf.MsgMaxCount || msgCount == -1 { msgCount = public.ExConf.MsgMaxCount } if msgCount < 1 { w.Render("/weixin/dataExport/404.html", &w.T) return nil } //订单数据存入session中 w.SetSession("dataexport_waitcreateorder", map[string]interface{}{ "id": id, //用户的筛选条件mongodb中的id string "data_count": msgCount, //匹配到的数据总数 int }) //邮箱验证 // w.DelSession("DataExportVerifyEmail_val") // w.DelSession("DataExportVerifyPhone_val") resEmail, _ := w.GetSession("DataExportVerifyEmail_val").(string) resPhone, _ := w.GetSession("DataExportVerifyPhone_val").(string) if resEmail != "" { lastSendDEVerify := util.Int64All(w.GetSession("CreatEVerifyTime")) timeSpaceing := lastSendDEVerify - time.Now().Unix() + 60*5 w.T["email"] = resEmail w.T["timeSpaceing"] = timeSpaceing } if resPhone != "" { w.T["phone"] = resPhone } if resEmail == "" || resPhone == "" { lastEmail, lastPhone := "", "" if lastOrder := public.Mysql.FindOne("dataexport_order", map[string]interface{}{ "user_id": userId, "product_type": "历史数据", }, "user_mail,user_phone", "create_time desc"); lastOrder != nil && len(*lastOrder) > 0 { //若有上一个订单,邮箱和手机号则取上个订单中的 lastEmail, _ = (*lastOrder)["user_mail"].(string) lastPhone, _ = (*lastOrder)["user_phone"].(string) } else { //若无上个订单,则取用户账户中设置的手机号和邮箱 userData, _ := mongodb.FindById("user", userId, `{"s_myemail":1,"s_phone":1,"s_m_phone":1}`) if userData != nil && len(*userData) > 0 { lastEmail, _ = (*userData)["s_myemail"].(string) lastPhone, _ = util.If((*userData)["s_phone"] != nil, (*userData)["s_phone"], (*userData)["s_m_phone"]).(string) } } if resEmail == "" && lastEmail != "" { w.T["email"] = lastEmail w.SetSession("EMVerifySucess", true) w.SetSession("DataExportVerifyEmail_val", lastEmail) } if resPhone == "" { setPhone := lastPhone if isPhone(openid) { //剑鱼助手手机号登录 setPhone = openid } if setPhone != "" { w.SetSession("DataExportVerifyPhone_val", lastPhone) } w.T["phone"] = lastPhone } } w.T["_id"] = w.GetString("id") w.T["msgCount"] = msgCount //信息总量 incurKey := fmt.Sprintf("PreviewData_%s_%d", w.GetSession("userId"), time.Now().Day()) //每日限制预览次数 w.T["PreviewData"] = util.IntAll(redis.Get("other", incurKey)) //字段包 spec := w.GetString("dataspec") if spec != "" { w.T["dataspec"] = spec } w.Render("/weixin/dataExport/dataExport_payOrder.html", &w.T) return nil } //微信数据导出 func (wd *WsDataExport) SearchExport() error { openid := util.ObjToString(wd.GetSession("s_m_openid")) userId := util.ObjToString(wd.GetSession("userId")) if userId == "" { return errors.New("未登录") } //接收超级搜索页面参数 reqData := public.BidSearchExport{ Keywords: wd.GetString("searchvalue"), //搜索词 Publishtime: wd.GetString("publishtime"), //发布时间 Area: wd.GetString("scope"), //地区 Subtype: wd.GetString("subtype"), //信息类型 Minprice: wd.GetString("minprice"), //最低价格 Maxprice: wd.GetString("maxprice"), //最高价格 Industry: strings.TrimSpace(wd.GetString("industry")), //选中的行业 SelectType: wd.GetString("selectType"), //标题 or 全文 Buyerclass: wd.GetString("buyerclass"), //采购单位行业 Hasbuyertel: wd.GetString("buyertel"), //是否有采购电话 Haswinnertel: wd.GetString("winnertel"), //是否有中标电话 SelectIds: strings.TrimSpace(wd.GetString("selectIds")), //选择信息id } saveData := reqData.PassBidSearchExport() saveData["s_openid"] = openid saveData["s_userid"] = userId //存入数据库 _id := mongodb.Save(dataexport.ExportTable, saveData) wd.ServeJson(bson.M{"_id": util.SE.Encode2Hex(_id)}) return nil }