util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. import {
  2. TOKENNAME,
  3. HTTP_REQUEST_URL
  4. } from '../config/app.js';
  5. import store from '../store';
  6. import i18n from './lang.js';
  7. import {
  8. pathToBase64
  9. } from '@/plugin/image-tools/index.js';
  10. // #ifdef APP-PLUS
  11. import permision from "./permission.js"
  12. // #endif
  13. export default {
  14. /**
  15. * opt object | string
  16. * to_url object | string
  17. * 例:
  18. * this.Tips('/pages/test/test'); 跳转不提示
  19. * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
  20. * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
  21. * tab=1 一定时间后跳转至 table上
  22. * tab=2 一定时间后跳转至非 table上
  23. * tab=3 一定时间后返回上页面
  24. * tab=4 关闭所有页面,打开到应用内的某个页面
  25. * tab=5 关闭当前页面,跳转到应用内的某个页面
  26. */
  27. Tips: function(opt, to_url) {
  28. console.log(opt,to_url)
  29. if (typeof opt == 'string') {
  30. to_url = opt;
  31. opt = {};
  32. }
  33. let title = opt.title || '',
  34. icon = opt.icon || 'none',
  35. endtime = opt.endtime || 2000,
  36. success = opt.success;
  37. if (title) uni.showToast({
  38. title: title,
  39. icon: icon,
  40. duration: endtime,
  41. success
  42. })
  43. if (to_url != undefined) {
  44. if (typeof to_url == 'object') {
  45. let tab = to_url.tab || 1,
  46. url = to_url.url || '';
  47. console.log(url);
  48. switch (tab) {
  49. case 1:
  50. //一定时间后跳转至 table
  51. setTimeout(function() {
  52. uni.navigateTo({
  53. url: url
  54. })
  55. }, endtime);
  56. break;
  57. case 2:
  58. //跳转至非table页面
  59. setTimeout(function() {
  60. uni.navigateTo({
  61. url: url,
  62. })
  63. }, endtime);
  64. break;
  65. case 3:
  66. //返回上页面
  67. setTimeout(function() {
  68. // #ifndef H5
  69. uni.navigateBack({
  70. delta: parseInt(url),
  71. })
  72. // #endif
  73. // #ifdef H5
  74. history.back();
  75. // #endif
  76. }, endtime);
  77. break;
  78. case 4:
  79. //关闭所有页面,打开到应用内的某个页面
  80. setTimeout(function() {
  81. uni.reLaunch({
  82. url: url,
  83. })
  84. }, endtime);
  85. break;
  86. case 5:
  87. //关闭当前页面,跳转到应用内的某个页面
  88. setTimeout(function() {
  89. uni.redirectTo({
  90. url: url,
  91. })
  92. }, endtime);
  93. break;
  94. }
  95. } else if (typeof to_url == 'function') {
  96. setTimeout(function() {
  97. to_url && to_url();
  98. }, endtime);
  99. } else {
  100. //没有提示时跳转不延迟
  101. setTimeout(function() {
  102. uni.navigateTo({
  103. url: to_url,
  104. })
  105. }, title ? endtime : 0);
  106. }
  107. }
  108. },
  109. /**
  110. * 移除数组中的某个数组并组成新的数组返回
  111. * @param array array 需要移除的数组
  112. * @param int index 需要移除的数组的键值
  113. * @param string | int 值
  114. * @return array
  115. *
  116. */
  117. ArrayRemove: function(array, index, value) {
  118. const valueArray = [];
  119. if (array instanceof Array) {
  120. for (let i = 0; i < array.length; i++) {
  121. if (typeof index == 'number' && array[index] != i) {
  122. valueArray.push(array[i]);
  123. } else if (typeof index == 'string' && array[i][index] != value) {
  124. valueArray.push(array[i]);
  125. }
  126. }
  127. }
  128. return valueArray;
  129. },
  130. /**
  131. * 生成海报获取文字
  132. * @param string text 为传入的文本
  133. * @param int num 为单行显示的字节长度
  134. * @return array
  135. */
  136. textByteLength: function(text, num) {
  137. let strLength = 0;
  138. let rows = 1;
  139. let str = 0;
  140. let arr = [];
  141. for (let j = 0; j < text.length; j++) {
  142. if (text.charCodeAt(j) > 255) {
  143. strLength += 2;
  144. if (strLength > rows * num) {
  145. strLength++;
  146. arr.push(text.slice(str, j));
  147. str = j;
  148. rows++;
  149. }
  150. } else {
  151. strLength++;
  152. if (strLength > rows * num) {
  153. arr.push(text.slice(str, j));
  154. str = j;
  155. rows++;
  156. }
  157. }
  158. }
  159. arr.push(text.slice(str, text.length));
  160. return [strLength, arr, rows] // [处理文字的总字节长度,每行显示内容的数组,行数]
  161. },
  162. /**
  163. * 获取分享海报
  164. * @param array arr2 海报素材
  165. * @param string store_name 素材文字
  166. * @param string price 价格
  167. * @param string ot_price 原始价格
  168. * @param function successFn 回调函数
  169. *
  170. *
  171. */
  172. PosterCanvas: function(arr2, store_name, price, ot_price, successFn) {
  173. let that = this;
  174. uni.showLoading({
  175. title: i18n.t(`海报生成中`),
  176. mask: true
  177. });
  178. const ctx = uni.createCanvasContext('myCanvas');
  179. ctx.clearRect(0, 0, 0, 0);
  180. /**
  181. * 只能获取合法域名下的图片信息,本地调试无法获取
  182. *
  183. */
  184. ctx.fillStyle = '#fff';
  185. ctx.fillRect(0, 0, 750, 1150);
  186. uni.getImageInfo({
  187. src: arr2[0],
  188. success: function(res) {
  189. const WIDTH = res.width;
  190. const HEIGHT = res.height;
  191. // ctx.drawImage(arr2[0], 0, 0, WIDTH, 1050);
  192. ctx.drawImage(arr2[1], 0, 0, WIDTH, WIDTH);
  193. ctx.save();
  194. let r = 110;
  195. let d = r * 2;
  196. let cx = 480;
  197. let cy = 790;
  198. ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
  199. // ctx.clip();
  200. ctx.drawImage(arr2[2], cx, cy, d, d);
  201. ctx.restore();
  202. const CONTENT_ROW_LENGTH = 20;
  203. let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name,
  204. CONTENT_ROW_LENGTH);
  205. if (contentRows > 2) {
  206. contentRows = 2;
  207. let textArray = contentArray.slice(0, 2);
  208. textArray[textArray.length - 1] += '……';
  209. contentArray = textArray;
  210. }
  211. ctx.setTextAlign('left');
  212. ctx.setFontSize(36);
  213. ctx.setFillStyle('#000');
  214. // let contentHh = 36 * 1.5;
  215. let contentHh = 36;
  216. for (let m = 0; m < contentArray.length; m++) {
  217. if (m) {
  218. ctx.fillText(contentArray[m], 50, 1000 + contentHh * m + 18, 1100);
  219. } else {
  220. ctx.fillText(contentArray[m], 50, 1000 + contentHh * m, 1100);
  221. }
  222. }
  223. ctx.setTextAlign('left')
  224. ctx.setFontSize(72);
  225. ctx.setFillStyle('#DA4F2A');
  226. ctx.fillText(i18n.t(`¥`) + price, 40, 820 + contentHh);
  227. ctx.setTextAlign('left')
  228. ctx.setFontSize(36);
  229. ctx.setFillStyle('#999');
  230. if(ot_price){
  231. ctx.fillText(i18n.t(`推荐码 `) + ot_price, 50, 876 + contentHh);
  232. // var underline = function(ctx, text, x, y, size, color, thickness, offset) {
  233. // var width = ctx.measureText(text).width;
  234. // switch (ctx.textAlign) {
  235. // case "center":
  236. // x -= (width / 2);
  237. // break;
  238. // case "right":
  239. // x -= width;
  240. // break;
  241. // }
  242. // y += size + offset;
  243. // ctx.beginPath();
  244. // ctx.strokeStyle = color;
  245. // ctx.lineWidth = thickness;
  246. // ctx.moveTo(x, y);
  247. // ctx.lineTo(x + width, y);
  248. // ctx.stroke();
  249. // }
  250. // underline(ctx, i18n.t(`¥`) + ot_price, 55, 865, 36, '#999', 2, 0)
  251. }
  252. ctx.setTextAlign('left')
  253. ctx.setFontSize(28);
  254. ctx.setFillStyle('#999');
  255. ctx.fillText(i18n.t(`长按或扫描查看`), 490, 1030 + contentHh);
  256. ctx.draw(true, function() {
  257. uni.canvasToTempFilePath({
  258. canvasId: 'myCanvas',
  259. fileType: 'png',
  260. destWidth: WIDTH,
  261. destHeight: HEIGHT,
  262. success: function(res) {
  263. uni.hideLoading();
  264. successFn && successFn(res.tempFilePath);
  265. }
  266. })
  267. });
  268. },
  269. fail: function(err) {
  270. uni.hideLoading();
  271. that.Tips({
  272. title: i18n.t(`无法获取图片信息`)
  273. });
  274. }
  275. })
  276. },
  277. /**
  278. * 获取砍价/拼团海报
  279. * @param array arr2 海报素材 背景图
  280. * @param string store_name 素材文字
  281. * @param string price 价格
  282. * @param string ot_price 原始价格
  283. * @param function successFn 回调函数
  284. *
  285. *
  286. */
  287. bargainPosterCanvas: function(arr2, title, label, msg, price, wd, hg, successFn) {
  288. let that = this;
  289. const ctx = uni.createCanvasContext('myCanvas');
  290. ctx.clearRect(0, 0, 0, 0);
  291. /**
  292. * 只能获取合法域名下的图片信息,本地调试无法获取
  293. *
  294. */
  295. ctx.fillStyle = '#fff';
  296. ctx.fillRect(0, 0, wd * 2, hg * 2);
  297. uni.getImageInfo({
  298. src: arr2[0],
  299. success: function(res) {
  300. const WIDTH = res.width;
  301. const HEIGHT = res.height;
  302. ctx.drawImage(arr2[0], 0, 0, wd, hg);
  303. // 保证在不同机型对应坐标准确
  304. let labelx = 0.6500 //标签x
  305. let labely = 0.166 //标签y
  306. let pricex = 0.1857 //价格x
  307. let pricey = 0.180 //价格x
  308. let codex = 0.385 //二维码
  309. let codey = 0.77
  310. let picturex = 0.1571 //商品图左上点
  311. let picturey = 0.2916
  312. let picturebx = 0.6857 //商品图右下点
  313. let pictureby = 0.4316
  314. let msgx = 0.1036 //msg
  315. let msgy = 0.2306
  316. let codew = 0.25
  317. ctx.drawImage(arr2[1], wd * picturex, hg * picturey, wd * picturebx, hg * pictureby);
  318. ctx.drawImage(arr2[2], wd * codex, hg * codey, wd * codew, wd * codew);
  319. ctx.save();
  320. //标题
  321. const CONTENT_ROW_LENGTH = 30;
  322. let [contentLeng, contentArray, contentRows] = that.textByteLength(title,
  323. CONTENT_ROW_LENGTH);
  324. if (contentRows > 2) {
  325. contentRows = 2;
  326. let textArray = contentArray.slice(0, 2);
  327. textArray[textArray.length - 1] += '…';
  328. contentArray = textArray;
  329. }
  330. ctx.setTextAlign('left');
  331. ctx.setFillStyle('#000');
  332. if (contentArray.length < 2) {
  333. ctx.setFontSize(22);
  334. } else {
  335. ctx.setFontSize(20);
  336. }
  337. let contentHh = 8;
  338. for (let m = 0; m < contentArray.length; m++) {
  339. if (m) {
  340. ctx.fillText(contentArray[m], 20, 35 + contentHh * m + 18, 1100);
  341. } else {
  342. ctx.fillText(contentArray[m], 20, 35, 1100);
  343. }
  344. }
  345. // 标签内容
  346. ctx.setTextAlign('left')
  347. ctx.setFontSize(16);
  348. ctx.setFillStyle('#FFF');
  349. ctx.fillText(label, wd * labelx, hg * labely);
  350. ctx.save();
  351. // 价格
  352. ctx.setFillStyle('red');
  353. ctx.setFontSize(26);
  354. ctx.fillText(price, wd * pricex, hg * pricey);
  355. ctx.save();
  356. // msg
  357. ctx.setFillStyle('#333');
  358. ctx.setFontSize(16);
  359. ctx.fillText(msg, wd * msgx, hg * msgy);
  360. ctx.save();
  361. ctx.draw(true, () => {
  362. uni.canvasToTempFilePath({
  363. canvasId: 'myCanvas',
  364. fileType: 'png',
  365. quality: 1,
  366. success: (res) => {
  367. successFn && successFn(res.tempFilePath);
  368. uni.hideLoading();
  369. }
  370. })
  371. });
  372. },
  373. fail: function(err) {
  374. uni.hideLoading();
  375. that.Tips({
  376. title: i18n.t(`无法获取图片信息`)
  377. });
  378. }
  379. })
  380. },
  381. /**
  382. * 用户信息分享海报
  383. * @param array arr2 海报素材 1背景 0二维码
  384. * @param string nickname 昵称
  385. * @param string sitename 价格
  386. * @param function successFn 回调函数
  387. *
  388. *
  389. */
  390. userPosterCanvas: function(arr2, nickname, sitename, index, w, h, successFn) {
  391. let that = this;
  392. const ctx = uni.createCanvasContext('myCanvas' + index);
  393. ctx.clearRect(0, 0, 0, 0);
  394. /**
  395. * 只能获取合法域名下的图片信息,本地调试无法获取
  396. *
  397. */
  398. uni.getImageInfo({
  399. src: arr2[1],
  400. success: function(res) {
  401. const WIDTH = res.width;
  402. const HEIGHT = res.height;
  403. ctx.fillStyle = '#fff';
  404. ctx.fillRect(0, 0, w, h);
  405. ctx.drawImage(arr2[1], 0, 0, w, h);
  406. ctx.setTextAlign('left')
  407. ctx.setFontSize(12);
  408. ctx.setFillStyle('#333');
  409. // x:240 y:426
  410. let codex = 0.1906
  411. let codey = 0.7746
  412. let codeSize = 0.21666
  413. let namex = 0.4283
  414. let namey = 0.8215
  415. let markx = 0.4283
  416. let marky = 0.8685
  417. ctx.drawImage(arr2[0], w * codex, h * codey, w * codeSize, w * codeSize);
  418. if (w < 270) {
  419. ctx.setFontSize(8);
  420. } else {
  421. ctx.setFontSize(10);
  422. }
  423. ctx.fillText(nickname, w * namex, h * namey);
  424. if (w < 270) {
  425. ctx.setFontSize(8);
  426. } else {
  427. ctx.setFontSize(10);
  428. }
  429. ctx.fillText(i18n.t(`邀请您加入`) + sitename, w * markx, h * marky);
  430. ctx.save();
  431. ctx.draw(true, function() {
  432. uni.canvasToTempFilePath({
  433. canvasId: 'myCanvas' + index,
  434. fileType: 'png',
  435. quality: 1,
  436. success: function(res) {
  437. successFn && successFn(res.tempFilePath);
  438. }
  439. })
  440. });
  441. },
  442. fail: function(err) {
  443. uni.hideLoading();
  444. that.Tips({
  445. title: i18n.t(`无法获取图片信息`)
  446. });
  447. }
  448. })
  449. },
  450. /*
  451. * 单图上传
  452. * @param object opt
  453. * @param callable successCallback 成功执行方法 data
  454. * @param callable errorCallback 失败执行方法
  455. */
  456. uploadImageOne: function(opt, successCallback, errorCallback) {
  457. let that = this;
  458. if (typeof opt === 'string') {
  459. let url = opt;
  460. opt = {};
  461. opt.url = url;
  462. }
  463. let count = opt.count || 1,
  464. sizeType = opt.sizeType || ['compressed'],
  465. sourceType = opt.sourceType || ['album', 'camera'],
  466. is_load = opt.is_load || true,
  467. uploadUrl = opt.url || '',
  468. inputName = opt.name || 'pics',
  469. fileType = opt.fileType || 'image';
  470. uni.chooseImage({
  471. count: count, //最多可以选择的图片总数
  472. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  473. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  474. success: function(res) {
  475. //启动上传等待中...
  476. uni.showLoading({
  477. title: i18n.t(`图片上传中`),
  478. });
  479. uni.uploadFile({
  480. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  481. filePath: res.tempFilePaths[0],
  482. fileType: fileType,
  483. name: inputName,
  484. formData: {
  485. 'filename': inputName
  486. },
  487. header: {
  488. // #ifdef MP
  489. "Content-Type": "multipart/form-data",
  490. // #endif
  491. [TOKENNAME]: 'Bearer ' + store.state.app.token
  492. },
  493. success: function(res) {
  494. uni.hideLoading();
  495. if (res.statusCode == 403) {
  496. that.Tips({
  497. title: res.data
  498. });
  499. } else {
  500. let data = res.data ? JSON.parse(res.data) : {};
  501. if (data.status == 200) {
  502. successCallback && successCallback(data)
  503. } else {
  504. errorCallback && errorCallback(data);
  505. that.Tips({
  506. title: data.msg
  507. });
  508. }
  509. }
  510. },
  511. fail: function(res) {
  512. uni.hideLoading();
  513. that.Tips({
  514. title: i18n.t(`上传图片失败`)
  515. });
  516. }
  517. })
  518. }
  519. })
  520. },
  521. /*
  522. * 单图上传压缩版
  523. * @param object opt
  524. * @param callable successCallback 成功执行方法 data
  525. * @param callable errorCallback 失败执行方法
  526. */
  527. uploadImageChange: function(opt, successCallback, errorCallback, sizeCallback) {
  528. let that = this;
  529. if (typeof opt === 'string') {
  530. let url = opt;
  531. opt = {};
  532. opt.url = url;
  533. }
  534. let count = opt.count || 1,
  535. sizeType = opt.sizeType || ['compressed'],
  536. sourceType = opt.sourceType || ['album', 'camera'],
  537. is_load = opt.is_load || true,
  538. uploadUrl = opt.url || '',
  539. inputName = opt.name || 'pics',
  540. fileType = opt.fileType || 'image';
  541. uni.chooseImage({
  542. count: count, //最多可以选择的图片总数
  543. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  544. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  545. success: function(res) {
  546. //启动上传等待中...
  547. let imgSrc
  548. uni.getImageInfo({
  549. src: res.tempFilePaths[0],
  550. success(ress) {
  551. uni.showLoading({
  552. title: i18n.t(`图片上传中`),
  553. });
  554. if (res.tempFiles[0].size <= 2097152) {
  555. uploadImg(ress.path)
  556. return
  557. }
  558. // uploadImg(canvasPath.tempFilePath)
  559. let canvasWidth, canvasHeight, xs, maxWidth = 750
  560. xs = ress.width / ress.height // 宽高比例
  561. if (ress.width > maxWidth) {
  562. canvasWidth = maxWidth // 这里是最大限制宽度
  563. canvasHeight = maxWidth / xs
  564. } else {
  565. canvasWidth = ress.width
  566. canvasHeight = ress.height
  567. }
  568. sizeCallback && sizeCallback({
  569. w: canvasWidth,
  570. h: canvasHeight
  571. })
  572. let canvas = uni.createCanvasContext('canvas');
  573. canvas.width = canvasWidth
  574. canvas.height = canvasHeight
  575. canvas.clearRect(0, 0, canvasWidth, canvasHeight);
  576. canvas.drawImage(ress.path, 0, 0, canvasWidth, canvasHeight)
  577. canvas.save();
  578. // 这里的画布drawImage是一种异步属性 可能存在未绘制全就执行了draw的问题 so添加延迟
  579. setTimeout(e => {
  580. canvas.draw(true, () => {
  581. uni.canvasToTempFilePath({
  582. canvasId: 'canvas',
  583. fileType: 'JPEG',
  584. destWidth: canvasWidth,
  585. destHeight: canvasHeight,
  586. quality: 0.7,
  587. success: function(canvasPath) {
  588. uploadImg(canvasPath
  589. .tempFilePath)
  590. }
  591. })
  592. });
  593. }, 200)
  594. }
  595. })
  596. }
  597. })
  598. function uploadImg(filePath) {
  599. uni.uploadFile({
  600. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  601. filePath,
  602. fileType: fileType,
  603. name: inputName,
  604. formData: {
  605. 'filename': inputName
  606. },
  607. header: {
  608. // #ifdef MP
  609. "Content-Type": "multipart/form-data",
  610. // #endif
  611. [TOKENNAME]: 'Bearer ' + store.state.app.token
  612. },
  613. success: function(res) {
  614. uni.hideLoading();
  615. if (res.statusCode == 403) {
  616. that.Tips({
  617. title: res.data
  618. });
  619. } else {
  620. let data = res.data ? JSON.parse(res.data) : {};
  621. if (data.status == 200) {
  622. successCallback && successCallback(data)
  623. } else {
  624. errorCallback && errorCallback(data);
  625. that.Tips({
  626. title: data.msg
  627. });
  628. }
  629. }
  630. },
  631. fail: function(res) {
  632. uni.hideLoading();
  633. that.Tips({
  634. title: i18n.t(`上传图片失败`)
  635. });
  636. }
  637. })
  638. }
  639. },
  640. /**
  641. * 处理服务器扫码带进来的参数
  642. * @param string param 扫码携带参数
  643. * @param string k 整体分割符 默认为:&
  644. * @param string p 单个分隔符 默认为:=
  645. * @return object
  646. *
  647. */
  648. // #ifdef MP
  649. getUrlParams: function(param, k, p) {
  650. if (typeof param != 'string') return {};
  651. k = k ? k : '&'; //整体参数分隔符
  652. p = p ? p : '='; //单个参数分隔符
  653. var value = {};
  654. if (param.indexOf(k) !== -1) {
  655. param = param.split(k);
  656. for (var val in param) {
  657. if (param[val].indexOf(p) !== -1) {
  658. var item = param[val].split(p);
  659. value[item[0]] = item[1];
  660. }
  661. }
  662. } else if (param.indexOf(p) !== -1) {
  663. var item = param.split(p);
  664. value[item[0]] = item[1];
  665. } else {
  666. return param;
  667. }
  668. return value;
  669. },
  670. // #endif
  671. /*
  672. * 合并数组
  673. */
  674. SplitArray(list, sp) {
  675. if (typeof list != 'object') return [];
  676. if (sp === undefined) sp = [];
  677. for (var i = 0; i < list.length; i++) {
  678. sp.push(list[i]);
  679. }
  680. return sp;
  681. },
  682. trim(backUrlCRshlcICwGdGY) {
  683. return String.prototype.trim.call(backUrlCRshlcICwGdGY);
  684. },
  685. $h: {
  686. //除法函数,用来得到精确的除法结果
  687. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  688. //调用:$h.Div(arg1,arg2)
  689. //返回值:arg1除以arg2的精确结果
  690. Div: function(arg1, arg2) {
  691. arg1 = parseFloat(arg1);
  692. arg2 = parseFloat(arg2);
  693. var t1 = 0,
  694. t2 = 0,
  695. r1, r2;
  696. try {
  697. t1 = arg1.toString().split(".")[1].length;
  698. } catch (e) {}
  699. try {
  700. t2 = arg2.toString().split(".")[1].length;
  701. } catch (e) {}
  702. r1 = Number(arg1.toString().replace(".", ""));
  703. r2 = Number(arg2.toString().replace(".", ""));
  704. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  705. },
  706. //加法函数,用来得到精确的加法结果
  707. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  708. //调用:$h.Add(arg1,arg2)
  709. //返回值:arg1加上arg2的精确结果
  710. Add: function(arg1, arg2) {
  711. arg2 = parseFloat(arg2);
  712. var r1, r2, m;
  713. try {
  714. r1 = arg1.toString().split(".")[1].length
  715. } catch (e) {
  716. r1 = 0
  717. }
  718. try {
  719. r2 = arg2.toString().split(".")[1].length
  720. } catch (e) {
  721. r2 = 0
  722. }
  723. m = Math.pow(100, Math.max(r1, r2));
  724. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  725. },
  726. //减法函数,用来得到精确的减法结果
  727. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  728. //调用:$h.Sub(arg1,arg2)
  729. //返回值:arg1减去arg2的精确结果
  730. Sub: function(arg1, arg2) {
  731. arg1 = parseFloat(arg1);
  732. arg2 = parseFloat(arg2);
  733. var r1, r2, m, n;
  734. try {
  735. r1 = arg1.toString().split(".")[1].length
  736. } catch (e) {
  737. r1 = 0
  738. }
  739. try {
  740. r2 = arg2.toString().split(".")[1].length
  741. } catch (e) {
  742. r2 = 0
  743. }
  744. m = Math.pow(10, Math.max(r1, r2));
  745. //动态控制精度长度
  746. n = (r1 >= r2) ? r1 : r2;
  747. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  748. },
  749. //乘法函数,用来得到精确的乘法结果
  750. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  751. //调用:$h.Mul(arg1,arg2)
  752. //返回值:arg1乘以arg2的精确结果
  753. Mul: function(arg1, arg2) {
  754. arg1 = parseFloat(arg1);
  755. arg2 = parseFloat(arg2);
  756. var m = 0,
  757. s1 = arg1.toString(),
  758. s2 = arg2.toString();
  759. try {
  760. m += s1.split(".")[1].length
  761. } catch (e) {}
  762. try {
  763. m += s2.split(".")[1].length
  764. } catch (e) {}
  765. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  766. },
  767. },
  768. // 获取地理位置;
  769. $L: {
  770. async getLocation() {
  771. // #ifdef APP-PLUS
  772. let status = await this.checkPermission();
  773. if (status !== 1) {
  774. return;
  775. }
  776. // #endif
  777. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  778. let status = await this.getSetting();
  779. if (status === 2) {
  780. this.openSetting();
  781. return;
  782. }
  783. // #endif
  784. this.doGetLocation();
  785. },
  786. doGetLocation() {
  787. uni.getLocation({
  788. success: (res) => {
  789. uni.removeStorageSync('CACHE_LONGITUDE');
  790. uni.removeStorageSync('CACHE_LATITUDE');
  791. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  792. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  793. },
  794. fail: (err) => {
  795. // #ifdef MP-BAIDU
  796. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  797. this.openSetting();
  798. }
  799. // #endif
  800. // #ifndef MP-BAIDU
  801. if (err.errMsg.indexOf("auth deny") >= 0) {
  802. uni.showToast({
  803. title: i18n.t(`访问位置被拒绝`)
  804. })
  805. } else {
  806. uni.showToast({
  807. title: err.errMsg
  808. })
  809. }
  810. // #endif
  811. }
  812. })
  813. },
  814. getSetting: function() {
  815. return new Promise((resolve, reject) => {
  816. uni.getSetting({
  817. success: (res) => {
  818. if (res.authSetting['scope.userLocation'] === undefined) {
  819. resolve(0);
  820. return;
  821. }
  822. if (res.authSetting['scope.userLocation']) {
  823. resolve(1);
  824. } else {
  825. resolve(2);
  826. }
  827. }
  828. });
  829. });
  830. },
  831. openSetting: function() {
  832. uni.openSetting({
  833. success: (res) => {
  834. if (res.authSetting && res.authSetting['scope.userLocation']) {
  835. this.doGetLocation();
  836. }
  837. },
  838. fail: (err) => {}
  839. })
  840. },
  841. async checkPermission() {
  842. let status = permision.isIOS ? await permision.requestIOS('location') :
  843. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  844. if (status === null || status === 1) {
  845. status = 1;
  846. } else if (status === 2) {
  847. uni.showModal({
  848. content: i18n.t(`系统定位已关闭`),
  849. confirmText: i18n.t(`确定`),
  850. showCancel: false,
  851. success: function(res) {}
  852. })
  853. } else if (status.code) {
  854. uni.showModal({
  855. content: status.message
  856. })
  857. } else {
  858. uni.showModal({
  859. content: i18n.t(`需要定位权限`),
  860. confirmText: i18n.t(`确定`),
  861. success: function(res) {
  862. if (res.confirm) {
  863. permision.gotoAppSetting();
  864. }
  865. }
  866. })
  867. }
  868. return status;
  869. },
  870. }
  871. }