util.js 23 KB

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