util.js 33 KB

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