util.js 32 KB

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