util.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import {
  2. TOKENNAME,
  3. HTTP_REQUEST_URL
  4. } from '../config/app.js';
  5. import store from '../store';
  6. import {
  7. pathToBase64
  8. } from '@/plugin/image-tools/index.js';
  9. // #ifdef APP-PLUS
  10. // import permision from "permission.js"
  11. // #endif
  12. export default {
  13. /**
  14. * opt object | string
  15. * to_url object | string
  16. * 例:
  17. * this.Tips('/pages/test/test'); 跳转不提示
  18. * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
  19. * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
  20. * tab=1 一定时间后跳转至 table上
  21. * tab=2 一定时间后跳转至非 table上
  22. * tab=3 一定时间后返回上页面
  23. * tab=4 关闭所有页面跳转至非table上
  24. * tab=5 关闭当前页面跳转至table上
  25. */
  26. Tips: function(opt, to_url) {
  27. if (typeof opt == 'string') {
  28. to_url = opt;
  29. opt = {};
  30. }
  31. let title = opt.title || '',
  32. icon = opt.icon || 'none',
  33. endtime = opt.endtime || 2000,
  34. success = opt.success;
  35. if (title) uni.showToast({
  36. title: title,
  37. icon: icon,
  38. duration: endtime,
  39. success
  40. })
  41. if (to_url != undefined) {
  42. if (typeof to_url == 'object') {
  43. let tab = to_url.tab || 1,
  44. url = to_url.url || '';
  45. switch (tab) {
  46. case 1:
  47. //一定时间后跳转至 table
  48. setTimeout(function() {
  49. uni.switchTab({
  50. url: url
  51. })
  52. }, endtime);
  53. break;
  54. case 2:
  55. //跳转至非table页面
  56. setTimeout(function() {
  57. uni.navigateTo({
  58. url: url,
  59. })
  60. }, endtime);
  61. break;
  62. case 3:
  63. //返回上页面
  64. setTimeout(function() {
  65. // #ifndef H5
  66. uni.navigateBack({
  67. delta: parseInt(url),
  68. })
  69. // #endif
  70. // #ifdef H5
  71. history.back();
  72. // #endif
  73. }, endtime);
  74. break;
  75. case 4:
  76. //关闭当前所有页面跳转至非table页面
  77. setTimeout(function() {
  78. uni.reLaunch({
  79. url: url,
  80. })
  81. }, endtime);
  82. break;
  83. case 5:
  84. //关闭当前页面跳转至非table页面
  85. setTimeout(function() {
  86. uni.redirectTo({
  87. url: url,
  88. })
  89. }, endtime);
  90. break;
  91. }
  92. } else if (typeof to_url == 'function') {
  93. setTimeout(function() {
  94. to_url && to_url();
  95. }, endtime);
  96. } else {
  97. //没有提示时跳转不延迟
  98. setTimeout(function() {
  99. uni.navigateTo({
  100. url: to_url,
  101. })
  102. }, title ? endtime : 0);
  103. }
  104. }
  105. },
  106. /**
  107. * 移除数组中的某个数组并组成新的数组返回
  108. * @param array array 需要移除的数组
  109. * @param int index 需要移除的数组的键值
  110. * @param string | int 值
  111. * @return array
  112. *
  113. */
  114. ArrayRemove: function(array, index, value) {
  115. const valueArray = [];
  116. if (array instanceof Array) {
  117. for (let i = 0; i < array.length; i++) {
  118. if (typeof index == 'number' && array[index] != i) {
  119. valueArray.push(array[i]);
  120. } else if (typeof index == 'string' && array[i][index] != value) {
  121. valueArray.push(array[i]);
  122. }
  123. }
  124. }
  125. return valueArray;
  126. },
  127. /**
  128. * 生成海报获取文字
  129. * @param string text 为传入的文本
  130. * @param int num 为单行显示的字节长度
  131. * @return array
  132. */
  133. textByteLength: function(text, num) {
  134. let strLength = 0;
  135. let rows = 1;
  136. let str = 0;
  137. let arr = [];
  138. for (let j = 0; j < text.length; j++) {
  139. if (text.charCodeAt(j) > 255) {
  140. strLength += 2;
  141. if (strLength > rows * num) {
  142. strLength++;
  143. arr.push(text.slice(str, j));
  144. str = j;
  145. rows++;
  146. }
  147. } else {
  148. strLength++;
  149. if (strLength > rows * num) {
  150. arr.push(text.slice(str, j));
  151. str = j;
  152. rows++;
  153. }
  154. }
  155. }
  156. arr.push(text.slice(str, text.length));
  157. return [strLength, arr, rows] // [处理文字的总字节长度,每行显示内容的数组,行数]
  158. },
  159. /*
  160. * 合并数组
  161. */
  162. SplitArray(list, sp) {
  163. if (typeof list != 'object') return [];
  164. if (sp === undefined) sp = [];
  165. for (var i = 0; i < list.length; i++) {
  166. sp.push(list[i]);
  167. }
  168. return sp;
  169. },
  170. trim(str) {
  171. return String.prototype.trim.call(str);
  172. },
  173. $h: {
  174. //除法函数,用来得到精确的除法结果
  175. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  176. //调用:$h.Div(arg1,arg2)
  177. //返回值:arg1除以arg2的精确结果
  178. Div: function(arg1, arg2) {
  179. arg1 = parseFloat(arg1);
  180. arg2 = parseFloat(arg2);
  181. var t1 = 0,
  182. t2 = 0,
  183. r1, r2;
  184. try {
  185. t1 = arg1.toString().split(".")[1].length;
  186. } catch (e) {}
  187. try {
  188. t2 = arg2.toString().split(".")[1].length;
  189. } catch (e) {}
  190. r1 = Number(arg1.toString().replace(".", ""));
  191. r2 = Number(arg2.toString().replace(".", ""));
  192. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  193. },
  194. //加法函数,用来得到精确的加法结果
  195. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  196. //调用:$h.Add(arg1,arg2)
  197. //返回值:arg1加上arg2的精确结果
  198. Add: function(arg1, arg2) {
  199. arg2 = parseFloat(arg2);
  200. var r1, r2, m;
  201. try {
  202. r1 = arg1.toString().split(".")[1].length
  203. } catch (e) {
  204. r1 = 0
  205. }
  206. try {
  207. r2 = arg2.toString().split(".")[1].length
  208. } catch (e) {
  209. r2 = 0
  210. }
  211. m = Math.pow(100, Math.max(r1, r2));
  212. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  213. },
  214. //减法函数,用来得到精确的减法结果
  215. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  216. //调用:$h.Sub(arg1,arg2)
  217. //返回值:arg1减去arg2的精确结果
  218. Sub: function(arg1, arg2) {
  219. arg1 = parseFloat(arg1);
  220. arg2 = parseFloat(arg2);
  221. var r1, r2, m, n;
  222. try {
  223. r1 = arg1.toString().split(".")[1].length
  224. } catch (e) {
  225. r1 = 0
  226. }
  227. try {
  228. r2 = arg2.toString().split(".")[1].length
  229. } catch (e) {
  230. r2 = 0
  231. }
  232. m = Math.pow(10, Math.max(r1, r2));
  233. //动态控制精度长度
  234. n = (r1 >= r2) ? r1 : r2;
  235. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  236. },
  237. //乘法函数,用来得到精确的乘法结果
  238. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  239. //调用:$h.Mul(arg1,arg2)
  240. //返回值:arg1乘以arg2的精确结果
  241. Mul: function(arg1, arg2) {
  242. arg1 = parseFloat(arg1);
  243. arg2 = parseFloat(arg2);
  244. var m = 0,
  245. s1 = arg1.toString(),
  246. s2 = arg2.toString();
  247. try {
  248. m += s1.split(".")[1].length
  249. } catch (e) {}
  250. try {
  251. m += s2.split(".")[1].length
  252. } catch (e) {}
  253. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  254. },
  255. },
  256. // 获取地理位置;
  257. $L: {
  258. async getLocation() {
  259. // #ifdef APP-PLUS
  260. let status = await this.checkPermission();
  261. if (status !== 1) {
  262. return;
  263. }
  264. // #endif
  265. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  266. let status = await this.getSetting();
  267. if (status === 2) {
  268. this.openSetting();
  269. return;
  270. }
  271. // #endif
  272. this.doGetLocation();
  273. },
  274. doGetLocation() {
  275. uni.getLocation({
  276. success: (res) => {
  277. uni.removeStorageSync('CACHE_LONGITUDE');
  278. uni.removeStorageSync('CACHE_LATITUDE');
  279. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  280. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  281. },
  282. fail: (err) => {
  283. // #ifdef MP-BAIDU
  284. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  285. this.openSetting();
  286. }
  287. // #endif
  288. // #ifndef MP-BAIDU
  289. if (err.errMsg.indexOf("auth deny") >= 0) {
  290. uni.showToast({
  291. title: "访问位置被拒绝"
  292. })
  293. } else {
  294. uni.showToast({
  295. title: err.errMsg
  296. })
  297. }
  298. // #endif
  299. }
  300. })
  301. },
  302. getSetting: function() {
  303. return new Promise((resolve, reject) => {
  304. uni.getSetting({
  305. success: (res) => {
  306. if (res.authSetting['scope.userLocation'] === undefined) {
  307. resolve(0);
  308. return;
  309. }
  310. if (res.authSetting['scope.userLocation']) {
  311. resolve(1);
  312. } else {
  313. resolve(2);
  314. }
  315. }
  316. });
  317. });
  318. },
  319. openSetting: function() {
  320. uni.openSetting({
  321. success: (res) => {
  322. if (res.authSetting && res.authSetting['scope.userLocation']) {
  323. this.doGetLocation();
  324. }
  325. },
  326. fail: (err) => {}
  327. })
  328. }
  329. // async checkPermission() {
  330. // let status = permision.isIOS ? await permision.requestIOS('location') :
  331. // await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  332. // if (status === null || status === 1) {
  333. // status = 1;
  334. // } else if (status === 2) {
  335. // uni.showModal({
  336. // content: "系统定位已关闭",
  337. // confirmText: "确定",
  338. // showCancel: false,
  339. // success: function(res) {}
  340. // })
  341. // } else if (status.code) {
  342. // uni.showModal({
  343. // content: status.message
  344. // })
  345. // } else {
  346. // uni.showModal({
  347. // content: "需要定位权限",
  348. // confirmText: "设置",
  349. // success: function(res) {
  350. // if (res.confirm) {
  351. // permision.gotoAppSetting();
  352. // }
  353. // }
  354. // })
  355. // }
  356. // return status;
  357. // },
  358. }
  359. }