134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package dbop
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/des"
|
|
"crypto/md5"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
|
|
"github.com/shockliu/logger"
|
|
)
|
|
|
|
const (
|
|
codekey = "HY&XC&MW"
|
|
)
|
|
|
|
var (
|
|
uOpenID map[int64]string
|
|
)
|
|
|
|
func init() {
|
|
uOpenID = make(map[int64]string)
|
|
}
|
|
|
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
|
padding := blockSize - len(ciphertext)%blockSize
|
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
return append(ciphertext, padtext...)
|
|
}
|
|
|
|
// ECB+PKCS5加密
|
|
func EncryptDES_ECB(data []byte) []byte {
|
|
block, err := des.NewCipher([]byte(codekey))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
bs := block.BlockSize()
|
|
//对明文数据进行补码
|
|
data = PKCS5Padding(data, bs)
|
|
out := make([]byte, len(data))
|
|
dst := out
|
|
for len(data) > 0 {
|
|
//对明文按照blocksize进行分块加密
|
|
//必要时可以使用go关键字进行并行加密
|
|
block.Encrypt(dst, data[:bs])
|
|
data = data[bs:]
|
|
dst = dst[bs:]
|
|
}
|
|
// return fmt.Sprintf("%X", out)
|
|
return out
|
|
}
|
|
|
|
// 测试验证发现,密码加密方式位
|
|
func EncryptPwd(code string) string {
|
|
enc := base64.StdEncoding.EncodeToString(EncryptDES_ECB([]byte(code)))
|
|
h := md5.New()
|
|
h.Write([]byte(enc))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
func SavePasswd(user int, passwd string) bool {
|
|
_, err := MDb.Exec("update aos_user set password=? where id=?;", EncryptPwd(passwd), user)
|
|
return err == nil
|
|
}
|
|
|
|
func PasswdAuth(user int, passwd string) bool {
|
|
var encPwd string
|
|
err := MDb.QueryRow("select password from aos_user where id=?;", user).Scan(&encPwd)
|
|
if err != nil {
|
|
logger.Debugf("获取用户[%d]密码失败%s\n", user, err)
|
|
return false
|
|
}
|
|
return EncryptPwd(passwd) == encPwd
|
|
}
|
|
|
|
func AppUserAuth(user, passwd string) (bool, string, int, error) {
|
|
var encPwd, name string
|
|
var id int
|
|
err := MDb.QueryRow("select password,id,name from aos_user where account=?;", user).Scan(&encPwd, &id, &name)
|
|
if err != nil {
|
|
return false, "", 0, err
|
|
}
|
|
// 密码md5计算后比较
|
|
//enc := EncryptPwd(passwd)
|
|
//logger.Debugf("[%s]->[%s]===[%s]\n", passwd, enc, encPwd)
|
|
if EncryptPwd(passwd) == encPwd {
|
|
return true, name, id, nil
|
|
}
|
|
return false, "", 0, errors.New("账号密码鉴权失败")
|
|
}
|
|
|
|
func ExistWxUser(user string) bool {
|
|
if len(user) == 0 {
|
|
return false
|
|
}
|
|
var cnt int
|
|
err := MDb.QueryRow("select count(1) from wxmp_user where openId=? and statu=1 ;", user).Scan(&cnt)
|
|
return (err == nil && cnt > 0)
|
|
}
|
|
|
|
func GetOpenId(id int64) string {
|
|
if openID, ok := uOpenID[id]; ok {
|
|
return openID
|
|
} else {
|
|
err := MDb.QueryRow("select openId from user_relation where channel=3 and id = ?;", id).Scan(&openID)
|
|
if err != nil {
|
|
logger.Debugf("查询用户商城openID失败%s\n", err)
|
|
} else {
|
|
uOpenID[id] = openID // 缓存
|
|
}
|
|
return openID
|
|
}
|
|
}
|
|
|
|
func GetMaintainer(id int) (user []int) {
|
|
rows, err := MDb.Query("select d.id from maintenance_user_place a RIGHT JOIN dqk_client b on a.place_id=b.location LEFT JOIN aos_user c on a.user_id=c.id left join dqk_user d on c.account=d.name where b.id=?;", id)
|
|
if err != nil {
|
|
logger.Debugf("查询设备[%d]维护人员失败%s\n", id, err)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
var uid int
|
|
for rows.Next() {
|
|
err = rows.Scan(&uid)
|
|
if err != nil {
|
|
logger.Warnf("获取设备%d维护人员失败%s\n", id, err)
|
|
continue
|
|
}
|
|
user = append(user, uid)
|
|
}
|
|
return
|
|
}
|