ws_dataExport.go 13 KB

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