task.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. if !isNew {
  120. //邮箱是否绑定
  121. if mail != "" {
  122. dataM[model.BindMail].Status = 1
  123. dataM[model.BindMail].FinishTime = now
  124. activity.Task(&model.Message{
  125. E_code: "task",
  126. E_userId: mgoUserId,
  127. E_time: time.Now().Unix(),
  128. E_app: "jyweb_node2",
  129. E_body: map[string]interface{}{
  130. "code": 1016,
  131. "types": "bindMail",
  132. "baseUserId": baseUserId,
  133. "positionId": positionId,
  134. },
  135. })
  136. }
  137. } else {
  138. //openid是否绑定
  139. if openid != "" {
  140. dataM[model.FollowWx].Status = 1
  141. dataM[model.FollowWx].FinishTime = now
  142. activity.Task(&model.Message{
  143. E_code: "task",
  144. E_userId: mgoUserId,
  145. E_time: time.Now().Unix(),
  146. E_app: "jyweb_node2",
  147. E_body: map[string]interface{}{
  148. "code": 1008, //首次订阅
  149. "types": "followWx",
  150. "baseUserId": baseUserId,
  151. "positionId": positionId,
  152. },
  153. })
  154. }
  155. }
  156. //判断是否已经创建任务
  157. integralTaskData := db.Tidb.SelectBySql(`select * from integral_task where user_id =? limit 1`, baseUserId)
  158. if integralTaskData == nil || len(*integralTaskData) <= 0 {
  159. db.Tidb.ExecTx("创建任务", func(tx *sql.Tx) bool {
  160. taskId := db.Tidb.InsertByTx(tx, "integral_task", map[string]interface{}{
  161. "user_id": baseUserId,
  162. "position_id": positionId,
  163. "type": qu.If(isNew, 2, 1),
  164. "create_time": now,
  165. })
  166. fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"}
  167. args := []interface{}{}
  168. //任务明细
  169. for _, v := range dataM {
  170. 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)
  171. }
  172. db.Tidb.InsertBatchByTx(tx, "integral_task_detail", fields, args)
  173. return true
  174. })
  175. } else {
  176. //部分初始化
  177. taskId := gconv.Int64((*integralTaskData)[0]["id"])
  178. fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"}
  179. args := []interface{}{}
  180. for _, v := range dataM {
  181. if v.Status == 0 {
  182. if db.Tidb.CountBySql(`select count(1) from integral_task_detail where user_id =? and type =?`, baseUserId, v.Type) > 0 {
  183. continue
  184. }
  185. //任务明细
  186. 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)
  187. }
  188. }
  189. if len(args) > 0 {
  190. db.Tidb.InsertBatch("integral_task_detail", fields, args)
  191. }
  192. if (*integralTaskData)[0]["end_time"] != nil {
  193. et := gconv.String((*integralTaskData)[0]["end_time"])
  194. loc, _ := time.LoadLocation("Asia/Shanghai")
  195. endtime, _ := time.ParseInLocation(date.Date_Full_Layout, et, loc)
  196. if time.Now().Before(endtime) {
  197. //当前时间未超过结束时间
  198. status = 1
  199. } else {
  200. status = 3
  201. }
  202. info["endTime"] = endtime.Unix()
  203. }
  204. success_status := gconv.Int((*integralTaskData)[0]["success_status"])
  205. if success_status == 1 {
  206. //已完成挑战
  207. status = 2
  208. }
  209. }
  210. // for _, v := range dataM {
  211. for _, v := range ts {
  212. data = append(data, &TaskInfo{
  213. Name: dataM[v.Type].Name,
  214. Desc: dataM[v.Type].Desc,
  215. PcHref: dataM[v.Type].PcHref,
  216. AppHref: dataM[v.Type].AppHref,
  217. WxHref: dataM[v.Type].WxHref,
  218. Point: dataM[v.Type].Point,
  219. Icon: dataM[v.Type].Icon,
  220. Type: dataM[v.Type].Type,
  221. Status: dataM[v.Type].Status,
  222. FinishTime: dataM[v.Type].FinishTime,
  223. })
  224. }
  225. if isNew {
  226. info["newbieTask"] = data
  227. info["taskType"] = 2
  228. } else {
  229. info["limitedTask"] = data
  230. info["taskType"] = 1
  231. }
  232. //查询完成状态
  233. info["status"] = status
  234. return info, nil
  235. }()
  236. if errMsg != nil {
  237. log.Printf("%s Task %s异常:%s\n", userId, errMsg.Error())
  238. }
  239. this.ServeJson(NewResult(rData, errMsg))
  240. }
  241. //EntSubscribe 判断企业是否订阅
  242. func EntSubscribe(entUserId, entId int64) int {
  243. if entId > 0 {
  244. //企业
  245. data, ok := db.Mgo.FindOne("entniche_rule", map[string]interface{}{
  246. "i_entid": entId,
  247. "i_userid": entUserId,
  248. })
  249. if data == nil || len(*data) <= 0 || !ok {
  250. return 0
  251. }
  252. o_entniche := gconv.Map((*data)["o_entniche"])
  253. a_items := gconv.Maps(o_entniche["a_items"])
  254. for _, v := range a_items {
  255. a_key := gconv.Maps(v["a_key"])
  256. if len(a_key) > 0 {
  257. return 1
  258. }
  259. }
  260. }
  261. return 0
  262. }
  263. func (this *Task) ConfirmChallenge() {
  264. sessVal := this.Session().GetMultiple()
  265. baseUserId := gconv.Int64(sessVal["base_user_id"])
  266. userId := gconv.String(sessVal["mgoUserId"])
  267. rData, errMsg := func() (interface{}, error) {
  268. infoMap := map[string]interface{}{}
  269. if string(this.Body()) == "" {
  270. return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1001)
  271. }
  272. body := xweb.FilterXSS(string(this.Body()))
  273. //接收参数
  274. json.Unmarshal([]byte(body), &infoMap)
  275. if len(infoMap) == 0 {
  276. return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1002)
  277. }
  278. if gconv.Int(infoMap["challenge"]) == 1 {
  279. dayLater := time.Now().Add(time.Duration(config.TaskConf.TaskDayTime-1) * time.Hour * 24)
  280. endTime := time.Date(dayLater.Year(), dayLater.Month(), dayLater.Day(), 23, 59, 59, 0, time.Local)
  281. if db.Tidb.Update("integral_task", map[string]interface{}{
  282. "user_id": baseUserId,
  283. }, map[string]interface{}{
  284. "end_time": endTime.Format(date.Date_Full_Layout),
  285. }) {
  286. go func() {
  287. //用户点击“确认挑战”给用户发消息
  288. wxUrl := "/front/sess/" + Se.EncodeString(userId+",_id,identityKeep,") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl)
  289. appUrl := "/jyapp/free/sess/" + Se.EncodeString(userId+",id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl)
  290. req := &message.MultipleSaveMsgReq{
  291. UserIds: userId,
  292. Title: config.PushConfig.Messages.ConfirmChallenge.Title,
  293. Content: config.PushConfig.Messages.ConfirmChallenge.Content,
  294. MsgType: config.PushConfig.Messages.ConfirmChallenge.MsgType,
  295. Link: config.PushConfig.Messages.ConfirmChallenge.PcUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl,
  296. Appid: config.PushConfig.Messages.ConfirmChallenge.Appid,
  297. AppPushUrl: appUrl,
  298. WxPushUrl: config.PushConfig.Webdomain + wxUrl,
  299. IosPushUrl: appUrl,
  300. }
  301. SendMsg("确认挑战", req)
  302. }()
  303. }
  304. //判断用户是否已经完成所有任务
  305. userMsg, ok := db.Mgo.FindById("user", userId, `{"l_registedate":1}`)
  306. if userMsg == nil || len(*userMsg) == 0 || !ok {
  307. return map[string]interface{}{"status": -1}, fmt.Errorf("未查询到用户")
  308. }
  309. //注册时间 判断是新手任务还是老用户限时任务
  310. l_registedate := gconv.Int64((*userMsg)["l_registedate"])
  311. isNew := l_registedate > config.TaskConf.TaskStartTime //是否注册时间处于新手任务开始时间
  312. ts := this.InitTask(isNew)
  313. taskDetailCount := db.Tidb.CountBySql(`select count(1) from integral_task_detail where user_id =? and status=1`, baseUserId)
  314. taskMsg := db.Tidb.SelectBySql(`select id from integral_task where user_id =? LIMIT 1`, baseUserId)
  315. if taskMsg == nil || len(*taskMsg) == 0 {
  316. return map[string]interface{}{"status": -1}, fmt.Errorf("任务创建失败")
  317. }
  318. taskId := gconv.Int64((*taskMsg)[0]["id"])
  319. if gconv.Int(taskDetailCount) == len(ts) {
  320. //任务完成 赠送超级订阅
  321. //判断是否完成所有任务且开启确认挑战
  322. if mrpc.SubVipHarvest(userId, 7, "") == nil {
  323. if db.Tidb.Update("integral_task", map[string]interface{}{"id": taskId}, map[string]interface{}{
  324. "success_status": 1,
  325. }) {
  326. go func() {
  327. wxUrl := "/front/sess/" + Se.EncodeString(userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl)
  328. appUrl := "/jyapp/free/sess/" + Se.EncodeString(userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl)
  329. SendMsg("获赠七天超级订阅服务", &message.MultipleSaveMsgReq{
  330. UserIds: userId,
  331. Title: config.PushConfig.Messages.GetVip.Title,
  332. Content: config.PushConfig.Messages.GetVip.Content,
  333. MsgType: config.PushConfig.Messages.GetVip.MsgType,
  334. Link: config.PushConfig.Messages.GetVip.PcUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl,
  335. Appid: config.PushConfig.Messages.GetVip.Appid,
  336. AppPushUrl: appUrl,
  337. WxPushUrl: config.PushConfig.Webdomain + wxUrl,
  338. IosPushUrl: appUrl,
  339. })
  340. }()
  341. }
  342. }
  343. }
  344. return map[string]interface{}{"status": 1}, nil
  345. }
  346. return map[string]interface{}{"status": 1}, nil
  347. }()
  348. if errMsg != nil {
  349. log.Printf("%s UserAccount ConfirmChallenge 异常:%s\n", baseUserId, errMsg.Error())
  350. }
  351. this.ServeJson(NewResult(rData, errMsg))
  352. }
  353. func (this *Task) Tes() {
  354. d := mrpc.SubVipHarvest("65433cecbd1d69bdca017505", 7, "")
  355. fmt.Println(d)
  356. return
  357. activity.Task(&model.Message{
  358. E_code: "task",
  359. E_userId: "65433cecbd1d69bdca017505", //5ed6cii...
  360. E_time: 1698800604, //1605223065
  361. E_app: "jyapp_node1", //jywx_node1/jyweb_node2/jyapp_node1/jysubscribe
  362. E_body: map[string]interface{}{
  363. "baseUserId": 3755163,
  364. "code": 1008,
  365. "num": 50,
  366. "positionId": 1205322910,
  367. "types": "followWx",
  368. }})
  369. }