user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package services
  2. import (
  3. "errors"
  4. "hongze/hongze_clpt/models"
  5. "hongze/hongze_clpt/utils"
  6. "strings"
  7. "time"
  8. )
  9. var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
  10. var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
  11. //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  12. func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
  13. wxUser.OpenId = userRecord.OpenId
  14. wxUser.UnionId = userRecord.UnionId
  15. wxUser.NickName = userRecord.NickName
  16. //wxUser.RealName = userRecord.RealName
  17. //wxUser.BindAccount = userRecord.BindAccount
  18. wxUser.Headimgurl = userRecord.Headimgurl
  19. wxUser.SessionKey = userRecord.SessionKey
  20. }
  21. func GetWxUserItemByOpenId(unionId string) (item *models.WxUserItem, err error) {
  22. //通过openid获取用户关联信息
  23. item = new(models.WxUserItem)
  24. item.UnionId = unionId // 先写入 unionId
  25. userRecord, userRecordErr := models.GetUserRecordByUnionId(unionId)
  26. if userRecordErr != nil && userRecordErr.Error() != utils.ErrNoRow() {
  27. err = userRecordErr
  28. return
  29. }
  30. //如果 userRecord 表中的手机号不为空,那么就通过手机号来获取详情
  31. if userRecord != nil {
  32. if userRecord.BindAccount != "" {
  33. user, userErr := models.GetWxUserItemByUserMobile(userRecord.BindAccount)
  34. if userErr != nil && userErr.Error() != utils.ErrNoRow() {
  35. err = userErr
  36. return
  37. }
  38. if user != nil {
  39. item = user
  40. }
  41. }
  42. }
  43. return
  44. }
  45. //获取 用户类型 //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户;6、冻结客户;7、流失客户
  46. func GetUserType(companyId int) (userType int, permissionStrnew string, err error) {
  47. var permissionStr, permissionZhegnshiStr string
  48. if companyId <= 1 {
  49. userType = 0
  50. } else {
  51. total, errs := models.GetCountCompanyDetailByIdGroup(companyId)
  52. if errs != nil {
  53. err = errs
  54. return
  55. }
  56. if total == 0 {
  57. userType = 0
  58. } else {
  59. companyDetail, errs := models.GetCompanyDetailByIdGroup(companyId)
  60. if errs != nil {
  61. err = errs
  62. return
  63. }
  64. permissionStr, errs = models.GetCompanyPermissionByUserNoStatus(companyId)
  65. if errs != nil {
  66. err = errs
  67. return
  68. }
  69. permissionZhegnshiStr, errs = models.GetCompanyPermissionByUserZhengShi(companyId)
  70. if errs != nil {
  71. err = errs
  72. return
  73. }
  74. //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户
  75. if companyDetail.Status == "永续" {
  76. userType = 1
  77. } else if companyDetail.Status == "试用" {
  78. userType = 5
  79. } else if companyDetail.Status == "正式" {
  80. if permissionStr == "专家" {
  81. userType = 4
  82. } else if strings.Count(permissionZhegnshiStr, "医药") == 2 && strings.Count(permissionZhegnshiStr, "消费") == 2 && strings.Count(permissionZhegnshiStr, "科技") == 2 && strings.Count(permissionZhegnshiStr, "智造") == 2 {
  83. userType = 2
  84. } else {
  85. userType = 3
  86. }
  87. if userType == 3 {
  88. if !strings.Contains(permissionStr, "医药") && !strings.Contains(permissionStr, "消费") && !strings.Contains(permissionStr, "科技") && !strings.Contains(permissionStr, "智造") {
  89. userType = 4
  90. }
  91. }
  92. } else if companyDetail.Status == "冻结" {
  93. userType = 6
  94. } else if companyDetail.Status == "流失" {
  95. userType = 7
  96. }
  97. }
  98. }
  99. permissionStrnew = permissionStr
  100. return
  101. }
  102. //用户绑定
  103. func BindWxUser(mobile, countryCode string) (wxUser *models.WxUserItem, err error) {
  104. if mobile == "" {
  105. err = errors.New("手机号或邮箱必填一个")
  106. return
  107. }
  108. //根据手机号获取用户信息
  109. if mobile != "" {
  110. tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
  111. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  112. err = wxUserErr
  113. return
  114. }
  115. wxUser = tmpWxUser
  116. }
  117. var userId int
  118. //如果查询出来的用户是nil,那么需要新增用户
  119. if wxUser == nil {
  120. user := &models.WxUser{
  121. CompanyId: 1,
  122. CreatedTime: time.Now(),
  123. FirstLogin: 1,
  124. Enabled: 1,
  125. RegisterPlatform: 7,
  126. RegisterTime: time.Now(),
  127. Mobile: mobile,
  128. //Email: email,
  129. IsRegister: 1,
  130. Source: 3,
  131. CountryCode: countryCode,
  132. OutboundMobile: mobile,
  133. OutboundCountryCode: countryCode,
  134. }
  135. tmpUserId, addUserErr := models.AddWxUser(user)
  136. if addUserErr != nil {
  137. err = addUserErr
  138. return
  139. }
  140. user.UserId = int(tmpUserId)
  141. userId = int(tmpUserId)
  142. wxUser, err = models.GetWxUserItemByUserId(userId)
  143. } else {
  144. userId = wxUser.UserId
  145. err = models.BindUserOutboundMobile(mobile, countryCode, userId)
  146. if err != nil {
  147. return
  148. }
  149. if wxUser.IsRegister == 0 {
  150. models.ModifyWxUserRegisterStatus(userId)
  151. }
  152. }
  153. return
  154. }
  155. //用户绑定用户手机号以及openid
  156. func BindWxUserMobileAndOpenid(mobile, openid, countryCode string) (wxUser *models.WxUserItem, err error) {
  157. if mobile == "" {
  158. err = errors.New("手机号或邮箱必填一个")
  159. return
  160. }
  161. var bindAccount string
  162. //根据手机号获取用户信息
  163. if mobile != "" {
  164. tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
  165. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  166. err = wxUserErr
  167. return
  168. }
  169. wxUser = tmpWxUser
  170. bindAccount = mobile
  171. }
  172. //查询openid的第三方(微信)信息
  173. userRecord, err := models.GetUserRecordByOpenId(openid)
  174. if err != nil {
  175. return
  176. }
  177. var userId int
  178. //如果查询出来的用户是nil,那么需要新增用户
  179. if wxUser == nil {
  180. user := &models.WxUser{
  181. CompanyId: 1,
  182. CreatedTime: time.Now(),
  183. FirstLogin: 1,
  184. Enabled: 1,
  185. RegisterPlatform: 7,
  186. RegisterTime: time.Now(),
  187. Mobile: mobile,
  188. //Email: email,
  189. IsRegister: 1,
  190. Source: 3,
  191. CountryCode: countryCode,
  192. OutboundMobile: mobile,
  193. OutboundCountryCode: countryCode,
  194. }
  195. tmpUserId, addUserErr := models.AddWxUser(user)
  196. if addUserErr != nil {
  197. err = addUserErr
  198. return
  199. }
  200. user.UserId = int(tmpUserId)
  201. userId = int(tmpUserId)
  202. wxUser, err = models.GetWxUserItemByUserId(userId)
  203. } else {
  204. userId = wxUser.UserId
  205. err = models.BindUserOutboundMobile(mobile, countryCode, userId)
  206. if err != nil {
  207. return
  208. }
  209. if wxUser.IsRegister == 0 {
  210. models.ModifyWxUserRegisterStatus(userId)
  211. }
  212. }
  213. //如果存在该手机号/邮箱,那么需要校验
  214. if userRecord.UserId > 0 && userRecord.UserId != userId {
  215. err = errors.New("用户已绑定,不允许重复绑定")
  216. return
  217. }
  218. err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
  219. if err != nil {
  220. return
  221. }
  222. userRecord.UserId = userId
  223. //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
  224. if userRecord.NickName == "" {
  225. oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
  226. if err == nil && oldUserRecord != nil {
  227. //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
  228. if oldUserRecord.NickName != "" {
  229. _ = models.ModifyUserRecordByDetail(userRecord.OpenId, userRecord.UnionId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
  230. }
  231. }
  232. }
  233. //格式化用户数据
  234. formatWxUserAndUserRecord(wxUser, userRecord)
  235. return
  236. }
  237. //添加第三方用户(微信)记录
  238. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
  239. find, err := models.GetUserRecordByOpenId(openId)
  240. if err != nil && err.Error() != utils.ErrNoRow() {
  241. return
  242. }
  243. if find != nil {
  244. userRecord = find
  245. return
  246. }
  247. userRecord = &models.UserRecord{
  248. OpenId: openId, //用户open_id
  249. UnionId: unionId, //用户union_id
  250. Subscribe: subscribe,
  251. NickName: nickName, //用户昵称,最大长度:32
  252. RealName: realName, //用户实际名称,最大长度:32
  253. Sex: sex, //普通用户性别,1为男性,2为女性
  254. Province: province, //普通用户个人资料填写的省份,最大长度:30
  255. City: city, //普通用户个人资料填写的城市,最大长度:30
  256. Country: country, //国家,如中国为CN,最大长度:30
  257. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  258. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  259. CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  260. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  261. }
  262. //recordId, err := models.AddUserRecord(userRecord)
  263. if err != nil {
  264. return
  265. }
  266. //userRecord.UserRecordId = int(recordId)
  267. return
  268. }
  269. //用户绑定
  270. func BindSession(mobile, countryCode string) (wxUser *models.WxUserItem, err error) {
  271. if mobile == "" {
  272. err = errors.New("手机号或邮箱必填一个")
  273. return
  274. }
  275. //var bindAccount string
  276. //根据手机号获取用户信息
  277. if mobile != "" {
  278. tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
  279. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  280. err = wxUserErr
  281. return
  282. }
  283. wxUser = tmpWxUser
  284. //bindAccount = mobile
  285. }
  286. var userId int
  287. //如果查询出来的用户是nil,那么需要新增用户
  288. if wxUser == nil {
  289. user := &models.WxUser{
  290. CompanyId: 1,
  291. CreatedTime: time.Now(),
  292. FirstLogin: 1,
  293. Enabled: 1,
  294. RegisterPlatform: 7,
  295. RegisterTime: time.Now(),
  296. Mobile: mobile,
  297. //Email: email,
  298. IsRegister: 1,
  299. Source: 3,
  300. CountryCode: countryCode,
  301. OutboundMobile: mobile,
  302. OutboundCountryCode: countryCode,
  303. }
  304. tmpUserId, addUserErr := models.AddWxUser(user)
  305. if addUserErr != nil {
  306. err = addUserErr
  307. return
  308. }
  309. user.UserId = int(tmpUserId)
  310. userId = int(tmpUserId)
  311. wxUser, err = models.GetWxUserItemByUserId(userId)
  312. } else {
  313. userId = wxUser.UserId
  314. err = models.BindUserOutboundMobile(mobile, countryCode, userId)
  315. if err != nil {
  316. return
  317. }
  318. if wxUser.IsRegister == 0 {
  319. models.ModifyWxUserRegisterStatus(userId)
  320. }
  321. }
  322. return
  323. }
  324. // 我的收藏
  325. func GetMicroRoadShowMycollect(pageSize, currentIndex int, audioIds, videoIds, activityVideoIds string) (respList []*models.MicroRoadShowPageList, total int, err error) {
  326. var e error
  327. // 根据每页数据量获取音视频配比
  328. startSize := utils.StartIndex(currentIndex, pageSize)
  329. videoList := make([]*models.MicroRoadShowPageList, 0)
  330. //音频的查询
  331. var audioCond string
  332. var audioPars []interface{}
  333. // 如果筛选条件为指定视频ID或只看视频则不做音频查询
  334. // 活动已发布且已结束
  335. audioCond += ` AND b.publish_status = 1 AND b.active_state = 3`
  336. if audioIds != "" {
  337. sliceId := strings.Split(audioIds, ",")
  338. var idSqlStr string
  339. for _, v := range sliceId {
  340. idSqlStr += "'" + v + "',"
  341. }
  342. idSqlStr = strings.TrimRight(idSqlStr, ",")
  343. audioCond += ` AND a.activity_voice_id IN (` + idSqlStr + `)`
  344. } else {
  345. audioCond += ` AND a.activity_voice_id = 0 `
  346. }
  347. //视频的处理
  348. var videoCond string
  349. var videoCondAct string
  350. if activityVideoIds != "" {
  351. sliceId := strings.Split(activityVideoIds, ",")
  352. var idSqlStr string
  353. for _, v := range sliceId {
  354. idSqlStr += "'" + v + "',"
  355. }
  356. idSqlStr = strings.TrimRight(idSqlStr, ",")
  357. videoCondAct += ` AND v.video_id IN (` + idSqlStr + `)`
  358. } else {
  359. videoCondAct += ` AND v.video_id = 0 `
  360. }
  361. var videoPars []interface{}
  362. var videoParsAct []interface{}
  363. if videoIds != "" {
  364. sliceId := strings.Split(videoIds, ",")
  365. var idSqlStr string
  366. for _, v := range sliceId {
  367. idSqlStr += "'" + v + "',"
  368. }
  369. idSqlStr = strings.TrimRight(idSqlStr, ",")
  370. videoCond += ` AND video_id IN (` + idSqlStr + `)`
  371. } else {
  372. videoCond += ` AND video_id = 0 `
  373. }
  374. videoCond += ` AND publish_status = 1`
  375. total, videoList, e = models.GetMicroRoadShowVideoPageListV8(startSize, pageSize, videoCond, videoPars, videoCondAct, videoParsAct, audioCond, audioPars, 0, 0, 0, 0)
  376. if e != nil {
  377. err = errors.New("获取微路演音视频列表失败, Err: " + e.Error())
  378. return
  379. }
  380. respList = videoList
  381. return
  382. }