util.js 23 KB

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