package front import ( . "JySEPlatform/util" "log" "regexp" "strconv" "time" qu "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/go-xweb/xweb" "app.yhyue.com/moapp/jybase/mongodb" "app.yhyue.com/moapp/jybase/sms" "github.com/dchest/captcha" "github.com/gorilla/sessions" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) var UserMenu map[string][]map[string]interface{} //存储菜单 func init() { UserMenu = make(map[string][]map[string]interface{}) } type Front struct { *xweb.Action login xweb.Mapper `xweb:"/"` //登录页面 code xweb.Mapper `xweb:"/code"` //获取图片验证码 sendVerifyCode xweb.Mapper `xweb:"/sendVerifyCode"` //获取手机验证码 chooseEnt xweb.Mapper `xweb:"/chooseEnt"` //选择企业 index xweb.Mapper `xweb:"/front/index"` //index页面 logout xweb.Mapper `xweb:"/front/logout"` //注销 updatePwd xweb.Mapper `xweb:"/front/updatepwd"` //更新密码 //user user xweb.Mapper `xweb:"/front/user"` //查所有用户 userDel xweb.Mapper `xweb:"/front/user/del"` //删除用户 userSave xweb.Mapper `xweb:"/front/user/save"` //保存用户 //menu menu xweb.Mapper `xweb:"/front/menu"` //查一级菜单 menuSave xweb.Mapper `xweb:"/front/menu/save"` //保存一级菜单 menuDel xweb.Mapper `xweb:"/front/menu/del"` //删除一级菜单 menuSecond xweb.Mapper `xweb:"/front/menuSecond"` //查二级菜单 menuSecondSave xweb.Mapper `xweb:"/front/menuSecond/save"` //保存二级菜单 menuSecondDel xweb.Mapper `xweb:"/front/menuSecond/del"` //删除二级菜单 personalMenu xweb.Mapper `xweb:"/front/personalMenu"` //role roleManager xweb.Mapper `xweb:"/front/role"` //角色权限管理 roleNew xweb.Mapper `xweb:"/front/role/save"` //新增角色 roleEdit xweb.Mapper `xweb:"/front/role/edit/(.*)"` //角色权限查看 roleFirst xweb.Mapper `xweb:"/front/role/first"` //一级权限的查看 roleSecond xweb.Mapper `xweb:"/front/role/second"` //二级权限的查看 roleSave xweb.Mapper `xweb:"/front/role/edit/save"` //权限编辑保存 roleDel xweb.Mapper `xweb:"/front/role/edit/del"` //权限编辑删除 roleSecondEdit xweb.Mapper `xweb:"/front/role/second/edit"` //二级权限编辑 } var store = sessions.NewCookieStore([]byte("jianyu_secret20210422")) func (f *Front) Login() { defer qu.Catch() if f.Method() == "POST" { // 1. 验证参数有效性 phone := f.GetString("phone") phoneCode := f.GetString("phoneCode") reg := regexp.MustCompile("^1([3456789])\\d{9}$") log.Println(phone) log.Println(phoneCode) if !reg.MatchString(phone) { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "手机号格式有误", }) return } if phoneCode == "" { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "短信验证码不能为空", }) return } // 2. 验证短信验证码 session, err := store.Get(f.Request, "dataTagLoginPhoneCode") if err != nil { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "验证码比对失败", }) log.Println("phone-session2获取失败") return } realPhoneCode := qu.ObjToString(session.Values["code"]) sessionPhone := qu.ObjToString(session.Values["phone"]) if sessionPhone == "" { log.Printf("短信验证码过期-%s \n", phone) f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "短信验证码过期", }) return } if phoneCode != realPhoneCode || phone != sessionPhone { log.Println("短信验证码错误") f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "短信验证码错误", }) return } // 清理验证过的验证码 session.Values["code"] = "" session.Values["phone"] = "" if err := session.Save(f.Request, f.ResponseWriter); err != nil { log.Println("session1清理出错,短信验证码") } log.Println("验证码验证通过") // 3. 验证用户导出权限 确认认用户是否有数据导出权限 loginUser := JyMysql.SelectBySql("select id, name,ent_id,phone,export_power,name from entniche_user where phone=? and export_power=1", phone) if loginUser == nil || len(*loginUser) == 0 { log.Println("该用户无数据导出权限") f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "无权限", }) return } // 4. 如果用户在多个企业并且都有剑鱼库里都有数据导出权限,则返回去让用户选择企业 if len(*loginUser) > 1 { f.SetSession("user", map[string]interface{}{ "phone": phone, }) f.ServeJson(map[string]interface{}{ "code": 1, "status": true, "message": "", }) return } LoginCheck(f, int((*loginUser)[0]["ent_id"].(int64)), (*loginUser)[0], phone) return } else { //f.Render("login.html") f.Render("/?nol=1") } } func (f *Front) ChooseEnt() { defer qu.Catch() // 选择企业页面 info := f.Session().GetMultiple() if info == nil || len(info) == 0 || info["userId"] == nil { f.Redirect("/seplatform/", 302) return } if f.Method() == "POST" { entId := f.GetString("ent_id") log.Println(entId) // 2. 确认该手机号有数据导出权限 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)) if loginUser == nil || len(*loginUser) == 0 { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "无权限", }) return } LoginCheck(f, qu.IntAll(entId), (*loginUser)[0], info["phone"].(string)) return } else { // get 请求时铺企业页面 // 查询 企业信息 name ent_id 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"]) f.T["ent_info"] = rs f.Render("choose_ent.html", &f.T) } } func (f *Front) Index() { defer qu.Catch() f.Render("index.html") } func (f *Front) Logout() { f.DelSession("user") f.Redirect("/seplatform/") } func (f *Front) UpdatePwd() { defer qu.Catch() id := f.GetString("id") password := f.GetString("pwd") set := bson.M{ "$set": bson.M{ "s_pwd": encrypt.SE.EncodeString(password), }, } if Mgo.UpdateById("user", id, set) { f.SetSession("pwd", password) } } func (f *Front) User() { defer qu.Catch() role, _ := Mgo.Find("role", nil, nil, nil, false, -1, -1) roleMap := make(map[string]string) for _, v := range *role { level := qu.ObjToString(v["level"]) if level != "" { roleMap[level] = qu.ObjToString(v["name"]) } } if f.Method() == "POST" { query := bson.M{ "s_role": bson.M{ "$ne": "0", }, } data, _ := Mgo.Find("user", query, `{"_id":1}`, nil, false, -1, -1) for _, d := range *data { d["s_pwd"] = encrypt.SE.DecodeString(qu.ObjToString(d["s_pwd"])) d["role_name"] = roleMap[qu.ObjToString(d["s_role"])] } f.ServeJson(map[string]interface{}{ "data": data, }) } else { f.T["role"] = roleMap f.Render("com/user.html", &f.T) } } func (f *Front) UserDel() { defer qu.Catch() _id := f.GetString("_id") query := bson.M{ "_id": mongodb.StringTOBsonId(_id), } b := Mgo.Del("user", query) f.ServeJson(map[string]interface{}{ "rep": b, }) } func (f *Front) UserSave() { _id := f.GetString("_id") role := f.GetString("role") name := f.GetString("name") email := f.GetString("email") pwd := f.GetString("pwd") set := bson.M{ "$set": bson.M{ "s_role": role, "s_name": name, "s_email": email, "s_pwd": encrypt.SE.EncodeString(pwd), "i_comeintime": time.Now().Unix(), }, } query := bson.M{ "_id": mongodb.StringTOBsonId(_id), } if _id == "" { query = bson.M{ "_id": primitive.NewObjectID(), } } b := Mgo.Update("user", query, set, true, false) //b := Mgo.UpdateById("user", _id, set) f.ServeJson(map[string]interface{}{ "rep": b, }) } func (f *Front) PersonalMenu() { list := UserMenu[qu.ObjToString(f.GetSession("email"))] f.ServeJson(map[string]interface{}{ "data": list, }) } func GetUserMenu(role string) []map[string]interface{} { var list []map[string]interface{} maps := map[string]interface{}{ "delete": false, } if role != "0" { maps["role."+role] = true } data, _ := Mgo.Find("menu_first", maps, nil, nil, false, -1, -1) for _, d := range *data { _id := d["_id"] maps = map[string]interface{}{ "delete": false, "s_pid": mongodb.BsonIdToSId(_id), } if role != "0" { maps["role."+role] = true } secdatas, _ := Mgo.Find("menu_second", maps, nil, nil, false, -1, -1) secmenumap := map[string]interface{}{} for index, secdata := range *secdatas { secmenumap[strconv.Itoa(index+1)] = secdata } if len(secmenumap) != 0 { d["secondmenu"] = secmenumap } list = append(list, d) } return list } // 获取图片验证码 func (f *Front) Code() { id := captcha.NewLen(4) //r := &http.Request{} f.Request.Header.Add("Cache-Control", "no-cache, no-store, must-revalidate") f.Request.Header.Add("Pragma", "no-cache") f.Request.Header.Add("Expires", "0") f.Request.Header.Add("Content-Type", "image/png") w := f.ResponseWriter session, err := store.Get(f.Request, "dataTagLoginImgCode") if err != nil { log.Println("session1获取失败") return } session.Values["dataTagLoginImgCode"] = id session.Options.MaxAge = 60 if err := session.Save(f.Request, w); err != nil { log.Println("session1保存错误,验证码 ", id) } err2 := captcha.WriteImage(w, id, 90, 30) if err2 != nil { log.Println("生成图片验证码错误,验证码 ", id) } return } // 发送手机验证码接口 func (f *Front) SendVerifyCode() { if f.Method() == "POST" { //1. 验证参数有效性 phone := f.GetString("phone") imgCode := f.GetString("imgCode") reg := regexp.MustCompile("^1(3|4|5|6|7|8|9)\\d{9}$") if !reg.MatchString(phone) { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "手机号格式有误", }) return } //2. 验证图片验证码 session, err := store.Get(f.Request, "dataTagLoginImgCode") if err != nil { log.Printf("图片验证码session获取失败-%s \n", phone) f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "获取失败", }) return } code := qu.ObjToString(session.Values["dataTagLoginImgCode"]) if code == "" { log.Printf("图片验证码过期-%s \n", phone) f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "图片验证码过期", }) return } if !captcha.VerifyString(code, imgCode) { log.Printf("图片验证码错误-%s \n", phone) f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "图片验证码错误", }) return } //3. 验证手机号是否存在 user := JyMysql.SelectBySql("select * from entniche_user where phone=? and export_power=1", phone) // 确认用户是否存在 if len(*user) == 0 || ((*user)[0])["phone"] != phone { log.Println("没有数据导出权限") f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "无权限", }) return } //4. 发送验证码 SendPhoneCode(f, phone) f.ServeJson(map[string]interface{}{ "code": 0, "status": true, "message": "发送成功", }) return } else { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "无效的请求方式", }) } } // 发送手机验证码方法 func SendPhoneCode(f *Front, phone string) { r := f.Request w := f.ResponseWriter session, err := store.Get(r, "dataTagLoginPhoneCode") if err != nil { log.Println("phone-session1获取失败") return } lastSentTime := qu.Int64All(session.Values["identCodeTime"]) //60秒之内不允许重复发 if lastSentTime > 0 && time.Now().Unix()-lastSentTime <= 60 { } s_ranNum := qu.GetRandom(6) //生成随机数 session.Values["code"] = s_ranNum session.Values["phone"] = phone session.Values["identCodeTime"] = time.Now().Unix() session.Options.MaxAge = 300 if err := session.Save(r, w); err != nil { log.Println("session1保存错误,验证码") } //发送短信 // param := map[string]string{"code": s_ranNum} log.Println("短信验证码", phone, s_ranNum) // SendSMS("2828060", phone, param) SendSMS("jypro186.jy360.cn:932", phone, s_ranNum) } func SendSMS(address, mobile string, params ...string) { sms.SendSms(address, "01", mobile, params...) } func LoginCheck(f *Front, entId int, loginUser map[string]interface{}, userPhone string) { // 1. 确认该企业有使用该系统的权限 查询配置文件appid 确认是否有使用该系统的权限 query2 := bson.M{ "ent_id": entId, } log.Println(entId) isExist, _ := Mgo.FindOne("datatag_export_config", query2) log.Println(isExist) if isExist == nil || len(*isExist) == 0 { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "该企业无权限", }) return } // 3. 查询企业管理员信息及手机号 entInfo := JyMysql.SelectBySql("select phone,name from entniche_info WHERE id = ?", entId) if entInfo == nil || len(*entInfo) == 0 { f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "企业信息查询失败", }) return } // 4. mongo库查询与之关联的企业appid entMgoInfo, ok := MgoCus.FindOne("user", bson.M{"entid": entId}) if !ok || entMgoInfo == nil || len(*entMgoInfo) == 0 { // 企业信息查询失败 f.ServeJson(map[string]interface{}{ "code": 0, "status": false, "message": "appid查询失败", }) return } // 5. 确认用户角色 var role int // 角色 1 管理员 3 普通用户 if userPhone != qu.ObjToString((*entInfo)[0]["phone"]) { role = 3 } else { role = 1 } // 确认用户角色 存session f.SetSession("user", map[string]interface{}{ "name": loginUser["name"], "phone": userPhone, "role": role, "appid": (*entMgoInfo)["appid"], "ent_id": qu.IntAll(entId), "id": qu.IntAll(loginUser["id"]), }) // 返回 f.ServeJson(map[string]interface{}{ "code": 0, "status": true, "role": role, "message": "", }) }