浏览代码

查询配置表由原来查询appid修改为查询企业id

fuwencai 4 年之前
父节点
当前提交
ff424bcbba
共有 2 个文件被更改,包括 109 次插入37 次删除
  1. 47 37
      src/front/front.go
  2. 62 0
      src/util/cost_util.go

+ 47 - 37
src/front/front.go

@@ -93,8 +93,6 @@ func (f *Front) Login() {
 			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)
@@ -132,7 +130,7 @@ func (f *Front) Login() {
 			})
 			return
 		}
-		// 手机号 如果用户在多个企业并且都有剑鱼库里都有数据导出权限,则让用户选择企业
+		// 4. 如果用户在多个企业并且都有剑鱼库里都有数据导出权限,则返回去让用户选择企业
 		if len(*loginUser) > 1 {
 			f.SetSession("user", map[string]interface{}{
 				"phone": phone,
@@ -145,7 +143,22 @@ func (f *Front) Login() {
 			return
 
 		}
-		// 4. 根据用户的企业id查询对应的管理员手机号  根据管理员手机号查询jyqyfw的企业表对应的appid 放到session里  以及确认用户角色
+		// 4.如果只有一个企业 则验证企业是否有使用该系统的权限
+		// 确认该企业有使用该系统的权限 查询 datatag_export_config 验证ent_id 确认是否有使用该系统的权限
+		query2 := bson.M{
+			"ent_id": (*loginUser)[0]["ent_id"],
+		}
+		isExist, _ := Mgo.FindOne("datatag_export_config", query2)
+		log.Println(isExist)
+		if len((*isExist)) == 0 {
+			f.ServeJson(map[string]interface{}{
+				"code":    0,
+				"status":  false,
+				"message": "该企业无权限",
+			})
+			return
+		}
+		// 5. 根据用户的企业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 {
@@ -157,13 +170,13 @@ func (f *Front) Login() {
 			return
 		}
 		var role int // 角色  1 管理员  3 普通用户
-		// 判断当前用户手机号是否是管理员手机号
+		// 5.1 判断当前用户手机号是否是管理员手机号
 		if phone != qu.ObjToString((*entInfo)[0]["phone"]) {
 			role = 3
 		} else {
 			role = 1
 		}
-		// 查询mongo企业信息库 获取appid
+		// 5.2 查询mongo企业信息库 获取appid
 		query := bson.M{
 			"phone":    qu.ObjToString((*entInfo)[0]["phone"]),
 			"username": qu.ObjToString((*entInfo)[0]["name"]),
@@ -178,7 +191,7 @@ func (f *Front) Login() {
 			})
 			return
 		}
-		//相关信息存入session
+		// 5.3 相关信息存入session
 		if entMgoInfo != nil && len(*entMgoInfo) > 0 {
 			f.SetSession("user", map[string]interface{}{
 				"name":          (*loginUser)[0]["name"],
@@ -204,8 +217,7 @@ func (f *Front) Login() {
 }
 func (f *Front) ChooseEnt() {
 	defer qu.Catch()
-	//选择页面
-	// 根据session信息查询
+	// 选择企业页面
 	tempSession := f.GetSession("user")
 	if tempSession == nil || tempSession == "" {
 		f.Redirect("/", 302)
@@ -216,18 +228,23 @@ func (f *Front) ChooseEnt() {
 		f.Redirect("/", 302)
 		return
 	}
-	if f.Method() == "GET" {
-		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"])
-		// 企业信息  name  ent_id
-		//铺页面
-		f.T["ent_info"] = rs
-		f.Render("choose_ent.html", &f.T)
-	} else {
-
+	if f.Method() == "POST" {
 		entId := f.GetString("ent_id")
-		log.Println(entId, "entId")
-		log.Println(info["phone"], "info[\"phone\"]")
-		//确认该手机号有数据导出权限
+		// 1. 确认该企业有使用该系统的权限 查询配置文件appid 确认是否有使用该系统的权限
+		query2 := bson.M{
+			"ent_id": entId,
+		}
+		isExist, _ := Mgo.FindOne("datatag_export_config", query2)
+		log.Println(isExist)
+		if len((*isExist)) == 0 {
+			f.ServeJson(map[string]interface{}{
+				"code":    0,
+				"status":  false,
+				"message": "该企业无权限",
+			})
+			return
+		}
+		// 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"], entId)
 		if len(*loginUser) == 0 {
 			f.ServeJson(map[string]interface{}{
@@ -237,9 +254,7 @@ func (f *Front) ChooseEnt() {
 			})
 			return
 		}
-
-		var role int // 角色  1 管理员  3 普通用户
-		// 判断当前用户手机号是否是管理员手机号
+		// 3. 查询企业管理员信息及手机号
 		entInfo := JyMysql.SelectBySql("select phone,name from entniche_info WHERE id = ?", (*loginUser)[0]["ent_id"])
 		if len(*entInfo) == 0 {
 			f.ServeJson(map[string]interface{}{
@@ -249,6 +264,7 @@ func (f *Front) ChooseEnt() {
 			})
 			return
 		}
+		// 4. mongo库查询与之关联的企业appid
 		query := bson.M{
 			"phone":    (*entInfo)[0]["phone"],
 			"username": (*entInfo)[0]["name"],
@@ -263,20 +279,8 @@ func (f *Front) ChooseEnt() {
 			})
 			return
 		}
-		// 确认该企业有使用该系统的权限 查询配置文件appid 确认是否有使用该系统的权限
-		query2 := bson.M{
-			"app_id": (*entMgoInfo)["appid"],
-		}
-		isExist, _ := Mgo.FindOne("datatag_export_config", query2)
-		log.Println(isExist)
-		if len((*isExist)) == 0 {
-			f.ServeJson(map[string]interface{}{
-				"code":    0,
-				"status":  false,
-				"message": "该企业无权限",
-			})
-			return
-		}
+		// 5. 确认用户角色
+		var role int // 角色  1 管理员  3 普通用户
 		if info["phone"] != qu.ObjToString((*entInfo)[0]["phone"]) {
 			role = 3
 		} else {
@@ -299,6 +303,11 @@ func (f *Front) ChooseEnt() {
 			"role":    role,
 			"message": "",
 		})
+	} 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)
 	}
 
 }
@@ -580,3 +589,4 @@ func SendSMS(tplcode /*模板代码*/, mobile /*手机号码*/ string, param map
 	text := strings.Join(tmp, "&")
 	sms.SendSms(mobile, tplcode, text)
 }
+

+ 62 - 0
src/util/cost_util.go

@@ -0,0 +1,62 @@
+package util
+
+import (
+	"database/sql"
+	"log"
+	"qfw/util"
+	"time"
+)
+
+// 扣费相关
+// 获取账户当前剩余量
+func GetCurrentCount2(entId, userId int) int {
+	count := 0
+	userData := JyMysql.FindOne("user_account", map[string]interface{}{"user_id": userId, "ent_id": entId}, "left_num", "")
+	// 如果不存在就初始化一条记录
+	if userData == nil {
+		nowStr := time.Now().Local().Format("2006-01-02 15:04:05")
+		JyMysql.Insert("user_account", map[string]interface{}{"user_id": userId, "ent_id": entId, "left_num": 0, "create_at": nowStr})
+		return count
+	}
+	count = util.IntAll((*userData)["left_num"])
+	return count
+}
+
+// 扣费
+func DeductNum2(entId int, newCount int, userId int, exportId int64) bool {
+	var flag bool
+	// 1. 查询账户余额
+	log.Println("扣费开始")
+	query := map[string]interface{}{
+		"user_id": userId,
+		"ent_id":  entId,
+	}
+	userAccountData := JyMysql.FindOne("user_account", query, "left_num", "")
+	//2.扣费
+	// 扣费前剩余
+	if userAccountData == nil {
+		log.Printf("用户扣费失败:user_id-%dent_id:%d\n", userId, entId)
+		flag = false
+		return flag
+	}
+	before := int((*userAccountData)["left_num"].(int64))
+	after := before - newCount
+	// 扣费
+	rechargeRs := JyMysql.ExecTx("用户扣费", func(tx *sql.Tx) bool {
+		// 更新剩余额度
+		updateNowStr := time.Now().Local().Format("2006-01-02 15:04:05")
+		ok1 := JyMysql.UpdateOrDeleteBySqlByTx(tx, "update user_account set left_num=?,update_at=? where user_id=? and ent_id=?", after, updateNowStr, userId, entId) > 0
+		// 生扣费记录
+		sqlStr := "insert into user_expend_record (user_id,ent_id,`before`,`after`,trade,create_at,export_id) values(?,?,?,?,?,?,?)"
+		ok2 := JyMysql.InsertBySqlByTx(tx, sqlStr, userId, entId, before, after, newCount, updateNowStr, exportId) != -1
+		return ok1 && ok2
+	})
+	if rechargeRs != true {
+		flag = false
+		log.Printf("用户扣费失败:user_id-%dent_id:%d\n", userId, entId)
+	} else {
+		flag = true
+	}
+	log.Println("扣费结束")
+	return flag
+}