示例#1
0
//时间戳+随机数+用户ID
func genOauthToken(uid int) (oauthToken string) {
	now := time.Now()
	rand.Seed(now.UnixNano())
	oauthToken = fmt.Sprintf("%v#%v#%v", now.Unix(), uid, rand.Int())

	if len(oauthToken) < 32 {
		oauthToken += pgpub.RepeatChar("0", "", 32-len(oauthToken))
	} else if len(oauthToken) > 32 {
		oauthToken = oauthToken[0:32]
	}

	return oauthToken
}
示例#2
0
func main() {
	now := time.Now()
	oauthToken := strconv.FormatInt(now.Unix(), 10)

	rand.Seed(now.UnixNano())
	oauthToken += strconv.Itoa(rand.Int())
	oauthToken += strconv.Itoa(888)

	if len(oauthToken) < 32 {
		oauthToken += pgpub.RepeatChar("0", "", 32-len(oauthToken))
	} else if len(oauthToken) > 32 {
		oauthToken = oauthToken[0:32]
	}
	fmt.Println(oauthToken)
}
示例#3
0
func dbCreate(stru interface{}, include bool, flds ...string) error {
	//ensure struct
	struVal := reflect.ValueOf(stru)
	if struVal.Kind() == reflect.Ptr {
		struVal = struVal.Elem()
	}
	if struVal.Kind() != reflect.Struct {
		return errors.New("DBCreate arg stru isn't struct!")
	}
	//get effective fields
	var effFlds []string
	if include && len(flds) == 0 {
		effFlds = getStruFldsExceptAutoPK(stru)

	} else if include && len(flds) > 0 {
		effFlds = flds

	} else if !include && len(flds) > 0 {
		dbCols := make([]string, 0)
		struFlds := getStruFldsExceptAutoPK(stru)
		for _, struFld := range struFlds {
			if pgpub.SearchStringArray(flds, struFld) == -1 {
				dbCols = append(dbCols, convertFldName2ColName(struFld))
			}
		}

	} else if !include && len(flds) == 0 {
		effFlds = getStruFldsExceptAutoPK(stru)

	}
	//assemble insert sql
	insertSql := bytes.NewBufferString("insert into ")
	insertSql.WriteString(getTableFromStruct(stru))
	insertSql.WriteString("(")
	for i, effFld := range effFlds {
		insertSql.WriteString(convertFldName2ColName(effFld))
		if i < len(effFlds)-1 {
			insertSql.WriteString(",")
		}
	}
	insertSql.WriteString(") values(")
	insertSql.WriteString(pgpub.RepeatChar("?", ",", len(effFlds)))
	insertSql.WriteString(")")
	//assemble sql params
	args := make([]interface{}, len(effFlds))
	struType := struVal.Type()
	for i, effFld := range effFlds {
		_, exsits := struType.FieldByName(effFld)
		if !exsits {
			err := fmt.Errorf("dbCreate struct field [%v] not exists!", effFld)
			seelog.Error(err)
			return err
		}

		fldVal := struVal.FieldByName(effFld)
		if timeVal, ok := fldVal.Interface().(time.Time); ok {
			//FIXME: more type convert
			args[i] = timeVal.Unix()
		} else {
			args[i] = fldVal.Interface()
		}
	}
	//db operate
	err := db.Ping()
	if err != nil {
		seelog.Errorf("dbcreate:insert %v ping mysql %v", stru, err)
		return err
	}

	execSql := insertSql.String()
	_, err = db.Exec(execSql, args...)
	if err != nil {
		seelog.Errorf("dbcreate: insert %v exec sql %v %v", stru, execSql, err)
	}

	return err
}