util.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. export default {
  2. /*
  3. * 合并数组
  4. */
  5. SplitArray(list, sp) {
  6. if (typeof list != 'object') return [];
  7. if (sp === undefined) sp = [];
  8. for (var i = 0; i < list.length; i++) {
  9. sp.push(list[i]);
  10. }
  11. return sp;
  12. },
  13. trim(str) {
  14. return String.prototype.trim.call(str);
  15. },
  16. $h: {
  17. //除法函数,用来得到精确的除法结果
  18. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  19. //调用:$h.Div(arg1,arg2)
  20. //返回值:arg1除以arg2的精确结果
  21. Div: function(arg1, arg2) {
  22. arg1 = parseFloat(arg1);
  23. arg2 = parseFloat(arg2);
  24. var t1 = 0,
  25. t2 = 0,
  26. r1, r2;
  27. try {
  28. t1 = arg1.toString().split(".")[1].length;
  29. } catch (e) {}
  30. try {
  31. t2 = arg2.toString().split(".")[1].length;
  32. } catch (e) {}
  33. r1 = Number(arg1.toString().replace(".", ""));
  34. r2 = Number(arg2.toString().replace(".", ""));
  35. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  36. },
  37. //加法函数,用来得到精确的加法结果
  38. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  39. //调用:$h.Add(arg1,arg2)
  40. //返回值:arg1加上arg2的精确结果
  41. Add: function(arg1, arg2) {
  42. arg2 = parseFloat(arg2);
  43. var r1, r2, m;
  44. try {
  45. r1 = arg1.toString().split(".")[1].length
  46. } catch (e) {
  47. r1 = 0
  48. }
  49. try {
  50. r2 = arg2.toString().split(".")[1].length
  51. } catch (e) {
  52. r2 = 0
  53. }
  54. m = Math.pow(100, Math.max(r1, r2));
  55. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  56. },
  57. //减法函数,用来得到精确的减法结果
  58. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  59. //调用:$h.Sub(arg1,arg2)
  60. //返回值:arg1减去arg2的精确结果
  61. Sub: function(arg1, arg2) {
  62. arg1 = parseFloat(arg1);
  63. arg2 = parseFloat(arg2);
  64. var r1, r2, m, n;
  65. try {
  66. r1 = arg1.toString().split(".")[1].length
  67. } catch (e) {
  68. r1 = 0
  69. }
  70. try {
  71. r2 = arg2.toString().split(".")[1].length
  72. } catch (e) {
  73. r2 = 0
  74. }
  75. m = Math.pow(10, Math.max(r1, r2));
  76. //动态控制精度长度
  77. n = (r1 >= r2) ? r1 : r2;
  78. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  79. },
  80. //乘法函数,用来得到精确的乘法结果
  81. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  82. //调用:$h.Mul(arg1,arg2)
  83. //返回值:arg1乘以arg2的精确结果
  84. Mul: function(arg1, arg2) {
  85. arg1 = parseFloat(arg1);
  86. arg2 = parseFloat(arg2);
  87. var m = 0,
  88. s1 = arg1.toString(),
  89. s2 = arg2.toString();
  90. try {
  91. m += s1.split(".")[1].length
  92. } catch (e) {}
  93. try {
  94. m += s2.split(".")[1].length
  95. } catch (e) {}
  96. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  97. },
  98. },
  99. // 获取地理位置;
  100. $L: {
  101. async getLocation() {
  102. // #ifdef APP-PLUS
  103. let status = await this.checkPermission();
  104. if (status !== 1) {
  105. return;
  106. }
  107. // #endif
  108. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  109. let status = await this.getSetting();
  110. if (status === 2) {
  111. this.openSetting();
  112. return;
  113. }
  114. // #endif
  115. this.doGetLocation();
  116. },
  117. doGetLocation() {
  118. uni.getLocation({
  119. type:'gcj02',
  120. success: (res) => {
  121. uni.removeStorageSync('CACHE_LONGITUDE');
  122. uni.removeStorageSync('CACHE_LATITUDE');
  123. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  124. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  125. },
  126. fail: (err) => {
  127. // #ifdef MP-BAIDU
  128. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  129. this.openSetting();
  130. }
  131. // #endif
  132. // #ifndef MP-BAIDU
  133. if (err.errMsg.indexOf("auth deny") >= 0) {
  134. uni.showToast({
  135. title: "访问位置被拒绝"
  136. })
  137. } else {
  138. uni.showToast({
  139. title: err.errMsg
  140. })
  141. }
  142. // #endif
  143. }
  144. })
  145. },
  146. getSetting: function() {
  147. return new Promise((resolve, reject) => {
  148. uni.getSetting({
  149. success: (res) => {
  150. if (res.authSetting['scope.userLocation'] === undefined) {
  151. resolve(0);
  152. return;
  153. }
  154. if (res.authSetting['scope.userLocation']) {
  155. resolve(1);
  156. } else {
  157. resolve(2);
  158. }
  159. }
  160. });
  161. });
  162. },
  163. openSetting: function() {
  164. uni.openSetting({
  165. success: (res) => {
  166. if (res.authSetting && res.authSetting['scope.userLocation']) {
  167. this.doGetLocation();
  168. }
  169. },
  170. fail: (err) => {}
  171. })
  172. },
  173. // #ifdef APP-PLUS
  174. async checkPermission() {
  175. let status = permision.isIOS ? await permision.requestIOS('location') :
  176. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  177. if (status === null || status === 1) {
  178. status = 1;
  179. } else if (status === 2) {
  180. uni.showModal({
  181. content: "系统定位已关闭",
  182. confirmText: "确定",
  183. showCancel: false,
  184. success: function(res) {}
  185. })
  186. } else if (status.code) {
  187. uni.showModal({
  188. content: status.message
  189. })
  190. } else {
  191. uni.showModal({
  192. content: "需要定位权限",
  193. confirmText: "设置",
  194. success: function(res) {
  195. if (res.confirm) {
  196. permision.gotoAppSetting();
  197. }
  198. }
  199. })
  200. }
  201. return status;
  202. },
  203. // #endif
  204. }
  205. }