util.js 34 KB

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