activity_signup.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "io"
  12. "io/ioutil"
  13. "net/http"
  14. "os"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // 获取所有关注了该产业大套餐客户的openid
  20. func GetCygxBigTypeUserIndustryFllowOpneidByActivityIds(activityId int) (items []*cygx.OpenIdList, err error) {
  21. companyIds, e := cygx.GetCygxIndustryFollowCompanyIdsByActivityId(activityId)
  22. if e != nil {
  23. err = e
  24. return
  25. }
  26. permissionList, e := cygx.GetCompanyPermissionByUsersZhengShi(*companyIds)
  27. if e != nil {
  28. err = e
  29. return
  30. }
  31. permissionListMap := make(map[int]string, 0)
  32. for _, item := range permissionList {
  33. if permission, ok := permissionListMap[item.CompanyId]; !ok {
  34. permissionListMap[item.CompanyId] = item.Permission
  35. } else {
  36. permissionListMap[item.CompanyId] = permission + "," + item.Permission
  37. }
  38. }
  39. var companyIdslice []string
  40. for k, v := range permissionListMap {
  41. permissionZhengShiStr := v
  42. if strings.Count(permissionZhengShiStr, "医药") == 2 && strings.Count(permissionZhengShiStr, "消费") == 2 && strings.Count(permissionZhengShiStr, "科技") == 2 && strings.Count(permissionZhengShiStr, "智造") == 2 {
  43. companyIdslice = append(companyIdslice, strconv.Itoa(k))
  44. }
  45. }
  46. finalIds := strings.Join(companyIdslice, ",")
  47. if finalIds != "" {
  48. var conditionuiUser string
  49. var parsUser []interface{}
  50. conditionuiUser = ` AND company_id IN (` + finalIds + `)`
  51. listUser, e := models.GetWxUserList(conditionuiUser, parsUser)
  52. if e != nil {
  53. err = e
  54. return
  55. }
  56. mapMobileUser := make(map[string]string)
  57. for _, v := range listUser {
  58. mapMobileUser[v.Mobile] = v.Mobile
  59. }
  60. var mobiles []string
  61. listFllow, e := cygx.GetCygxForeverUserIndustryFllowOpneidByActivityIds(activityId)
  62. if e != nil {
  63. err = e
  64. return
  65. }
  66. for _, v := range listFllow {
  67. if mapMobileUser[v.Mobile] != "" {
  68. mobiles = append(mobiles, v.Mobile)
  69. }
  70. }
  71. items, e = cygx.GetUserRecordListByMobileArr(mobiles)
  72. //items, e = models.GetCygxUserIndustryFllowOpneidByActivityIdAndCompanyIds(activityId, finalIds)
  73. if e != nil {
  74. err = e
  75. return
  76. }
  77. }
  78. return
  79. }
  80. // 获取所有关注了该产业行业套餐客户的openid
  81. func GetCygxIndustryPackageUserIndustryFllowOpneidByActivityIds(activityId int) (items []*cygx.OpenIdList, err error) {
  82. companyIds, e := cygx.GetCygxIndustryFollowCompanyIdsByActivityId(activityId)
  83. if e != nil {
  84. err = e
  85. return
  86. }
  87. permissionList, e := cygx.GetCompanyPermissionByUsersZhengShi(*companyIds)
  88. if e != nil {
  89. err = e
  90. return
  91. }
  92. activityInfo, errInfo := cygx.GetAddActivityInfoById(activityId)
  93. if errInfo != nil {
  94. err = errInfo
  95. return
  96. }
  97. permissionListMap := make(map[int]string, 0)
  98. for _, item := range permissionList {
  99. if permission, ok := permissionListMap[item.CompanyId]; !ok {
  100. permissionListMap[item.CompanyId] = item.Permission
  101. } else {
  102. permissionListMap[item.CompanyId] = permission + "," + item.Permission
  103. }
  104. }
  105. var companyIdslice []string
  106. for k, v := range permissionListMap {
  107. permissionZhengShiStr := v
  108. if strings.Count(permissionZhengShiStr, "医药") == 2 && strings.Count(permissionZhengShiStr, "消费") == 2 && strings.Count(permissionZhengShiStr, "科技") == 2 && strings.Count(permissionZhengShiStr, "智造") == 2 {
  109. continue
  110. } else if strings.Contains(permissionZhengShiStr, activityInfo.ChartPermissionName) {
  111. companyIdslice = append(companyIdslice, strconv.Itoa(k))
  112. }
  113. }
  114. finalIds := strings.Join(companyIdslice, ",")
  115. if finalIds != "" {
  116. var conditionuiUser string
  117. var parsUser []interface{}
  118. conditionuiUser = ` AND company_id IN (` + finalIds + `)`
  119. listUser, e := models.GetWxUserList(conditionuiUser, parsUser)
  120. if e != nil {
  121. err = e
  122. return
  123. }
  124. mapMobileUser := make(map[string]string)
  125. for _, v := range listUser {
  126. mapMobileUser[v.Mobile] = v.Mobile
  127. }
  128. var mobiles []string
  129. listFllow, e := cygx.GetCygxForeverUserIndustryFllowOpneidByActivityIds(activityId)
  130. if e != nil {
  131. err = e
  132. return
  133. }
  134. for _, v := range listFllow {
  135. if mapMobileUser[v.Mobile] != "" {
  136. mobiles = append(mobiles, v.Mobile)
  137. }
  138. }
  139. items, e = cygx.GetUserRecordListByMobileArr(mobiles)
  140. //items, e = models.GetCygxUserIndustryFllowOpneidByActivityIdAndCompanyIds(activityId, finalIds)
  141. if e != nil {
  142. err = e
  143. return
  144. }
  145. }
  146. return
  147. }
  148. // 获取所有关注了该产业其他行业套餐客户的openid
  149. func GetCygxOtherIndustryPackageUserIndustryFllowOpneidByActivityIds(activityId int) (items []*cygx.OpenIdList, err error) {
  150. companyIds, e := cygx.GetCygxIndustryFollowCompanyIdsByActivityId(activityId)
  151. if e != nil {
  152. err = e
  153. return
  154. }
  155. permissionList, e := cygx.GetCompanyPermissionByUsersZhengShi(*companyIds)
  156. if e != nil {
  157. err = e
  158. return
  159. }
  160. activityInfo, errInfo := cygx.GetAddActivityInfoById(activityId)
  161. if errInfo != nil {
  162. err = errInfo
  163. return
  164. }
  165. permissionListMap := make(map[int]string, 0)
  166. for _, item := range permissionList {
  167. if permission, ok := permissionListMap[item.CompanyId]; !ok {
  168. permissionListMap[item.CompanyId] = item.Permission
  169. } else {
  170. permissionListMap[item.CompanyId] = permission + "," + item.Permission
  171. }
  172. }
  173. var companyIdslice []string
  174. for k, v := range permissionListMap {
  175. permissionZhengShiStr := v
  176. if strings.Count(permissionZhengShiStr, "医药") == 2 && strings.Count(permissionZhengShiStr, "消费") == 2 && strings.Count(permissionZhengShiStr, "科技") == 2 && strings.Count(permissionZhengShiStr, "智造") == 2 {
  177. continue
  178. } else if !strings.Contains(permissionZhengShiStr, activityInfo.ChartPermissionName) {
  179. companyIdslice = append(companyIdslice, strconv.Itoa(k))
  180. }
  181. }
  182. finalIds := strings.Join(companyIdslice, ",")
  183. if finalIds != "" {
  184. //items, e = models.GetCygxUserIndustryFllowOpneidByActivityIdAndCompanyIds(activityId, finalIds)
  185. var conditionuiUser string
  186. var parsUser []interface{}
  187. conditionuiUser = ` AND company_id IN (` + finalIds + `)`
  188. listUser, e := models.GetWxUserList(conditionuiUser, parsUser)
  189. if e != nil {
  190. err = e
  191. return
  192. }
  193. mapMobileUser := make(map[string]string)
  194. for _, v := range listUser {
  195. mapMobileUser[v.Mobile] = v.Mobile
  196. }
  197. var mobiles []string
  198. listFllow, e := cygx.GetCygxForeverUserIndustryFllowOpneidByActivityIds(activityId)
  199. if e != nil {
  200. err = e
  201. return
  202. }
  203. for _, v := range listFllow {
  204. if mapMobileUser[v.Mobile] != "" {
  205. mobiles = append(mobiles, v.Mobile)
  206. }
  207. }
  208. items, e = cygx.GetUserRecordListByMobileArr(mobiles)
  209. if e != nil {
  210. err = e
  211. return
  212. }
  213. }
  214. return
  215. }
  216. // 获取所有关注了该产业其他行业套餐客户的openid
  217. func GetCygxInteractiveUserOpneidByActivityIds(activityId int) (items []*cygx.OpenIdList, err error) {
  218. userIds, e := models.GetCygxInteractiveUserByActivityId(activityId)
  219. if e != nil {
  220. err = e
  221. return
  222. }
  223. var idSlice []string
  224. for _, id := range userIds {
  225. idSlice = append(idSlice, strconv.Itoa(*id))
  226. }
  227. ids := strings.Join(idSlice, ",")
  228. if ids != "" {
  229. var conditionuiUser string
  230. var parsUser []interface{}
  231. conditionuiUser = ` AND user_id IN (` + ids + `)`
  232. listUser, e := models.GetWxUserList(conditionuiUser, parsUser)
  233. if e != nil {
  234. err = e
  235. return
  236. }
  237. mapMobileUser := make(map[string]string)
  238. for _, v := range listUser {
  239. mapMobileUser[v.Mobile] = v.Mobile
  240. }
  241. var mobiles []string
  242. listFllow, e := cygx.GetCygxForeverUserIndustryFllowOpneidByActivityIds(activityId)
  243. if e != nil {
  244. err = e
  245. return
  246. }
  247. for _, v := range listFllow {
  248. if mapMobileUser[v.Mobile] != "" {
  249. mobiles = append(mobiles, v.Mobile)
  250. }
  251. }
  252. items, e = cygx.GetUserRecordListByMobileArr(mobiles)
  253. //items, e = models.GetCygxUserOpneidByUserIds(ids)
  254. if e != nil {
  255. err = e
  256. return
  257. }
  258. }
  259. return
  260. }
  261. // ReturnBodyRule 微信返回通过结果集
  262. type ReturnBodyRule struct {
  263. ErrorCode int `json:"errcode,omitempty"`
  264. ErrorMessage string `json:"errmsg,omitempty"`
  265. }
  266. func init232() {
  267. MakeActivitySigninImg(2161)
  268. }
  269. // MakeActivitySigninImg 生成活动签到图片
  270. func MakeActivitySigninImg12(activityId int) (err error) {
  271. var msg string
  272. defer func() {
  273. if err != nil {
  274. fmt.Println(err)
  275. go alarm_msg.SendAlarmMsg("扫码签到日志记录,失败,活动ID:"+strconv.Itoa(activityId)+err.Error()+";msg:"+msg, 3)
  276. }
  277. }()
  278. itemToken, err := models.GetWxToken(utils.WxCygxAppId, utils.WxCygxAppSecret)
  279. if err != nil {
  280. return
  281. }
  282. if itemToken.AccessToken == "" {
  283. msg = "accessToken is empty"
  284. return
  285. }
  286. var envVersion string
  287. var resourceUrl string
  288. if utils.RunMode == "release" {
  289. envVersion = "release"
  290. } else {
  291. envVersion = "develop"
  292. }
  293. url := "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + itemToken.AccessToken
  294. method := "POST"
  295. payload := strings.NewReader(`{
  296. "page":"` + utils.WX_MSG_PATH_ACTIVITY_SIGNIN + `",
  297. "scene":"` + strconv.Itoa(activityId) + `",
  298. "env_version":"` + envVersion + `",
  299. "check_path":false,
  300. "auto_color":true
  301. }`)
  302. client := &http.Client{}
  303. req, err := http.NewRequest(method, url, payload)
  304. if err != nil {
  305. msg = "获取微信二维码失败,Err:" + err.Error()
  306. return
  307. }
  308. req.Header.Add("Content-Type", "application/json")
  309. postBody, err := client.Do(req)
  310. if err != nil {
  311. msg = "获取微信二维码失败,Err:" + err.Error()
  312. return
  313. }
  314. defer postBody.Body.Close()
  315. uploadDir := "static/img/share/"
  316. uuid := utils.GetRandStringNoSpecialChar(28)
  317. if !utils.FileIsExist(uploadDir) {
  318. err = os.MkdirAll(uploadDir, 0755)
  319. if err != nil {
  320. msg = "生成文件夹失败,Err:" + err.Error()
  321. return
  322. }
  323. }
  324. imagePath := uploadDir + uuid + ".jpg"
  325. switch header := postBody.Header.Get("Content-Type"); {
  326. case strings.HasPrefix(header, "application/json"):
  327. tokenResp := ReturnBodyRule{}
  328. decoder := json.NewDecoder(postBody.Body)
  329. if decodeErr := decoder.Decode(&tokenResp); decodeErr != nil {
  330. msg = "获取微信二维码失败,Err:" + decodeErr.Error()
  331. return
  332. }
  333. case strings.HasPrefix(header, "image"):
  334. reply, e := ioutil.ReadAll(postBody.Body)
  335. if e != nil {
  336. err = e
  337. msg = "获取微信二维码失败,Err:" + err.Error()
  338. return
  339. }
  340. imageContent, e := os.Create(imagePath)
  341. if e != nil {
  342. err = e
  343. msg = "获取微信二维码失败,Err:" + err.Error()
  344. return
  345. }
  346. writeStringRes, e := io.WriteString(imageContent, string(reply))
  347. if e != nil {
  348. err = e
  349. fmt.Println(writeStringRes)
  350. return
  351. }
  352. closeErr := imageContent.Close()
  353. if closeErr != nil {
  354. err = closeErr
  355. return
  356. }
  357. randStr := utils.GetRandStringNoSpecialChar(28)
  358. fileName := randStr + ".jpg"
  359. savePath := uploadDir + time.Now().Format("200601/20060102/")
  360. savePath += fileName
  361. //上传到阿里云
  362. err = services.UploadFileToAliyun(fileName, imagePath, savePath)
  363. if err != nil {
  364. fmt.Println("文件上传失败,Err:" + err.Error())
  365. return
  366. }
  367. fileHost := "https://hzstatic.hzinsights.com/"
  368. resourceUrl = fileHost + savePath
  369. defer func() {
  370. os.Remove(imagePath)
  371. }()
  372. default:
  373. msg = "生成二维码失败"
  374. return
  375. }
  376. fmt.Println(resourceUrl)
  377. return
  378. }
  379. // 校验报名截止时间
  380. func CheckSiginupDeadline(activityInfo *cygx.ActivityDetail) (checkTime bool, popupMsg string) {
  381. checkTime = true
  382. if activityInfo.SiginupDeadline != utils.EmptyDateTimeStr && activityInfo.SiginupDeadline != "" {
  383. timeResp := utils.StrTimeToTime(activityInfo.SiginupDeadline)
  384. if timeResp.Before(time.Now()) {
  385. checkTime = false
  386. popupMsg = "该活动已截止报名"
  387. }
  388. }
  389. return
  390. }
  391. // 校验报名点数
  392. func CheckActivityPoints(activityInfo *cygx.ActivityDetail, wxUser *models.WxUser) (checkPoints bool, popupMsg, companyPoints, activityPoints string, err error) {
  393. checkPoints = true
  394. if activityInfo.IsResearchPoints == 1 {
  395. //获取活动对用户要扣的点
  396. userPointsNum, e := cygx.GetCygxActivityPointsSetUserNum(activityInfo.ActivityId)
  397. if e != nil {
  398. err = errors.New("GetCygxActivityPointsSetUserNum, Err: " + e.Error())
  399. return
  400. }
  401. // 获取用户所在公司剩余的点
  402. companyPointsNum, e := cygx.GetCompanyPoints(wxUser.CompanyId)
  403. if e != nil && e.Error() != utils.ErrNoRow() {
  404. err = errors.New("GetCompanyPoints, Err: " + e.Error())
  405. return
  406. }
  407. if companyPointsNum-userPointsNum < 0 {
  408. checkPoints = false
  409. popupMsg = "点数不足"
  410. }
  411. companyPoints = fmt.Sprint(companyPointsNum)
  412. activityPoints = fmt.Sprint(userPointsNum)
  413. }
  414. return
  415. }
  416. // 校验报名是否需要绑定邮箱
  417. func CheckActivityUserEmail(activityInfo *cygx.ActivityDetail, wxUser *models.WxUser) (checkEmail bool, popupMsg string) {
  418. checkEmail = true
  419. if activityInfo.IsNeedEmail == 1 {
  420. if wxUser.Email == "" {
  421. checkEmail = false
  422. popupMsg = "应上市公司要求,该会议报名需\n提供邮箱,用户邮箱为空"
  423. }
  424. }
  425. return
  426. }
  427. // 校验报名顺序 截止时间>点数>邮箱
  428. func CheckActivityUserAll(activityInfo *cygx.ActivityDetail, wxUser *models.WxUser) (popupMsg string, err error) {
  429. _, popupMsg = CheckSiginupDeadline(activityInfo)
  430. if popupMsg != "" {
  431. return
  432. }
  433. _, popupMsg, _, _, err = CheckActivityPoints(activityInfo, wxUser)
  434. if popupMsg != "" {
  435. return
  436. }
  437. _, popupMsg = CheckActivityUserEmail(activityInfo, wxUser)
  438. if popupMsg != "" {
  439. return
  440. }
  441. return
  442. }