task.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package task
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "strconv"
  8. "time"
  9. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  10. . "app.yhyue.com/moapp/jybase/api"
  11. qu "app.yhyue.com/moapp/jybase/common"
  12. "app.yhyue.com/moapp/jybase/date"
  13. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  14. "app.yhyue.com/moapp/message/config"
  15. "app.yhyue.com/moapp/message/db"
  16. "app.yhyue.com/moapp/message/handler/activity"
  17. "app.yhyue.com/moapp/message/model"
  18. . "app.yhyue.com/moapp/message/rpc"
  19. mrpc "app.yhyue.com/moapp/message/rpc"
  20. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  21. "github.com/gogf/gf/v2/util/gconv"
  22. )
  23. type Task struct {
  24. *xweb.Action
  25. task xweb.Mapper `xweb:"/task"` //获取任务
  26. confirmChallenge xweb.Mapper `xweb:"/confirmChallenge"` //确认挑战
  27. tes xweb.Mapper `xweb:"/tes"`
  28. }
  29. type TaskInfo struct {
  30. Name string `json:"name"`
  31. Desc string `json:"desc"`
  32. PcHref string `json:"pcHref"`
  33. AppHref string `json:"appHref"`
  34. WxHref string `json:"wxHref"`
  35. Point int `json:"point"`
  36. Icon string `json:"icon"`
  37. Type string `json:"type"`
  38. Status int `json:"status"`
  39. FinishTime string `json:"finishTime"`
  40. }
  41. //InitTask 初始化任务
  42. func (this *Task) InitTask(isNew bool) []*config.TaskStruct {
  43. if isNew {
  44. return config.TaskConf.NewTask
  45. }
  46. return config.TaskConf.OldTask
  47. }
  48. //Task 获取任务
  49. func (this *Task) Task() {
  50. sessVal := this.Session().GetMultiple()
  51. mgoUserId := gconv.String(sessVal["mgoUserId"])
  52. userId := gconv.String(sessVal["userId"])
  53. positionId := gconv.Int64(sessVal["positionId"])
  54. baseUserId := gconv.Int64(sessVal["base_user_id"])
  55. now := date.NowFormat(date.Date_Full_Layout)
  56. //
  57. rData, errMsg := func() (interface{}, error) {
  58. info := map[string]interface{}{}
  59. data := []*TaskInfo{}
  60. dataM := map[string]*TaskInfo{}
  61. //完成状态 0未开始/过期 1正在进行 2已完成
  62. status := 0
  63. //查询用户任务情况
  64. taskDetail := db.Tidb.SelectBySql(`select * from integral_task_detail where user_id =?`, baseUserId)
  65. if taskDetail != nil && len(*taskDetail) > 0 {
  66. for _, v := range *taskDetail {
  67. name := gconv.String(v["name"])
  68. desc := gconv.String(v["description"])
  69. pcHref := gconv.String(v["pc_href"])
  70. appHref := gconv.String(v["app_href"])
  71. wxHref := gconv.String(v["wx_href"])
  72. point := gconv.Int(v["point"])
  73. icon := gconv.String(v["icon"])
  74. types := gconv.String(v["type"])
  75. detailStatus := gconv.Int(v["status"])
  76. finishTime := gconv.String(v["finish_time"])
  77. tf := &TaskInfo{
  78. Name: name,
  79. Desc: desc,
  80. PcHref: pcHref,
  81. AppHref: appHref,
  82. WxHref: wxHref,
  83. Point: point,
  84. Icon: icon,
  85. Type: types,
  86. Status: detailStatus,
  87. FinishTime: finishTime,
  88. }
  89. dataM[types] = tf
  90. }
  91. }
  92. userMsg, ok := db.Mgo.FindById("user", mgoUserId, `{"s_m_openid":1,"l_registedate":1,"s_m_phone":1,"s_phone":1,"s_myemail":1,"s_nickname":1,"s_headimageurl":1,"s_password":1,"s_company":1,"s_unionid":1,"o_jy":1,"o_vipjy":1,"o_member_jy":1,"i_app_login_task":1}`)
  93. log.Println("===>", userMsg)
  94. if userMsg == nil || len(*userMsg) == 0 || !ok {
  95. return nil, fmt.Errorf("未查询到用户")
  96. }
  97. //注册时间 判断是新手任务还是老用户限时任务
  98. l_registedate := gconv.Int64((*userMsg)["l_registedate"])
  99. isNew := l_registedate > config.TaskConf.TaskStartTime //是否注册时间处于新手任务开始时间
  100. ts := this.InitTask(isNew)
  101. //初始化任务
  102. for _, v := range ts {
  103. if dataM[v.Type] == nil {
  104. dataM[v.Type] = &TaskInfo{
  105. Name: v.Name,
  106. Desc: v.Desc,
  107. PcHref: v.PcHref,
  108. AppHref: v.AppHref,
  109. WxHref: v.WxHref,
  110. Point: v.Point,
  111. Icon: v.Icon,
  112. Type: v.Type,
  113. Status: 0,
  114. }
  115. }
  116. }
  117. mail := gconv.String((*userMsg)["s_myemail"])
  118. openid := gconv.String((*userMsg)["s_m_openid"])
  119. phone := gconv.String((*userMsg)["s_phone"])
  120. if phone == "" {
  121. phone = gconv.String((*userMsg)["s_m_phone"])
  122. }
  123. if !isNew {
  124. //邮箱是否绑定
  125. if mail != "" {
  126. dataM[model.BindMail].Status = 1
  127. dataM[model.BindMail].FinishTime = now
  128. activity.Task(&model.Message{
  129. E_code: "task",
  130. E_userId: mgoUserId,
  131. E_time: time.Now().Unix(),
  132. E_app: "jyweb_node2",
  133. E_body: map[string]interface{}{
  134. "code": 1016,
  135. "types": "bindMail",
  136. "baseUserId": baseUserId,
  137. "positionId": positionId,
  138. },
  139. })
  140. }
  141. } else {
  142. //openid是否绑定
  143. if openid != "" {
  144. dataM[model.FollowWx].Status = 1
  145. dataM[model.FollowWx].FinishTime = now
  146. activity.Task(&model.Message{
  147. E_code: "task",
  148. E_userId: mgoUserId,
  149. E_time: time.Now().Unix(),
  150. E_app: "jyweb_node2",
  151. E_body: map[string]interface{}{
  152. "code": 1008, //首次订阅
  153. "types": "followWx",
  154. "baseUserId": baseUserId,
  155. "positionId": positionId,
  156. },
  157. })
  158. }
  159. if phone != "" {
  160. dataM[model.BindPhone].Status = 1
  161. dataM[model.BindPhone].FinishTime = now
  162. activity.Task(&model.Message{
  163. E_code: "task",
  164. E_userId: mgoUserId,
  165. E_time: time.Now().Unix(),
  166. E_app: "jyweb_node2",
  167. E_body: map[string]interface{}{
  168. "code": 1007, //首次订阅
  169. "types": "bindPhone",
  170. "baseUserId": baseUserId,
  171. "positionId": positionId,
  172. },
  173. })
  174. }
  175. }
  176. //判断是否已经创建任务
  177. integralTaskData := db.Tidb.SelectBySql(`select * from integral_task where user_id =? limit 1`, baseUserId)
  178. if integralTaskData == nil || len(*integralTaskData) <= 0 {
  179. db.Tidb.ExecTx("创建任务", func(tx *sql.Tx) bool {
  180. taskId := db.Tidb.InsertByTx(tx, "integral_task", map[string]interface{}{
  181. "user_id": baseUserId,
  182. "position_id": positionId,
  183. "type": qu.If(isNew, 2, 1),
  184. "create_time": now,
  185. })
  186. fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"}
  187. args := []interface{}{}
  188. //任务明细
  189. for _, v := range dataM {
  190. args = append(args, taskId, baseUserId, positionId, v.Name, v.Desc, v.Icon, v.Point, v.PcHref, v.WxHref, v.AppHref, v.Status, qu.If(v.FinishTime == "", nil, v.FinishTime), now, v.Type)
  191. }
  192. db.Tidb.InsertBatchByTx(tx, "integral_task_detail", fields, args)
  193. return true
  194. })
  195. } else {
  196. //部分初始化
  197. taskId := gconv.Int64((*integralTaskData)[0]["id"])
  198. fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"}
  199. args := []interface{}{}
  200. for _, v := range dataM {
  201. if v.Status == 0 {
  202. if db.Tidb.CountBySql(`select count(1) from integral_task_detail where user_id =? and type =?`, baseUserId, v.Type) > 0 {
  203. continue
  204. }
  205. //任务明细
  206. args = append(args, taskId, baseUserId, positionId, v.Name, v.Desc, v.Icon, v.Point, v.PcHref, v.WxHref, v.AppHref, v.Status, qu.If(v.FinishTime == "", nil, v.FinishTime), now, v.Type)
  207. }
  208. }
  209. if len(args) > 0 {
  210. db.Tidb.InsertBatch("integral_task_detail", fields, args)
  211. }
  212. if (*integralTaskData)[0]["end_time"] != nil {
  213. et := gconv.String((*integralTaskData)[0]["end_time"])
  214. loc, _ := time.LoadLocation("Asia/Shanghai")
  215. endtime, _ := time.ParseInLocation(date.Date_Full_Layout, et, loc)
  216. if time.Now().Before(endtime) {
  217. //当前时间未超过结束时间
  218. status = 1
  219. } else {
  220. status = 3
  221. }
  222. info["endTime"] = endtime.Unix()
  223. }
  224. success_status := gconv.Int((*integralTaskData)[0]["success_status"])
  225. if success_status == 1 {
  226. //已完成挑战
  227. status = 2
  228. }
  229. }
  230. // for _, v := range dataM {
  231. for _, v := range ts {
  232. data = append(data, &TaskInfo{
  233. Name: dataM[v.Type].Name,
  234. Desc: dataM[v.Type].Desc,
  235. PcHref: dataM[v.Type].PcHref,
  236. AppHref: dataM[v.Type].AppHref,
  237. WxHref: dataM[v.Type].WxHref,
  238. Point: dataM[v.Type].Point,
  239. Icon: dataM[v.Type].Icon,
  240. Type: dataM[v.Type].Type,
  241. Status: dataM[v.Type].Status,
  242. FinishTime: dataM[v.Type].FinishTime,
  243. })
  244. }
  245. if isNew {
  246. info["newbieTask"] = data
  247. info["taskType"] = 2
  248. } else {
  249. info["limitedTask"] = data
  250. info["taskType"] = 1
  251. }
  252. //查询完成状态
  253. info["status"] = status
  254. return info, nil
  255. }()
  256. if errMsg != nil {
  257. log.Printf("%s Task %s异常:%s\n", userId, errMsg.Error())
  258. }
  259. this.ServeJson(NewResult(rData, errMsg))
  260. }
  261. //EntSubscribe 判断企业是否订阅
  262. func EntSubscribe(entUserId, entId int64) int {
  263. if entId > 0 {
  264. //企业
  265. data, ok := db.Mgo.FindOne("entniche_rule", map[string]interface{}{
  266. "i_entid": entId,
  267. "i_userid": entUserId,
  268. })
  269. if data == nil || len(*data) <= 0 || !ok {
  270. return 0
  271. }
  272. o_entniche := gconv.Map((*data)["o_entniche"])
  273. a_items := gconv.Maps(o_entniche["a_items"])
  274. for _, v := range a_items {
  275. a_key := gconv.Maps(v["a_key"])
  276. if len(a_key) > 0 {
  277. return 1
  278. }
  279. }
  280. }
  281. return 0
  282. }
  283. func (this *Task) ConfirmChallenge() {
  284. sessVal := this.Session().GetMultiple()
  285. baseUserId := gconv.Int64(sessVal["base_user_id"])
  286. userId := gconv.String(sessVal["mgoUserId"])
  287. rData, errMsg := func() (interface{}, error) {
  288. infoMap := map[string]interface{}{}
  289. if string(this.Body()) == "" {
  290. return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1001)
  291. }
  292. body := xweb.FilterXSS(string(this.Body()))
  293. //接收参数
  294. json.Unmarshal([]byte(body), &infoMap)
  295. if len(infoMap) == 0 {
  296. return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1002)
  297. }
  298. if gconv.Int(infoMap["challenge"]) == 1 {
  299. //判断用户是否已经完成所有任务
  300. userMsg, ok := db.Mgo.FindById("user", userId, `{"l_registedate":1}`)
  301. if userMsg == nil || len(*userMsg) == 0 || !ok {
  302. return map[string]interface{}{"status": -1}, fmt.Errorf("未查询到用户")
  303. }
  304. //注册时间 判断是新手任务还是老用户限时任务
  305. l_registedate := gconv.Int64((*userMsg)["l_registedate"])
  306. isNew := l_registedate > config.TaskConf.TaskStartTime //是否注册时间处于新手任务开始时间
  307. dayLater := time.Now().Add(time.Duration(config.TaskConf.TaskDayTime-1) * time.Hour * 24)
  308. endTime := time.Date(dayLater.Year(), dayLater.Month(), dayLater.Day(), 23, 59, 59, 0, time.Local)
  309. if db.Tidb.Update("integral_task", map[string]interface{}{
  310. "user_id": baseUserId,
  311. }, map[string]interface{}{
  312. "end_time": endTime.Format(date.Date_Full_Layout),
  313. }) {
  314. go func() {
  315. //用户点击“确认挑战”给用户发消息
  316. wxUrl := "/front/sess/" + Se.EncodeString(userId+",_id,identityKeep,") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl)
  317. appUrl := "/jyapp/free/sess/" + Se.EncodeString(userId+",id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl)
  318. req := &message.MultipleSaveMsgReq{
  319. UserIds: userId,
  320. Title: config.PushConfig.Messages.ConfirmChallenge.Title,
  321. Content: fmt.Sprintf(config.PushConfig.Messages.ConfirmChallenge.Content, qu.If(isNew, "新手", "限时").(string)),
  322. MsgType: config.PushConfig.Messages.ConfirmChallenge.MsgType,
  323. Link: config.PushConfig.Messages.ConfirmChallenge.PcUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl,
  324. Appid: config.PushConfig.Messages.ConfirmChallenge.Appid,
  325. AppPushUrl: appUrl,
  326. WxPushUrl: config.PushConfig.Webdomain + wxUrl,
  327. IosPushUrl: appUrl,
  328. }
  329. SendMsg("确认挑战", req)
  330. }()
  331. }
  332. ts := this.InitTask(isNew)
  333. taskDetailCount := db.Tidb.CountBySql(`select count(1) from integral_task_detail where user_id =? and status=1`, baseUserId)
  334. taskMsg := db.Tidb.SelectBySql(`select id from integral_task where user_id =? LIMIT 1`, baseUserId)
  335. if taskMsg == nil || len(*taskMsg) == 0 {
  336. return map[string]interface{}{"status": -1}, fmt.Errorf("任务创建失败")
  337. }
  338. taskId := gconv.Int64((*taskMsg)[0]["id"])
  339. if gconv.Int(taskDetailCount) == len(ts) {
  340. //任务完成 赠送超级订阅
  341. //判断是否完成所有任务且开启确认挑战
  342. if mrpc.SubVipHarvest(userId, 7, "") == nil {
  343. if db.Tidb.Update("integral_task", map[string]interface{}{"id": taskId}, map[string]interface{}{
  344. "success_status": 1,
  345. }) {
  346. go func() {
  347. wxUrl := "/front/sess/" + Se.EncodeString(userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl)
  348. appUrl := "/jyapp/free/sess/" + Se.EncodeString(userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl)
  349. SendMsg("获赠七天超级订阅服务", &message.MultipleSaveMsgReq{
  350. UserIds: userId,
  351. Title: config.PushConfig.Messages.GetVip.Title,
  352. Content: fmt.Sprintf(config.PushConfig.Messages.GetVip.Content, qu.If(isNew, "新手", "限时").(string)),
  353. MsgType: config.PushConfig.Messages.GetVip.MsgType,
  354. Link: config.PushConfig.Messages.GetVip.PcUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl,
  355. Appid: config.PushConfig.Messages.GetVip.Appid,
  356. AppPushUrl: appUrl,
  357. WxPushUrl: config.PushConfig.Webdomain + wxUrl,
  358. IosPushUrl: appUrl,
  359. })
  360. }()
  361. }
  362. }
  363. }
  364. return map[string]interface{}{"status": 1}, nil
  365. }
  366. return map[string]interface{}{"status": 1}, nil
  367. }()
  368. if errMsg != nil {
  369. log.Printf("%s UserAccount ConfirmChallenge 异常:%s\n", baseUserId, errMsg.Error())
  370. }
  371. this.ServeJson(NewResult(rData, errMsg))
  372. }
  373. func (this *Task) Tes() {
  374. d := mrpc.SubVipHarvest("65433cecbd1d69bdca017505", 7, "")
  375. fmt.Println(d)
  376. return
  377. activity.Task(&model.Message{
  378. E_code: "task",
  379. E_userId: "65433cecbd1d69bdca017505", //5ed6cii...
  380. E_time: 1698800604, //1605223065
  381. E_app: "jyapp_node1", //jywx_node1/jyweb_node2/jyapp_node1/jysubscribe
  382. E_body: map[string]interface{}{
  383. "baseUserId": 3755163,
  384. "code": 1008,
  385. "num": 50,
  386. "positionId": 1205322910,
  387. "types": "followWx",
  388. }})
  389. }