utils.js
7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Created by DEV005 on 2017/8/31.
*/
import React, { Component } from 'react';
import { BackHandler,AppState,StyleSheet,View,Text
, NativeModules,AsyncStorage,NavigationActions,
ActivityIndicator,TouchableOpacity,Image,Touch,Platform,Dimensions
} from 'react-native';
import Toast from 'react-native-root-toast';
import AppService from "../service/AppService";
/**
* 冒一个时间比较短的Toast
* @param content
*/
export const xnToast = (content) => {
if (global.toast !== undefined) {
Toast.hide(toast);
};
global.toast = Toast.show(content.toString(), {
duration: Toast.durations.LONG,
position: Toast.positions.CENTER,
shadow: true,
animation: true,
hideOnPress: true,
delay: 0
});
};
export const formatStringWithHtml = (originString) => {
if (originString === undefined) {
return '';
}
const newString = originString
.replace(/ /g, ' ')
.replace(/"/g, '"')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
return newString;
};
// 获取主题色
export function getHomeColor(){
if(global.homeColor == undefined){
// #8A0886 紫色测试
global.homeColor = "#3399ff";
return String(global.homeColor);
}else{
return String(global.homeColor);
}
}
// 获取app传入的配置参数,这个方法只需要调用一次
export async function getConfigTheme(){
try {
var result= await NativeModules.system.configTheme();
global.isNew = true;
global.role = (JSON.parse(result).role ||"" ).toString();
global.homeColor = (JSON.parse(result).themeColor ||"#3399ff").toString();
global.weChatBySmallProgramAppId = (JSON.parse(result).WeChatBySmallProgramAppId ||"" ).toString();
if(Platform.OS === 'ios'){
global.showWchatShare = JSON.parse(result).showWchatShare || false;
}else{
global.showWchatShare = JSON.parse(result).showWchatShare || false;
}
// 是否是导航进入
global.navigateMode = JSON.parse(result).navigateMode || false;
if(global.navigateMode === 'true' || global.navigateMode == true){
global.navigateMode = true;
}else{
global.navigateMode = false;
}
// app分支,默认配置为测试环境
global.appTarget = (JSON.parse(result).appTarget ||"xntalkTest" ).toString();
}catch(e){
global.isNew = false;
// #8A0886 紫色测试
global.homeColor = "#3399ff";
global.showWchatShare = false;
global.navigateMode = false;
}finally{
console.log('getConfigTheme 完成!!!');
console.log(global.appTarget);
console.log('-----------------------');
console.log(global);
}
}
export function isIphoneX() {
let dimen = Dimensions.get('window');
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTVOS &&
(dimen.height === 812 || dimen.width === 812)
);
}
export function isIphoneMax() {
let dimen = Dimensions.get('window');
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTVOS &&
(dimen.height > 812 || dimen.width > 812)
);
}
export function getHeaderHeight() {
if( Platform.OS === 'android'){
return 48;
}else if(Platform.OS === 'ios') {
if(isIphoneX()){
return 88;
}else {
return 64;
}
}
}
export function xnBorderWidth() {
if(isIphoneX()){
return 1;
}else if(isIphoneMax()){
return 1;
} else {
return 0.5;
}
}
export function getHeaderPadding() {
if( Platform.OS === 'android'){
return 0;
}else if(Platform.OS === 'ios') {
if(isIphoneX()){
return 44;
}else {
return 20;
}
}
}
export function getFooterBottom() {
if( Platform.OS === 'android'){
return 0;
}else if(Platform.OS === 'ios') {
if(isIphoneX()){
return 0;
}else if(isIphoneMax()){
return 34;
}else{
return 0;
}
}
}
export function toPersonalPageWithUserId(userId){
if(!!!userId){
xnToast('无法与该用户私信');
}else if(userId == global.userId){
xnToast('不能与自己私信');
}else{
let _this = this;
let vm = {
userId: userId,
};
AppService.getUnionByUserId(vm).then(data => {
if (data.errors == null || data.errors.length > 0) {
xnToast(data.errors[0].message);
return
}
console.log(data);
if(!!data.union){
let union = data.union;
if(!!union.imId){
NativeModules.system.presentPersonal(union.imId);
}else{
xnToast('无法与该用户私信');
}
}else{
xnToast('无法与该用户私信');
}
});
}
}
//代码如下所示:
export function convertCurrency(money) {
//汉字的数字
var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
//基本单位
var cnIntRadice = new Array('', '拾', '佰', '仟');
//对应整数部分扩展单位
var cnIntUnits = new Array('', '万', '亿', '兆');
//对应小数部分单位
var cnDecUnits = new Array('角', '分', '毫', '厘');
//整数金额时后面跟的字符
var cnInteger = '整';
//整型完以后的单位
var cnIntLast = '元';
//最大处理的数字
var maxNum = 999999999999999.9999;
//金额整数部分
var integerNum;
//金额小数部分
var decimalNum;
//输出的中文金额字符串
var chineseStr = '';
//分离金额后用的数组,预定义
var parts;
if (money == '') { return ''; }
money = parseFloat(money);
if (money >= maxNum) {
//超出最大处理数字
return '';
}
if (money == 0) {
chineseStr = cnNums[0] + cnIntLast + cnInteger;
return chineseStr;
}
//转换为字符串
money = money.toString();
if (money.indexOf('.') == -1) {
integerNum = money;
decimalNum = '';
} else {
parts = money.split('.');
integerNum = parts[0];
decimalNum = parts[1].substr(0, 4);
}
//获取整型部分转换
if (parseInt(integerNum, 10) > 0) {
var zeroCount = 0;
var IntLen = integerNum.length;
for (var i = 0; i < IntLen; i++) {
var n = integerNum.substr(i, 1);
var p = IntLen - i - 1;
var q = p / 4;
var m = p % 4;
if (n == '0') {
zeroCount++;
} else {
if (zeroCount > 0) {
chineseStr += cnNums[0];
}
//归零
zeroCount = 0;
chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if (m == 0 && zeroCount < 4) {
chineseStr += cnIntUnits[q];
}
}
chineseStr += cnIntLast;
}
//小数部分
if (decimalNum != '') {
var decLen = decimalNum.length;
for (var i = 0; i < decLen; i++) {
var n = decimalNum.substr(i, 1);
if (n != '0') {
chineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
if (chineseStr == '') {
chineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (decimalNum == '') {
chineseStr += cnInteger;
}
return chineseStr;
}
const xnUtils={
xnToast:xnToast,
isIphoneX:isIphoneX,
getHeaderHeight:getHeaderHeight,
getHeaderPadding:getHeaderPadding,
getFooterBottom:getFooterBottom,
xnBorderWidth:xnBorderWidth,
}
export default xnUtils;