ws_dataExport.go 13 KB

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