util.js 33 KB

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