util.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. // #ifdef APP
  620. let boolad = uni.getStorageSync("boolLoad") || false
  621. // if (permision.isIOS) {
  622. if (boolad) {
  623. upimg()
  624. } else {
  625. let title = '申请相册权限';
  626. let content = "是否允许访问相册用于上传图片?"
  627. uni.showModal({
  628. title,
  629. content,
  630. cancelText: '拒绝',
  631. confirmText: '允许',
  632. success: res => {
  633. if (res.confirm) {
  634. uni.setStorageSync("boolLoad", true)
  635. upimg()
  636. }
  637. },
  638. });
  639. }
  640. // #endif
  641. // #ifndef APP
  642. upimg()
  643. // #endif
  644. function upimg() {
  645. if (typeof opt === 'string') {
  646. let url = opt;
  647. opt = {};
  648. opt.url = url;
  649. }
  650. let count = opt.count || 1,
  651. sizeType = opt.sizeType || ['compressed'],
  652. sourceType = opt.sourceType || ['album', 'camera'],
  653. is_load = opt.is_load || true,
  654. uploadUrl = opt.url || '',
  655. inputName = opt.name || 'pics',
  656. fileType = opt.fileType || 'image';
  657. uni.chooseImage({
  658. count: count, //最多可以选择的图片总数
  659. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  660. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  661. success: function(res) {
  662. //启动上传等待中...
  663. uni.showLoading({
  664. title: '图片上传中',
  665. });
  666. uni.uploadFile({
  667. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  668. filePath: res.tempFilePaths[0],
  669. fileType: fileType,
  670. name: inputName,
  671. formData: {
  672. 'filename': inputName
  673. },
  674. header: {
  675. // #ifdef MP
  676. "Content-Type": "multipart/form-data",
  677. // #endif
  678. [TOKENNAME]: 'Bearer ' + store.state.app.token
  679. },
  680. success: function(res) {
  681. uni.hideLoading();
  682. if (res.statusCode == 403) {
  683. that.Tips({
  684. title: res.data
  685. });
  686. } else if (res.statusCode == 413) {
  687. that.Tips({
  688. title: '上传图片失败,请重新上传小尺寸图片'
  689. });
  690. } else {
  691. let data = res.data ? JSON.parse(res.data) : {};
  692. if (data.status == 200) {
  693. successCallback && successCallback(data)
  694. } else {
  695. errorCallback && errorCallback(data);
  696. that.Tips({
  697. title: data.msg
  698. });
  699. }
  700. }
  701. },
  702. fail: function(res) {
  703. uni.hideLoading();
  704. that.Tips({
  705. title: '上传图片失败'
  706. });
  707. }
  708. })
  709. }
  710. })
  711. }
  712. },
  713. /*
  714. * 单图上传压缩版
  715. * @param object opt
  716. * @param callable successCallback 成功执行方法 data
  717. * @param callable errorCallback 失败执行方法
  718. */
  719. uploadImageChange: function(opt, successCallback, errorCallback, sizeCallback) {
  720. let that = this;
  721. // #ifdef APP
  722. let boolad = uni.getStorageSync("boolLoad") || false
  723. // if (permision.isIOS) {
  724. if (boolad || plus.os.name == "iOS") {
  725. upimg()
  726. } else {
  727. let title = '申请相册权限';
  728. let content = "是否允许访问相册用于上传图片?"
  729. uni.showModal({
  730. title,
  731. content,
  732. cancelText: '拒绝',
  733. confirmText: '允许',
  734. success: res => {
  735. if (res.confirm) {
  736. uni.setStorageSync("boolLoad", true)
  737. upimg()
  738. }
  739. },
  740. });
  741. }
  742. // #endif
  743. // #ifndef APP
  744. upimg()
  745. // #endif
  746. function upimg() {
  747. if (typeof opt === 'string') {
  748. let url = opt;
  749. opt = {};
  750. opt.url = url;
  751. }
  752. let count = opt.count || 1,
  753. sizeType = opt.sizeType || ['compressed'],
  754. sourceType = opt.sourceType || ['album', 'camera'],
  755. is_load = opt.is_load || true,
  756. uploadUrl = opt.url || '',
  757. inputName = opt.name || 'pics',
  758. fileType = opt.fileType || 'image';
  759. uni.chooseImage({
  760. count: count, //最多可以选择的图片总数
  761. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  762. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  763. success: function(res) {
  764. //启动上传等待中...
  765. let imgSrc
  766. let objImg = res.tempFilePaths;
  767. objImg.forEach(item => {
  768. uni.getImageInfo({
  769. src: item,
  770. success(ress) {
  771. uni.showLoading({
  772. title: '图片上传中',
  773. });
  774. if (res.tempFiles[0].size <= 2097152) {
  775. uploadImg(ress.path)
  776. return
  777. }
  778. // uploadImg(canvasPath.tempFilePath)
  779. let canvasWidth, canvasHeight, xs, maxWidth = 750
  780. xs = ress.width / ress.height // 宽高比例
  781. if (ress.width > maxWidth) {
  782. canvasWidth = maxWidth // 这里是最大限制宽度
  783. canvasHeight = maxWidth / xs
  784. } else {
  785. canvasWidth = ress.width
  786. canvasHeight = ress.height
  787. }
  788. sizeCallback && sizeCallback({
  789. w: canvasWidth,
  790. h: canvasHeight
  791. })
  792. let canvas = uni.createCanvasContext('canvas');
  793. canvas.width = canvasWidth
  794. canvas.height = canvasHeight
  795. canvas.clearRect(0, 0, canvasWidth, canvasHeight);
  796. canvas.drawImage(ress.path, 0, 0, canvasWidth,
  797. canvasHeight)
  798. canvas.save();
  799. // 这里的画布drawImage是一种异步属性 可能存在未绘制全就执行了draw的问题 so添加延迟
  800. setTimeout(e => {
  801. canvas.draw(true, () => {
  802. uni.canvasToTempFilePath({
  803. canvasId: 'canvas',
  804. fileType: 'JPEG',
  805. destWidth: canvasWidth,
  806. destHeight: canvasHeight,
  807. quality: 0.7,
  808. success: function(
  809. canvasPath
  810. ) {
  811. uploadImg(
  812. canvasPath
  813. .tempFilePath
  814. )
  815. }
  816. })
  817. });
  818. }, 200)
  819. }
  820. })
  821. })
  822. }
  823. })
  824. }
  825. function uploadImg(filePath) {
  826. uni.uploadFile({
  827. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  828. filePath,
  829. fileType: fileType,
  830. name: inputName,
  831. formData: {
  832. 'filename': inputName
  833. },
  834. header: {
  835. // #ifdef MP
  836. "Content-Type": "multipart/form-data",
  837. // #endif
  838. [TOKENNAME]: 'Bearer ' + store.state.app.token
  839. },
  840. success: function(res) {
  841. uni.hideLoading();
  842. if (res.statusCode == 403) {
  843. that.Tips({
  844. title: res.data
  845. });
  846. } else {
  847. let data = res.data ? JSON.parse(res.data) : {};
  848. if (data.status == 200) {
  849. successCallback && successCallback(data)
  850. } else {
  851. errorCallback && errorCallback(data);
  852. that.Tips({
  853. title: data.msg
  854. });
  855. }
  856. }
  857. },
  858. fail: function(res) {
  859. uni.hideLoading();
  860. that.Tips({
  861. title: '上传图片失败'
  862. });
  863. }
  864. })
  865. }
  866. },
  867. /**
  868. * 处理客服不同的跳转;
  869. *
  870. */
  871. getCustomer(userInfo, url, storeInfo, show) {
  872. let self = this;
  873. customerType().then(res => {
  874. let data = res.data;
  875. if (data.customer_type == 1) {
  876. uni.makePhoneCall({
  877. phoneNumber: data.customer_phone
  878. });
  879. } else if (data.customer_type == 2) {
  880. let href = data.customer_url;
  881. let hrefO = href + '?uid=' + userInfo.uid + '&nickName=' + userInfo.nickname + '&phone=' +
  882. userInfo.phone + '&sex=' + userInfo.sex + '&avatar=' + userInfo.avatar +
  883. '&openid=' + userInfo.openid;
  884. let hrefT = href + '&uid=' + userInfo.uid + '&nickName=' + userInfo.nickname + '&phone=' +
  885. userInfo.phone + '&sex=' + userInfo.sex + '&avatar=' + userInfo.avatar +
  886. '&openid=' + userInfo.openid;
  887. let urls = encodeURIComponent(href.indexOf('?') === -1 ? hrefO : hrefT);
  888. if (data.customer_url.indexOf('work.weixin.qq.com') > 0) {
  889. // #ifdef MP
  890. wx.openCustomerServiceChat({
  891. extInfo: {
  892. url: data.customer_url
  893. },
  894. corpId: data.wechat_work_corpid,
  895. showMessageCard: show?true:false,
  896. sendMessageTitle: show?storeInfo.store_name:'',
  897. sendMessagePath: show?storeInfo.path:'',
  898. sendMessageImg: show?storeInfo.image:'',
  899. success(res) {},
  900. fail(err) {
  901. self.Tips({
  902. title: err.errMsg
  903. });
  904. }
  905. })
  906. // #endif
  907. // #ifdef H5
  908. return window.location.href = data.customer_url
  909. // #endif
  910. // #ifdef APP-PLUS
  911. plus.runtime.openURL(data.customer_url)
  912. // #endif
  913. } else {
  914. uni.navigateTo({
  915. url: `/pages/annex/web_view/index?url=${urls}`
  916. });
  917. }
  918. } else {
  919. uni.navigateTo({
  920. url: url || '/pages/extension/customer_list/chat'
  921. })
  922. }
  923. }).catch(err => {
  924. self.Tips({
  925. title: err
  926. });
  927. })
  928. },
  929. /**
  930. * 小程序头像获取上传
  931. * @param uploadUrl 上传接口地址
  932. * @param filePath 上传文件路径
  933. * @param successCallback success回调
  934. * @param errorCallback err回调
  935. */
  936. uploadImgs(uploadUrl, filePath, successCallback, errorCallback) {
  937. let that = this;
  938. uni.uploadFile({
  939. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  940. filePath: filePath,
  941. fileType: 'image',
  942. name: 'pics',
  943. formData: {
  944. 'filename': 'pics'
  945. },
  946. header: {
  947. // #ifdef MP
  948. "Content-Type": "multipart/form-data",
  949. // #endif
  950. [TOKENNAME]: 'Bearer ' + store.state.app.token
  951. },
  952. success: (res) => {
  953. uni.hideLoading();
  954. if (res.statusCode == 403) {
  955. that.Tips({
  956. title: res.data
  957. });
  958. } else if (res.statusCode == 413) {
  959. that.Tips({
  960. title: '上传图片失败,请重新上传小尺寸图片'
  961. });
  962. } else {
  963. let data = res.data ? JSON.parse(res.data) : {};
  964. if (data.status == 200) {
  965. successCallback && successCallback(data)
  966. } else {
  967. errorCallback && errorCallback(data);
  968. that.Tips({
  969. title: data.msg
  970. });
  971. }
  972. }
  973. },
  974. fail: (err) => {
  975. uni.hideLoading();
  976. that.Tips({
  977. title: '上传图片失败'
  978. });
  979. }
  980. })
  981. },
  982. /**
  983. * 小程序比较版本信息
  984. * @param v1 当前版本
  985. * @param v2 进行比较的版本
  986. * @return boolen
  987. *
  988. */
  989. compareVersion(v1, v2) {
  990. v1 = v1.split('.')
  991. v2 = v2.split('.')
  992. const len = Math.max(v1.length, v2.length)
  993. while (v1.length < len) {
  994. v1.push('0')
  995. }
  996. while (v2.length < len) {
  997. v2.push('0')
  998. }
  999. for (let i = 0; i < len; i++) {
  1000. const num1 = parseInt(v1[i])
  1001. const num2 = parseInt(v2[i])
  1002. if (num1 > num2) {
  1003. return 1
  1004. } else if (num1 < num2) {
  1005. return -1
  1006. }
  1007. }
  1008. return 0
  1009. },
  1010. /**
  1011. * 处理服务器扫码带进来的参数
  1012. * @param string param 扫码携带参数
  1013. * @param string k 整体分割符 默认为:&
  1014. * @param string p 单个分隔符 默认为:=
  1015. * @return object
  1016. *
  1017. */
  1018. // #ifdef MP
  1019. getUrlParams: function(param, k, p) {
  1020. if (typeof param != 'string') return {};
  1021. k = k ? k : '&'; //整体参数分隔符
  1022. p = p ? p : '='; //单个参数分隔符
  1023. var value = {};
  1024. if (param.indexOf(k) !== -1) {
  1025. param = param.split(k);
  1026. for (var val in param) {
  1027. if (param[val].indexOf(p) !== -1) {
  1028. var item = param[val].split(p);
  1029. value[item[0]] = item[1];
  1030. }
  1031. }
  1032. } else if (param.indexOf(p) !== -1) {
  1033. var item = param.split(p);
  1034. value[item[0]] = item[1];
  1035. } else {
  1036. return param;
  1037. }
  1038. return value;
  1039. },
  1040. // #endif
  1041. /*
  1042. * 合并数组
  1043. */
  1044. SplitArray(list, sp) {
  1045. if (typeof list != 'object') return [];
  1046. if (sp === undefined) sp = [];
  1047. for (var i = 0; i < list.length; i++) {
  1048. sp.push(list[i]);
  1049. }
  1050. return sp;
  1051. },
  1052. trim(backUrlCRshlcICwGdGY) {
  1053. return String.prototype.trim.call(backUrlCRshlcICwGdGY);
  1054. },
  1055. $h: {
  1056. //除法函数,用来得到精确的除法结果
  1057. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  1058. //调用:$h.Div(arg1,arg2)
  1059. //返回值:arg1除以arg2的精确结果
  1060. Div: function(arg1, arg2) {
  1061. arg1 = parseFloat(arg1);
  1062. arg2 = parseFloat(arg2);
  1063. var t1 = 0,
  1064. t2 = 0,
  1065. r1, r2;
  1066. try {
  1067. t1 = arg1.toString().split(".")[1].length;
  1068. } catch (e) {}
  1069. try {
  1070. t2 = arg2.toString().split(".")[1].length;
  1071. } catch (e) {}
  1072. r1 = Number(arg1.toString().replace(".", ""));
  1073. r2 = Number(arg2.toString().replace(".", ""));
  1074. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  1075. },
  1076. //加法函数,用来得到精确的加法结果
  1077. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  1078. //调用:$h.Add(arg1,arg2)
  1079. //返回值:arg1加上arg2的精确结果
  1080. Add: function(arg1, arg2) {
  1081. arg2 = parseFloat(arg2);
  1082. var r1, r2, m;
  1083. try {
  1084. r1 = arg1.toString().split(".")[1].length
  1085. } catch (e) {
  1086. r1 = 0
  1087. }
  1088. try {
  1089. r2 = arg2.toString().split(".")[1].length
  1090. } catch (e) {
  1091. r2 = 0
  1092. }
  1093. m = Math.pow(100, Math.max(r1, r2));
  1094. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  1095. },
  1096. //减法函数,用来得到精确的减法结果
  1097. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  1098. //调用:$h.Sub(arg1,arg2)
  1099. //返回值:arg1减去arg2的精确结果
  1100. Sub: function(arg1, arg2) {
  1101. arg1 = parseFloat(arg1);
  1102. arg2 = parseFloat(arg2);
  1103. var r1, r2, m, n;
  1104. try {
  1105. r1 = arg1.toString().split(".")[1].length
  1106. } catch (e) {
  1107. r1 = 0
  1108. }
  1109. try {
  1110. r2 = arg2.toString().split(".")[1].length
  1111. } catch (e) {
  1112. r2 = 0
  1113. }
  1114. m = Math.pow(10, Math.max(r1, r2));
  1115. //动态控制精度长度
  1116. n = (r1 >= r2) ? r1 : r2;
  1117. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  1118. },
  1119. //乘法函数,用来得到精确的乘法结果
  1120. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  1121. //调用:$h.Mul(arg1,arg2)
  1122. //返回值:arg1乘以arg2的精确结果
  1123. Mul: function(arg1, arg2) {
  1124. arg1 = parseFloat(arg1);
  1125. arg2 = parseFloat(arg2);
  1126. var m = 0,
  1127. s1 = arg1.toString(),
  1128. s2 = arg2.toString();
  1129. try {
  1130. m += s1.split(".")[1].length
  1131. } catch (e) {}
  1132. try {
  1133. m += s2.split(".")[1].length
  1134. } catch (e) {}
  1135. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  1136. },
  1137. },
  1138. // 获取地理位置;
  1139. $L: {
  1140. async getLocation() {
  1141. // #ifdef APP-PLUS
  1142. let status = await this.checkPermission();
  1143. if (status !== 1) {
  1144. return;
  1145. }
  1146. // #endif
  1147. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  1148. let status = await this.getSetting();
  1149. if (status === 2) {
  1150. this.openSetting();
  1151. return;
  1152. }
  1153. // #endif
  1154. this.doGetLocation();
  1155. },
  1156. doGetLocation() {
  1157. uni.getLocation({
  1158. success: (res) => {
  1159. uni.removeStorageSync('CACHE_LONGITUDE');
  1160. uni.removeStorageSync('CACHE_LATITUDE');
  1161. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  1162. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  1163. },
  1164. fail: (err) => {
  1165. // #ifdef MP-BAIDU
  1166. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  1167. this.openSetting();
  1168. }
  1169. // #endif
  1170. // #ifndef MP-BAIDU
  1171. if (err.errMsg.indexOf("auth deny") >= 0) {
  1172. uni.showToast({
  1173. title: "访问位置被拒绝"
  1174. })
  1175. } else {
  1176. uni.showToast({
  1177. title: err.errMsg
  1178. })
  1179. }
  1180. // #endif
  1181. }
  1182. })
  1183. },
  1184. getSetting: function() {
  1185. return new Promise((resolve, reject) => {
  1186. uni.getSetting({
  1187. success: (res) => {
  1188. if (res.authSetting['scope.userLocation'] === undefined) {
  1189. resolve(0);
  1190. return;
  1191. }
  1192. if (res.authSetting['scope.userLocation']) {
  1193. resolve(1);
  1194. } else {
  1195. resolve(2);
  1196. }
  1197. }
  1198. });
  1199. });
  1200. },
  1201. openSetting: function() {
  1202. uni.openSetting({
  1203. success: (res) => {
  1204. if (res.authSetting && res.authSetting['scope.userLocation']) {
  1205. this.doGetLocation();
  1206. }
  1207. },
  1208. fail: (err) => {}
  1209. })
  1210. },
  1211. async checkPermission() {
  1212. let status = permision.isIOS ? await permision.requestIOS('location') :
  1213. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  1214. if (status === null || status === 1) {
  1215. status = 1;
  1216. } else if (status === 2) {
  1217. uni.showModal({
  1218. content: "系统定位已关闭",
  1219. confirmText: "确定",
  1220. showCancel: false,
  1221. success: function(res) {}
  1222. })
  1223. } else if (status.code) {
  1224. uni.showModal({
  1225. content: status.message
  1226. })
  1227. } else {
  1228. uni.showModal({
  1229. content: "需要定位权限",
  1230. confirmText: "设置",
  1231. success: function(res) {
  1232. if (res.confirm) {
  1233. permision.gotoAppSetting();
  1234. }
  1235. }
  1236. })
  1237. }
  1238. return status;
  1239. },
  1240. }
  1241. }