package controllers import ( "Cold_Api/Nats/NatsServer" "Cold_Api/conf" "Cold_Api/controllers/lib" "Cold_Api/models/Account" "Cold_Api/models/Company" "Cold_Api/models/Device" "Cold_Api/models/System" "Cold_Api/models/Warning" "encoding/json" "fmt" "github.com/beego/beego/v2/adapter/orm" "github.com/beego/beego/v2/core/logs" beego "github.com/beego/beego/v2/server/web" uuid "github.com/satori/go.uuid" "github.com/shopspring/decimal" "github.com/xuri/excelize/v2" "math" "os" "strconv" "strings" "time" ) type UserController struct { beego.Controller Admin_r Account.Admin // 登陆的用户 T_pid int // 公司id } func (c *UserController) Prepare() { GetCookie := c.Ctx.GetCookie("User_tokey") GetString := c.GetString("User_tokey") User_tokey := GetCookie if len(User_tokey) == 0 { User_tokey = GetString } if Account.Admin_r == nil { return } c.Admin_r = *Account.Admin_r T_pid := c.Admin_r.T_pid EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey) if EntryPid > 0 { T_pid = EntryPid } c.T_pid = T_pid } // 获取微信二维码 ( 扫描后 ,Nats 自动绑定在 Admin->T_wx ) func (c *UserController) User_WxQRCode() { T_name := c.GetString("T_name") WxQRCode_str, _ := NatsServer.Wx_GenerateQRCode("-|" + lib.AesEncryptCBC(c.Admin_r.T_uuid+"/"+T_name, "0123456789012345") + "|- @宝智达冷链 微信公众号通知") WxQRCode_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + WxQRCode_str c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: WxQRCode_str} c.ServeJSON() return } // 用户绑定的公司列表 func (c *UserController) Company_Tree() { var r_jsons lib.R_JSONS T_name := c.GetString("T_name") r_jsons.Data = Account.Read_Company_Tree(c.Admin_r, T_name) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) Company_Transport_List() { var r_jsons lib.R_JSONS T_name := c.GetString("T_name") r_jsons.Data = Account.Read_Transport_Company_List(T_name) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 所有公司树 func (c *UserController) Company_List() { var r_jsons lib.R_JSONS T_name := c.GetString("T_name") r_jsons.Data = Account.Read_Company_List(T_name) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) Company_Get() { var r_jsons lib.R_JSONS Company_r, err := Account.Read_Company_ById(c.T_pid) if err != nil { logs.Warning(lib.FuncName(), "获取公司信息失败", err) } r_jsons.Data = Account.CompanyToCompany_R(Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) Company_Add() { T_mid, _ := c.GetInt("T_mid") T_name := c.GetString("T_name") T_plan := c.GetString("T_plan") T_data := c.GetString("T_data") T_v3d := c.GetString("T_v3d") T_type, _ := c.GetInt("T_type") var_ := Account.Company{ T_mid: T_mid, T_name: T_name, T_State: 1, T_plan: T_plan, T_data: T_data, T_v3d: T_v3d, T_warning: 1, T_type: T_type, } id, err := Account.Add_Company(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"} c.ServeJSON() return } if c.Admin_r.T_pids != "*" { T_pids := []int{int(id)} if err = Account.Add_Company_bind(c.Admin_r, T_pids); err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"} c.ServeJSON() return } } System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return } func (c *UserController) Company_Edit() { Id, Id_err := c.GetInt("Id") if Id_err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_name := c.GetString("T_name") T_type, _ := c.GetInt("T_type") if len(T_name) > 0 { Company_r.T_name = T_name } if T_type > 0 { Company_r.T_type = T_type } is := Account.Update_Company(Company_r, "T_name", "T_type") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改", Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 数字孪生 func (c *UserController) Company_DigitalTwin() { Id, Id_err := c.GetInt("Id") if Id_err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_plan := c.GetString("T_plan") T_data := c.GetString("T_data") T_v3d := c.GetString("T_v3d") T_warning, _ := c.GetInt("T_warning") Company_r.T_plan = T_plan Company_r.T_data = T_data Company_r.T_v3d = T_v3d Company_r.T_warning = T_warning is := Account.Update_Company(Company_r, "T_plan", "T_data", "T_v3d", "T_warning") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改数字孪生", Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 充值 func (c *UserController) Company_Pay() { Id, Id_err := c.GetInt("Id") if Id_err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_balance, _ := c.GetFloat("T_balance") T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + T_balance).Round(2).Float64() Company_r.T_money = float32(T_money64) is := Account.Update_Company(Company_r, "T_money") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"} c.ServeJSON() return } // 添加充值记录 bill := Company.CompanyBill{ T_pid: Company_r.Id, T_type: "充值", T_project: "充值", T_bill: "充值", T_charging: float32(T_balance), T_balance: float32(T_money64), } _, err = Company.Add_CompanyBill(bill) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "充值", Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 报警统计 func (c *UserController) Company_Warning() { Id, Id_err := c.GetInt("Id") if Id_err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_warning, _ := c.GetInt("T_warning") if T_warning > 0 { Company_r.T_warning = T_warning } is := Account.Update_Company(Company_r, "T_warning") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改报警统计", Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) Company_Del() { Id, Id_err := c.GetInt("Id") if Id_err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } ids := Account.ReadCompanyIds_T_path(Company_r.T_path) if cnt := Account.Read_Admin_Num_ByT_pid(ids); cnt > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"} c.ServeJSON() return } o := orm.NewOrm() o.Begin() for _, id := range ids { if is := Account.Delete_Company(o, id); !is { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } if err = Account.Delete_Company_bind_T_pids(o, id); err != nil { o.Rollback() c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } } o.Commit() System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "删除", Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 进入公司 func (c *UserController) Company_Entry() { GetCookie := c.Ctx.GetCookie("User_tokey") GetString := c.GetString("User_tokey") User_tokey := GetCookie if len(User_tokey) == 0 { User_tokey = GetString } T_pid, _ := c.GetInt("T_pid") // 操作pid company_r, err := Account.Read_Company_ById(T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } visit := false if c.Admin_r.T_pid > 0 && strings.Contains(company_r.T_path, fmt.Sprintf("/%d/", c.Admin_r.T_pid)) { visit = true } if c.Admin_r.T_pid == 0 { if c.Admin_r.T_pids == "*" { visit = true } else { pids := lib.SplitStringToIntIds(c.Admin_r.T_pids, "P") if len(pids) > 0 { list := Account.ReadCompanyIds_T_pids(pids) for _, v := range list { if T_pid == v { visit = true break } } } } } if !visit { c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"} c.ServeJSON() return } Account.Redis_Tokey_T_pid_Set(User_tokey, T_pid) System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "进入公司", T_pid) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 记账扣费 func (c *UserController) Company_Charging() { Id, Id_err := c.GetInt("Id") if Id_err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_Charging, _ := c.GetInt("T_Charging") Company_r.T_Charging = T_Charging is := Account.Update_Company(Company_r, "T_Charging") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "记账扣费", Company_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 检查 Tokey 是否过期 // 用户管理 func (c *UserController) User_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") Account.Read_Power_All_Map() r_jsons.Data, r_jsons.Num = Account.Read_Admin_List(c.T_pid, T_name, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) User_Get() { T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_R(admin_r)} c.ServeJSON() return } func (c *UserController) User_Info() { type User_ struct { User Account.Admin_R //Power Account.Power // 权限 //Menu map[string]Account.Menu_R // 菜单 //Menu []Account.Menu // 菜单 } Admin_r := User_{} Admin_r.User = Account.AdminToAdmin_R(c.Admin_r) //Admin_r.Power, _ = Account.Read_Power_ById(admin_r.T_power) //Admin_r.Menu = Account.Read_Menu_Tree(Power.T_menu, T_pid) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Admin_r} c.ServeJSON() return } func (c *UserController) User_Post() { admin_r := c.Admin_r T_name := c.GetString("T_name") T_pass := c.GetString("T_pass") T_phone := c.GetString("T_phone") T_mail := c.GetString("T_mail") T_wx := c.GetString("T_wx") if len(T_pass) < 8 && len(T_pass) > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"} c.ServeJSON() return } if len(T_name) > 0 { admin_r.T_name = T_name } if len(T_pass) >= 8 { admin_r.T_pass = T_pass } if len(T_phone) > 0 { admin_r.T_phone = T_phone } if len(T_mail) > 0 { admin_r.T_mail = T_mail } if len(T_wx) > 0 { if T_wx == "-" { T_wx = "" } admin_r.T_wx = T_wx } is := Account.Update_Admin(admin_r, "T_name", "T_pass", "T_phone", "T_mail", "T_wx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } admin_r.T_pass = "******" System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "修改个人信息", admin_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) User_Add() { T_power, _ := c.GetInt("T_power") T_name := c.GetString("T_name") T_user := c.GetString("T_user") T_pass := c.GetString("T_pass") T_phone := c.GetString("T_phone") T_mail := c.GetString("T_mail") T_wx := c.GetString("T_wx") T_uuid := uuid.NewV4().String() var_ := Account.Admin{ T_uuid: T_uuid, T_pid: c.T_pid, T_name: T_name, T_user: T_user, T_phone: T_phone, T_mail: T_mail, T_wx: T_wx, T_pass: T_pass, T_power: T_power, T_State: 1, } if len(T_pass) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"} c.ServeJSON() return } _, err := Account.Read_Admin_T_user(T_user) if err == nil { c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"} c.ServeJSON() return } _, err = Account.Add_Admin(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"} c.ServeJSON() return } var_.T_pass = "******" System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) User_Edit() { T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } T_power, T_power_err := c.GetInt("T_power") T_name := c.GetString("T_name") T_pass := c.GetString("T_pass") T_phone := c.GetString("T_phone") T_mail := c.GetString("T_mail") if T_power_err == nil { r.T_power = T_power } if len(T_pass) < 8 && len(T_pass) > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"} c.ServeJSON() return } if len(T_name) > 0 { r.T_name = T_name } if len(T_pass) >= 8 { r.T_pass = T_pass } if len(T_phone) > 0 { r.T_phone = T_phone } if len(T_mail) > 0 { r.T_mail = T_mail } is := Account.Update_Admin(r, "T_power", "T_pid", "T_name", "T_pass", "T_phone", "T_mail") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } if len(T_name) > 0 { if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, T_name); !is { logs.Info("修改用户 => 修改用户关联报警通知失败", T_uuid, T_name) } } r.T_pass = "******" System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "编辑", r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) User_Del() { T_uuid := c.GetString("T_uuid") if len(T_uuid) <= 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } if is := Account.Delete_Admin(admin_r_lod); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is { logs.Info("删除用户 => 删除用户关联报警通知失败", T_uuid) } System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "删除", T_uuid) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 管理员 func (c *UserController) User_Home() { // 报警标识 //设备管理-设备报警 callthepolice //公司管理-设备报警 companyCallthepolice //T_warning_sign := c.GetString("T_warning_sign") power, err := Account.Read_Power_ById(c.Admin_r.T_power) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"} c.ServeJSON() return } ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid) var T_warning_sign string for _, v := range ret.MenuPermission { if v == Warning.CompanyCallthepolice { T_warning_sign = Warning.CompanyCallthepolice break } if v == Warning.Callthepolice { T_warning_sign = Warning.Callthepolice } } // 验证登录 admin_r := c.Admin_r type NoticeRecord struct { Sms int // 短信 Phone int // 电话 } type Info struct { T_name string // 姓名 T_pids string // 内部用户绑定的公司id T_pid int // 公司id T_Days int // 服务天数 Admin []Account.Admin_ // 专属管理员 WarningSign string // 报警标识 WarningNum int64 // 报警数量 UntreatedWarningNum int64 // 未处理报警数量 NewsNum int64 // 消息数量 WorkOrderNum int64 // 工单数量 WarningRate float32 // 设备报警率 UntreatedWorkOrderNum int64 // 未处理工单数量 Balance float32 // 余额 NoticeRecord NoticeRecord // 通知记录 CompanyKey string CompanyName string } var info Info info.T_name = admin_r.T_name info.T_pid = admin_r.T_pid var Company_r Account.Company if c.T_pid > 0 { Company_r, err = Account.Read_Company_ById(c.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } info.CompanyKey = Company_r.T_key info.CompanyName = Company_r.T_name } if admin_r.T_pid > 0 { info.WarningSign = T_warning_sign bindSN, err := Account.Read_UserDevice_List(c.Admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } power, _ := Account.Read_Power_ById(admin_r.T_power) company, _ := Account.Read_Company_ById(admin_r.T_pid) info.T_Days = lib.GetDiffDays(time.Now(), company.CreateTime) AdminList := Account.Read_Admin_List_T_pids(admin_r.T_pid) var Admin_ []Account.Admin_ for _, v := range AdminList { Admin_ = append(Admin_, Account.Admin_RToAdmin_(v)) } info.Admin = Admin_ T_pids := []int{} // 拥有公司管理-设备报警权限 统计公司及其所有子公司的今日报警数量、未处理报警数量、我的消息数量 if T_warning_sign == Warning.CompanyCallthepolice { T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path) info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, power.T_warning, true) info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 3, bindSN, power.T_warning, false) info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, "T1003|T1002|", false) } // 拥有设备管理-设备报警权限 统计本公司今日报警数量、未处理报警数量、我的消息数量 if T_warning_sign == Warning.Callthepolice { info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, power.T_warning, true) info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 3, bindSN, power.T_warning, false) info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, "T1003|T1002|", false) } info.Balance = company.T_money info.NoticeRecord.Sms = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 1) info.NoticeRecord.Phone = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 2) } if admin_r.T_pid == 0 { if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" { info.T_pids = admin_r.T_pids info.WarningRate = Warning.Read_WarningRate_Yesterday(admin_r.T_uuid) } info.WorkOrderNum, info.UntreatedWorkOrderNum = NatsServer.Read_WorkOrderT_State_Count(admin_r.T_pids) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info} c.ServeJSON() return } // 内部用户管理 func (c *UserController) Admin_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") if c.Admin_r.T_pid > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"} c.ServeJSON() return } T_bind_company, _ := c.GetInt("T_bind_company") // 管理员绑定公司 0-全部 1-绑定公司 Account.Read_Power_All_Map() userList, num := Account.Read_Admin_List(0, T_name, page, page_z) r_jsons.Data, r_jsons.Num = userList, num // 过滤已绑定公司列表 if T_bind_company == 1 { var adminList []Account.Admin_R for _, v := range userList { if len(v.T_pids) == 0 || v.T_pids == "*" { continue } adminList = append(adminList, v) } r_jsons.Data = adminList } r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) Admin_Get() { T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_R(admin_r)} c.ServeJSON() return } func (c *UserController) Admin_Add() { T_power, _ := c.GetInt("T_power") T_name := c.GetString("T_name") T_user := c.GetString("T_user") T_pass := c.GetString("T_pass") T_phone := c.GetString("T_phone") T_mail := c.GetString("T_mail") T_wx := c.GetString("T_wx") var_ := Account.Admin{ T_uuid: uuid.NewV4().String(), T_pid: 0, T_name: T_name, T_user: T_user, T_phone: T_phone, T_mail: T_mail, T_wx: T_wx, T_pass: T_pass, T_power: T_power, T_State: 1, } if len(T_pass) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"} c.ServeJSON() return } _, err := Account.Read_Admin_T_user(T_user) if err == nil { c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"} c.ServeJSON() return } _, err = Account.Add_Admin(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"} c.ServeJSON() return } var_.T_pass = "******" System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) Admin_Edit() { T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } T_power, T_power_err := c.GetInt("T_power") T_name := c.GetString("T_name") T_pass := c.GetString("T_pass") T_phone := c.GetString("T_phone") T_mail := c.GetString("T_mail") T_wx := c.GetString("T_wx") if T_power_err == nil { r.T_power = T_power } if len(T_pass) < 8 && len(T_pass) > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"} c.ServeJSON() return } if len(T_name) > 0 { r.T_name = T_name } if len(T_pass) >= 8 { r.T_pass = T_pass } if len(T_phone) > 0 { r.T_phone = T_phone } if len(T_mail) > 0 { r.T_mail = T_mail } if len(T_wx) > 0 { r.T_wx = T_wx } is := Account.Update_Admin(r, "T_power", "T_name", "T_pass", "T_phone", "T_mail", "T_wx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } if len(T_name) > 0 { if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, "内管-"+T_name); !is { logs.Info("修改内部用户 => 修改内部用户关联报警通知失败", T_uuid, T_name) } } r.T_pass = "********" System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "修改", r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) Admin_Del() { T_uuid := c.GetString("T_uuid") if len(T_uuid) <= 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } if admin_r_lod.Id == 1 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"} c.ServeJSON() return } if is := Account.Delete_Admin(admin_r_lod); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is { logs.Info("删除内部用户 => 删除内部用户关联报警通知失败", T_uuid) } System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "删除", T_uuid) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 报警通知的用户列表 func (c *UserController) User_Notice_List() { T_name := c.GetString("T_name") type R_JSONS struct { //必须的大写开头 AdminList []Account.Admin_R UserList []Account.Admin_R } var AdminList, UserList []Account.Admin_R var r_jsons R_JSONS // 操作pid company_r, err := Account.Read_Company_ById(c.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } T_pids := []int{} for _, v := range strings.Split(strings.Trim(company_r.T_path, "/"), "/") { pid, _ := strconv.Atoi(v) T_pids = append(T_pids, pid) } // 绑定公司id的内部用户列表 AdminList = Account.Read_Admin_List_T_pids(c.T_pid) // 绑定公司id的公司用户列表 //r_jsons.UserList = Account.Read_Admin_List_T_pid(c.T_pid) // 绑定上级公司用户列表 UserList = Account.Read_Parent_Admin_List(T_pids) if len(T_name) > 0 { for _, v := range AdminList { if strings.Contains(v.T_name, T_name) { r_jsons.AdminList = append(r_jsons.AdminList, v) } } for _, v := range UserList { if strings.Contains(v.T_name, T_name) { r_jsons.UserList = append(r_jsons.UserList, v) } } } else { r_jsons.AdminList = AdminList r_jsons.UserList = UserList } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 管理员绑定公司 func (c *UserController) AdminCompanyBind_List() { var r_jsons lib.R_JSONS T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } r_jsons.Data = Account.Read_Company_List_ByT_pids(r.T_pids) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) AdminCompanyBind_Add() { T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } T_pid, _ := c.GetInt("T_pid") if T_pid == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"} c.ServeJSON() return } r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } if r.T_pids == "*" { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } Company_r, err := Account.Read_Company_ById(T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"} c.ServeJSON() return } T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path) if err = Account.Add_Company_bind(r, T_pids); err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"} c.ServeJSON() return } System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定添加", T_uuid+"-"+strconv.Itoa(T_pid)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) AdminCompanyBind_Del() { T_uuid := c.GetString("T_uuid") if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } T_pid, _ := c.GetInt("T_pid") if T_pid == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"} c.ServeJSON() return } r, err := Account.Read_Admin_ByUuid(T_uuid) Company_r, err := Account.Read_Company_ById(T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"} c.ServeJSON() return } T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path) if len(T_pids) == 0 { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } if err = Account.Delete_Company_bind(r, T_pids); err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"} c.ServeJSON() return } if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is { logs.Info("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid) } System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除", T_uuid+"-"+strconv.Itoa(T_pid)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 权限管理 func (c *UserController) Power_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } Name := c.GetString("T_name") r_jsons.Data, r_jsons.Num = Account.Read_Power_List(Name, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 添加用户 权限选择列表 func (c *UserController) Power_List_All() { r, err := Account.Read_Power_ById(c.Admin_r.T_power) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } var r_jsons lib.R_JSONS r_jsons.Data = Account.Read_Power_List_ALL_User(r.T_select) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) Power_Get() { type Data struct { Power Account.Power_ Menu []Account.Menu Menu_checked []int Select []Account.Power_R Warning []Warning.WarningType_R } var data Data id, id_err := c.GetInt("T_id") if id_err != nil || id == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } power, err := Account.Read_Power_ById(id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } data.Power = Account.PowerToPower_(power) data.Menu, data.Menu_checked = Account.Read_Menu_List_All() if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"} c.ServeJSON() return } if power.T_menu != "*" { data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M") } if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"} c.ServeJSON() return } data.Select = Account.Read_Power_List_ALL_T_select(power.T_select) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_select Err!"} c.ServeJSON() return } data.Warning = Warning.Read_WarningType_All_Power_T_Warning(power.T_warning) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_warning Err!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data} c.ServeJSON() return } func (c *UserController) Power_Add() { T_name := c.GetString("T_name") T_select := c.GetString("T_select") T_warning := c.GetString("T_warning") T_menu := c.GetString("T_menu") var_ := Account.Power{ T_name: T_name, T_select: T_select, T_warning: T_warning, T_menu: T_menu, T_State: 1, } _, err := Account.Add_Power(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) Power_Edit() { Id, Id_err := c.GetInt("T_id") if Id_err != nil || Id == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } if Id == 1 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权修改!"} c.ServeJSON() return } r, err := Account.Read_Power_ById(Id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_name := c.GetString("T_name") T_select := c.GetString("T_select") T_warning := c.GetString("T_warning") T_menu := c.GetString("T_menu") if len(T_name) > 0 { r.T_name = T_name } if len(T_select) > 0 { r.T_select = T_select } if len(T_warning) > 0 { r.T_warning = T_warning } if len(T_menu) > 0 { r.T_menu = T_menu } is := Account.Update_Power(r, "T_name", "T_select", "T_warning", "T_menu") if !is { c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"} c.ServeJSON() return } System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "修改", r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) Power_Del() { id, id_err := c.GetInt("T_id") if id_err != nil || id == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } r, err := Account.Read_Power_ById(id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } if cnt := Account.Read_Admin_Num_ByT_power(id); cnt > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"} c.ServeJSON() return } is := Account.Delete_Power(r) if !is { c.Data["json"] = lib.JSONS{Code: 302, Msg: "删除失败!"} c.ServeJSON() return } System.Add_UserLogs(c.Admin_r.T_uuid, "权限管理", "删除", strconv.Itoa(id)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 菜单列表 func (c *UserController) User_Menu_List() { type R_JSONS struct { //必须的大写开头 Data interface{} Permission interface{} } var r_jsons R_JSONS power, err := Account.Read_Power_ById(c.Admin_r.T_power) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"} c.ServeJSON() return } //logs.Info("登录用户 菜单 pid 为:", c.T_pid) ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid) r_jsons.Data = ret.Menu r_jsons.Permission = ret.Permission c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 权限菜单列表 func (c *UserController) Menu_List_All() { var r_jsons lib.R_JSONS r_jsons.Data, _ = Account.Read_Menu_List_All() c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) Menu_Excel() { filename := fmt.Sprintf("冷链3.0菜单") f := excelize.NewFile() Style1, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"}, }) Style2, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "left", Color: "000000", Style: 1}, {Type: "top", Color: "000000", Style: 1}, {Type: "bottom", Color: "000000", Style: 1}, {Type: "right", Color: "000000", Style: 1}, }, }) f.MergeCell("Sheet1", "A1", "G1") f.SetRowStyle("Sheet1", 1, 1, Style1) f.SetCellValue("Sheet1", "A1", "Menu") f.SetRowHeight("Sheet1", 1, 30) f.MergeCell("Sheet1", "H1", "M1") f.SetRowStyle("Sheet1", 1, 1, Style1) f.SetCellValue("Sheet1", "H1", "API") f.SetRowHeight("Sheet1", 1, 30) f.SetCellStyle("Sheet1", "A2", "G2", Style2) f.SetRowHeight("Sheet1", 2, 25) // 这里设置表头 f.SetCellValue("Sheet1", "A2", "ID") f.SetCellValue("Sheet1", "B2", "t_mid") f.SetCellValue("Sheet1", "C2", "t_name") f.SetCellValue("Sheet1", "D2", "t_permission") f.SetCellValue("Sheet1", "E2", "t_sort") f.SetCellValue("Sheet1", "F2", "t_pid") f.SetCellValue("Sheet1", "G2", "t_icon") f.SetCellValue("Sheet1", "H2", "ID") f.SetCellValue("Sheet1", "I2", "t__menu__id") f.SetCellValue("Sheet1", "J2", "t_name") f.SetCellValue("Sheet1", "K2", "t_uri") f.SetCellValue("Sheet1", "L2", "t_method") f.SetCellValue("Sheet1", "M2", "t_enable") // 设置列宽 f.SetColWidth("Sheet1", "A", "M", 15) line := 2 menu := Account.Read_Menu_All() for _, m := range menu { Menu_Call(f, m, line) } Style11, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Size: 10, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "top", Color: "000000", Style: 1}, }, }) Style22, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Size: 10, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "left", Color: "000000", Style: 1}, }, }) Style33, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Size: 10, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "right", Color: "000000", Style: 1}, }, }) Style44, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Size: 10, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "bottom", Color: "000000", Style: 1}, }, }) //// 循环写入数据 for _, v := range menu { line1 := line line = Menu_Call(f, v, line) f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line1), fmt.Sprintf("M%d", line1), Style11) f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("A%d", line), Style22) f.SetCellStyle("Sheet1", fmt.Sprintf("M%d", line), fmt.Sprintf("M%d", line), Style33) f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("M%d", line), Style44) } timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0)) // 保存文件 if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { fmt.Println(err) } var url string ////// 上传 OSS //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") //if !is { // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"} // c.ServeJSON() // return //} ////删除目录 //err = os.Remove("ofile/" + timeStr + ".xlsx") //if err != nil { // fmt.Println(err) //} c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return } func Menu_Call(f *excelize.File, v Account.Menu, line int) int { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.Id) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_mid) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_name) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_permission) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_sort) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_pid) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_icon) apiList := Account.Read_API_List_ByMenuId(v.Id) for _, api := range apiList { f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), api.Id) f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), api.T_Menu_Id) f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), api.T_name) f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), api.T_uri) f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), api.T_method) f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), api.T_enable) line++ } return line } // 流量池 func (c *UserController) Flow_Pool() { var r_jsons lib.R_JSONS r_jsons.Data, _ = lib.Yidong_group_data_margin() c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 账单下载 func (c *UserController) CompanyBill_Excel() { T_month := c.GetString("T_month") _, err := time.Parse("2006-01", T_month) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"} c.ServeJSON() return } year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1] company, _ := Account.Read_Company_ById(c.Admin_r.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } filename := fmt.Sprintf("%s%s年%s月账单", company.T_name, year, month) f := excelize.NewFile() Style1, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"}, }) Style2, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "left", Color: "000000", Style: 1}, {Type: "top", Color: "000000", Style: 1}, {Type: "bottom", Color: "000000", Style: 1}, {Type: "right", Color: "000000", Style: 1}, }, }) f.MergeCell("Sheet1", "A1", "G1") f.SetRowStyle("Sheet1", 1, 1, Style1) f.SetCellValue("Sheet1", "A1", filename) f.SetRowHeight("Sheet1", 1, 30) f.SetCellStyle("Sheet1", "A2", "G2", Style2) f.SetRowHeight("Sheet1", 2, 25) // 这里设置表头 f.SetCellValue("Sheet1", "A2", "编号") f.SetCellValue("Sheet1", "B2", "项目") f.SetCellValue("Sheet1", "C2", "说明") f.SetCellValue("Sheet1", "D2", "扣费/充值") f.SetCellValue("Sheet1", "E2", "金额(元)") f.SetCellValue("Sheet1", "F2", "余额(元)") f.SetCellValue("Sheet1", "G2", "时间") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 10) f.SetColWidth("Sheet1", "B", "B", 50) f.SetColWidth("Sheet1", "C", "C", 15) f.SetColWidth("Sheet1", "D", "D", 12) f.SetColWidth("Sheet1", "E", "E", 15) f.SetColWidth("Sheet1", "F", "F", 15) f.SetColWidth("Sheet1", "G", "G", 20) line := 2 CompanyBill_List, err := Company.Read_CompanyBill_List(c.Admin_r.T_pid, T_month) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } // 循环写入数据 for i, v := range CompanyBill_List { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_project) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_bill) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_type) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_charging) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_balance) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CreateTime.Format("2006-01-02")) } Style4, _ := f.NewStyle( &excelize.Style{ Font: &excelize.Font{Size: 12, Family: "宋体"}, Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: []excelize.Border{ {Type: "left", Color: "000000", Style: 1}, {Type: "top", Color: "000000", Style: 1}, {Type: "bottom", Color: "000000", Style: 1}, {Type: "right", Color: "000000", Style: 1}, }, }) f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("G%d", line), Style4) timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0)) // 保存文件 if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { fmt.Println(err) } var url string //// 上传 OSS url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"} c.ServeJSON() return } //删除目录 err = os.Remove("ofile/" + timeStr + ".xlsx") if err != nil { fmt.Println(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return } // 获取微信支付二维码 func (c *UserController) WxPay_QRCode() { if c.Admin_r.T_pid == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "非公司用户不能充值"} c.ServeJSON() return } Total, _ := c.GetFloat("Total") Title := "冷链3.0-" + c.Admin_r.T_user res, err := lib.PayTransactionNative(Title, Total) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()} c.ServeJSON() return } if res.Code != 200 { c.Data["json"] = lib.JSONS{Code: 202, Msg: res.Message} c.ServeJSON() return } var_ := Company.CompanyPayOrder{ T_pid: c.Admin_r.T_pid, T_type: "微信", T_title: Title, T_total: Total, T_order_no: res.OrderNo, T_remark: c.Admin_r.T_uuid, } _, err = Company.Add_CompanyPayOrder(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加订单失败!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: res} c.ServeJSON() return } // 微信支付回调 func (c *UserController) WxPay_Notify() { type RequestBody struct { TradeNo string Status string } type JSON struct { Code int Message string } buf := make([]byte, 1024) n, _ := c.Ctx.Request.Body.Read(buf) logs.Info("Body================ ", string(buf[0:n])) //解密 adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey)) //adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey)) var body RequestBody logs.Info("data================ ", string(adc_str)) err := json.Unmarshal(adc_str, &body) if err != nil { c.Data["json"] = JSON{Code: 202, Message: "json.Unmarshal is err:" + err.Error()} c.ServeJSON() } payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(body.TradeNo) if err != nil { c.Data["json"] = JSON{Code: 202, Message: "查询失败!"} c.ServeJSON() return } //交易状态 //SUCCESS:支付成功 //REFUND:转入退款 //NOTPAY:未支付 //CLOSED:已关闭 //REVOKED:已撤销(付款码支付) //USERPAYING:用户支付中(付款码支付) //PAYERROR:支付失败(其他原因,如银行返回失败) payOrder.T_status = body.Status err = Company.Update_CompanyPayOrder(payOrder, "T_status") if err != nil { c.Data["json"] = JSON{Code: 202, Message: "更新状态失败!"} c.ServeJSON() return } // 添加充值记录 // 余额加上充值金额 if body.Status == "SUCCESS" { Company_r, err := Account.Read_Company_ById(payOrder.T_pid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + payOrder.T_total).Round(2).Float64() Company_r.T_money = float32(T_money64) is := Account.Update_Company(Company_r, "T_money") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"} c.ServeJSON() return } } c.Data["json"] = JSON{Code: 200, Message: "成功"} c.ServeJSON() return } // 获取微信支付订单状态 func (c *UserController) WxPay_GetOrderState() { OrderNo := c.GetString("OrderNo") payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(OrderNo) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功", Data: payOrder} c.ServeJSON() return } // 设备分类绑定 func (c *UserController) UserDeviceBind_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_uuid := c.GetString("T_uuid") admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } bindSN, err := Account.Read_UserDevice_List(admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } if len(bindSN) == 0 { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } r_jsons.Data, r_jsons.Num = Device.Read_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, "", "", -1, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) UserDeviceUnbind_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_name := c.GetString("T_name") T_uuid := c.GetString("T_uuid") admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } bindSN, err := Account.Read_UserDevice_List(admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } r_jsons.Data, r_jsons.Num = Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *UserController) UserDeviceBind_Add() { T_sn := c.GetString("T_sn") T_uuid := c.GetString("T_uuid") T_oneHey, _ := c.GetInt("T_oneKey") // 一键绑定 if T_oneHey == 1 { admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } bindSN, err := Account.Read_UserDevice_List(admin_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"} c.ServeJSON() return } Unbind_Device_List, _ := Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, "", 0, 9999) var UserDeviceList []Account.UserDevice for _, r := range Unbind_Device_List { UserDeviceList = append(UserDeviceList, Account.UserDevice{T_sn: r.T_sn, T_uuid: T_uuid}) } if _, err = Account.Add_UserDevice_Multi(T_uuid, UserDeviceList); err != nil { c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"} c.ServeJSON() return } System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "一键绑定主机操作", T_uuid+"-"+T_sn) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } if len(T_uuid) == 0 || len(T_sn) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"} c.ServeJSON() return } if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } device, err := Device.Read_Device_ByT_sn(T_sn) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } if admin_r.T_pid != device.T_pid { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"} c.ServeJSON() return } userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn) if userdievice.Id > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"} c.ServeJSON() return } if _, err = Account.Add_UserDevice(Account.UserDevice{T_sn: T_sn, T_uuid: T_uuid}); err != nil { c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"} c.ServeJSON() return } System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "绑定主机操作", T_uuid+"-"+T_sn) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *UserController) UserDeviceBind_Del() { T_sn := c.GetString("T_sn") T_uuid := c.GetString("T_uuid") if len(T_uuid) == 0 || len(T_sn) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"} c.ServeJSON() return } if len(T_uuid) < 8 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } device, err := Device.Read_Device_ByT_sn(T_sn) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"} c.ServeJSON() return } admin_r, err := Account.Read_Admin_ByUuid(T_uuid) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"} c.ServeJSON() return } if admin_r.T_pid != device.T_pid { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"} c.ServeJSON() return } userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn) if err != nil { if err.Error() == orm.ErrNoRows.Error() { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑失败!"} c.ServeJSON() return } if err = Account.Delete_UserDevice(Account.UserDevice{Id: userdievice.Id, T_sn: T_sn, T_uuid: T_uuid}); err != nil { c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"} c.ServeJSON() return } System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "解绑主机操作", T_uuid+"-"+T_sn) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return }