front.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. package front
  2. import (
  3. . "JySEPlatform/util"
  4. "log"
  5. "regexp"
  6. "strconv"
  7. "time"
  8. qu "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/encrypt"
  10. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  11. "app.yhyue.com/moapp/jybase/mongodb"
  12. "app.yhyue.com/moapp/jybase/sms"
  13. "github.com/dchest/captcha"
  14. "github.com/gorilla/sessions"
  15. "go.mongodb.org/mongo-driver/bson"
  16. "go.mongodb.org/mongo-driver/bson/primitive"
  17. )
  18. var UserMenu map[string][]map[string]interface{} //存储菜单
  19. func init() {
  20. UserMenu = make(map[string][]map[string]interface{})
  21. }
  22. type Front struct {
  23. *xweb.Action
  24. login xweb.Mapper `xweb:"/"` //登录页面
  25. code xweb.Mapper `xweb:"/code"` //获取图片验证码
  26. sendVerifyCode xweb.Mapper `xweb:"/sendVerifyCode"` //获取手机验证码
  27. chooseEnt xweb.Mapper `xweb:"/chooseEnt"` //选择企业
  28. index xweb.Mapper `xweb:"/front/index"` //index页面
  29. logout xweb.Mapper `xweb:"/front/logout"` //注销
  30. updatePwd xweb.Mapper `xweb:"/front/updatepwd"` //更新密码
  31. //user
  32. user xweb.Mapper `xweb:"/front/user"` //查所有用户
  33. userDel xweb.Mapper `xweb:"/front/user/del"` //删除用户
  34. userSave xweb.Mapper `xweb:"/front/user/save"` //保存用户
  35. //menu
  36. menu xweb.Mapper `xweb:"/front/menu"` //查一级菜单
  37. menuSave xweb.Mapper `xweb:"/front/menu/save"` //保存一级菜单
  38. menuDel xweb.Mapper `xweb:"/front/menu/del"` //删除一级菜单
  39. menuSecond xweb.Mapper `xweb:"/front/menuSecond"` //查二级菜单
  40. menuSecondSave xweb.Mapper `xweb:"/front/menuSecond/save"` //保存二级菜单
  41. menuSecondDel xweb.Mapper `xweb:"/front/menuSecond/del"` //删除二级菜单
  42. personalMenu xweb.Mapper `xweb:"/front/personalMenu"`
  43. //role
  44. roleManager xweb.Mapper `xweb:"/front/role"` //角色权限管理
  45. roleNew xweb.Mapper `xweb:"/front/role/save"` //新增角色
  46. roleEdit xweb.Mapper `xweb:"/front/role/edit/(.*)"` //角色权限查看
  47. roleFirst xweb.Mapper `xweb:"/front/role/first"` //一级权限的查看
  48. roleSecond xweb.Mapper `xweb:"/front/role/second"` //二级权限的查看
  49. roleSave xweb.Mapper `xweb:"/front/role/edit/save"` //权限编辑保存
  50. roleDel xweb.Mapper `xweb:"/front/role/edit/del"` //权限编辑删除
  51. roleSecondEdit xweb.Mapper `xweb:"/front/role/second/edit"` //二级权限编辑
  52. }
  53. var store = sessions.NewCookieStore([]byte("jianyu_secret20210422"))
  54. func (f *Front) Login() {
  55. defer qu.Catch()
  56. if f.Method() == "POST" {
  57. // 1. 验证参数有效性
  58. phone := f.GetString("phone")
  59. phoneCode := f.GetString("phoneCode")
  60. reg := regexp.MustCompile("^1([3456789])\\d{9}$")
  61. log.Println(phone)
  62. log.Println(phoneCode)
  63. if !reg.MatchString(phone) {
  64. f.ServeJson(map[string]interface{}{
  65. "code": 0,
  66. "status": false,
  67. "message": "手机号格式有误",
  68. })
  69. return
  70. }
  71. if phoneCode == "" {
  72. f.ServeJson(map[string]interface{}{
  73. "code": 0,
  74. "status": false,
  75. "message": "短信验证码不能为空",
  76. })
  77. return
  78. }
  79. // 2. 验证短信验证码
  80. session, err := store.Get(f.Request, "dataTagLoginPhoneCode")
  81. if err != nil {
  82. f.ServeJson(map[string]interface{}{
  83. "code": 0,
  84. "status": false,
  85. "message": "验证码比对失败",
  86. })
  87. log.Println("phone-session2获取失败")
  88. return
  89. }
  90. realPhoneCode := qu.ObjToString(session.Values["code"])
  91. sessionPhone := qu.ObjToString(session.Values["phone"])
  92. if sessionPhone == "" {
  93. log.Printf("短信验证码过期-%s \n", phone)
  94. f.ServeJson(map[string]interface{}{
  95. "code": 0,
  96. "status": false,
  97. "message": "短信验证码过期",
  98. })
  99. return
  100. }
  101. if phoneCode != realPhoneCode || phone != sessionPhone {
  102. log.Println("短信验证码错误")
  103. f.ServeJson(map[string]interface{}{
  104. "code": 0,
  105. "status": false,
  106. "message": "短信验证码错误",
  107. })
  108. return
  109. }
  110. // 清理验证过的验证码
  111. session.Values["code"] = ""
  112. session.Values["phone"] = ""
  113. if err := session.Save(f.Request, f.ResponseWriter); err != nil {
  114. log.Println("session1清理出错,短信验证码")
  115. }
  116. log.Println("验证码验证通过")
  117. // 3. 验证用户导出权限 确认认用户是否有数据导出权限
  118. loginUser := JyMysql.SelectBySql("select id, name,ent_id,phone,export_power,name from entniche_user where phone=? and export_power=1", phone)
  119. if loginUser == nil || len(*loginUser) == 0 {
  120. log.Println("该用户无数据导出权限")
  121. f.ServeJson(map[string]interface{}{
  122. "code": 0,
  123. "status": false,
  124. "message": "无权限",
  125. })
  126. return
  127. }
  128. // 4. 如果用户在多个企业并且都有剑鱼库里都有数据导出权限,则返回去让用户选择企业
  129. if len(*loginUser) > 1 {
  130. f.SetSession("user", map[string]interface{}{
  131. "phone": phone,
  132. })
  133. f.ServeJson(map[string]interface{}{
  134. "code": 1,
  135. "status": true,
  136. "message": "",
  137. })
  138. return
  139. }
  140. LoginCheck(f, int((*loginUser)[0]["ent_id"].(int64)), (*loginUser)[0], phone)
  141. return
  142. } else {
  143. //f.Render("login.html")
  144. f.Render("/?nol=1")
  145. }
  146. }
  147. func (f *Front) ChooseEnt() {
  148. defer qu.Catch()
  149. // 选择企业页面
  150. info := f.Session().GetMultiple()
  151. if info == nil || len(info) == 0 || info["userId"] == nil {
  152. f.Redirect("/seplatform/", 302)
  153. return
  154. }
  155. if f.Method() == "POST" {
  156. entId := f.GetString("ent_id")
  157. log.Println(entId)
  158. // 2. 确认该手机号有数据导出权限
  159. loginUser := JyMysql.SelectBySql("select id, name,ent_id,phone,export_power,name from entniche_user where phone=? and export_power=1 and ent_id=?", info["phone"], qu.IntAll(entId))
  160. if loginUser == nil || len(*loginUser) == 0 {
  161. f.ServeJson(map[string]interface{}{
  162. "code": 0,
  163. "status": false,
  164. "message": "无权限",
  165. })
  166. return
  167. }
  168. LoginCheck(f, qu.IntAll(entId), (*loginUser)[0], info["phone"].(string))
  169. return
  170. } else { // get 请求时铺企业页面
  171. // 查询 企业信息 name ent_id
  172. rs := JyMysql.SelectBySql("select a.ent_id, b.name ent_name from entniche_user a, entniche_info b WHERE a.phone =? and export_power=1 and a.ent_id=b.id", info["phone"])
  173. f.T["ent_info"] = rs
  174. f.Render("choose_ent.html", &f.T)
  175. }
  176. }
  177. func (f *Front) Index() {
  178. defer qu.Catch()
  179. f.Render("index.html")
  180. }
  181. func (f *Front) Logout() {
  182. f.DelSession("user")
  183. f.Redirect("/seplatform/")
  184. }
  185. func (f *Front) UpdatePwd() {
  186. defer qu.Catch()
  187. id := f.GetString("id")
  188. password := f.GetString("pwd")
  189. set := bson.M{
  190. "$set": bson.M{
  191. "s_pwd": encrypt.SE.EncodeString(password),
  192. },
  193. }
  194. if Mgo.UpdateById("user", id, set) {
  195. f.SetSession("pwd", password)
  196. }
  197. }
  198. func (f *Front) User() {
  199. defer qu.Catch()
  200. role, _ := Mgo.Find("role", nil, nil, nil, false, -1, -1)
  201. roleMap := make(map[string]string)
  202. for _, v := range *role {
  203. level := qu.ObjToString(v["level"])
  204. if level != "" {
  205. roleMap[level] = qu.ObjToString(v["name"])
  206. }
  207. }
  208. if f.Method() == "POST" {
  209. query := bson.M{
  210. "s_role": bson.M{
  211. "$ne": "0",
  212. },
  213. }
  214. data, _ := Mgo.Find("user", query, `{"_id":1}`, nil, false, -1, -1)
  215. for _, d := range *data {
  216. d["s_pwd"] = encrypt.SE.DecodeString(qu.ObjToString(d["s_pwd"]))
  217. d["role_name"] = roleMap[qu.ObjToString(d["s_role"])]
  218. }
  219. f.ServeJson(map[string]interface{}{
  220. "data": data,
  221. })
  222. } else {
  223. f.T["role"] = roleMap
  224. f.Render("com/user.html", &f.T)
  225. }
  226. }
  227. func (f *Front) UserDel() {
  228. defer qu.Catch()
  229. _id := f.GetString("_id")
  230. query := bson.M{
  231. "_id": mongodb.StringTOBsonId(_id),
  232. }
  233. b := Mgo.Del("user", query)
  234. f.ServeJson(map[string]interface{}{
  235. "rep": b,
  236. })
  237. }
  238. func (f *Front) UserSave() {
  239. _id := f.GetString("_id")
  240. role := f.GetString("role")
  241. name := f.GetString("name")
  242. email := f.GetString("email")
  243. pwd := f.GetString("pwd")
  244. set := bson.M{
  245. "$set": bson.M{
  246. "s_role": role,
  247. "s_name": name,
  248. "s_email": email,
  249. "s_pwd": encrypt.SE.EncodeString(pwd),
  250. "i_comeintime": time.Now().Unix(),
  251. },
  252. }
  253. query := bson.M{
  254. "_id": mongodb.StringTOBsonId(_id),
  255. }
  256. if _id == "" {
  257. query = bson.M{
  258. "_id": primitive.NewObjectID(),
  259. }
  260. }
  261. b := Mgo.Update("user", query, set, true, false)
  262. //b := Mgo.UpdateById("user", _id, set)
  263. f.ServeJson(map[string]interface{}{
  264. "rep": b,
  265. })
  266. }
  267. func (f *Front) PersonalMenu() {
  268. list := UserMenu[qu.ObjToString(f.GetSession("email"))]
  269. f.ServeJson(map[string]interface{}{
  270. "data": list,
  271. })
  272. }
  273. func GetUserMenu(role string) []map[string]interface{} {
  274. var list []map[string]interface{}
  275. maps := map[string]interface{}{
  276. "delete": false,
  277. }
  278. if role != "0" {
  279. maps["role."+role] = true
  280. }
  281. data, _ := Mgo.Find("menu_first", maps, nil, nil, false, -1, -1)
  282. for _, d := range *data {
  283. _id := d["_id"]
  284. maps = map[string]interface{}{
  285. "delete": false,
  286. "s_pid": mongodb.BsonIdToSId(_id),
  287. }
  288. if role != "0" {
  289. maps["role."+role] = true
  290. }
  291. secdatas, _ := Mgo.Find("menu_second", maps, nil, nil, false, -1, -1)
  292. secmenumap := map[string]interface{}{}
  293. for index, secdata := range *secdatas {
  294. secmenumap[strconv.Itoa(index+1)] = secdata
  295. }
  296. if len(secmenumap) != 0 {
  297. d["secondmenu"] = secmenumap
  298. }
  299. list = append(list, d)
  300. }
  301. return list
  302. }
  303. // 获取图片验证码
  304. func (f *Front) Code() {
  305. id := captcha.NewLen(4)
  306. //r := &http.Request{}
  307. f.Request.Header.Add("Cache-Control", "no-cache, no-store, must-revalidate")
  308. f.Request.Header.Add("Pragma", "no-cache")
  309. f.Request.Header.Add("Expires", "0")
  310. f.Request.Header.Add("Content-Type", "image/png")
  311. w := f.ResponseWriter
  312. session, err := store.Get(f.Request, "dataTagLoginImgCode")
  313. if err != nil {
  314. log.Println("session1获取失败")
  315. return
  316. }
  317. session.Values["dataTagLoginImgCode"] = id
  318. session.Options.MaxAge = 60
  319. if err := session.Save(f.Request, w); err != nil {
  320. log.Println("session1保存错误,验证码 ", id)
  321. }
  322. err2 := captcha.WriteImage(w, id, 90, 30)
  323. if err2 != nil {
  324. log.Println("生成图片验证码错误,验证码 ", id)
  325. }
  326. return
  327. }
  328. // 发送手机验证码接口
  329. func (f *Front) SendVerifyCode() {
  330. if f.Method() == "POST" {
  331. //1. 验证参数有效性
  332. phone := f.GetString("phone")
  333. imgCode := f.GetString("imgCode")
  334. reg := regexp.MustCompile("^1(3|4|5|6|7|8|9)\\d{9}$")
  335. if !reg.MatchString(phone) {
  336. f.ServeJson(map[string]interface{}{
  337. "code": 0,
  338. "status": false,
  339. "message": "手机号格式有误",
  340. })
  341. return
  342. }
  343. //2. 验证图片验证码
  344. session, err := store.Get(f.Request, "dataTagLoginImgCode")
  345. if err != nil {
  346. log.Printf("图片验证码session获取失败-%s \n", phone)
  347. f.ServeJson(map[string]interface{}{
  348. "code": 0,
  349. "status": false,
  350. "message": "获取失败",
  351. })
  352. return
  353. }
  354. code := qu.ObjToString(session.Values["dataTagLoginImgCode"])
  355. if code == "" {
  356. log.Printf("图片验证码过期-%s \n", phone)
  357. f.ServeJson(map[string]interface{}{
  358. "code": 0,
  359. "status": false,
  360. "message": "图片验证码过期",
  361. })
  362. return
  363. }
  364. if !captcha.VerifyString(code, imgCode) {
  365. log.Printf("图片验证码错误-%s \n", phone)
  366. f.ServeJson(map[string]interface{}{
  367. "code": 0,
  368. "status": false,
  369. "message": "图片验证码错误",
  370. })
  371. return
  372. }
  373. //3. 验证手机号是否存在
  374. user := JyMysql.SelectBySql("select * from entniche_user where phone=? and export_power=1", phone)
  375. // 确认用户是否存在
  376. if len(*user) == 0 || ((*user)[0])["phone"] != phone {
  377. log.Println("没有数据导出权限")
  378. f.ServeJson(map[string]interface{}{
  379. "code": 0,
  380. "status": false,
  381. "message": "无权限",
  382. })
  383. return
  384. }
  385. //4. 发送验证码
  386. SendPhoneCode(f, phone)
  387. f.ServeJson(map[string]interface{}{
  388. "code": 0,
  389. "status": true,
  390. "message": "发送成功",
  391. })
  392. return
  393. } else {
  394. f.ServeJson(map[string]interface{}{
  395. "code": 0,
  396. "status": false,
  397. "message": "无效的请求方式",
  398. })
  399. }
  400. }
  401. // 发送手机验证码方法
  402. func SendPhoneCode(f *Front, phone string) {
  403. r := f.Request
  404. w := f.ResponseWriter
  405. session, err := store.Get(r, "dataTagLoginPhoneCode")
  406. if err != nil {
  407. log.Println("phone-session1获取失败")
  408. return
  409. }
  410. lastSentTime := qu.Int64All(session.Values["identCodeTime"])
  411. //60秒之内不允许重复发
  412. if lastSentTime > 0 && time.Now().Unix()-lastSentTime <= 60 {
  413. }
  414. s_ranNum := qu.GetRandom(6) //生成随机数
  415. session.Values["code"] = s_ranNum
  416. session.Values["phone"] = phone
  417. session.Values["identCodeTime"] = time.Now().Unix()
  418. session.Options.MaxAge = 300
  419. if err := session.Save(r, w); err != nil {
  420. log.Println("session1保存错误,验证码")
  421. }
  422. //发送短信
  423. // param := map[string]string{"code": s_ranNum}
  424. log.Println("短信验证码", phone, s_ranNum)
  425. // SendSMS("2828060", phone, param)
  426. SendSMS("jypro186.jy360.cn:932", phone, s_ranNum)
  427. }
  428. func SendSMS(address, mobile string, params ...string) {
  429. sms.SendSms(address, "01", mobile, params...)
  430. }
  431. func LoginCheck(f *Front, entId int, loginUser map[string]interface{}, userPhone string) {
  432. // 1. 确认该企业有使用该系统的权限 查询配置文件appid 确认是否有使用该系统的权限
  433. query2 := bson.M{
  434. "ent_id": entId,
  435. }
  436. log.Println(entId)
  437. isExist, _ := Mgo.FindOne("datatag_export_config", query2)
  438. log.Println(isExist)
  439. if isExist == nil || len(*isExist) == 0 {
  440. f.ServeJson(map[string]interface{}{
  441. "code": 0,
  442. "status": false,
  443. "message": "该企业无权限",
  444. })
  445. return
  446. }
  447. // 3. 查询企业管理员信息及手机号
  448. entInfo := JyMysql.SelectBySql("select phone,name from entniche_info WHERE id = ?", entId)
  449. if entInfo == nil || len(*entInfo) == 0 {
  450. f.ServeJson(map[string]interface{}{
  451. "code": 0,
  452. "status": false,
  453. "message": "企业信息查询失败",
  454. })
  455. return
  456. }
  457. // 4. mongo库查询与之关联的企业appid
  458. entMgoInfo, ok := MgoCus.FindOne("user", bson.M{"entid": entId})
  459. if !ok || entMgoInfo == nil || len(*entMgoInfo) == 0 {
  460. // 企业信息查询失败
  461. f.ServeJson(map[string]interface{}{
  462. "code": 0,
  463. "status": false,
  464. "message": "appid查询失败",
  465. })
  466. return
  467. }
  468. // 5. 确认用户角色
  469. var role int // 角色 1 管理员 3 普通用户
  470. if userPhone != qu.ObjToString((*entInfo)[0]["phone"]) {
  471. role = 3
  472. } else {
  473. role = 1
  474. }
  475. // 确认用户角色 存session
  476. f.SetSession("user", map[string]interface{}{
  477. "name": loginUser["name"],
  478. "phone": userPhone,
  479. "role": role,
  480. "appid": (*entMgoInfo)["appid"],
  481. "ent_id": qu.IntAll(entId),
  482. "id": qu.IntAll(loginUser["id"]),
  483. })
  484. // 返回
  485. f.ServeJson(map[string]interface{}{
  486. "code": 0,
  487. "status": true,
  488. "role": role,
  489. "message": "",
  490. })
  491. }