go-zero/tools/goctl/api/dartgen/genvars.go
2020-08-08 16:40:10 +08:00

67 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dartgen
import (
"io/ioutil"
"os"
"github.com/tal-tech/go-zero/core/logx"
)
func genVars(dir string) error {
e := os.MkdirAll(dir, 0755)
if e != nil {
logx.Error(e)
return e
}
if !fileExists(dir + "vars.dart") {
e = ioutil.WriteFile(dir+"vars.dart", []byte(`const serverHost='demo-crm.xiaoheiban.cn';`), 0644)
if e != nil {
logx.Error(e)
return e
}
}
if !fileExists(dir + "kv.dart") {
e = ioutil.WriteFile(dir+"kv.dart", []byte(`import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../data/tokens.dart';
/// 保存tokens到本地
///
/// 传入null则删除本地tokens
/// 返回true设置成功 false设置失败
Future<bool> setTokens(Tokens tokens) async {
var sp = await SharedPreferences.getInstance();
if (tokens == null) {
sp.remove('tokens');
return true;
}
return await sp.setString('tokens', jsonEncode(tokens.toJson()));
}
/// 获取本地存储的tokens
///
/// 如果没有则返回null
Future<Tokens> getTokens() async {
try {
var sp = await SharedPreferences.getInstance();
var str = sp.getString('tokens');
if (str.isEmpty) {
return null;
}
return Tokens.fromJson(jsonDecode(str));
} catch (e) {
print(e);
return null;
}
}
`), 0644)
if e != nil {
logx.Error(e)
return e
}
}
return nil
}