user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/chart_permission"
  7. "hongze/hongze_yb/models/tables/company"
  8. "hongze/hongze_yb/models/tables/company_product"
  9. "hongze/hongze_yb/models/tables/rddp/msg_code"
  10. "hongze/hongze_yb/models/tables/wx_user"
  11. "hongze/hongze_yb/models/tables/yb_apply_record"
  12. "hongze/hongze_yb/models/tables/yb_message"
  13. "hongze/hongze_yb/services"
  14. companyService "hongze/hongze_yb/services/company"
  15. "hongze/hongze_yb/services/user"
  16. "hongze/hongze_yb/utils"
  17. "strings"
  18. "time"
  19. )
  20. // SendSmsCode 发送手机短信
  21. func SendSmsCode(openid, mobile, areaNum string) (err error, errMsg string) {
  22. if mobile == "" {
  23. err = errors.New("请输入手机号")
  24. return
  25. }
  26. msgCode := utils.GetRandDigit(4)
  27. var result bool
  28. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  29. //国内的短信
  30. result = services.SendSmsCode(mobile, msgCode)
  31. } else {
  32. //国际短信
  33. result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
  34. }
  35. //发送成功
  36. if result {
  37. item := &msg_code.MsgCode{
  38. OpenID: openid,
  39. Mobile: mobile,
  40. Code: msgCode,
  41. ExpiredIn: time.Now().Add(15 * time.Minute).Unix(),
  42. Enabled: 1,
  43. CreatedTime: time.Time{},
  44. LastUpdatedTime: time.Time{},
  45. }
  46. err = item.Create()
  47. } else {
  48. err = errors.New("短信发送失败")
  49. }
  50. return
  51. }
  52. // SendEmailCode 发送邮件
  53. func SendEmailCode(openid, email string) (err error, errMsg string) {
  54. if email == "" {
  55. err = errors.New("请输入邮箱地址")
  56. return
  57. }
  58. if !utils.ValidateEmailFormatat(email) {
  59. err = errors.New("邮箱格式错误,请重新输入")
  60. return
  61. }
  62. msgCode := utils.GetRandDigit(4)
  63. content := "尊敬的用户:</br>您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>弘则研究团队 </br>" + time.Now().Format(utils.FormatDateCN)
  64. title := "弘则研究登陆验证"
  65. //发送邮件
  66. result, err := services.SendEmailByHz(title, content, email)
  67. if result {
  68. item := &msg_code.MsgCode{
  69. OpenID: openid,
  70. Mobile: email,
  71. Code: msgCode,
  72. ExpiredIn: time.Now().Add(15 * time.Minute).Unix(),
  73. Enabled: 1,
  74. CreatedTime: time.Time{},
  75. LastUpdatedTime: time.Time{},
  76. }
  77. err = item.Create()
  78. } else {
  79. err = errors.New("发送失败,Err:" + err.Error())
  80. }
  81. return
  82. }
  83. type CompanyPermission struct {
  84. Name string `json:"name" description:"分类名称"`
  85. PermissionList []PermissionList `json:"permission_list" description:"权限列表"`
  86. }
  87. type PermissionList struct {
  88. Name string `json:"name" description:"权限名称"`
  89. StartDate time.Time `json:"start_date" description:"开始日期"`
  90. EndDate time.Time `json:"end_date" description:"结束日期"`
  91. Status string `json:"status" description:"状态"`
  92. }
  93. type Detail struct {
  94. CompanyName string `json:"company_name" description:"客户名称(公司名称)"`
  95. Status string `json:"status" description:"客户状态"`
  96. IsSuspend int8 `json:"is_suspend" description:"启用与否字段:1:暂停,0:启用"`
  97. RealName string `json:"real_name" description:"用户名称"`
  98. Mobile string `json:"mobile" description:"手机号"`
  99. Email string `json:"email" description:"邮箱"`
  100. SellerName string `json:"seal_name" description:"所属销售名称"`
  101. SellerMobile string `json:"seal_mobile" description:"所属销售手机号"`
  102. PermissionList []CompanyPermission `json:"permission_list" description:"权限列表"`
  103. IsInner int `json:"is_inner" description:"是否为内部员工"`
  104. AdminInfo *admin2.Admin `json:"admin_info" description:"系统管理员信息"`
  105. UnRead int64 `json:"un_read" description:"消息未读数"`
  106. NickName string `json:"nick_name" description:"用户昵称"`
  107. HeadImgUrl string `json:"head_img_url" description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  108. UserId uint64 `json:"user_id" description:"用户ID"`
  109. }
  110. // GetUserInfo 获取用户我的页面详情数据
  111. func GetUserInfo(userInfo user.UserInfo) (userDetail Detail, err error, errMsg string) {
  112. list := make([]CompanyPermission, 0)
  113. companyName := userInfo.Note
  114. status := ``
  115. var isSuspend int8
  116. sellerName := ``
  117. sellerMobile := ``
  118. if userInfo.CompanyID > 1 {
  119. companyInfo, tmpErr := company.GetByCompanyId(userInfo.CompanyID)
  120. if tmpErr != nil {
  121. err = tmpErr
  122. if tmpErr == utils.ErrNoRow {
  123. err = errors.New("找不到该客户")
  124. errMsg = "找不到该客户"
  125. return
  126. }
  127. errMsg = "系统异常4001"
  128. return
  129. }
  130. companyName = companyInfo.CompanyName
  131. companyProduct, tmpErr := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  132. //如果有ficc权限,那么就去查询相关权限
  133. if tmpErr == nil {
  134. status = companyProduct.Status //产品状态
  135. isSuspend = companyProduct.IsSuspend
  136. //销售信息
  137. adminInfo, tmpErr := admin2.GetByAdminId(companyProduct.SellerID)
  138. if tmpErr == nil {
  139. sellerName = adminInfo.RealName
  140. sellerMobile = adminInfo.Mobile
  141. }
  142. //获取所有的权限分类列表
  143. allPermissionList, tmpErr := companyService.GetPermissionListByProductId(1)
  144. if tmpErr != nil {
  145. err = tmpErr
  146. return
  147. }
  148. permissionClassMap := make(map[int]*chart_permission.ChartPermission)
  149. for _, chartPermission := range allPermissionList {
  150. permissionClassMap[int(chartPermission.ChartPermissionID)] = chartPermission
  151. }
  152. // 获取权限列表
  153. permissionList, tmpErr := companyService.GetValidPermissionByCompany2ProductId(userInfo.CompanyID, 1)
  154. if tmpErr != nil {
  155. err = tmpErr
  156. return
  157. }
  158. permissionMap := make(map[string][]PermissionList)
  159. for _, permission := range permissionList {
  160. chartPermission, ok := permissionClassMap[permission.ChartPermissionID]
  161. if ok {
  162. tmpPermission := PermissionList{
  163. Name: chartPermission.PermissionName,
  164. StartDate: permission.StartDate,
  165. EndDate: permission.EndDate,
  166. Status: permission.Status,
  167. }
  168. if tmpPermissionList, ok2 := permissionMap[chartPermission.ClassifyName]; ok2 {
  169. permissionMap[chartPermission.ClassifyName] = append(tmpPermissionList, tmpPermission)
  170. } else {
  171. tmpPermissionList := make([]PermissionList, 0)
  172. permissionMap[chartPermission.ClassifyName] = append(tmpPermissionList, tmpPermission)
  173. }
  174. }
  175. }
  176. for name, tmpPermissionList := range permissionMap {
  177. list = append(list, CompanyPermission{
  178. Name: name,
  179. PermissionList: tmpPermissionList,
  180. })
  181. }
  182. }
  183. }
  184. // 是否为内部员工
  185. isInner := 0
  186. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  187. if err != nil {
  188. err = errors.New("获取用户系统管理员信息失败")
  189. errMsg = "系统异常4002"
  190. return
  191. }
  192. if ok {
  193. isInner = 1
  194. }
  195. // 查询消息未读数
  196. unRead, err := yb_message.GetUnreadByUserId(userInfo.UserID)
  197. if err != nil {
  198. errMsg = "查询消息未读数失败"
  199. return
  200. }
  201. headimgurl := userInfo.Headimgurl
  202. if headimgurl == "" {
  203. headimgurl = utils.DEFAULT_HONGZE_USER_LOGO
  204. }
  205. userDetail = Detail{
  206. CompanyName: companyName, //客户名称(公司名称)
  207. Status: status, //产品状态
  208. IsSuspend: isSuspend, //是否启用
  209. RealName: userInfo.RealName,
  210. Mobile: userInfo.Mobile,
  211. Email: userInfo.Email,
  212. SellerName: sellerName,
  213. SellerMobile: sellerMobile,
  214. PermissionList: list, //权限列表
  215. IsInner: isInner, // 是否为内部员工
  216. AdminInfo: adminInfo, // 系统管理员信息
  217. UnRead: unRead,
  218. NickName: userInfo.NickName,
  219. HeadImgUrl: headimgurl,
  220. UserId: userInfo.UserID,
  221. }
  222. return
  223. }
  224. // SetUserInfo 设置用户的个人信息,头像和昵称
  225. func SetUserInfo(userInfo user.UserInfo, req userReq.SetUserInfoReq) (err error) {
  226. // todo 用户如果没有设置头像,用默认头像也允许上传
  227. // 校验昵称长度
  228. // 校验头像地址是否正确
  229. if !strings.Contains(req.HeadImgUrl, "http://") && !strings.Contains(req.HeadImgUrl, "https://") {
  230. err = errors.New("上传头像出错")
  231. return
  232. }
  233. wxUser := userInfo.WxUser
  234. wxUser.NickName = req.NickName
  235. wxUser.Headimgurl = req.HeadImgUrl
  236. err = wxUser.Update([]string{"nick_name","headimgurl"})
  237. if err != nil {
  238. err = errors.New("更新用户信息操作失败")
  239. return
  240. }
  241. return
  242. }
  243. type Record struct {
  244. BusinessCardURL string `json:"business_card_url" description:"名片"`
  245. RealName string `json:"real_name" description:"姓名"`
  246. CompanyName string `json:"company_name" description:"客户备注的公司名称"`
  247. Mobile string `json:"mobile" description:"手机号"`
  248. }
  249. // GetLastApplyInfo 获取最近的一套申请记录
  250. func GetLastApplyInfo(userId int) (record Record, err error) {
  251. recordInfo, err := yb_apply_record.GetLastNotOpRecordByUserId(userId)
  252. if err != nil {
  253. if err == utils.ErrNoRow {
  254. err = nil
  255. }
  256. return
  257. }
  258. record = Record{
  259. BusinessCardURL: recordInfo.BusinessCardURL,
  260. RealName: recordInfo.RealName,
  261. CompanyName: recordInfo.CompanyName,
  262. Mobile: recordInfo.Mobile,
  263. }
  264. return
  265. }
  266. // Apply 客户申请
  267. func Apply(userId int, companyId int64, mobile, email string, applyInfo userReq.ApplyReq) (ybApplyRecord *yb_apply_record.YbApplyRecord, err error) {
  268. sellerName := ``
  269. status := `潜在用户`
  270. companyName := applyInfo.CompanyName
  271. tmpmYbApplyRecord, err := yb_apply_record.GetLastNotOpRecordByUserId(userId)
  272. if err == nil {
  273. ybApplyRecord = tmpmYbApplyRecord // 申请来源每一种只存在一条数据
  274. return
  275. } else {
  276. if err != utils.ErrNoRow {
  277. return
  278. }
  279. }
  280. // 联系人信息
  281. wxUserInfo, err := wx_user.GetByUserId(userId)
  282. if err != nil {
  283. return
  284. }
  285. if companyId > 1 {
  286. //客户信息
  287. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  288. if tmpErr != nil {
  289. err = tmpErr
  290. return
  291. }
  292. companyName = companyInfo.CompanyName
  293. //客户产品权限
  294. companyProductList, tmpErr := company_product.GetListByCompanyId(companyId)
  295. if tmpErr != nil {
  296. err = tmpErr
  297. return
  298. }
  299. sellerNameList := make([]string, 0)
  300. statusList := make([]string, 0)
  301. for _, companyProductInfo := range companyProductList {
  302. if companyProductInfo.ProductID == 2 && status == "潜在用户" {
  303. status = "权益用户"
  304. }
  305. if companyProductInfo.ProductID == 1 {
  306. sellerName = companyProductInfo.SellerName
  307. status = companyProductInfo.Status
  308. if companyProductInfo.IsSuspend == 1 {
  309. status = `试用暂停`
  310. }
  311. }
  312. }
  313. if len(sellerNameList) > 0 && len(statusList) > 0 {
  314. sellerName = strings.Join(sellerNameList, ",")
  315. status = strings.Join(statusList, ",")
  316. }
  317. }
  318. ybApplyRecord = &yb_apply_record.YbApplyRecord{
  319. UserID: userId,
  320. BusinessCardURL: applyInfo.BusinessCardUrl,
  321. RealName: applyInfo.RealName,
  322. CompanyName: applyInfo.CompanyName,
  323. Mobile: mobile,
  324. Email: email,
  325. Permission: applyInfo.Permission,
  326. SellerName: sellerName,
  327. Status: status,
  328. CompanyIDPay: int(companyId),
  329. CompanyNamePay: companyName,
  330. CreateTime: time.Now(),
  331. Source: applyInfo.Source,
  332. SourceAgent: applyInfo.SourceAgent,
  333. FromPage: applyInfo.FromPage,
  334. }
  335. err = ybApplyRecord.Create()
  336. if err != nil {
  337. return
  338. }
  339. // 修改客户信息
  340. wxUserInfo.RealName = applyInfo.RealName
  341. wxUserInfo.Note = applyInfo.CompanyName
  342. wxUserInfo.IsNote = 1
  343. applyMethod := wxUserInfo.ApplyMethod //0:未申请,1:已付费客户申请试用,2:非客户申请试用
  344. applyMethod = 2
  345. if sellerName != "" {
  346. applyMethod = 1
  347. }
  348. wxUserInfo.ApplyMethod = applyMethod
  349. _ = wxUserInfo.Update([]string{"RealName", "Note", "IsNote", "ApplyMethod"})
  350. return
  351. }
  352. // GetUserTabBar 获取用户小程序TabBar
  353. func GetUserTabBar(userInfo user.UserInfo, version string) (list []string, err error) {
  354. // user-我的; activity-活动; chart-图表; report-研报; buy-已购
  355. list = []string{"activity", "user", "chart"}
  356. if version == "yb3.0" {
  357. list = []string{"activity", "user", "chart", "report"}
  358. companyProduct, tmpErr := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  359. if tmpErr != nil {
  360. if tmpErr == utils.ErrNoRow {
  361. err = nil
  362. }
  363. return
  364. }
  365. // 已购仅付费用户可见
  366. if strings.Contains("永续,正式", companyProduct.Status) {
  367. list = append(list, "buy")
  368. }
  369. }
  370. return
  371. }