util.js 23 KB

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