util.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2024 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. * 字符串截取
  24. * @obj 传入的数据
  25. * @state 0 某个参数之前 1某个参数之后
  26. *
  27. *
  28. * **/
  29. stringIntercept: function(obj, state, type) {
  30. var index = obj.lastIndexOf(type);
  31. if (state == 0) {
  32. obj = obj.substring(0, index);
  33. } else {
  34. obj = obj.substring(index + 1, obj.length);
  35. }
  36. return obj;
  37. },
  38. /**
  39. * opt object | string
  40. * to_url object | string
  41. * 例:
  42. * this.Tips('/pages/test/test'); 跳转不提示
  43. * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
  44. * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
  45. * tab=1 一定时间后跳转至 table上
  46. * tab=2 一定时间后跳转至非 table上
  47. * tab=3 一定时间后返回上页面
  48. * tab=4 关闭所有页面跳转至非table上
  49. * tab=5 关闭当前页面跳转至table上
  50. */
  51. Tips: function(opt, to_url) {
  52. if (typeof opt == 'string') {
  53. to_url = opt;
  54. opt = {};
  55. }
  56. let title = opt.title || '',
  57. icon = opt.icon || 'none',
  58. endtime = opt.endtime || 2000,
  59. success = opt.success;
  60. if (title) uni.showToast({
  61. title: title,
  62. icon: icon,
  63. duration: endtime,
  64. success
  65. })
  66. if (to_url != undefined) {
  67. if (typeof to_url == 'object') {
  68. let tab = to_url.tab || 1,
  69. url = to_url.url || '';
  70. switch (tab) {
  71. case 1:
  72. //一定时间后跳转至 table
  73. setTimeout(function() {
  74. uni.switchTab({
  75. url: url
  76. })
  77. }, endtime);
  78. break;
  79. case 2:
  80. //跳转至非table页面
  81. setTimeout(function() {
  82. uni.navigateTo({
  83. url: url,
  84. })
  85. }, endtime);
  86. break;
  87. case 3:
  88. //返回上页面
  89. setTimeout(function() {
  90. // #ifndef H5
  91. uni.navigateBack({
  92. delta: parseInt(url),
  93. })
  94. // #endif
  95. // #ifdef H5
  96. history.back();
  97. // #endif
  98. }, endtime);
  99. break;
  100. case 4:
  101. //关闭当前所有页面跳转至非table页面
  102. setTimeout(function() {
  103. uni.reLaunch({
  104. url: url,
  105. })
  106. }, endtime);
  107. break;
  108. case 5:
  109. //关闭当前页面跳转至非table页面
  110. setTimeout(function() {
  111. uni.redirectTo({
  112. url: url,
  113. })
  114. }, endtime);
  115. break;
  116. }
  117. } else if (typeof to_url == 'function') {
  118. setTimeout(function() {
  119. to_url && to_url();
  120. }, endtime);
  121. } else {
  122. //没有提示时跳转不延迟
  123. setTimeout(function() {
  124. uni.navigateTo({
  125. url: to_url,
  126. })
  127. }, title ? endtime : 0);
  128. }
  129. }
  130. },
  131. /**
  132. * 移除数组中的某个数组并组成新的数组返回
  133. * @param array array 需要移除的数组
  134. * @param int index 需要移除的数组的键值
  135. * @param string | int 值
  136. * @return array
  137. *
  138. */
  139. ArrayRemove: function(array, index, value) {
  140. const valueArray = [];
  141. if (array instanceof Array) {
  142. for (let i = 0; i < array.length; i++) {
  143. if (typeof index == 'number' && array[index] != i) {
  144. valueArray.push(array[i]);
  145. } else if (typeof index == 'string' && array[i][index] != value) {
  146. valueArray.push(array[i]);
  147. }
  148. }
  149. }
  150. return valueArray;
  151. },
  152. /**
  153. * 生成海报获取文字
  154. * @param string text 为传入的文本
  155. * @param int num 为单行显示的字节长度
  156. * @return array
  157. */
  158. textByteLength: function(text, num) {
  159. let strLength = 0;
  160. let rows = 1;
  161. let str = 0;
  162. let arr = [];
  163. for (let j = 0; j < text.length; j++) {
  164. if (text.charCodeAt(j) > 255) {
  165. strLength += 2;
  166. if (strLength > rows * num) {
  167. strLength++;
  168. arr.push(text.slice(str, j));
  169. str = j;
  170. rows++;
  171. }
  172. } else {
  173. strLength++;
  174. if (strLength > rows * num) {
  175. arr.push(text.slice(str, j));
  176. str = j;
  177. rows++;
  178. }
  179. }
  180. }
  181. arr.push(text.slice(str, text.length));
  182. return [strLength, arr, rows] // [处理文字的总字节长度,每行显示内容的数组,行数]
  183. },
  184. /**
  185. * 获取分享海报
  186. * @param array arr2 海报素材
  187. * @param string store_name 素材文字
  188. * @param string price 价格
  189. * @param function successFn 回调函数
  190. *
  191. *
  192. */
  193. PosterCanvas: function(arr2, store_name, price, successFn, errFun) {
  194. let that = this;
  195. const ctx = uni.createCanvasContext('myCanvas');
  196. ctx.clearRect(0, 0, 0, 0);
  197. /**
  198. * 只能获取合法域名下的图片信息,本地调试无法获取
  199. *
  200. */
  201. uni.getImageInfo({
  202. src: arr2[0],
  203. success: function(res) {
  204. console.log(res, 'getImageInfo')
  205. const WIDTH = res.width;
  206. const HEIGHT = res.height;
  207. ctx.drawImage(arr2[0], 0, 0, WIDTH, HEIGHT);
  208. ctx.drawImage(arr2[1], 0, 0, WIDTH, WIDTH);
  209. ctx.save();
  210. let r = 90;
  211. let d = r * 2;
  212. let cx = 40;
  213. let cy = 990;
  214. ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
  215. // ctx.clip();
  216. ctx.drawImage(arr2[2], cx, cy, d, d);
  217. ctx.restore();
  218. const CONTENT_ROW_LENGTH = 40;
  219. let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name, CONTENT_ROW_LENGTH);
  220. if (contentRows > 2) {
  221. contentRows = 2;
  222. let textArray = contentArray.slice(0, 2);
  223. textArray[textArray.length - 1] += '……';
  224. contentArray = textArray;
  225. }
  226. ctx.setTextAlign('center');
  227. ctx.setFontSize(32);
  228. let contentHh = 32 * 1.3;
  229. for (let m = 0; m < contentArray.length; m++) {
  230. ctx.fillText(contentArray[m], WIDTH / 2, 820 + contentHh * m);
  231. }
  232. ctx.setTextAlign('center')
  233. ctx.setFontSize(48);
  234. ctx.setFillStyle('red');
  235. ctx.fillText('¥' + price, WIDTH / 2, 880 + contentHh);
  236. ctx.draw(true, function() {
  237. uni.canvasToTempFilePath({
  238. canvasId: 'myCanvas',
  239. fileType: 'png',
  240. destWidth: WIDTH,
  241. destHeight: HEIGHT,
  242. success: function(res) {
  243. uni.hideLoading();
  244. successFn && successFn(res.tempFilePath);
  245. },
  246. fail: function(err) {
  247. uni.hideLoading();
  248. errFun && errFun(err);
  249. }
  250. })
  251. });
  252. },
  253. fail: function(err) {
  254. uni.hideLoading();
  255. that.Tips({
  256. title: '无法获取图片信息'
  257. });
  258. errFun && errFun(err);
  259. }
  260. })
  261. },
  262. /**
  263. * 获取分享海报
  264. * @param array arr2 海报素材
  265. * @param string store_name 素材文字
  266. * @param string price 价格
  267. * @param function successFn 回调函数
  268. *
  269. *
  270. */
  271. goodsPosterCanvas: function(arr2, store_name, price, site_name, ot_price, successFn, errFun) {
  272. let that = this;
  273. const ctx = uni.createCanvasContext('myCanvas');
  274. ctx.clearRect(0, 0, 0, 0);
  275. /**
  276. * 只能获取合法域名下的图片信息,本地调试无法获取
  277. *
  278. */
  279. uni.getImageInfo({
  280. src: arr2[0],
  281. success: function(res) {
  282. const WIDTH = res.width;
  283. const HEIGHT = res.height;
  284. ctx.drawImage(arr2[0], 0, 0, WIDTH, HEIGHT+50);
  285. ctx.save();
  286. let r = 90;
  287. let d = r * 2;
  288. let cx = 555;
  289. let cy = 910;
  290. let ux = 50;
  291. let uy = 50;
  292. ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
  293. that.handleBorderRect(ctx, 30, 30, 50, 50, 25);
  294. ctx.clip();
  295. if(arr2[3]){
  296. ctx.drawImage(arr2[3], 30, 30, 50, 50);
  297. }
  298. ctx.restore();
  299. ctx.drawImage(arr2[2], cx, cy-30, d-30, d-30);
  300. ctx.save();
  301. ctx.restore();
  302. ctx.setTextAlign('left');
  303. ctx.setFontSize(24);
  304. ctx.setFillStyle('#282828');
  305. ctx.fillText(site_name, r, 62);
  306. const CONTENT_ROW_LENGTH = 26;
  307. let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name, CONTENT_ROW_LENGTH);
  308. if (contentRows > 2) {
  309. contentRows = 2;
  310. let textArray = contentArray.slice(0, 2);
  311. textArray[textArray.length - 1] = textArray[textArray.length - 1].slice(0,textArray[textArray.length - 1].length-1)
  312. textArray[textArray.length - 1] += '…';
  313. contentArray = textArray;
  314. }
  315. ctx.setFontSize(32);
  316. let contentHh = 32 * 1.3;
  317. for (let m = 0; m < contentArray.length; m++) {
  318. ctx.fillText(contentArray[m], 30, cy + contentHh * m);
  319. }
  320. ctx.setFontSize(40);
  321. ctx.setFillStyle('red');
  322. ctx.fillText('¥' + price, 30, 990 + contentHh);
  323. ctx.setFontSize(26);
  324. ctx.setFillStyle('#999999');
  325. ctx.beginPath();
  326. const textWidth = ctx.measureText(ot_price+'¥').width + 16; //检查字体的宽度
  327. //绘制数字中间的矩形
  328. ctx.setFillStyle('#999999');
  329. ctx.rect(35, 1062,textWidth-10, 1);
  330. ctx.fill();
  331. ctx.closePath();
  332. ctx.fillText('¥' + ot_price, 35, 1030 + contentHh);
  333. // ctx.clip();
  334. // ctx.restore();
  335. that.handleBorderRect(ctx, 30, 108, WIDTH-60, WIDTH-20, 12);
  336. ctx.clip();
  337. ctx.drawImage(arr2[1], 30, 108, WIDTH-60, WIDTH-20);
  338. ctx.draw(true, function() {
  339. uni.canvasToTempFilePath({
  340. canvasId: 'myCanvas',
  341. fileType: 'png',
  342. destWidth: WIDTH,
  343. destHeight: HEIGHT,
  344. success: function(res) {
  345. uni.hideLoading();
  346. successFn && successFn(res.tempFilePath);
  347. },
  348. fail: function(err) {
  349. uni.hideLoading();
  350. errFun && errFun(err);
  351. }
  352. })
  353. });
  354. },
  355. fail: function(err) {
  356. uni.hideLoading();
  357. that.Tips({
  358. title: '无法获取图片信息'
  359. });
  360. errFun && errFun(err);
  361. }
  362. })
  363. },
  364. /**
  365. * 获取视频分享海报
  366. * @param array arr2 海报素材
  367. * @param string store_name 素材文字
  368. * @param string price 价格
  369. * @param function successFn 回调函数
  370. *
  371. *
  372. */
  373. videoPosterCanvas: function(arr2, content, nickname, successFn, errFun) {
  374. let that = this;
  375. const ctx = uni.createCanvasContext('myCanvas');
  376. ctx.clearRect(0, 0, 0, 0);
  377. /**
  378. * 只能获取合法域名下的图片信息,本地调试无法获取
  379. *
  380. */
  381. uni.getImageInfo({
  382. src: arr2[0],
  383. success: function(res) {
  384. const WIDTH = res.width;
  385. const HEIGHT = res.height;
  386. let r = 90;
  387. let d = r * 2;
  388. let cx = 40;
  389. let cy = 760;
  390. let ux = 50;
  391. let uy = 700;
  392. ctx.drawImage(arr2[0], 0, 0, WIDTH, HEIGHT);
  393. ctx.drawImage(arr2[1], 32, 32, WIDTH-64, WIDTH-64);
  394. ctx.strokeStyle = "#ffffff";
  395. ctx.save();
  396. ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
  397. ctx.drawImage(arr2[2], 530, 760, d, d);
  398. that.handleBorderRect(ctx, ux, uy, r, r, 45);
  399. ctx.clip();
  400. ctx.stockStyle ="#ffffff";
  401. ctx.drawImage(arr2[3], ux, uy, r, r);
  402. ctx.restore();
  403. ctx.setTextAlign('left')
  404. ctx.setFontSize(28);
  405. ctx.setFillStyle('#282828');
  406. ctx.fillText(nickname, r+60, 760);
  407. ctx.setTextAlign('left')
  408. ctx.setFontSize(28);
  409. ctx.setFillStyle('#282828');
  410. const CONTENT_ROW_LENGTH = 25;
  411. let [contentLeng, contentArray, contentRows] = that.textByteLength(content, CONTENT_ROW_LENGTH);
  412. if (contentRows > 2) {
  413. contentRows = 2;
  414. let textArray = contentArray.slice(0, 2);
  415. textArray[textArray.length - 1] += '……';
  416. contentArray = textArray;
  417. }
  418. ctx.setTextAlign('left');
  419. ctx.font = 'bold 32px Arial';
  420. let contentHh = 32 * 1.3;
  421. for (let m = 0; m < contentArray.length; m++) {
  422. ctx.fillText(contentArray[m], 55, 850 + contentHh * m);
  423. }
  424. ctx.draw(true, function() {
  425. uni.canvasToTempFilePath({
  426. canvasId: 'myCanvas',
  427. fileType: 'png',
  428. destWidth: WIDTH,
  429. destHeight: HEIGHT,
  430. success: function(res) {
  431. uni.hideLoading();
  432. successFn && successFn(res.tempFilePath);
  433. },
  434. fail: function(err) {
  435. uni.hideLoading();
  436. errFun && errFun(err);
  437. }
  438. })
  439. });
  440. },
  441. fail: function(err) {
  442. uni.hideLoading();
  443. that.Tips({
  444. title: '无法获取图片信息'
  445. });
  446. errFun && errFun(err);
  447. }
  448. })
  449. },
  450. /**
  451. * 图片圆角设置
  452. * @param string x x轴位置
  453. * @param string y y轴位置
  454. * @param string w 图片宽
  455. * @param string y 图片高
  456. * @param string r 圆角值
  457. */
  458. handleBorderRect(ctx, x, y, w, h, r) {
  459. ctx.beginPath();
  460. // 左上角
  461. ctx.arc(x + r, y + r, r, Math.PI, 1.5 * Math.PI);
  462. ctx.moveTo(x + r, y);
  463. ctx.lineTo(x + w - r, y);
  464. ctx.lineTo(x + w, y + r);
  465. // 右上角
  466. ctx.arc(x + w - r, y + r, r, 1.5 * Math.PI, 2 * Math.PI);
  467. ctx.lineTo(x + w, y + h - r);
  468. ctx.lineTo(x + w - r, y + h);
  469. // 右下角
  470. ctx.arc(x + w - r, y + h - r, r, 0, 0.5 * Math.PI);
  471. ctx.lineTo(x + r, y + h);
  472. ctx.lineTo(x, y + h - r);
  473. // 左下角
  474. ctx.arc(x + r, y + h - r, r, 0.5 * Math.PI, Math.PI);
  475. ctx.lineTo(x, y + r);
  476. ctx.lineTo(x + r, y);
  477. ctx.fill();
  478. ctx.closePath();
  479. },
  480. /**
  481. * 用户信息分享海报
  482. * @param array arr2 海报素材 1背景 0二维码
  483. * @param string nickname 昵称
  484. * @param string sitename 价格
  485. * @param function successFn 回调函数
  486. *
  487. *
  488. */
  489. userPosterCanvas: function(arr2, nickname, sitename, index, w, h, successFn) {
  490. let that = this;
  491. const ctx = uni.createCanvasContext('myCanvas' + index);
  492. ctx.clearRect(0, 0, 0, 0);
  493. /**
  494. * 只能获取合法域名下的图片信息,本地调试无法获取
  495. *
  496. */
  497. uni.getImageInfo({
  498. src: arr2[1],
  499. success: function(res) {
  500. const WIDTH = res.width;
  501. const HEIGHT = res.height;
  502. // ctx.fillStyle = '#fff';
  503. ctx.fillRect(0, 0, w, h);
  504. // ctx.drawImage(arr2[0], 0, 0, WIDTH, HEIGHT);
  505. ctx.drawImage(arr2[1], 0, 0, w, h);
  506. ctx.setTextAlign('left')
  507. ctx.setFontSize(12);
  508. ctx.setFillStyle('#333');
  509. let codex = 0.1906
  510. let codey = 0.7746
  511. let codeSize = 0.21666
  512. let namex = 0.4283
  513. let namey = 0.8215
  514. let markx = 0.4283
  515. let marky = 0.8685
  516. ctx.drawImage(arr2[0], w * codex, h * codey, w * codeSize, w * codeSize);
  517. if (w < 270) {
  518. ctx.setFontSize(8);
  519. } else {
  520. ctx.setFontSize(10);
  521. }
  522. ctx.fillText(nickname, w * namex, h * namey);
  523. if (w < 270) {
  524. ctx.setFontSize(8);
  525. } else {
  526. ctx.setFontSize(10);
  527. }
  528. const CONTENT_ROW_LENGTH = 28;
  529. let [contentLeng, contentArray, contentRows] = that.textByteLength(sitename, CONTENT_ROW_LENGTH);
  530. if (contentRows > 2) {
  531. contentRows = 2;
  532. let textArray = contentArray.slice(0, 2);
  533. textArray[textArray.length - 1] += '……';
  534. contentArray = textArray;
  535. }
  536. // ctx.setTextAlign('left');
  537. // ctx.font = 'bold 32px Arial';
  538. let contentHh = 12 * 1.3;
  539. for (let m = 0; m < contentArray.length; m++) {
  540. ctx.fillText(contentArray[m], w * markx, h * marky + contentHh * m);
  541. }
  542. // ctx.fillText(sitename, w * markx, h * marky);
  543. ctx.save();
  544. ctx.draw(false,setTimeout(()=>{
  545. uni.canvasToTempFilePath({
  546. canvasId: 'myCanvas' + index,
  547. fileType: 'png',
  548. destWidth: WIDTH,
  549. destHeight: HEIGHT,
  550. success: function(res) {
  551. successFn && successFn(res.tempFilePath);
  552. },
  553. fail: function(err) {
  554. console.log(err)
  555. uni.hideLoading();
  556. }
  557. })
  558. },1000))
  559. },
  560. fail: function(err) {
  561. uni.hideLoading();
  562. that.Tips({
  563. title: '无法获取图片信息'
  564. });
  565. }
  566. })
  567. },
  568. /*
  569. * 单图上传
  570. * @param object opt
  571. * @param callable successCallback 成功执行方法 data
  572. * @param callable errorCallback 失败执行方法
  573. */
  574. uploadImageOne: function(opt, successCallback, errorCallback) {
  575. let that = this;
  576. if (typeof opt === 'string') {
  577. let url = opt;
  578. opt = {};
  579. opt.url = url;
  580. }
  581. let count = opt.count || 1,
  582. sizeType = opt.sizeType || ['compressed'],
  583. sourceType = opt.sourceType || ['album', 'camera'],
  584. is_load = opt.is_load || true,
  585. uploadUrl = opt.url || '',
  586. inputName = opt.name || 'field';
  587. uni.chooseImage({
  588. count: count, //最多可以选择的图片总数
  589. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  590. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  591. success: async (res)=> {
  592. let image = [];
  593. let filesLen = res.tempFiles.length;
  594. let exceeded_list = [];
  595. let uploadMaxSize = 10;
  596. let imageList = [];
  597. let urlPath = HTTP_REQUEST_URL + '/api/' + uploadUrl + '/' + inputName
  598. if (count === 1) {
  599. successCallback && successCallback(await that.uploadFile(urlPath, res.tempFilePaths[
  600. 0], opt, '图片上传中'))
  601. } else {
  602. for (let i = 0; i < res.tempFiles.length; i++) {
  603. if (Math.ceil(res.tempFiles[i].size / 1024) < uploadMaxSize * 1024) {
  604. image.push(res.tempFiles[i].path);
  605. } else {
  606. exceeded_list.push(i + 1);
  607. filesLen = filesLen - 1;
  608. // #ifdef APP-PLUS
  609. plus.nativeUI.alert(
  610. `第${[...new Set(exceeded_list)].join(',')}张图片超出限制${uploadMaxSize}MB,已过滤`
  611. );
  612. // #endif
  613. // #ifndef APP-PLUS
  614. uni.showModal({
  615. content: `第${[...new Set(exceeded_list)].join(',')}张图片超出限制${uploadMaxSize}MB,已过滤`
  616. });
  617. // #endif
  618. continue;
  619. }
  620. }
  621. for (const key in image) {
  622. let data = await that.uploadFile(urlPath, image[key], opt, '图片上传中')
  623. imageList.push(data.data.path)
  624. }
  625. successCallback && successCallback(imageList)
  626. }
  627. }
  628. })
  629. },
  630. uploadFile(urlPath, localPath, opt, message) {
  631. let that = this;
  632. return new Promise(async (resolve) => {
  633. //启动上传等待中...
  634. if (message) uni.showLoading({
  635. title: message,
  636. });
  637. uni.uploadFile({
  638. url: urlPath,
  639. filePath: localPath,
  640. name: opt.name || 'field',
  641. header: {
  642. // #ifdef MP
  643. "Content-Type": "multipart/form-data",
  644. // #endif
  645. [TOKENNAME]: 'Bearer ' + store.state.app.token
  646. },
  647. success: function(res) {
  648. uni.hideLoading();
  649. if (res.statusCode == 403) {
  650. that.Tips({
  651. title: res.data
  652. });
  653. } else {
  654. let data = res.data ? JSON.parse(res.data) : {};
  655. if (data.status == 200) {
  656. resolve(data);
  657. } else {
  658. that.Tips({
  659. title: data.message
  660. });
  661. }
  662. }
  663. },
  664. fail: function(res) {
  665. uni.hideLoading();
  666. that.Tips({
  667. title: res
  668. });
  669. }
  670. })
  671. })
  672. },
  673. /**
  674. * 小程序头像获取上传
  675. * @param uploadUrl 上传接口地址
  676. * @param filePath 上传文件路径
  677. * @param successCallback success回调
  678. * @param errorCallback err回调
  679. */
  680. uploadImgs(uploadUrl, filePath, successCallback, errorCallback) {
  681. let that = this;
  682. let inputName = 'pics';
  683. uni.uploadFile({
  684. url: HTTP_REQUEST_URL + '/api/' + uploadUrl + '/' + inputName,
  685. filePath: filePath,
  686. fileType: 'image',
  687. name: 'pics',
  688. formData: {
  689. 'filename': 'pics'
  690. },
  691. header: {
  692. // #ifdef MP
  693. "Content-Type": "multipart/form-data",
  694. // #endif
  695. [TOKENNAME]: 'Bearer ' + store.state.app.token
  696. },
  697. success: (res) => {
  698. uni.hideLoading();
  699. if (res.statusCode == 403) {
  700. that.Tips({
  701. title: res.data
  702. });
  703. } else {
  704. let data = res.data ? JSON
  705. .parse(res.data) : {};
  706. if (data.status == 200) {
  707. successCallback &&
  708. successCallback(data)
  709. } else {
  710. errorCallback &&
  711. errorCallback(data);
  712. that.Tips({
  713. title: data.message
  714. });
  715. }
  716. }
  717. },
  718. fail: (err) => {
  719. console.log(err)
  720. uni.hideLoading();
  721. that.Tips({
  722. title: i18n.t(`上传图片失败`)
  723. });
  724. }
  725. })
  726. },
  727. serialize: function(obj) {
  728. var str = [];
  729. for (var p in obj)
  730. if (obj.hasOwnProperty(p)) {
  731. str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
  732. }
  733. return str.join("&");
  734. },
  735. getNowUrl: function() {
  736. const pages = getCurrentPages(),
  737. page = pages[pages.length - 1],
  738. query = this.serialize(page.options || {});
  739. return page.route + (query ? '?' + query : '');
  740. },
  741. /**
  742. * 处理服务器扫码带进来的参数
  743. * @param string param 扫码携带参数
  744. * @param string k 整体分割符 默认为:&
  745. * @param string p 单个分隔符 默认为:=
  746. * @return object
  747. *
  748. */
  749. // #ifdef MP
  750. getUrlParams: function(param, k, p) {
  751. if (typeof param != 'string') return {};
  752. k = k ? k : '&'; //整体参数分隔符
  753. p = p ? p : '='; //单个参数分隔符
  754. var value = {};
  755. if (param.indexOf(k) !== -1) {
  756. param = param.split(k);
  757. for (var val in param) {
  758. if (param[val].indexOf(p) !== -1) {
  759. var item = param[val].split(p);
  760. value[item[0]] = item[1];
  761. }
  762. }
  763. } else if (param.indexOf(p) !== -1) {
  764. var item = param.split(p);
  765. value[item[0]] = item[1];
  766. } else {
  767. return param;
  768. }
  769. return value;
  770. },
  771. // #endif
  772. /*
  773. * 合并数组
  774. */
  775. SplitArray(list, sp) {
  776. if (typeof list != 'object') return [];
  777. if (sp === undefined) sp = [];
  778. for (var i = 0; i < list.length; i++) {
  779. sp.push(list[i]);
  780. }
  781. return sp;
  782. },
  783. trim(str) {
  784. return String.prototype.trim.call(str);
  785. },
  786. $h: {
  787. //除法函数,用来得到精确的除法结果
  788. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  789. //调用:$h.Div(arg1,arg2)
  790. //返回值:arg1除以arg2的精确结果
  791. Div: function(arg1, arg2) {
  792. arg1 = parseFloat(arg1);
  793. arg2 = parseFloat(arg2);
  794. var t1 = 0,
  795. t2 = 0,
  796. r1, r2;
  797. try {
  798. t1 = arg1.toString().split(".")[1].length;
  799. } catch (e) {}
  800. try {
  801. t2 = arg2.toString().split(".")[1].length;
  802. } catch (e) {}
  803. r1 = Number(arg1.toString().replace(".", ""));
  804. r2 = Number(arg2.toString().replace(".", ""));
  805. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  806. },
  807. //加法函数,用来得到精确的加法结果
  808. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  809. //调用:$h.Add(arg1,arg2)
  810. //返回值:arg1加上arg2的精确结果
  811. Add: function(arg1, arg2) {
  812. arg2 = parseFloat(arg2);
  813. var r1, r2, m;
  814. try {
  815. r1 = arg1.toString().split(".")[1].length
  816. } catch (e) {
  817. r1 = 0
  818. }
  819. try {
  820. r2 = arg2.toString().split(".")[1].length
  821. } catch (e) {
  822. r2 = 0
  823. }
  824. m = Math.pow(100, Math.max(r1, r2));
  825. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  826. },
  827. //减法函数,用来得到精确的减法结果
  828. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  829. //调用:$h.Sub(arg1,arg2)
  830. //返回值:arg1减去arg2的精确结果
  831. Sub: function(arg1, arg2) {
  832. arg1 = parseFloat(arg1);
  833. arg2 = parseFloat(arg2);
  834. var r1, r2, m, n;
  835. try {
  836. r1 = arg1.toString().split(".")[1].length
  837. } catch (e) {
  838. r1 = 0
  839. }
  840. try {
  841. r2 = arg2.toString().split(".")[1].length
  842. } catch (e) {
  843. r2 = 0
  844. }
  845. m = Math.pow(10, Math.max(r1, r2));
  846. //动态控制精度长度
  847. n = (r1 >= r2) ? r1 : r2;
  848. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  849. },
  850. //乘法函数,用来得到精确的乘法结果
  851. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  852. //调用:$h.Mul(arg1,arg2)
  853. //返回值:arg1乘以arg2的精确结果
  854. Mul: function(arg1, arg2) {
  855. arg1 = parseFloat(arg1);
  856. arg2 = parseFloat(arg2);
  857. var m = 0,
  858. s1 = arg1.toString(),
  859. s2 = arg2.toString();
  860. try {
  861. m += s1.split(".")[1].length
  862. } catch (e) {}
  863. try {
  864. m += s2.split(".")[1].length
  865. } catch (e) {}
  866. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  867. },
  868. },
  869. // 获取地理位置;
  870. $L: {
  871. async getLocation() {
  872. // #ifdef APP-PLUS
  873. let status = await this.checkPermission();
  874. if (status !== 1) {
  875. return;
  876. }
  877. // #endif
  878. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  879. let status = await this.getSetting();
  880. if (status === 2) {
  881. this.openSetting();
  882. return;
  883. }
  884. // #endif
  885. this.doGetLocation();
  886. },
  887. doGetLocation() {
  888. uni.getLocation({
  889. success: (res) => {
  890. uni.removeStorageSync('CACHE_LONGITUDE');
  891. uni.removeStorageSync('CACHE_LATITUDE');
  892. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  893. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  894. },
  895. fail: (err) => {
  896. // #ifdef MP-BAIDU
  897. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  898. this.openSetting();
  899. }
  900. // #endif
  901. // #ifndef MP-BAIDU
  902. if (err.errMsg.indexOf("auth deny") >= 0) {
  903. uni.showToast({
  904. title: "访问位置被拒绝"
  905. })
  906. } else {
  907. uni.showToast({
  908. title: err.errMsg
  909. })
  910. }
  911. // #endif
  912. }
  913. })
  914. },
  915. getSetting: function() {
  916. return new Promise((resolve, reject) => {
  917. uni.getSetting({
  918. success: (res) => {
  919. if (res.authSetting['scope.userLocation'] === undefined) {
  920. resolve(0);
  921. return;
  922. }
  923. if (res.authSetting['scope.userLocation']) {
  924. resolve(1);
  925. } else {
  926. resolve(2);
  927. }
  928. }
  929. });
  930. });
  931. },
  932. openSetting: function() {
  933. uni.openSetting({
  934. success: (res) => {
  935. if (res.authSetting && res.authSetting['scope.userLocation']) {
  936. this.doGetLocation();
  937. }
  938. },
  939. fail: (err) => {}
  940. })
  941. },
  942. async checkPermission() {
  943. let status = permision.isIOS ? await permision.requestIOS('location') :
  944. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  945. if (status === null || status === 1) {
  946. status = 1;
  947. } else if (status === 2) {
  948. uni.showModal({
  949. content: "系统定位已关闭",
  950. confirmText: "确定",
  951. showCancel: false,
  952. success: function(res) {}
  953. })
  954. } else if (status.code) {
  955. uni.showModal({
  956. content: status.message
  957. })
  958. } else {
  959. uni.showModal({
  960. content: "需要定位权限",
  961. confirmText: "设置",
  962. success: function(res) {
  963. if (res.confirm) {
  964. permision.gotoAppSetting();
  965. }
  966. }
  967. })
  968. }
  969. return status;
  970. },
  971. },
  972. /**
  973. * 跳转路径封装函数
  974. * @param url 跳转路径
  975. */
  976. JumpPath: function(url) {
  977. let arr = url.split('@APPID=');
  978. if (arr.length > 1) {
  979. //#ifdef MP
  980. let pathArr = arr[1].split('?')
  981. if(pathArr.length > 1){}
  982. uni.navigateToMiniProgram({
  983. appId: pathArr.length > 1 ? pathArr[0] : arr[arr.length-1], // 此为生活缴费appid
  984. path: arr[0], // 此为生活缴费首页路径
  985. envVersion: "release",
  986. success: res => {
  987. console.log("打开成功", res);
  988. },
  989. fail: err => {}
  990. })
  991. //#endif
  992. //#ifndef MP
  993. this.Tips({
  994. title: 'h5与app端不支持跳转外部小程序'
  995. });
  996. //#endif
  997. } else {
  998. if (url.indexOf("http") != -1) {
  999. uni.navigateTo({
  1000. url: `/pages/annex/web_view/index?url=${url}`
  1001. });
  1002. } else {
  1003. if (['/pages/goods_cate/goods_cate','/pages/plant_grass/index','/pages/order_addcart/order_addcart','/pages/user/index'
  1004. ]
  1005. .indexOf(url) == -1) {
  1006. uni.navigateTo({
  1007. url
  1008. })
  1009. } else {
  1010. uni.switchTab({
  1011. url
  1012. })
  1013. }
  1014. }
  1015. }
  1016. },
  1017. }