user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. package user
  2. import (
  3. "errors"
  4. userReq "hongze/hongze_yb/models/request/user"
  5. admin2 "hongze/hongze_yb/models/tables/admin"
  6. "hongze/hongze_yb/models/tables/company"
  7. "hongze/hongze_yb/models/tables/company_product"
  8. "hongze/hongze_yb/models/tables/rddp/chart_permission"
  9. "hongze/hongze_yb/models/tables/rddp/msg_code"
  10. "hongze/hongze_yb/models/tables/research_variety_tag_relation"
  11. "hongze/hongze_yb/models/tables/sys_role_admin"
  12. "hongze/hongze_yb/models/tables/wx_user"
  13. "hongze/hongze_yb/models/tables/yb_apply_record"
  14. "hongze/hongze_yb/models/tables/yb_index_tab"
  15. "hongze/hongze_yb/models/tables/yb_message"
  16. "hongze/hongze_yb/services"
  17. companyService "hongze/hongze_yb/services/company"
  18. "hongze/hongze_yb/services/user"
  19. "hongze/hongze_yb/utils"
  20. "strings"
  21. "time"
  22. )
  23. // SendSmsCode 发送手机短信
  24. func SendSmsCode(openid, mobile, areaNum string) (err error, errMsg string) {
  25. if mobile == "" {
  26. err = errors.New("请输入手机号")
  27. return
  28. }
  29. msgCode := utils.GetRandDigit(4)
  30. var result bool
  31. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  32. //国内的短信
  33. result = services.SendSmsCode(mobile, msgCode)
  34. } else {
  35. //国际短信
  36. result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
  37. }
  38. //发送成功
  39. if result {
  40. item := &msg_code.MsgCode{
  41. OpenID: openid,
  42. Mobile: mobile,
  43. Code: msgCode,
  44. ExpiredIn: time.Now().Add(15 * time.Minute).Unix(),
  45. Enabled: 1,
  46. CreatedTime: time.Time{},
  47. LastUpdatedTime: time.Time{},
  48. }
  49. err = item.Create()
  50. } else {
  51. err = errors.New("短信发送失败")
  52. }
  53. return
  54. }
  55. // SendEmailCode 发送邮件
  56. func SendEmailCode(openid, email string) (err error, errMsg string) {
  57. if email == "" {
  58. err = errors.New("请输入邮箱地址")
  59. return
  60. }
  61. if !utils.ValidateEmailFormatat(email) {
  62. err = errors.New("邮箱格式错误,请重新输入")
  63. return
  64. }
  65. msgCode := utils.GetRandDigit(4)
  66. content := "尊敬的用户:</br>您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>弘则研究团队 </br>" + time.Now().Format(utils.FormatDateCN)
  67. title := "弘则研究登陆验证"
  68. //发送邮件
  69. result, err := services.SendEmailByHz(title, content, email)
  70. if result {
  71. item := &msg_code.MsgCode{
  72. OpenID: openid,
  73. Mobile: email,
  74. Code: msgCode,
  75. ExpiredIn: time.Now().Add(15 * time.Minute).Unix(),
  76. Enabled: 1,
  77. CreatedTime: time.Time{},
  78. LastUpdatedTime: time.Time{},
  79. }
  80. err = item.Create()
  81. } else {
  82. err = errors.New("发送失败,Err:" + err.Error())
  83. }
  84. return
  85. }
  86. type CompanyPermission struct {
  87. Name string `json:"name" description:"分类名称"`
  88. PermissionList []PermissionList `json:"permission_list" description:"权限列表"`
  89. }
  90. type PermissionList struct {
  91. Name string `json:"name" description:"权限名称"`
  92. StartDate time.Time `json:"start_date" description:"开始日期"`
  93. EndDate time.Time `json:"end_date" description:"结束日期"`
  94. Status string `json:"status" description:"状态"`
  95. }
  96. type Detail struct {
  97. CompanyName string `json:"company_name" description:"客户名称(公司名称)"`
  98. Status string `json:"status" description:"客户状态"`
  99. IsSuspend int8 `json:"is_suspend" description:"启用与否字段:1:暂停,0:启用"`
  100. RealName string `json:"real_name" description:"用户名称"`
  101. Mobile string `json:"mobile" description:"手机号"`
  102. Email string `json:"email" description:"邮箱"`
  103. SellerName string `json:"seal_name" description:"所属销售名称"`
  104. SellerMobile string `json:"seal_mobile" description:"所属销售手机号"`
  105. PermissionList []CompanyPermission `json:"permission_list" description:"权限列表"`
  106. IsInner int `json:"is_inner" description:"是否为内部员工"`
  107. IsResearcher int `json:"is_researcher" description:"是否为研究员"`
  108. IsVoiceAdmin int `json:"is_voice_admin" description:"是否为语音管理员"`
  109. AdminInfo *admin2.Admin `json:"admin_info" description:"系统管理员信息"`
  110. UnRead int64 `json:"un_read" description:"消息未读数"`
  111. NickName string `json:"nick_name" description:"用户昵称"`
  112. HeadImgUrl string `json:"head_img_url" description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  113. UserId uint64 `json:"user_id" description:"用户ID"`
  114. IsBind int `json:"is_bind" description:"是否绑定了微信"`
  115. }
  116. // GetUserInfo 获取用户我的页面详情数据
  117. func GetUserInfo(userInfo user.UserInfo) (userDetail Detail, err error, errMsg string) {
  118. list := make([]CompanyPermission, 0)
  119. companyName := userInfo.Note
  120. status := ``
  121. var isSuspend int8
  122. sellerName := ``
  123. sellerMobile := ``
  124. if userInfo.CompanyID > 1 {
  125. companyInfo, tmpErr := company.GetByCompanyId(userInfo.CompanyID)
  126. if tmpErr != nil {
  127. err = tmpErr
  128. if tmpErr == utils.ErrNoRow {
  129. err = errors.New("找不到该客户")
  130. errMsg = "找不到该客户"
  131. return
  132. }
  133. errMsg = "系统异常4001"
  134. return
  135. }
  136. companyName = companyInfo.CompanyName
  137. companyProduct, tmpErr := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  138. //如果有ficc权限,那么就去查询相关权限
  139. if tmpErr == nil {
  140. status = companyProduct.Status //产品状态
  141. isSuspend = companyProduct.IsSuspend
  142. //销售信息
  143. adminInfo, tmpErr := admin2.GetByAdminId(companyProduct.SellerID)
  144. if tmpErr == nil {
  145. sellerName = adminInfo.RealName
  146. sellerMobile = adminInfo.Mobile
  147. }
  148. //获取所有的权限分类列表
  149. allPermissionList, tmpErr := companyService.GetPermissionListByProductId(1)
  150. if tmpErr != nil {
  151. err = tmpErr
  152. return
  153. }
  154. permissionClassMap := make(map[int]*chart_permission.ChartPermission)
  155. for _, chartPermission := range allPermissionList {
  156. permissionClassMap[int(chartPermission.ChartPermissionID)] = chartPermission
  157. }
  158. // 获取权限列表
  159. permissionList, tmpErr := companyService.GetValidPermissionByCompany2ProductId(userInfo.CompanyID, 1)
  160. if tmpErr != nil {
  161. err = tmpErr
  162. return
  163. }
  164. permissionMap := make(map[string][]PermissionList)
  165. for _, permission := range permissionList {
  166. chartPermission, ok := permissionClassMap[permission.ChartPermissionID]
  167. if ok {
  168. tmpPermission := PermissionList{
  169. Name: chartPermission.PermissionName,
  170. StartDate: permission.StartDate,
  171. EndDate: permission.EndDate,
  172. Status: permission.Status,
  173. }
  174. if tmpPermissionList, ok2 := permissionMap[chartPermission.ClassifyName]; ok2 {
  175. permissionMap[chartPermission.ClassifyName] = append(tmpPermissionList, tmpPermission)
  176. } else {
  177. tmpPermissionList := make([]PermissionList, 0)
  178. permissionMap[chartPermission.ClassifyName] = append(tmpPermissionList, tmpPermission)
  179. }
  180. }
  181. }
  182. for name, tmpPermissionList := range permissionMap {
  183. list = append(list, CompanyPermission{
  184. Name: name,
  185. PermissionList: tmpPermissionList,
  186. })
  187. }
  188. }
  189. }
  190. // 查询消息未读数
  191. unRead, err := yb_message.GetUnreadByUserId(userInfo.UserID)
  192. if err != nil {
  193. errMsg = "查询消息未读数失败"
  194. return
  195. }
  196. // 用户头像
  197. headimgurl := userInfo.Headimgurl
  198. if headimgurl == "" {
  199. // 未绑定-灰色头像
  200. headimgurl = utils.DEFAULT_HONGZE_USER_LOGO_GRAY
  201. if userInfo.UserID > 0 {
  202. headimgurl = utils.DEFAULT_HONGZE_USER_LOGO
  203. }
  204. }
  205. // 查询是否为内部员工、研究员、语音管理员
  206. isInner, isResearcher, isVoiceAdmin, adminInfo, e := GetUserIdentity(int(userInfo.CompanyID), userInfo.Mobile, userInfo.Email)
  207. if e != nil {
  208. return
  209. }
  210. isBind := 0
  211. if userInfo.UserID > 0 {
  212. isBind = 1
  213. }
  214. userDetail = Detail{
  215. CompanyName: companyName, //客户名称(公司名称)
  216. Status: status, //产品状态
  217. IsSuspend: isSuspend, //是否启用
  218. RealName: userInfo.RealName,
  219. Mobile: userInfo.Mobile,
  220. Email: userInfo.Email,
  221. SellerName: sellerName,
  222. SellerMobile: sellerMobile,
  223. PermissionList: list, //权限列表
  224. IsInner: isInner, // 是否为内部员工
  225. IsResearcher: isResearcher, // 是否为研究员
  226. IsVoiceAdmin: isVoiceAdmin, // 语音管理员
  227. AdminInfo: adminInfo, // 系统管理员信息
  228. UnRead: unRead,
  229. NickName: userInfo.NickName,
  230. HeadImgUrl: headimgurl,
  231. UserId: userInfo.UserID,
  232. IsBind: isBind,
  233. }
  234. return
  235. }
  236. // SetUserInfo 设置用户的个人信息,头像和昵称
  237. func SetUserInfo(userInfo user.UserInfo, req userReq.SetUserInfoReq) (err error) {
  238. // todo 用户如果没有设置头像,用默认头像也允许上传
  239. // 校验昵称长度
  240. // 校验头像地址是否正确
  241. if !strings.Contains(req.HeadImgUrl, "http://") && !strings.Contains(req.HeadImgUrl, "https://") {
  242. err = errors.New("上传头像出错")
  243. return
  244. }
  245. wxUser := userInfo.WxUser
  246. wxUser.NickName = req.NickName
  247. wxUser.Headimgurl = req.HeadImgUrl
  248. err = wxUser.Update([]string{"nick_name", "headimgurl"})
  249. if err != nil {
  250. err = errors.New("更新用户信息操作失败")
  251. return
  252. }
  253. return
  254. }
  255. type Record struct {
  256. BusinessCardURL string `json:"business_card_url" description:"名片"`
  257. RealName string `json:"real_name" description:"姓名"`
  258. CompanyName string `json:"company_name" description:"客户备注的公司名称"`
  259. Mobile string `json:"mobile" description:"手机号"`
  260. }
  261. // GetLastApplyInfo 获取最近的一套申请记录
  262. func GetLastApplyInfo(userId int) (record Record, err error) {
  263. recordInfo, err := yb_apply_record.GetLastNotOpRecordByUserId(userId)
  264. if err != nil {
  265. if err == utils.ErrNoRow {
  266. err = nil
  267. }
  268. return
  269. }
  270. record = Record{
  271. BusinessCardURL: recordInfo.BusinessCardURL,
  272. RealName: recordInfo.RealName,
  273. CompanyName: recordInfo.CompanyName,
  274. Mobile: recordInfo.Mobile,
  275. }
  276. return
  277. }
  278. // Apply 客户申请
  279. func Apply(userId int, companyId int64, mobile, email string, applyInfo userReq.ApplyReq) (ybApplyRecord *yb_apply_record.YbApplyRecord, err error) {
  280. sellerName := ``
  281. status := `潜在用户`
  282. companyName := applyInfo.CompanyName
  283. tmpmYbApplyRecord, err := yb_apply_record.GetLastNotOpRecordByUserId(userId)
  284. if err == nil {
  285. ybApplyRecord = tmpmYbApplyRecord // 申请来源每一种只存在一条数据
  286. return
  287. } else {
  288. if err != utils.ErrNoRow {
  289. return
  290. }
  291. }
  292. // 联系人信息
  293. wxUserInfo, err := wx_user.GetByUserId(userId)
  294. if err != nil {
  295. return
  296. }
  297. if companyId > 1 {
  298. //客户信息
  299. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  300. if tmpErr != nil {
  301. err = tmpErr
  302. return
  303. }
  304. companyName = companyInfo.CompanyName
  305. //客户产品权限
  306. companyProductList, tmpErr := company_product.GetListByCompanyId(companyId)
  307. if tmpErr != nil {
  308. err = tmpErr
  309. return
  310. }
  311. sellerNameList := make([]string, 0)
  312. statusList := make([]string, 0)
  313. for _, companyProductInfo := range companyProductList {
  314. if companyProductInfo.ProductID == 2 && status == "潜在用户" {
  315. status = "权益用户"
  316. }
  317. if companyProductInfo.ProductID == 1 {
  318. sellerName = companyProductInfo.SellerName
  319. status = companyProductInfo.Status
  320. if companyProductInfo.IsSuspend == 1 {
  321. status = `试用暂停`
  322. }
  323. }
  324. }
  325. if len(sellerNameList) > 0 && len(statusList) > 0 {
  326. sellerName = strings.Join(sellerNameList, ",")
  327. status = strings.Join(statusList, ",")
  328. }
  329. }
  330. ybApplyRecord = &yb_apply_record.YbApplyRecord{
  331. UserID: userId,
  332. BusinessCardURL: applyInfo.BusinessCardUrl,
  333. RealName: applyInfo.RealName,
  334. CompanyName: applyInfo.CompanyName,
  335. Mobile: mobile,
  336. Email: email,
  337. Permission: applyInfo.Permission,
  338. SellerName: sellerName,
  339. Status: status,
  340. CompanyIDPay: int(companyId),
  341. CompanyNamePay: companyName,
  342. CreateTime: time.Now(),
  343. Source: applyInfo.Source,
  344. SourceAgent: applyInfo.SourceAgent,
  345. FromPage: applyInfo.FromPage,
  346. }
  347. err = ybApplyRecord.Create()
  348. if err != nil {
  349. return
  350. }
  351. // 修改客户信息
  352. wxUserInfo.RealName = applyInfo.RealName
  353. wxUserInfo.Note = applyInfo.CompanyName
  354. wxUserInfo.IsNote = 1
  355. applyMethod := wxUserInfo.ApplyMethod //0:未申请,1:已付费客户申请试用,2:非客户申请试用
  356. applyMethod = 2
  357. if sellerName != "" {
  358. applyMethod = 1
  359. }
  360. wxUserInfo.ApplyMethod = applyMethod
  361. _ = wxUserInfo.Update([]string{"RealName", "Note", "IsNote", "ApplyMethod"})
  362. return
  363. }
  364. // GetUserTabBar 获取用户小程序TabBar
  365. func GetUserTabBar(userInfo user.UserInfo, version string) (list []string, err error) {
  366. where := make(map[string]interface{})
  367. where["version"] = version
  368. where["position"] = 2
  369. where["is_show"] = 1
  370. tabs, e := yb_index_tab.GetListByCondition(where)
  371. if e != nil {
  372. err = errors.New("获取TabBar失败, Err: " + e.Error())
  373. return
  374. }
  375. if len(tabs) == 0 {
  376. return
  377. }
  378. authOk := false
  379. companyProduct, e := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  380. if e != nil && e != utils.ErrNoRow {
  381. err = errors.New("获取客户信息失败, Err: " + e.Error())
  382. return
  383. }
  384. if companyProduct != nil && companyProduct.Status != "" {
  385. if strings.Contains("永续,正式", companyProduct.Status) {
  386. authOk = true
  387. }
  388. }
  389. for _, v := range tabs {
  390. if v.AuthCheck == 1 && !authOk {
  391. continue
  392. }
  393. list = append(list, v.Mark)
  394. }
  395. return
  396. }
  397. // PcSendSmsCode 发送手机短信
  398. func PcSendSmsCode(mobile, areaNum string) (err error, errMsg string) {
  399. if mobile == "" {
  400. err = errors.New("请输入手机号")
  401. return
  402. }
  403. msgCode := utils.GetRandDigit(4)
  404. var result bool
  405. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  406. //国内的短信
  407. result = services.SendSmsCode(mobile, msgCode)
  408. } else {
  409. //国际短信
  410. result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
  411. }
  412. //发送成功
  413. if result {
  414. item := &msg_code.MsgCode{
  415. Mobile: mobile,
  416. Code: msgCode,
  417. ExpiredIn: time.Now().Add(15 * time.Minute).Unix(),
  418. Enabled: 1,
  419. CreatedTime: time.Time{},
  420. LastUpdatedTime: time.Time{},
  421. }
  422. err = item.Create()
  423. } else {
  424. err = errors.New("短信发送失败")
  425. }
  426. return
  427. }
  428. // PcSendEmailCode 发送邮件
  429. func PcSendEmailCode(email string) (err error, errMsg string) {
  430. if email == "" {
  431. err = errors.New("请输入邮箱地址")
  432. return
  433. }
  434. if !utils.ValidateEmailFormatat(email) {
  435. err = errors.New("邮箱格式错误,请重新输入")
  436. return
  437. }
  438. msgCode := utils.GetRandDigit(4)
  439. content := "尊敬的用户:</br>您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>弘则研究团队 </br>" + time.Now().Format(utils.FormatDateCN)
  440. title := "弘则研究登陆验证"
  441. //发送邮件
  442. result, err := services.SendEmailByHz(title, content, email)
  443. if result {
  444. item := &msg_code.MsgCode{
  445. Mobile: email,
  446. Code: msgCode,
  447. ExpiredIn: time.Now().Add(15 * time.Minute).Unix(),
  448. Enabled: 1,
  449. CreatedTime: time.Time{},
  450. LastUpdatedTime: time.Time{},
  451. }
  452. err = item.Create()
  453. } else {
  454. err = errors.New("发送失败")
  455. }
  456. return
  457. }
  458. // TopTab
  459. type TopTab struct {
  460. Tab string `json:"tab" description:"选项卡名称"`
  461. Mark string `json:"mark" description:"选项卡标识"`
  462. Icon string `json:"icon" description:"icon图标"`
  463. Sort int `json:"sort" description:"排序"`
  464. }
  465. // GetTopTab 获取小程序首页顶部选项卡
  466. func GetTopTab(userInfo user.UserInfo, version string) (list []*TopTab, err error) {
  467. where := make(map[string]interface{})
  468. where["version"] = version
  469. where["position"] = 1
  470. where["is_show"] = 1
  471. tabs, e := yb_index_tab.GetListByCondition(where)
  472. if e != nil {
  473. err = errors.New("获取TabBar失败, Err: " + e.Error())
  474. return
  475. }
  476. if len(tabs) == 0 {
  477. return
  478. }
  479. authOk := false
  480. companyProduct, e := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  481. if e != nil && e != utils.ErrNoRow {
  482. err = errors.New("获取客户信息失败, Err: " + e.Error())
  483. return
  484. }
  485. if companyProduct != nil && companyProduct.Status != "" {
  486. if strings.Contains("永续,正式", companyProduct.Status) {
  487. authOk = true
  488. }
  489. }
  490. list = make([]*TopTab, 0)
  491. for _, v := range tabs {
  492. if v.AuthCheck == 1 && !authOk {
  493. continue
  494. }
  495. list = append(list, &TopTab{
  496. Tab: v.Tab,
  497. Mark: v.Mark,
  498. Icon: v.DefaultIcon,
  499. Sort: v.Sort,
  500. })
  501. }
  502. return
  503. }
  504. // GetUserIdentity 获取用户身份(内部员工、研究员、语音管理员)
  505. func GetUserIdentity(companyId int, mobile, email string) (isInner, isResearcher, isVoiceAdmin int, adminInfo *admin2.Admin, err error) {
  506. if companyId != utils.HzCompanyId {
  507. return
  508. }
  509. if mobile == "" && email == "" {
  510. return
  511. }
  512. var e error
  513. // 通过手机号/邮箱获取管理员信息
  514. if mobile != "" {
  515. adminInfo, e = admin2.GetAdminByMobile(mobile)
  516. if e != nil && e != utils.ErrNoRow {
  517. return
  518. }
  519. } else {
  520. adminInfo, e = admin2.GetAdminByEmail(email)
  521. if e != nil && e != utils.ErrNoRow {
  522. return
  523. }
  524. }
  525. // 仅内部员工才判断研究员、语音管理员身份
  526. if adminInfo != nil && adminInfo.AdminID > 0 && adminInfo.Enabled == 1 {
  527. isInner = 1
  528. // 研究员
  529. researchGroupList, e := research_variety_tag_relation.GetResearchVarietyTagRelationByAdminId(int(adminInfo.AdminID))
  530. if e != nil {
  531. err = errors.New("用户研究员信息有误")
  532. return
  533. }
  534. if len(researchGroupList) > 0 {
  535. isResearcher = 1
  536. }
  537. // 语音管理员
  538. voiceAdmin, e := sys_role_admin.GetVoiceAdmin(int(adminInfo.AdminID))
  539. if e != nil && e != utils.ErrNoRow {
  540. err = errors.New("用户语音管理员信息有误")
  541. return
  542. }
  543. if voiceAdmin != nil && voiceAdmin.Id > 0 {
  544. isVoiceAdmin = 1
  545. }
  546. }
  547. return
  548. }