|
@@ -1,15 +1,20 @@
|
|
|
package front
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/dchest/captcha"
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
+ "github.com/gorilla/sessions"
|
|
|
+ "gopkg.in/mgo.v2/bson"
|
|
|
"log"
|
|
|
mongoutil "qfw/mongodb"
|
|
|
qu "qfw/util"
|
|
|
+ "qfw/util/sms"
|
|
|
+ "regexp"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
. "util"
|
|
|
-
|
|
|
- "github.com/go-xweb/xweb"
|
|
|
- "gopkg.in/mgo.v2/bson"
|
|
|
)
|
|
|
|
|
|
var UserMenu map[string][]map[string]interface{} //存储菜单
|
|
@@ -19,7 +24,10 @@ func init() {
|
|
|
|
|
|
type Front struct {
|
|
|
*xweb.Action
|
|
|
- login xweb.Mapper `xweb:"/"` //登录页面
|
|
|
+ login xweb.Mapper `xweb:"/"` //登录页面
|
|
|
+ code xweb.Mapper `xweb:"/code"` //获取图片验证码
|
|
|
+ sendVerifyCode xweb.Mapper `xweb:"/sendVerifyCode"` //获取手机验证码
|
|
|
+
|
|
|
index xweb.Mapper `xweb:"/front/index"` //index页面
|
|
|
logout xweb.Mapper `xweb:"/front/logout"` //注销
|
|
|
updatePwd xweb.Mapper `xweb:"/front/updatepwd"` //更新密码
|
|
@@ -46,43 +54,125 @@ type Front struct {
|
|
|
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" {
|
|
|
- email := f.GetString("email")
|
|
|
- password := f.GetString("pwd")
|
|
|
- //f.SetSession("password", password)
|
|
|
- passwordEn := qu.SE.EncodeString(password)
|
|
|
+ // 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 {
|
|
|
+ log.Println("phone-session2获取失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ realPhoneCode := qu.ObjToString(session.Values["code"])
|
|
|
+ log.Println("realPhoneCode", realPhoneCode)
|
|
|
+ log.Println("param.phoneCode", phoneCode)
|
|
|
+ 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 len(*loginUser) == 0 || ((*loginUser)[0])["phone"] != phone {
|
|
|
+ log.Println("无权限")
|
|
|
+ f.ServeJson(map[string]interface{}{
|
|
|
+ "code": 0,
|
|
|
+ "status": false,
|
|
|
+ "message": "无权限",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 4. 根据用户的企业id查询对应的管理员手机号 根据管理员手机号查询jyqyfw的企业表对应的appid 放到session里 以及确认用户角色
|
|
|
+ log.Println((*loginUser)[0]["ent_id"], "=====================")
|
|
|
+ entInfo := JyMysql.SelectBySql("select phone,name from entniche_info WHERE id = ?", (*loginUser)[0]["ent_id"])
|
|
|
+ if len(*entInfo) == 0 {
|
|
|
+ f.ServeJson(map[string]interface{}{
|
|
|
+ "code": 0,
|
|
|
+ "status": false,
|
|
|
+ "message": "企业信息查询失败",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var role int // 角色 1 管理员 3 普通用户
|
|
|
+ // 判断当前用户手机号是否是管理员手机号
|
|
|
+ if phone != qu.ObjToString((*entInfo)[0]["phone"]) {
|
|
|
+ role = 3
|
|
|
+ } else {
|
|
|
+ role = 1
|
|
|
+ }
|
|
|
+ // 查询mongo企业信息库 获取appid
|
|
|
query := bson.M{
|
|
|
- "s_email": email,
|
|
|
- "s_pwd": passwordEn,
|
|
|
- }
|
|
|
- user, _ := Mgo.FindOne("user", query)
|
|
|
- checked := false
|
|
|
- s_id := ""
|
|
|
- if user != nil && len(*user) > 0 {
|
|
|
- checked = true
|
|
|
+ "phone": qu.ObjToString((*entInfo)[0]["phone"]),
|
|
|
+ "username": qu.ObjToString((*entInfo)[0]["name"]),
|
|
|
+ }
|
|
|
+ entMgoInfo, ok := MgoCus.FindOne("user", query)
|
|
|
+ if !ok {
|
|
|
+ // 企业信息查询失败
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //相关信息存入session
|
|
|
+ if entMgoInfo != nil && len(*entMgoInfo) > 0 {
|
|
|
f.SetSession("user", map[string]interface{}{
|
|
|
- "name": (*user)["s_name"],
|
|
|
- "role": (*user)["s_role"],
|
|
|
- "pwd": password,
|
|
|
- "email": email,
|
|
|
- "id": mongoutil.BsonIdToSId((*user)["_id"]),
|
|
|
+ "name": (*loginUser)[0]["name"],
|
|
|
+ "phone": phone,
|
|
|
+ "role": role,
|
|
|
+ "appid": (*entMgoInfo)["appid"],
|
|
|
+ "ent_id": (*loginUser)[0]["entid"],
|
|
|
+ "id": (*loginUser)[0]["id"],
|
|
|
})
|
|
|
- UserMenu[email] = GetUserMenu(qu.ObjToString((*user)["s_role"]))
|
|
|
- if (*user)["s_role"] == "3" {
|
|
|
- log.Println("users", (*user)["s_name"])
|
|
|
- users, ok := Mgo.FindOne("cuser", map[string]interface{}{"s_name": (*user)["s_name"], "b_delete": false})
|
|
|
- log.Println("users", users)
|
|
|
- if users != nil && ok {
|
|
|
- s_id = mongoutil.BsonIdToSId((*users)["_id"])
|
|
|
- }
|
|
|
- }
|
|
|
+ //UserMenu[email] = GetUserMenu(qu.ObjToString((*user)["s_role"]))
|
|
|
+
|
|
|
}
|
|
|
f.ServeJson(map[string]interface{}{
|
|
|
- "checked": checked,
|
|
|
- "role": (*user)["s_role"],
|
|
|
- "id": s_id,
|
|
|
+ "code": 0,
|
|
|
+ "status": false,
|
|
|
+ "role": role,
|
|
|
+ "message": "企业信息查询失败",
|
|
|
})
|
|
|
} else {
|
|
|
f.Render("login.html")
|
|
@@ -227,3 +317,146 @@ func GetUserMenu(role string) []map[string]interface{} {
|
|
|
}
|
|
|
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
|
|
|
+ }
|
|
|
+ fmt.Println("code", code)
|
|
|
+ fmt.Println("img", imgCode)
|
|
|
+ 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)
|
|
|
+ fmt.Println(user, "-----------------================")
|
|
|
+ // 确认用户是否存在
|
|
|
+ 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)
|
|
|
+}
|
|
|
+
|
|
|
+//第三个参数是可变参数,可以传入多个,但要和模板相匹配
|
|
|
+func SendSMS(tplcode /*模板代码*/, mobile /*手机号码*/ string, param map[string]string) {
|
|
|
+ tmp := []string{}
|
|
|
+ for k, v := range param {
|
|
|
+ tmp = append(tmp, "#"+k+"#="+v)
|
|
|
+ }
|
|
|
+ text := strings.Join(tmp, "&")
|
|
|
+ sms.SendSms(mobile, tplcode, text)
|
|
|
+}
|