Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

142 linhas
3.0 KiB

há 4 anos
package common
import (
"bytes"
"errors"
"fmt"
"github.com/CodyGuo/win"
"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode"
goQrcode "github.com/skip2/go-qrcode"
há 4 anos
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"image"
há 4 anos
"io/ioutil"
"log"
há 4 anos
"math/rand"
"os"
"runtime"
"strconv"
"time"
)
func Rand(min, max int) int {
if min > max {
panic("min: min cannot be greater than max")
}
if int31 := 1<<31 - 1; max > int31 {
panic("max: max can not be greater than " + strconv.Itoa(int31))
}
if min == max {
return min
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max+1-min) + min
}
func GbkToUtf8(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
func Utf8ToGbk(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
func NewRandStr(length int) string {
s:=[]string{
"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x",
"y", "z", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z",
}
str:=""
for i:=1;i<=length;i++ {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
str+=s[r.Intn(len(s)-1)]
}
return str
}
func Substr(s string,start,end int) string {
strRune:=[]rune(s)
if start==-1 {
return string(strRune[:end])
}
if end==-1 {
return string(strRune[start:])
}
return string(strRune[start:end])
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func OpenImage(qrPath string) {
if "windows" == runtime.GOOS { // Windowsϵͳ
cmd := "cmd /c rundll32.exe C:\\Windows\\System32\\shimgvw.dll,ImageView_Fullscreen " + qrPath
if err := ExecRun(cmd); err != nil {
log.Println(cmd)
log.Fatal(err)
há 4 anos
}
} else { // ��Windowsϵͳ(Linux��)��������̨
//��������
file, _ := os.Open(qrPath)
img, _, _ := image.Decode(file)
bmp, _ := gozxing.NewBinaryBitmapFromImage(img)
qrReader := qrcode.NewQRCodeReader()
res, _ := qrReader.Decode(bmp, nil)
//��������̨
qr, err := goQrcode.New(res.String(), goQrcode.High)
if err != nil {
log.Println("��ά����ȡ�ɹ���������ͼƬ�þ���APPɨ��")
}
fmt.Println(qr.ToSmallString(false))
há 4 anos
}
}
func Hour2Unix(hour string) (time.Time, error) {
return time.ParseInLocation(DateTimeFormatStr, time.Now().Format(DateFormatStr) + " " + hour, time.Local)
}
func ExecRun(cmd string) error {
lpCmdLine := win.StringToBytePtr(cmd)
ret := win.WinExec(lpCmdLine, win.SW_HIDE)
if ret <= 31 {
return errors.New(winExecError[ret])
}
return nil
}