commonutil.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { Linking, Alert } from 'react-native';
  2. export default class commonutil {
  3. //获取appCode
  4. static getAppCode() {
  5. return '1006';
  6. }
  7. static async getFileUserInfo() {
  8. let a = await global.storage
  9. .load({
  10. key: 'userInfo'
  11. })
  12. .then((result) => {
  13. return result;
  14. })
  15. .catch((err) => {
  16. console.log('ERROR' + err.message);
  17. return null;
  18. });
  19. return a;
  20. }
  21. static async saveUserInfo() {
  22. global.storage
  23. .save({
  24. key: 'userInfo',
  25. data: JSON.stringify(global.userInfo)
  26. })
  27. .then((result) => {
  28. return result;
  29. })
  30. .catch((err) => {
  31. return 'error';
  32. });
  33. }
  34. //验证手机号
  35. static isPoneAvailable(str) {
  36. let myreg = /^[1][0-9]{10}$/;
  37. if (str.length == 0 || str == null) {
  38. return false;
  39. } else if (!myreg.test(str)) {
  40. return false;
  41. } else {
  42. return true;
  43. }
  44. }
  45. /**
  46. *map转化为对象(map所有键都是字符串,可以将其转换为对象)
  47. */
  48. static strMapToObj(strMap) {
  49. let obj = Object.create(null);
  50. for (let [ k, v ] of strMap) {
  51. obj[k] = v;
  52. }
  53. return obj;
  54. }
  55. /**
  56. *map转换为json
  57. */
  58. static mapToJson(map) {
  59. return JSON.stringify(this.strMapToObj(map));
  60. }
  61. /**
  62. *对象转换为Map
  63. */
  64. static objToStrMap(obj) {
  65. let strMap = new Map();
  66. for (let k of Object.keys(obj)) {
  67. strMap.set(k, obj[k]);
  68. }
  69. return strMap;
  70. }
  71. /**
  72. *json转换为map
  73. */
  74. static jsonToMap(jsonStr) {
  75. return this.objToStrMap(JSON.parse(jsonStr));
  76. }
  77. //调用打电话功能
  78. static callPhone(phone) {
  79. // let tel = '1008611'; // 目标电话
  80. Alert.alert('提示', phone, [
  81. {
  82. text: '取消',
  83. onPress: () => {
  84. console.log('取消');
  85. }
  86. },
  87. {
  88. text: '确定',
  89. onPress: () => {
  90. Linking.canOpenURL('tel:' + phone)
  91. .then((supported) => {
  92. if (!supported) {
  93. console.log('Can not handle tel:' + phone);
  94. } else {
  95. return Linking.openURL('tel:' + phone);
  96. }
  97. })
  98. .catch((error) => console.log('tel error', error));
  99. }
  100. }
  101. ]);
  102. }
  103. }