ws_dataExport.go 14 KB

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