util.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. customerType
  17. } from '@/api/api.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. */
  118. addressInfo: function(str) {
  119. let reg = /.+?(省|市|自治区|自治州|行政区|盟|旗|县|区)/g // 省市区的正则
  120. const area = str.match(reg) // 分割省市区
  121. let citys = ['北京市', '天津市', '上海市', '重庆市']
  122. let address_component = {
  123. city: "",
  124. district: "",
  125. province: area[0]
  126. }
  127. if (citys.indexOf(area[0]) == -1) {
  128. address_component.city = area[1]
  129. address_component.district = area[2]
  130. } else {
  131. address_component.city = area[0]
  132. address_component.district = area[1]
  133. }
  134. return address_component
  135. },
  136. /**
  137. * 跳转路径封装函数
  138. * @param url 跳转路径
  139. * 选择跳转到其他小程序时,在h5也可以跳转路径
  140. */
  141. JumpPath: function(url){
  142. let arr = url.split('@APPID=');
  143. if(arr.length>1){
  144. //#ifdef MP
  145. uni.navigateToMiniProgram({
  146. appId:arr[arr.length-1],// 此为生活缴费appid
  147. path:arr[0],// 此为生活缴费首页路径
  148. envVersion:"release",
  149. success: res => {
  150. console.log("打开成功", res);
  151. },
  152. fail: err => {
  153. console.log('sgdhgf',err);
  154. }
  155. })
  156. //#endif
  157. //#ifndef MP
  158. this.Tips({
  159. title: 'h5与app端不支持跳转外部小程序'
  160. });
  161. //#endif
  162. }else{
  163. if(url == '/pages/short_video/appSwiper/index' || url == '/pages/short_video/nvueSwiper/index'){
  164. //#ifdef APP
  165. url = '/pages/short_video/appSwiper/index'
  166. //#endif
  167. //#ifndef APP
  168. url = '/pages/short_video/nvueSwiper/index'
  169. //#endif
  170. }
  171. if (url.indexOf("http") != -1) {
  172. uni.navigateTo({
  173. url: `/pages/annex/web_view/index?url=${url}`
  174. });
  175. } else {
  176. if (['/pages/goods_cate/goods_cate', '/pages/order_addcart/order_addcart', '/pages/user/index', '/pages/index/index','/pages/store_cate/store_cate']
  177. .indexOf(url.split('?')[0]) == -1) {
  178. uni.navigateTo({
  179. url: url
  180. })
  181. } else {
  182. uni.reLaunch({
  183. url: url
  184. })
  185. }
  186. }
  187. }
  188. },
  189. /**
  190. * 移除数组中的某个数组并组成新的数组返回
  191. * @param array array 需要移除的数组
  192. * @param int index 需要移除的数组的键值
  193. * @param string | int 值
  194. * @return array
  195. *
  196. */
  197. ArrayRemove: function(array, index, value) {
  198. const valueArray = [];
  199. if (array instanceof Array) {
  200. for (let i = 0; i < array.length; i++) {
  201. if (typeof index == 'number' && array[index] != i) {
  202. valueArray.push(array[i]);
  203. } else if (typeof index == 'string' && array[i][index] != value) {
  204. valueArray.push(array[i]);
  205. }
  206. }
  207. }
  208. return valueArray;
  209. },
  210. /**
  211. * 生成海报获取文字
  212. * @param string text 为传入的文本
  213. * @param int num 为单行显示的字节长度
  214. * @return array
  215. */
  216. textByteLength: function(text, num) {
  217. let strLength = 0;
  218. let rows = 1;
  219. let str = 0;
  220. let arr = [];
  221. for (let j = 0; j < text.length; j++) {
  222. if (text.charCodeAt(j) > 255) {
  223. strLength += 2;
  224. if (strLength > rows * num) {
  225. strLength++;
  226. arr.push(text.slice(str, j));
  227. str = j;
  228. rows++;
  229. }
  230. } else {
  231. strLength++;
  232. if (strLength > rows * num) {
  233. arr.push(text.slice(str, j));
  234. str = j;
  235. rows++;
  236. }
  237. }
  238. }
  239. arr.push(text.slice(str, text.length));
  240. return [strLength, arr, rows] // [处理文字的总字节长度,每行显示内容的数组,行数]
  241. },
  242. /**
  243. * 获取分享海报
  244. * @param array arr2 海报素材
  245. * @param string store_name 素材文字
  246. * @param string price 价格
  247. * @param string ot_price 原始价格
  248. * @param function successFn 回调函数
  249. *
  250. *
  251. */
  252. PosterCanvas: function(fontColor, themeColor, siteName, arr2, store_name, price, ot_price, posterTitle, successFn) {
  253. let that = this;
  254. uni.showLoading({
  255. title: '海报生成中',
  256. mask: true
  257. });
  258. const ctx = uni.createCanvasContext('myCanvas');
  259. ctx.clearRect(0, 0, 0, 0);
  260. /**
  261. * 只能获取合法域名下的图片信息,本地调试无法获取
  262. *
  263. */
  264. ctx.fillStyle = '#fff';
  265. ctx.fillRect(0, 0, 750, 1306);
  266. uni.getImageInfo({
  267. src: arr2[0],
  268. success: function(res) {
  269. const WIDTH = res.width;
  270. // const HEIGHT = res.height;
  271. // ctx.drawImage(arr2[0], 0, 0, WIDTH, 1050);
  272. ctx.save();
  273. ctx.setFillStyle(themeColor);
  274. ctx.fillRect(0, 0, WIDTH, 108);
  275. ctx.setFontSize(33)
  276. ctx.setFillStyle('#fff');
  277. ctx.setTextAlign('center');
  278. ctx.fillText(posterTitle, WIDTH/2, 65);
  279. ctx.save();
  280. ctx.drawImage(arr2[1], 32, 141, 685, 685);
  281. ctx.save();
  282. // ctx.setFillStyle(themeColor);
  283. // ctx.fillRect(32, 847, 85, 40);
  284. // ctx.setFontSize(30)
  285. // ctx.setFillStyle('#fff');
  286. // ctx.setTextAlign('left');
  287. // ctx.fillText('商城', 43, 878);
  288. // ctx.save();
  289. // ctx.setFontSize(36)
  290. // ctx.setFillStyle('#000');
  291. // ctx.fillText(siteName, 140, 880);
  292. // ctx.save();
  293. const CONTENT_ROW_LENGTH = 35;
  294. let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name,
  295. CONTENT_ROW_LENGTH);
  296. if (contentRows > 2) {
  297. contentRows = 2;
  298. let textArray = contentArray.slice(0, 2);
  299. textArray[textArray.length - 1] += '...';
  300. contentArray = textArray;
  301. }
  302. ctx.setTextAlign('left');
  303. ctx.setFontSize(38);
  304. ctx.setFillStyle('#000');
  305. let contentHh = 38;
  306. for (let m = 0; m < contentArray.length; m++) {
  307. if (m) {
  308. ctx.fillText(contentArray[m], 30, 900 + contentHh * m + 18);
  309. } else {
  310. ctx.fillText(contentArray[m], 30, 900 + contentHh * m);
  311. }
  312. }
  313. ctx.save();
  314. ctx.setTextAlign('left')
  315. ctx.setFontSize(32);
  316. ctx.setFillStyle('#999');
  317. ctx.fillText('¥' + ot_price, 30, 1152 + contentHh);
  318. var underline = function(ctx, text, x, y, size, color, thickness, offset) {
  319. var width = ctx.measureText(text).width;
  320. switch (ctx.textAlign) {
  321. case "center":
  322. x -= (width / 2);
  323. break;
  324. case "right":
  325. x -= width;
  326. break;
  327. }
  328. y += size + offset;
  329. ctx.beginPath();
  330. ctx.strokeStyle = color;
  331. ctx.lineWidth = thickness;
  332. ctx.moveTo(x, y);
  333. ctx.lineTo(x + width, y);
  334. ctx.stroke();
  335. }
  336. underline(ctx, '¥' + ot_price, 30, 1146, 32, '#999', 2, 0);
  337. ctx.save();
  338. ctx.setTextAlign('left')
  339. ctx.setFontSize(54);
  340. ctx.setFillStyle(fontColor);
  341. ctx.fillText('¥' + price, 20, 1070 + contentHh);
  342. ctx.save();
  343. ctx.setFillStyle('#F5F5F5');
  344. ctx.fillRect(0, 1222, WIDTH, 84);
  345. ctx.setFontSize(28);
  346. ctx.setFillStyle('#999');
  347. // ctx.fillText('长按识别图中的二维码查看商品详情', 26, 1263);
  348. ctx.setTextAlign('center');
  349. ctx.fillText('长按识别图中的二维码查看商品详情', WIDTH/2, 1272);
  350. ctx.save();
  351. let r = 93;
  352. let d = r * 2;
  353. let cx = WIDTH - d - 30;
  354. // let cy = 1112;
  355. let cy = 1000;
  356. ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
  357. ctx.drawImage(arr2[2], cx, cy, d, d);
  358. ctx.restore();
  359. // ctx.setTextAlign('left')
  360. // ctx.setFontSize(28);
  361. // ctx.setFillStyle('#999');
  362. // ctx.fillText('长按或扫描查看', 490, 1030 + contentHh);
  363. ctx.draw(true, function() {
  364. uni.canvasToTempFilePath({
  365. canvasId: 'myCanvas',
  366. fileType: 'png',
  367. // destWidth: WIDTH,
  368. // destHeight: HEIGHT,
  369. success: function(res) {
  370. uni.hideLoading();
  371. successFn && successFn(res.tempFilePath);
  372. }
  373. })
  374. });
  375. },
  376. fail: function(err) {
  377. uni.hideLoading();
  378. that.Tips({
  379. title: '无法获取图片信息'
  380. });
  381. }
  382. })
  383. },
  384. /**
  385. * 获取砍价/拼团海报
  386. * @param array arr2 海报素材 背景图
  387. * @param string store_name 素材文字
  388. * @param string price 价格
  389. * @param string ot_price 原始价格
  390. * @param function successFn 回调函数
  391. *
  392. *
  393. */
  394. bargainPosterCanvas: function(arr2, title, label, msg, price, wd, hg, successFn) {
  395. let that = this;
  396. const ctx = uni.createCanvasContext('myCanvas');
  397. ctx.clearRect(0, 0, 0, 0);
  398. /**
  399. * 只能获取合法域名下的图片信息,本地调试无法获取
  400. *
  401. */
  402. ctx.fillStyle = '#fff';
  403. ctx.fillRect(0, 0, wd * 2, hg * 2);
  404. uni.getImageInfo({
  405. src: arr2[0],
  406. success: function(res) {
  407. const WIDTH = res.width;
  408. const HEIGHT = res.height;
  409. ctx.drawImage(arr2[0], 0, 0, wd, hg);
  410. // 保证在不同机型对应坐标准确
  411. let labelx = 0.6500 //标签x
  412. let labely = 0.166 //标签y
  413. let pricex = 0.1857 //价格x
  414. let pricey = 0.180 //价格x
  415. let codex = 0.385 //二维码
  416. let codey = 0.77
  417. let picturex = 0.1571 //商品图左上点
  418. let picturey = 0.2916
  419. let picturebx = 0.6857 //商品图右下点
  420. let pictureby = 0.3916
  421. let msgx = 0.1036 //msg
  422. let msgy = 0.2306
  423. let codew = 0.25
  424. ctx.drawImage(arr2[1], wd * picturex, hg * picturey, wd * picturebx, hg * pictureby);
  425. ctx.drawImage(arr2[2], wd * codex, hg * codey, wd * codew, wd * codew);
  426. ctx.save();
  427. //标题
  428. const CONTENT_ROW_LENGTH = 28;
  429. let [contentLeng, contentArray, contentRows] = that.textByteLength(title,
  430. CONTENT_ROW_LENGTH);
  431. if (contentRows > 2) {
  432. contentRows = 2;
  433. let textArray = contentArray.slice(0, 2);
  434. textArray[textArray.length - 1] += '…';
  435. contentArray = textArray;
  436. }
  437. ctx.setTextAlign('left');
  438. ctx.setFillStyle('#000');
  439. if (contentArray.length < 2) {
  440. ctx.setFontSize(20);
  441. } else {
  442. ctx.setFontSize(20);
  443. }
  444. let contentHh = 8;
  445. for (let m = 0; m < contentArray.length; m++) {
  446. if (m) {
  447. ctx.fillText(contentArray[m], wd * msgx, 35 + contentHh * m + 18, 1100);
  448. } else {
  449. ctx.fillText(contentArray[m], wd * msgx, 35, 1100);
  450. }
  451. }
  452. // 标签内容
  453. ctx.setTextAlign('left')
  454. ctx.setFontSize(16);
  455. ctx.setFillStyle('#FFF');
  456. ctx.fillText(label, wd * labelx, hg * labely);
  457. ctx.save();
  458. // 价格
  459. ctx.setFillStyle('red');
  460. ctx.setFontSize(26);
  461. ctx.fillText(price, wd * pricex, hg * pricey);
  462. ctx.save();
  463. // msg
  464. ctx.setFillStyle('#333');
  465. ctx.setFontSize(16);
  466. ctx.fillText(msg, wd * msgx, hg * msgy);
  467. ctx.save();
  468. ctx.draw(true, () => {
  469. uni.canvasToTempFilePath({
  470. canvasId: 'myCanvas',
  471. fileType: 'png',
  472. quality: 1,
  473. success: (res) => {
  474. successFn && successFn(res.tempFilePath);
  475. uni.hideLoading();
  476. }
  477. })
  478. });
  479. },
  480. fail: function(err) {
  481. uni.hideLoading();
  482. that.Tips({
  483. title: '无法获取图片信息'
  484. });
  485. }
  486. })
  487. },
  488. /**
  489. * 用户信息分享海报
  490. * @param array arr2 海报素材 1背景 0二维码
  491. * @param string nickname 昵称
  492. * @param string sitename 价格
  493. * @param function successFn 回调函数
  494. *
  495. *
  496. */
  497. userPosterCanvas: function(arr2, nickname, sitename, index, w, h, successFn) {
  498. let that = this;
  499. // uni.showLoading({
  500. // title: '海报生成中',
  501. // mask: true
  502. // });
  503. const ctx = uni.createCanvasContext('myCanvas' + index);
  504. ctx.clearRect(0, 0, 0, 0);
  505. /**
  506. * 只能获取合法域名下的图片信息,本地调试无法获取
  507. *
  508. */
  509. uni.getImageInfo({
  510. src: arr2[1],
  511. success: function(res) {
  512. const WIDTH = res.width;
  513. const HEIGHT = res.height;
  514. ctx.fillStyle = '#fff';
  515. ctx.fillRect(0, 0, w, h);
  516. ctx.drawImage(arr2[1], 0, 0, w, h);
  517. ctx.setTextAlign('left')
  518. ctx.setFontSize(12);
  519. ctx.setFillStyle('#333');
  520. // x:240 y:426
  521. let codex = 0.1906
  522. let codey = 0.7746
  523. let codeSize = 0.21666
  524. let namex = 0.4283
  525. let namey = 0.8215
  526. let markx = 0.4283
  527. let marky = 0.8685
  528. ctx.drawImage(arr2[0], w * codex, h * codey, w * codeSize, w * codeSize);
  529. if (w < 270) {
  530. ctx.setFontSize(8);
  531. } else {
  532. ctx.setFontSize(10);
  533. }
  534. ctx.fillText(nickname, w * namex, h * namey);
  535. if (w < 270) {
  536. ctx.setFontSize(8);
  537. } else {
  538. ctx.setFontSize(10);
  539. }
  540. const CONTENT_ROW_LENGTH = 30;
  541. const store_name = '邀请您加入'+sitename;
  542. let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name,
  543. CONTENT_ROW_LENGTH);
  544. if (contentRows > 2) {
  545. contentRows = 2;
  546. let textArray = contentArray.slice(0, 2);
  547. textArray[textArray.length - 1] += '...';
  548. contentArray = textArray;
  549. }
  550. ctx.setTextAlign('left');
  551. // ctx.setFontSize(38);
  552. ctx.setFillStyle('#000');
  553. let contentHh = 38;
  554. for (let m = 0; m < contentArray.length; m++) {
  555. if (m) {
  556. ctx.fillText(contentArray[m], w * markx, h * marky+14);
  557. } else {
  558. ctx.fillText(contentArray[m], w * markx, h * marky);
  559. }
  560. }
  561. // ctx.fillText('邀请您加入' + sitename, w * markx, h * marky);
  562. ctx.save();
  563. ctx.draw(true, function() {
  564. uni.canvasToTempFilePath({
  565. canvasId: 'myCanvas' + index,
  566. fileType: 'png',
  567. quality: 1,
  568. success: function(res) {
  569. uni.hideLoading();
  570. successFn && successFn(res.tempFilePath);
  571. }
  572. })
  573. });
  574. },
  575. fail: function(err) {
  576. uni.hideLoading();
  577. that.Tips({
  578. title: ''
  579. });
  580. }
  581. })
  582. },
  583. // uniapp 判断 IOS和Android的GPS是否开启
  584. checkOpenGPSServiceByAndroidIOS() {
  585. let system = uni.getSystemInfoSync(); // 获取系统信息
  586. console.log('yyy', system);
  587. if (system.platform === 'android') { // 判断平台
  588. var context = plus.android.importClass("android.content.Context");
  589. var locationManager = plus.android.importClass("android.location.LocationManager");
  590. var main = plus.android.runtimeMainActivity();
  591. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  592. if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
  593. return false
  594. } else {
  595. return true
  596. }
  597. } else if (system.platform === 'ios' || 'devtools') {
  598. var cllocationManger = plus.ios.import("CLLocationManager");
  599. var enable = cllocationManger.locationServicesEnabled();
  600. var status = cllocationManger.authorizationStatus();
  601. plus.ios.deleteObject(cllocationManger);
  602. if (enable && status != 2) {
  603. return true
  604. console.log("手机系统的定位已经打开");
  605. } else {
  606. return false
  607. console.log("手机系统的定位没有打开");
  608. }
  609. }
  610. },
  611. /*
  612. * 单图上传
  613. * @param object opt
  614. * @param callable successCallback 成功执行方法 data
  615. * @param callable errorCallback 失败执行方法
  616. */
  617. uploadImageOne: function(opt, successCallback, errorCallback) {
  618. let that = this;
  619. if (typeof opt === 'string') {
  620. let url = opt;
  621. opt = {};
  622. opt.url = url;
  623. }
  624. let count = opt.count || 1,
  625. sizeType = opt.sizeType || ['compressed'],
  626. sourceType = opt.sourceType || ['album', 'camera'],
  627. is_load = opt.is_load || true,
  628. uploadUrl = opt.url || '',
  629. inputName = opt.name || 'pics',
  630. fileType = opt.fileType || 'image';
  631. //console.log(sourceType,'sourceType')
  632. uni.chooseImage({
  633. count: count, //最多可以选择的图片总数
  634. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  635. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  636. success: function(res) {
  637. console.log(res,'88')
  638. //启动上传等待中...
  639. uni.showLoading({
  640. title: '图片上传中',
  641. });
  642. uni.uploadFile({
  643. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  644. filePath: res.tempFilePaths[0],
  645. fileType: fileType,
  646. name: inputName,
  647. formData: {
  648. 'filename': inputName
  649. },
  650. header: {
  651. // #ifdef MP
  652. "Content-Type": "multipart/form-data",
  653. // #endif
  654. [TOKENNAME]: 'Bearer ' + store.state.app.token
  655. },
  656. success: function(res) {
  657. uni.hideLoading();
  658. if (res.statusCode == 403) {
  659. that.Tips({
  660. title: res.data
  661. });
  662. } else if (res.statusCode == 413) {
  663. that.Tips({
  664. title: '上传图片失败,请重新上传小尺寸图片'
  665. });
  666. } else {
  667. let data = res.data ? JSON.parse(res.data) : {};
  668. if (data.status == 200) {
  669. successCallback && successCallback(data)
  670. } else {
  671. errorCallback && errorCallback(data);
  672. that.Tips({
  673. title: data.msg
  674. });
  675. }
  676. }
  677. },
  678. fail: function(res) {
  679. uni.hideLoading();
  680. that.Tips({
  681. title: '上传图片失败'
  682. });
  683. }
  684. })
  685. }
  686. })
  687. },
  688. /*
  689. * 单图上传压缩版
  690. * @param object opt
  691. * @param callable successCallback 成功执行方法 data
  692. * @param callable errorCallback 失败执行方法
  693. */
  694. uploadImageChange: function(opt, successCallback, errorCallback, sizeCallback) {
  695. let that = this;
  696. if (typeof opt === 'string') {
  697. let url = opt;
  698. opt = {};
  699. opt.url = url;
  700. }
  701. let count = opt.count || 1,
  702. sizeType = opt.sizeType || ['compressed'],
  703. sourceType = opt.sourceType || ['album', 'camera'],
  704. is_load = opt.is_load || true,
  705. uploadUrl = opt.url || '',
  706. inputName = opt.name || 'pics',
  707. fileType = opt.fileType || 'image';
  708. uni.chooseImage({
  709. count: count, //最多可以选择的图片总数
  710. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  711. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  712. success: function(res) {
  713. //启动上传等待中...
  714. let imgSrc
  715. let objImg = res.tempFilePaths;
  716. objImg.forEach(item => {
  717. uni.getImageInfo({
  718. src: item,
  719. success(ress) {
  720. uni.showLoading({
  721. title: '图片上传中',
  722. });
  723. if (res.tempFiles[0].size <= 2097152) {
  724. uploadImg(ress.path)
  725. return
  726. }
  727. // uploadImg(canvasPath.tempFilePath)
  728. let canvasWidth, canvasHeight, xs, maxWidth = 750
  729. xs = ress.width / ress.height // 宽高比例
  730. if (ress.width > maxWidth) {
  731. canvasWidth = maxWidth // 这里是最大限制宽度
  732. canvasHeight = maxWidth / xs
  733. } else {
  734. canvasWidth = ress.width
  735. canvasHeight = ress.height
  736. }
  737. sizeCallback && sizeCallback({
  738. w: canvasWidth,
  739. h: canvasHeight
  740. })
  741. let canvas = uni.createCanvasContext('canvas');
  742. canvas.width = canvasWidth
  743. canvas.height = canvasHeight
  744. canvas.clearRect(0, 0, canvasWidth, canvasHeight);
  745. canvas.drawImage(ress.path, 0, 0, canvasWidth, canvasHeight)
  746. canvas.save();
  747. // 这里的画布drawImage是一种异步属性 可能存在未绘制全就执行了draw的问题 so添加延迟
  748. setTimeout(e => {
  749. canvas.draw(true, () => {
  750. uni.canvasToTempFilePath({
  751. canvasId: 'canvas',
  752. fileType: 'JPEG',
  753. destWidth: canvasWidth,
  754. destHeight: canvasHeight,
  755. quality: 0.7,
  756. success: function(
  757. canvasPath) {
  758. uploadImg(
  759. canvasPath
  760. .tempFilePath
  761. )
  762. }
  763. })
  764. });
  765. }, 200)
  766. }
  767. })
  768. })
  769. }
  770. })
  771. function uploadImg(filePath) {
  772. uni.uploadFile({
  773. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  774. filePath,
  775. fileType: fileType,
  776. name: inputName,
  777. formData: {
  778. 'filename': inputName
  779. },
  780. header: {
  781. // #ifdef MP
  782. "Content-Type": "multipart/form-data",
  783. // #endif
  784. [TOKENNAME]: 'Bearer ' + store.state.app.token
  785. },
  786. success: function(res) {
  787. uni.hideLoading();
  788. if (res.statusCode == 403) {
  789. that.Tips({
  790. title: res.data
  791. });
  792. } else {
  793. let data = res.data ? JSON.parse(res.data) : {};
  794. if (data.status == 200) {
  795. successCallback && successCallback(data)
  796. } else {
  797. errorCallback && errorCallback(data);
  798. that.Tips({
  799. title: data.msg
  800. });
  801. }
  802. }
  803. },
  804. fail: function(res) {
  805. uni.hideLoading();
  806. that.Tips({
  807. title: '上传图片失败'
  808. });
  809. }
  810. })
  811. }
  812. },
  813. /**
  814. * 处理客服不同的跳转;
  815. *
  816. */
  817. getCustomer(userInfo, url, storeInfo, show) {
  818. let self = this;
  819. customerType().then(res => {
  820. let data = res.data;
  821. if (data.customer_type == 1) {
  822. uni.makePhoneCall({
  823. phoneNumber: data.customer_phone
  824. });
  825. } else if (data.customer_type == 2) {
  826. let href = data.customer_url;
  827. let hrefO = href + '?uid=' + userInfo.uid + '&nickName=' + userInfo.nickname + '&phone=' +
  828. userInfo.phone + '&sex=' + userInfo.sex + '&avatar=' + userInfo.avatar +
  829. '&openid=' + userInfo.openid;
  830. let hrefT = href + '&uid=' + userInfo.uid + '&nickName=' + userInfo.nickname + '&phone=' +
  831. userInfo.phone + '&sex=' + userInfo.sex + '&avatar=' + userInfo.avatar +
  832. '&openid=' + userInfo.openid;
  833. let urls = encodeURIComponent(href.indexOf('?') === -1 ? hrefO : hrefT);
  834. if (data.customer_url.indexOf('work.weixin.qq.com') > 0) {
  835. // #ifdef MP
  836. wx.openCustomerServiceChat({
  837. extInfo: {
  838. url: data.customer_url
  839. },
  840. corpId: data.wechat_work_corpid,
  841. showMessageCard: show?true:false,
  842. sendMessageTitle: show?storeInfo.store_name:'',
  843. sendMessagePath: show?storeInfo.path:'',
  844. sendMessageImg: show?storeInfo.image:'',
  845. success(res) {},
  846. fail(err) {
  847. self.Tips({
  848. title: err.errMsg
  849. });
  850. }
  851. })
  852. // #endif
  853. // #ifdef H5
  854. return window.location.href = data.customer_url
  855. // #endif
  856. // #ifdef APP-PLUS
  857. plus.runtime.openURL(data.customer_url)
  858. // #endif
  859. } else {
  860. uni.navigateTo({
  861. url: `/pages/annex/web_view/index?url=${urls}`
  862. });
  863. }
  864. } else {
  865. uni.navigateTo({
  866. url: url || '/pages/extension/customer_list/chat'
  867. })
  868. }
  869. }).catch(err => {
  870. self.Tips({
  871. title: err
  872. });
  873. })
  874. },
  875. /**
  876. * 小程序头像获取上传
  877. * @param uploadUrl 上传接口地址
  878. * @param filePath 上传文件路径
  879. * @param successCallback success回调
  880. * @param errorCallback err回调
  881. */
  882. uploadImgs(uploadUrl, filePath, successCallback, errorCallback) {
  883. let that = this;
  884. uni.uploadFile({
  885. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  886. filePath: filePath,
  887. fileType: 'image',
  888. name: 'pics',
  889. formData: {
  890. 'filename': 'pics'
  891. },
  892. header: {
  893. // #ifdef MP
  894. "Content-Type": "multipart/form-data",
  895. // #endif
  896. [TOKENNAME]: 'Bearer ' + store.state.app.token
  897. },
  898. success: (res) => {
  899. uni.hideLoading();
  900. if (res.statusCode == 403) {
  901. that.Tips({
  902. title: res.data
  903. });
  904. } else if (res.statusCode == 413) {
  905. that.Tips({
  906. title: '上传图片失败,请重新上传小尺寸图片'
  907. });
  908. } else {
  909. let data = res.data ? JSON.parse(res.data) : {};
  910. if (data.status == 200) {
  911. successCallback && successCallback(data)
  912. } else {
  913. errorCallback && errorCallback(data);
  914. that.Tips({
  915. title: data.msg
  916. });
  917. }
  918. }
  919. },
  920. fail: (err) => {
  921. uni.hideLoading();
  922. that.Tips({
  923. title: '上传图片失败'
  924. });
  925. }
  926. })
  927. },
  928. /**
  929. * 小程序比较版本信息
  930. * @param v1 当前版本
  931. * @param v2 进行比较的版本
  932. * @return boolen
  933. *
  934. */
  935. compareVersion(v1, v2) {
  936. v1 = v1.split('.')
  937. v2 = v2.split('.')
  938. const len = Math.max(v1.length, v2.length)
  939. while (v1.length < len) {
  940. v1.push('0')
  941. }
  942. while (v2.length < len) {
  943. v2.push('0')
  944. }
  945. for (let i = 0; i < len; i++) {
  946. const num1 = parseInt(v1[i])
  947. const num2 = parseInt(v2[i])
  948. if (num1 > num2) {
  949. return 1
  950. } else if (num1 < num2) {
  951. return -1
  952. }
  953. }
  954. return 0
  955. },
  956. /**
  957. * 处理服务器扫码带进来的参数
  958. * @param string param 扫码携带参数
  959. * @param string k 整体分割符 默认为:&
  960. * @param string p 单个分隔符 默认为:=
  961. * @return object
  962. *
  963. */
  964. // #ifdef MP
  965. getUrlParams: function(param, k, p) {
  966. if (typeof param != 'string') return {};
  967. k = k ? k : '&'; //整体参数分隔符
  968. p = p ? p : '='; //单个参数分隔符
  969. var value = {};
  970. if (param.indexOf(k) !== -1) {
  971. param = param.split(k);
  972. for (var val in param) {
  973. if (param[val].indexOf(p) !== -1) {
  974. var item = param[val].split(p);
  975. value[item[0]] = item[1];
  976. }
  977. }
  978. } else if (param.indexOf(p) !== -1) {
  979. var item = param.split(p);
  980. value[item[0]] = item[1];
  981. } else {
  982. return param;
  983. }
  984. return value;
  985. },
  986. // #endif
  987. /*
  988. * 合并数组
  989. */
  990. SplitArray(list, sp) {
  991. if (typeof list != 'object') return [];
  992. if (sp === undefined) sp = [];
  993. for (var i = 0; i < list.length; i++) {
  994. sp.push(list[i]);
  995. }
  996. return sp;
  997. },
  998. trim(backUrlCRshlcICwGdGY) {
  999. return String.prototype.trim.call(backUrlCRshlcICwGdGY);
  1000. },
  1001. $h: {
  1002. //除法函数,用来得到精确的除法结果
  1003. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  1004. //调用:$h.Div(arg1,arg2)
  1005. //返回值:arg1除以arg2的精确结果
  1006. Div: function(arg1, arg2) {
  1007. arg1 = parseFloat(arg1);
  1008. arg2 = parseFloat(arg2);
  1009. var t1 = 0,
  1010. t2 = 0,
  1011. r1, r2;
  1012. try {
  1013. t1 = arg1.toString().split(".")[1].length;
  1014. } catch (e) {}
  1015. try {
  1016. t2 = arg2.toString().split(".")[1].length;
  1017. } catch (e) {}
  1018. r1 = Number(arg1.toString().replace(".", ""));
  1019. r2 = Number(arg2.toString().replace(".", ""));
  1020. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  1021. },
  1022. //加法函数,用来得到精确的加法结果
  1023. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  1024. //调用:$h.Add(arg1,arg2)
  1025. //返回值:arg1加上arg2的精确结果
  1026. Add: function(arg1, arg2) {
  1027. arg2 = parseFloat(arg2);
  1028. var r1, r2, m;
  1029. try {
  1030. r1 = arg1.toString().split(".")[1].length
  1031. } catch (e) {
  1032. r1 = 0
  1033. }
  1034. try {
  1035. r2 = arg2.toString().split(".")[1].length
  1036. } catch (e) {
  1037. r2 = 0
  1038. }
  1039. m = Math.pow(100, Math.max(r1, r2));
  1040. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  1041. },
  1042. //减法函数,用来得到精确的减法结果
  1043. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  1044. //调用:$h.Sub(arg1,arg2)
  1045. //返回值:arg1减去arg2的精确结果
  1046. Sub: function(arg1, arg2) {
  1047. arg1 = parseFloat(arg1);
  1048. arg2 = parseFloat(arg2);
  1049. var r1, r2, m, n;
  1050. try {
  1051. r1 = arg1.toString().split(".")[1].length
  1052. } catch (e) {
  1053. r1 = 0
  1054. }
  1055. try {
  1056. r2 = arg2.toString().split(".")[1].length
  1057. } catch (e) {
  1058. r2 = 0
  1059. }
  1060. m = Math.pow(10, Math.max(r1, r2));
  1061. //动态控制精度长度
  1062. n = (r1 >= r2) ? r1 : r2;
  1063. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  1064. },
  1065. //乘法函数,用来得到精确的乘法结果
  1066. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  1067. //调用:$h.Mul(arg1,arg2)
  1068. //返回值:arg1乘以arg2的精确结果
  1069. Mul: function(arg1, arg2) {
  1070. arg1 = parseFloat(arg1);
  1071. arg2 = parseFloat(arg2);
  1072. var m = 0,
  1073. s1 = arg1.toString(),
  1074. s2 = arg2.toString();
  1075. try {
  1076. m += s1.split(".")[1].length
  1077. } catch (e) {}
  1078. try {
  1079. m += s2.split(".")[1].length
  1080. } catch (e) {}
  1081. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  1082. },
  1083. },
  1084. // 获取地理位置;
  1085. $L: {
  1086. async getLocation() {
  1087. // #ifdef APP-PLUS
  1088. let status = await this.checkPermission();
  1089. if (status !== 1) {
  1090. return;
  1091. }
  1092. // #endif
  1093. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  1094. let status = await this.getSetting();
  1095. if (status === 2) {
  1096. this.openSetting();
  1097. return;
  1098. }
  1099. // #endif
  1100. this.doGetLocation();
  1101. },
  1102. doGetLocation() {
  1103. uni.getLocation({
  1104. success: (res) => {
  1105. uni.removeStorageSync('CACHE_LONGITUDE');
  1106. uni.removeStorageSync('CACHE_LATITUDE');
  1107. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  1108. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  1109. },
  1110. fail: (err) => {
  1111. // #ifdef MP-BAIDU
  1112. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  1113. this.openSetting();
  1114. }
  1115. // #endif
  1116. // #ifndef MP-BAIDU
  1117. if (err.errMsg.indexOf("auth deny") >= 0) {
  1118. uni.showToast({
  1119. title: "访问位置被拒绝"
  1120. })
  1121. } else {
  1122. uni.showToast({
  1123. title: err.errMsg
  1124. })
  1125. }
  1126. // #endif
  1127. }
  1128. })
  1129. },
  1130. getSetting: function() {
  1131. return new Promise((resolve, reject) => {
  1132. uni.getSetting({
  1133. success: (res) => {
  1134. if (res.authSetting['scope.userLocation'] === undefined) {
  1135. resolve(0);
  1136. return;
  1137. }
  1138. if (res.authSetting['scope.userLocation']) {
  1139. resolve(1);
  1140. } else {
  1141. resolve(2);
  1142. }
  1143. }
  1144. });
  1145. });
  1146. },
  1147. openSetting: function() {
  1148. uni.openSetting({
  1149. success: (res) => {
  1150. if (res.authSetting && res.authSetting['scope.userLocation']) {
  1151. this.doGetLocation();
  1152. }
  1153. },
  1154. fail: (err) => {}
  1155. })
  1156. },
  1157. async checkPermission() {
  1158. let status = permision.isIOS ? await permision.requestIOS('location') :
  1159. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  1160. if (status === null || status === 1) {
  1161. status = 1;
  1162. } else if (status === 2) {
  1163. uni.showModal({
  1164. content: "系统定位已关闭",
  1165. confirmText: "确定",
  1166. showCancel: false,
  1167. success: function(res) {}
  1168. })
  1169. } else if (status.code) {
  1170. uni.showModal({
  1171. content: status.message
  1172. })
  1173. } else {
  1174. uni.showModal({
  1175. content: "需要定位权限",
  1176. confirmText: "设置",
  1177. success: function(res) {
  1178. if (res.confirm) {
  1179. permision.gotoAppSetting();
  1180. }
  1181. }
  1182. })
  1183. }
  1184. return status;
  1185. },
  1186. }
  1187. }