ws_dataExport.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package front
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "jfw/config"
  7. "jfw/jyutil"
  8. "qfw/util/dataexport"
  9. "qfw/util/jy"
  10. "strconv"
  11. "github.com/go-xweb/xweb"
  12. // "jfw/pay"
  13. "jfw/public"
  14. "jfw/wx"
  15. "log"
  16. "net/url"
  17. "qfw/util"
  18. "qfw/util/redis"
  19. "strings"
  20. "time"
  21. "go.mongodb.org/mongo-driver/bson"
  22. )
  23. type WsDataExport struct {
  24. *xweb.Action
  25. searchExport xweb.Mapper `xweb:"/front/wx_dataExport/searchExport"` //微信数据导出
  26. toPreview xweb.Mapper `xweb:"/front/wx_dataExport/toPreview/(.*)"` //微信数据导出-预览页面
  27. getPreview xweb.Mapper `xweb:"/front/wx_dataExport/getPreview"` //微信数据导出-预览数据
  28. submitOrder xweb.Mapper `xweb:"/front/wx_dataExport/submitOrder"` //微信数据导出-提交订单页面
  29. wxToOrderDetail xweb.Mapper `xweb:"/front/wx_dataExport/wxToOrderDetail"` //微信数据导出-订单详情
  30. }
  31. func init() {
  32. xweb.AddAction(&WsDataExport{})
  33. }
  34. func (w *WsDataExport) WxToOrderDetail() error {
  35. myOpenid := ""
  36. if openid := w.GetSession("s_m_openid"); openid != nil {
  37. myOpenid = openid.(string)
  38. } else {
  39. if w.GetString("state") == "wx" {
  40. //微信跳回来的
  41. code := w.GetString("code")
  42. if code != "" {
  43. openid := jyutil.Getopenid(code)
  44. if openid != "" {
  45. FindUserAndCreateSess(openid, w.Session(), "wx", false)
  46. }
  47. }
  48. } else {
  49. if public.CheckWxBrowser(w.Request) {
  50. //所有参数都不再使用,跳到微信验证用户
  51. return w.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(w.Site()+w.Url()), "wx"), 302)
  52. }
  53. }
  54. userid := util.ObjToString(w.GetSession("userId"))
  55. myOpenid, _ = w.Session().Get("s_m_openid").(string)
  56. if userid == "" {
  57. return errors.New("未登录")
  58. }
  59. }
  60. myUserId, _ := w.Session().Get("userId").(string)
  61. orderCode := w.GetString("orderCode")
  62. //分享
  63. w.T["signature"] = wx.SignJSSDK(w.Site() + w.Url())
  64. w.T["openid"] = se.EncodeString(myOpenid)
  65. mynickname, _ := w.Session().Get("s_nickname").(string)
  66. myavatar, _ := w.Session().Get("s_avatar").(string)
  67. w.T["nickname"] = mynickname
  68. w.T["avatar"] = myavatar
  69. //-----------------------
  70. orderDetail := map[string]interface{}{}
  71. filter := dataexport.SieveCondition{}
  72. queryMap := map[string]interface{}{
  73. "order_code": orderCode,
  74. "user_id": myUserId,
  75. }
  76. if orderCode != "" {
  77. orderDetail = *public.Mysql.FindOne(tableName_order, queryMap, "", "")
  78. }
  79. orderDetail["order_code"] = orderCode
  80. if orderDetail["pay_money"] != nil {
  81. orderDetail["pay_money"] = float64(orderDetail["pay_money"].(int64)) / 100
  82. }
  83. if orderDetail["filter"] != nil {
  84. err := json.Unmarshal([]byte(orderDetail["filter"].(string)), &filter)
  85. if err == nil {
  86. publishtime := filter.PublishTime
  87. if publishtime != "" {
  88. timeArr := strings.Split(publishtime, "_")
  89. if len(timeArr) == 2 {
  90. start, err := strconv.ParseInt(timeArr[0], 10, 64)
  91. end, erro := strconv.ParseInt(timeArr[1], 10, 64)
  92. if err == nil && erro == nil {
  93. filter.PublishTime = util.FormatDateByInt64(&start, layout_date) + "-" + util.FormatDateByInt64(&end, layout_date)
  94. } else if err == nil && erro != nil {
  95. filter.PublishTime = util.FormatDateByInt64(&start, layout_date) + "-"
  96. } else if err != nil && erro == nil {
  97. filter.PublishTime = "-" + util.FormatDateByInt64(&end, layout_date)
  98. }
  99. }
  100. }
  101. filter.MinPrice = public.GetPriceDes_SieveCondition(filter.MinPrice, filter.MaxPrice)
  102. orderDetail["filter"] = filter
  103. } else {
  104. log.Println("筛选条件-关键词-结构体反序列化-错误", err)
  105. }
  106. }
  107. if orderDetail["applybill_type"] != nil && orderDetail["applybill_type"].(int64) == 0 {
  108. orderDetail["applybill_type"] = "个人"
  109. } else if orderDetail["applybill_type"] != nil && orderDetail["applybill_type"].(int64) == 1 {
  110. orderDetail["applybill_type"] = "单位"
  111. } else {
  112. orderDetail["applybill_type"] = "-"
  113. }
  114. if orderDetail["applybill_status"] != nil && orderDetail["applybill_status"].(int64) == 1 {
  115. orderDetail["applybill_status"] = "T" //已申请
  116. } else {
  117. orderDetail["applybill_status"] = "F" //未申请
  118. }
  119. if orderDetail["pay_time"] != nil {
  120. pay_time := strings.Replace(orderDetail["pay_time"].(string), "-", ".", -1)
  121. orderDetail["pay_time"] = pay_time
  122. }
  123. if orderDetail["create_time"] != nil {
  124. create_time := strings.Replace(orderDetail["create_time"].(string), "-", ".", -1)
  125. orderDetail["create_time"] = create_time
  126. }
  127. orderMoney := orderDetail["order_money"]
  128. if orderMoney != nil {
  129. orderDetail["order_money"] = float64(orderMoney.(int64)) / 100
  130. }
  131. //加密
  132. orderStatus := util.IntAll((orderDetail)["order_status"])
  133. orderDetail["order_status"] = orderStatus
  134. if orderStatus == 1 {
  135. orderDetail["transaction_id"] = func() string {
  136. table := ""
  137. payway := util.ObjToString(orderDetail["pay_way"])
  138. if strings.Contains(payway, "wx") {
  139. payway = "微信"
  140. table = "weixin_pay"
  141. } else if strings.Contains(payway, "ali") {
  142. payway = "支付宝"
  143. table = "ali_pay"
  144. } else {
  145. return ""
  146. }
  147. orderDetail["pay_way"] = payway
  148. wxPayMap := map[string]interface{}{}
  149. wxPayMap["out_trade_no"] = orderDetail["out_trade_no"]
  150. wxpay := public.Mysql.FindOne(table, wxPayMap, "", "")
  151. if wxpay == nil {
  152. return ""
  153. }
  154. return util.ObjToString((*wxpay)["transaction_id"])
  155. }()
  156. }
  157. w.T["o"] = orderDetail
  158. w.Render("/weixin/dataExport/dataExport_toOrderDetail.html", &w.T)
  159. return nil
  160. }
  161. //
  162. func (w *WsDataExport) GetPreview() error {
  163. userId := util.ObjToString(w.GetSession("userId"))
  164. if userId == "" {
  165. return errors.New("未登录")
  166. }
  167. _res := make(map[string]interface{})
  168. //数据预览每天限制50次
  169. incurKey := fmt.Sprintf("PreviewData_%s_%d", w.GetSession("userId"), time.Now().Day())
  170. times := util.IntAll(redis.Get("other", incurKey))
  171. if times >= 50 {
  172. _res["msg"] = "超出预览次数"
  173. w.ServeJson(map[string]interface{}{
  174. "res": _res,
  175. })
  176. return nil
  177. }
  178. if times == 0 {
  179. redis.Put("other", incurKey, 1, 24*60*60)
  180. } else {
  181. redis.Incr("other", incurKey)
  182. }
  183. _id := util.SE.Decode4Hex(w.GetString("_id"))
  184. dataType := w.GetString("dataType")
  185. //从500条数据中筛选字段最全五条
  186. scd := dataexport.GetSqlObjFromId(public.MQFW, _id)
  187. kws := scd.Keyword
  188. res, err := dataexport.GetDataExportSearchResult(public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.DbName, public.DbConf.Elasticsearch.Main.Address, scd, dataType, -1)
  189. if res == nil || err != nil {
  190. return w.Render("/pc/dataExport_noDataErr.html", &w.T)
  191. }
  192. msgCount := dataexport.GetDataExportSearchCountByScdId(public.MQFW, public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.DbName, public.DbConf.Elasticsearch.Main.Address, _id)
  193. //格式化字段
  194. res_screen := dataexport.ScreenData(res, dataType, 20, kws)
  195. list := dataexport.FormatExportData(public.Mgo_Ent, &res_screen, config.Sysconfig["webdomain"].(string), dataType, true)
  196. if msgCount > 20000 {
  197. msgCount = 20000
  198. }
  199. _res["data"] = subUrl(list, dataType)
  200. _res["dataType"] = dataType
  201. _res["total"] = msgCount
  202. w.ServeJson(map[string]interface{}{
  203. "res": _res,
  204. })
  205. return nil
  206. }
  207. //预览
  208. func (w *WsDataExport) ToPreview(_id string) error {
  209. defer util.Catch()
  210. openid, _ := w.Session().Get("s_m_openid").(string)
  211. userid, _ := w.Session().Get("userId").(string)
  212. if userid == "" {
  213. if w.GetString("state") == "wx" {
  214. //微信跳回来的
  215. code := w.GetString("code")
  216. if code != "" {
  217. openid = jyutil.Getopenid(code)
  218. if openid != "" {
  219. FindUserAndCreateSess(openid, w.Session(), "wx", false)
  220. }
  221. }
  222. } else {
  223. if public.CheckWxBrowser(w.Request) {
  224. //所有参数都不再使用,跳到微信验证用户
  225. return w.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(w.Site()+w.Url()), "wx"), 302)
  226. }
  227. }
  228. userid = util.ObjToString(w.GetSession("userId"))
  229. }
  230. w.T["id"] = _id
  231. w.T["dataType"] = w.GetString("dataType")
  232. w.T["ttf"] = public.GetFontVersion() + "_" + public.PC
  233. return w.Render("/weixin/dataExport/dataExport_previewData.html")
  234. }
  235. //创建订单页面
  236. func (w *WsDataExport) SubmitOrder() error {
  237. defer util.Catch()
  238. id := util.SE.Decode4Hex(w.GetString("id"))
  239. openid := util.ObjToString(w.GetSession("s_m_openid"))
  240. userId := util.ObjToString(w.GetSession("userId"))
  241. if userId == "" {
  242. if w.GetString("state") == "wx" {
  243. //微信跳回来的
  244. code := w.GetString("code")
  245. if code != "" {
  246. openid = jyutil.Getopenid(code)
  247. if openid != "" {
  248. FindUserAndCreateSess(openid, w.Session(), "wx", false)
  249. }
  250. }
  251. } else {
  252. if public.CheckWxBrowser(w.Request) {
  253. //所有参数都不再使用,跳到微信验证用户
  254. return w.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(w.Site()+w.Url()), "wx"), 302)
  255. }
  256. }
  257. userId = util.ObjToString(w.GetSession("userId"))
  258. if userId == "" {
  259. return errors.New("未登录")
  260. }
  261. }
  262. msgCount := dataexport.GetDataExportSearchCountByScdId(public.MQFW, public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.DbName, public.DbConf.Elasticsearch.Main.Address, id)
  263. if msgCount > public.ExConf.MsgMaxCount || msgCount == -1 {
  264. msgCount = public.ExConf.MsgMaxCount
  265. }
  266. if msgCount < 1 {
  267. w.Render("/weixin/dataExport/404.html", &w.T)
  268. return nil
  269. }
  270. //订单数据存入session中
  271. w.SetSession("dataexport_waitcreateorder", map[string]interface{}{
  272. "id": id, //用户的筛选条件mongodb中的id string
  273. "data_count": msgCount, //匹配到的数据总数 int
  274. })
  275. //邮箱验证
  276. // w.DelSession("DataExportVerifyEmail_val")
  277. // w.DelSession("DataExportVerifyPhone_val")
  278. resEmail, _ := w.GetSession("DataExportVerifyEmail_val").(string)
  279. resPhone, _ := w.GetSession("DataExportVerifyPhone_val").(string)
  280. if resEmail != "" {
  281. lastSendDEVerify := util.Int64All(w.GetSession("CreatEVerifyTime"))
  282. timeSpaceing := lastSendDEVerify - time.Now().Unix() + 60*5
  283. w.T["email"] = resEmail
  284. w.T["timeSpaceing"] = timeSpaceing
  285. }
  286. if resPhone != "" {
  287. w.T["phone"] = resPhone
  288. }
  289. if resEmail == "" || resPhone == "" {
  290. lastEmail, lastPhone := "", ""
  291. if lastPhone, lastEmail = dataexport.GetLastExportPhoneAndMail(public.Mysql, userId, util.ObjToString(w.GetSession("entUserId"))); lastPhone == "" || lastEmail == "" {
  292. userData, _ := mongodb.FindById("user", userId, `{"s_myemail":1,"s_phone":1,"s_m_phone":1}`)
  293. if userData != nil && len(*userData) > 0 {
  294. if lastEmail == "" {
  295. lastEmail, _ = (*userData)["s_myemail"].(string)
  296. }
  297. if lastPhone == "" {
  298. lastPhone, _ = util.If((*userData)["s_phone"] != nil, (*userData)["s_phone"], (*userData)["s_m_phone"]).(string)
  299. }
  300. }
  301. }
  302. if resEmail == "" && lastEmail != "" {
  303. w.T["email"] = lastEmail
  304. w.SetSession("EMVerifySucess", true)
  305. w.SetSession("DataExportVerifyEmail_val", lastEmail)
  306. }
  307. if resPhone == "" {
  308. setPhone := lastPhone
  309. if isPhone(openid) { //剑鱼助手手机号登录
  310. setPhone = openid
  311. }
  312. if setPhone != "" {
  313. w.SetSession("DataExportVerifyPhone_val", lastPhone)
  314. }
  315. w.T["phone"] = lastPhone
  316. }
  317. }
  318. w.T["_id"] = w.GetString("id")
  319. w.T["msgCount"] = msgCount //信息总量
  320. incurKey := fmt.Sprintf("PreviewData_%s_%d", w.GetSession("userId"), time.Now().Day()) //每日限制预览次数
  321. w.T["PreviewData"] = util.IntAll(redis.Get("other", incurKey))
  322. //字段包
  323. spec := w.GetString("dataspec")
  324. if spec != "" {
  325. w.T["dataspec"] = spec
  326. }
  327. w.Render("/weixin/dataExport/dataExport_payOrder.html", &w.T)
  328. return nil
  329. }
  330. //微信数据导出
  331. func (wd *WsDataExport) SearchExport() error {
  332. openid := util.ObjToString(wd.GetSession("s_m_openid"))
  333. userId := util.ObjToString(wd.GetSession("userId"))
  334. if userId == "" {
  335. return errors.New("未登录")
  336. }
  337. //接收超级搜索页面参数
  338. reqData := public.BidSearchExport{
  339. Keywords: wd.GetString("searchvalue"), //搜索词
  340. Publishtime: wd.GetString("publishtime"), //发布时间
  341. Area: wd.GetString("scope"), //地区
  342. Subtype: wd.GetString("subtype"), //信息类型
  343. Minprice: wd.GetString("minprice"), //最低价格
  344. Maxprice: wd.GetString("maxprice"), //最高价格
  345. Industry: strings.TrimSpace(wd.GetString("industry")), //选中的行业
  346. SelectType: wd.GetString("selectType"), //标题 or 全文
  347. Buyerclass: wd.GetString("buyerclass"), //采购单位行业
  348. Hasbuyertel: wd.GetString("buyertel"), //是否有采购电话
  349. Haswinnertel: wd.GetString("winnertel"), //是否有中标电话
  350. SelectIds: strings.TrimSpace(wd.GetString("selectIds")), //选择信息id
  351. Notkey: wd.GetString("notkey"), //排除词
  352. City: wd.GetString("city"), //城市
  353. FileExists: wd.GetString("fileExists"), //有无附件
  354. }
  355. saveData := reqData.PassBidSearchExport()
  356. saveData["selectType"] = strings.Join(jy.GetVipState(public.Mysql, public.MQFW, userId).
  357. GetQueryItems(wd.GetString("selectType"), util.Int64All(config.Sysconfig["bidSearchOldUserLimit"])), ",")
  358. saveData["s_openid"] = openid
  359. saveData["s_userid"] = userId
  360. saveData["comeinfrom"] = "supersearchPage"
  361. region := util.If(reqData.Area == "全国", "", reqData.Area).(string)
  362. if region != "" && reqData.City != "" {
  363. region += "," + reqData.City
  364. } else {
  365. region = reqData.City
  366. }
  367. saveData["region"] = strings.Split(region, ",")
  368. //是否开启 正文 标题同时搜索只搜正文的开关
  369. saveData["searchTypeSwitch"], _ = config.Sysconfig["searchTypeSwitch"].(bool)
  370. //存入数据库
  371. _id := mongodb.Save(dataexport.ExportTable, saveData)
  372. wd.ServeJson(bson.M{"_id": util.SE.Encode2Hex(_id)})
  373. return nil
  374. }