util.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. * @param array array 需要移除的数组
  118. * @param int index 需要移除的数组的键值
  119. * @param string | int 值
  120. * @return array
  121. *
  122. */
  123. ArrayRemove: function(array, index, value) {
  124. const valueArray = [];
  125. if (array instanceof Array) {
  126. for (let i = 0; i < array.length; i++) {
  127. if (typeof index == 'number' && array[index] != i) {
  128. valueArray.push(array[i]);
  129. } else if (typeof index == 'string' && array[i][index] != value) {
  130. valueArray.push(array[i]);
  131. }
  132. }
  133. }
  134. return valueArray;
  135. },
  136. /**
  137. * 生成海报获取文字
  138. * @param string text 为传入的文本
  139. * @param int num 为单行显示的字节长度
  140. * @return array
  141. */
  142. textByteLength: function(text, num) {
  143. let strLength = 0;
  144. let rows = 1;
  145. let str = 0;
  146. let arr = [];
  147. for (let j = 0; j < text.length; j++) {
  148. if (text.charCodeAt(j) > 255) {
  149. strLength += 2;
  150. if (strLength > rows * num) {
  151. strLength++;
  152. arr.push(text.slice(str, j));
  153. str = j;
  154. rows++;
  155. }
  156. } else {
  157. strLength++;
  158. if (strLength > rows * num) {
  159. arr.push(text.slice(str, j));
  160. str = j;
  161. rows++;
  162. }
  163. }
  164. }
  165. arr.push(text.slice(str, text.length));
  166. return [strLength, arr, rows] // [处理文字的总字节长度,每行显示内容的数组,行数]
  167. },
  168. /**
  169. * 获取分享海报
  170. * @param array arr2 海报素材
  171. * @param string store_name 素材文字
  172. * @param string price 价格
  173. * @param string ot_price 原始价格
  174. * @param function successFn 回调函数
  175. *
  176. *
  177. */
  178. PosterCanvas: function(fontColor, themeColor, siteName, arr2, store_name, price, ot_price, successFn) {
  179. let that = this;
  180. uni.showLoading({
  181. title: '海报生成中',
  182. mask: true
  183. });
  184. const ctx = uni.createCanvasContext('myCanvas');
  185. ctx.clearRect(0, 0, 0, 0);
  186. /**
  187. * 只能获取合法域名下的图片信息,本地调试无法获取
  188. *
  189. */
  190. ctx.fillStyle = '#fff';
  191. ctx.fillRect(0, 0, 750, 1300);
  192. uni.getImageInfo({
  193. src: arr2[0],
  194. success: function(res) {
  195. const WIDTH = res.width;
  196. // const HEIGHT = res.height;
  197. // ctx.drawImage(arr2[0], 0, 0, WIDTH, 1050);
  198. ctx.save();
  199. ctx.setFillStyle(themeColor);
  200. ctx.fillRect(0, 0, WIDTH, 108);
  201. ctx.setFontSize(33)
  202. ctx.setFillStyle('#fff');
  203. ctx.setTextAlign('center');
  204. ctx.fillText('品牌官方 · 交易保障 · 优质口碑 · 售后无忧', WIDTH/2, 65);
  205. ctx.save();
  206. ctx.drawImage(arr2[1], 32, 141, 685, 685);
  207. ctx.save();
  208. ctx.setFillStyle(themeColor);
  209. ctx.fillRect(32, 847, 85, 40);
  210. ctx.setFontSize(30)
  211. ctx.setFillStyle('#fff');
  212. ctx.setTextAlign('left');
  213. ctx.fillText('商城', 43, 878);
  214. ctx.save();
  215. ctx.setFontSize(36)
  216. ctx.setFillStyle('#000');
  217. ctx.fillText(siteName, 140, 880);
  218. ctx.save();
  219. const CONTENT_ROW_LENGTH = 36;
  220. let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name,
  221. CONTENT_ROW_LENGTH);
  222. if (contentRows > 2) {
  223. contentRows = 2;
  224. let textArray = contentArray.slice(0, 2);
  225. textArray[textArray.length - 1] += '...';
  226. contentArray = textArray;
  227. }
  228. ctx.setTextAlign('left');
  229. ctx.setFontSize(36);
  230. ctx.setFillStyle('#000');
  231. let contentHh = 36;
  232. for (let m = 0; m < contentArray.length; m++) {
  233. if (m) {
  234. ctx.fillText(contentArray[m], 30, 955 + contentHh * m + 18);
  235. } else {
  236. ctx.fillText(contentArray[m], 30, 955 + contentHh * m);
  237. }
  238. }
  239. ctx.save();
  240. ctx.setTextAlign('left')
  241. ctx.setFontSize(32);
  242. ctx.setFillStyle('#999');
  243. ctx.fillText('¥' + ot_price, 30, 1040 + contentHh);
  244. var underline = function(ctx, text, x, y, size, color, thickness, offset) {
  245. var width = ctx.measureText(text).width;
  246. switch (ctx.textAlign) {
  247. case "center":
  248. x -= (width / 2);
  249. break;
  250. case "right":
  251. x -= width;
  252. break;
  253. }
  254. y += size + offset;
  255. ctx.beginPath();
  256. ctx.strokeStyle = color;
  257. ctx.lineWidth = thickness;
  258. ctx.moveTo(x, y);
  259. ctx.lineTo(x + width, y);
  260. ctx.stroke();
  261. }
  262. underline(ctx, '¥' + ot_price, 30, 1032, 32, '#999', 2, 0);
  263. ctx.save();
  264. ctx.setTextAlign('left')
  265. ctx.setFontSize(55);
  266. ctx.setFillStyle(fontColor);
  267. ctx.fillText('¥' + price, 20, 1110 + contentHh);
  268. ctx.save();
  269. ctx.setFillStyle('#F5F5F5');
  270. ctx.fillRect(0, 1200, WIDTH, 113);
  271. ctx.setFontSize(30);
  272. ctx.setFillStyle('#999');
  273. ctx.fillText('长按识别图中的二维码查看商品详情', 26, 1263);
  274. ctx.save();
  275. let r = 80;
  276. let d = r * 2;
  277. let cx = 565;
  278. let cy = 1112;
  279. ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
  280. ctx.drawImage(arr2[2], cx, cy, d, d);
  281. ctx.restore();
  282. // ctx.setTextAlign('left')
  283. // ctx.setFontSize(28);
  284. // ctx.setFillStyle('#999');
  285. // ctx.fillText('长按或扫描查看', 490, 1030 + contentHh);
  286. ctx.draw(true, function() {
  287. uni.canvasToTempFilePath({
  288. canvasId: 'myCanvas',
  289. fileType: 'png',
  290. // destWidth: WIDTH,
  291. // destHeight: HEIGHT,
  292. success: function(res) {
  293. uni.hideLoading();
  294. successFn && successFn(res.tempFilePath);
  295. }
  296. })
  297. });
  298. },
  299. fail: function(err) {
  300. uni.hideLoading();
  301. that.Tips({
  302. title: '无法获取图片信息'
  303. });
  304. }
  305. })
  306. },
  307. /**
  308. * 获取砍价/拼团海报
  309. * @param array arr2 海报素材 背景图
  310. * @param string store_name 素材文字
  311. * @param string price 价格
  312. * @param string ot_price 原始价格
  313. * @param function successFn 回调函数
  314. *
  315. *
  316. */
  317. bargainPosterCanvas: function(arr2, title, label, msg, price, wd, hg, successFn) {
  318. let that = this;
  319. const ctx = uni.createCanvasContext('myCanvas');
  320. ctx.clearRect(0, 0, 0, 0);
  321. /**
  322. * 只能获取合法域名下的图片信息,本地调试无法获取
  323. *
  324. */
  325. ctx.fillStyle = '#fff';
  326. ctx.fillRect(0, 0, wd * 2, hg * 2);
  327. uni.getImageInfo({
  328. src: arr2[0],
  329. success: function(res) {
  330. const WIDTH = res.width;
  331. const HEIGHT = res.height;
  332. ctx.drawImage(arr2[0], 0, 0, wd, hg);
  333. // 保证在不同机型对应坐标准确
  334. let labelx = 0.6500 //标签x
  335. let labely = 0.166 //标签y
  336. let pricex = 0.1857 //价格x
  337. let pricey = 0.180 //价格x
  338. let codex = 0.385 //二维码
  339. let codey = 0.77
  340. let picturex = 0.1571 //商品图左上点
  341. let picturey = 0.2916
  342. let picturebx = 0.6857 //商品图右下点
  343. let pictureby = 0.3916
  344. let msgx = 0.1036 //msg
  345. let msgy = 0.2306
  346. let codew = 0.25
  347. ctx.drawImage(arr2[1], wd * picturex, hg * picturey, wd * picturebx, hg * pictureby);
  348. ctx.drawImage(arr2[2], wd * codex, hg * codey, wd * codew, wd * codew);
  349. ctx.save();
  350. //标题
  351. const CONTENT_ROW_LENGTH = 30;
  352. let [contentLeng, contentArray, contentRows] = that.textByteLength(title,
  353. CONTENT_ROW_LENGTH);
  354. if (contentRows > 2) {
  355. contentRows = 2;
  356. let textArray = contentArray.slice(0, 2);
  357. textArray[textArray.length - 1] += '…';
  358. contentArray = textArray;
  359. }
  360. ctx.setTextAlign('left');
  361. ctx.setFillStyle('#000');
  362. if (contentArray.length < 2) {
  363. ctx.setFontSize(22);
  364. } else {
  365. ctx.setFontSize(20);
  366. }
  367. let contentHh = 8;
  368. for (let m = 0; m < contentArray.length; m++) {
  369. if (m) {
  370. ctx.fillText(contentArray[m], 20, 35 + contentHh * m + 18, 1100);
  371. } else {
  372. ctx.fillText(contentArray[m], 20, 35, 1100);
  373. }
  374. }
  375. // 标签内容
  376. ctx.setTextAlign('left')
  377. ctx.setFontSize(16);
  378. ctx.setFillStyle('#FFF');
  379. ctx.fillText(label, wd * labelx, hg * labely);
  380. ctx.save();
  381. // 价格
  382. ctx.setFillStyle('red');
  383. ctx.setFontSize(26);
  384. ctx.fillText(price, wd * pricex, hg * pricey);
  385. ctx.save();
  386. // msg
  387. ctx.setFillStyle('#333');
  388. ctx.setFontSize(16);
  389. ctx.fillText(msg, wd * msgx, hg * msgy);
  390. ctx.save();
  391. ctx.draw(true, () => {
  392. uni.canvasToTempFilePath({
  393. canvasId: 'myCanvas',
  394. fileType: 'png',
  395. quality: 1,
  396. success: (res) => {
  397. successFn && successFn(res.tempFilePath);
  398. uni.hideLoading();
  399. }
  400. })
  401. });
  402. },
  403. fail: function(err) {
  404. uni.hideLoading();
  405. that.Tips({
  406. title: '无法获取图片信息'
  407. });
  408. }
  409. })
  410. },
  411. /**
  412. * 用户信息分享海报
  413. * @param array arr2 海报素材 1背景 0二维码
  414. * @param string nickname 昵称
  415. * @param string sitename 价格
  416. * @param function successFn 回调函数
  417. *
  418. *
  419. */
  420. userPosterCanvas: function(arr2, nickname, sitename, index, w, h, successFn) {
  421. let that = this;
  422. uni.showLoading({
  423. title: '海报生成中',
  424. mask: true
  425. });
  426. const ctx = uni.createCanvasContext('myCanvas' + index);
  427. ctx.clearRect(0, 0, 0, 0);
  428. /**
  429. * 只能获取合法域名下的图片信息,本地调试无法获取
  430. *
  431. */
  432. uni.getImageInfo({
  433. src: arr2[1],
  434. success: function(res) {
  435. const WIDTH = res.width;
  436. const HEIGHT = res.height;
  437. ctx.fillStyle = '#fff';
  438. ctx.fillRect(0, 0, w, h);
  439. ctx.drawImage(arr2[1], 0, 0, w, h);
  440. ctx.setTextAlign('left')
  441. ctx.setFontSize(12);
  442. ctx.setFillStyle('#333');
  443. // x:240 y:426
  444. let codex = 0.1906
  445. let codey = 0.7746
  446. let codeSize = 0.21666
  447. let namex = 0.4283
  448. let namey = 0.8215
  449. let markx = 0.4283
  450. let marky = 0.8685
  451. ctx.drawImage(arr2[0], w * codex, h * codey, w * codeSize, w * codeSize);
  452. if (w < 270) {
  453. ctx.setFontSize(8);
  454. } else {
  455. ctx.setFontSize(10);
  456. }
  457. ctx.fillText(nickname, w * namex, h * namey);
  458. if (w < 270) {
  459. ctx.setFontSize(8);
  460. } else {
  461. ctx.setFontSize(10);
  462. }
  463. ctx.fillText('邀请您加入' + sitename, w * markx, h * marky);
  464. ctx.save();
  465. ctx.draw(true, function() {
  466. uni.canvasToTempFilePath({
  467. canvasId: 'myCanvas' + index,
  468. fileType: 'png',
  469. quality: 1,
  470. success: function(res) {
  471. uni.hideLoading();
  472. successFn && successFn(res.tempFilePath);
  473. }
  474. })
  475. });
  476. },
  477. fail: function(err) {
  478. uni.hideLoading();
  479. that.Tips({
  480. title: '无法获取图片信息'
  481. });
  482. }
  483. })
  484. },
  485. /*
  486. * 单图上传
  487. * @param object opt
  488. * @param callable successCallback 成功执行方法 data
  489. * @param callable errorCallback 失败执行方法
  490. */
  491. uploadImageOne: function(opt, successCallback, errorCallback) {
  492. let that = this;
  493. if (typeof opt === 'string') {
  494. let url = opt;
  495. opt = {};
  496. opt.url = url;
  497. }
  498. let count = opt.count || 1,
  499. sizeType = opt.sizeType || ['compressed'],
  500. sourceType = opt.sourceType || ['album', 'camera'],
  501. is_load = opt.is_load || true,
  502. uploadUrl = opt.url || '',
  503. inputName = opt.name || 'pics',
  504. fileType = opt.fileType || 'image';
  505. uni.chooseImage({
  506. count: count, //最多可以选择的图片总数
  507. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  508. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  509. success: function(res) {
  510. //启动上传等待中...
  511. uni.showLoading({
  512. title: '图片上传中',
  513. });
  514. uni.uploadFile({
  515. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  516. filePath: res.tempFilePaths[0],
  517. fileType: fileType,
  518. name: inputName,
  519. formData: {
  520. 'filename': inputName
  521. },
  522. header: {
  523. // #ifdef MP
  524. "Content-Type": "multipart/form-data",
  525. // #endif
  526. [TOKENNAME]: 'Bearer ' + store.state.app.token
  527. },
  528. success: function(res) {
  529. uni.hideLoading();
  530. if (res.statusCode == 403) {
  531. that.Tips({
  532. title: res.data
  533. });
  534. } else if(res.statusCode == 413){
  535. that.Tips({
  536. title: '上传图片失败,请重新上传小尺寸图片'
  537. });
  538. }else {
  539. let data = res.data ? JSON.parse(res.data) : {};
  540. if (data.status == 200) {
  541. successCallback && successCallback(data)
  542. } else {
  543. errorCallback && errorCallback(data);
  544. that.Tips({
  545. title: data.msg
  546. });
  547. }
  548. }
  549. },
  550. fail: function(res) {
  551. uni.hideLoading();
  552. that.Tips({
  553. title: '上传图片失败'
  554. });
  555. }
  556. })
  557. }
  558. })
  559. },
  560. /*
  561. * 单图上传压缩版
  562. * @param object opt
  563. * @param callable successCallback 成功执行方法 data
  564. * @param callable errorCallback 失败执行方法
  565. */
  566. uploadImageChange: function(opt, successCallback, errorCallback, sizeCallback) {
  567. let that = this;
  568. if (typeof opt === 'string') {
  569. let url = opt;
  570. opt = {};
  571. opt.url = url;
  572. }
  573. let count = opt.count || 1,
  574. sizeType = opt.sizeType || ['compressed'],
  575. sourceType = opt.sourceType || ['album', 'camera'],
  576. is_load = opt.is_load || true,
  577. uploadUrl = opt.url || '',
  578. inputName = opt.name || 'pics',
  579. fileType = opt.fileType || 'image';
  580. uni.chooseImage({
  581. count: count, //最多可以选择的图片总数
  582. sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
  583. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  584. success: function(res) {
  585. //启动上传等待中...
  586. let imgSrc
  587. let objImg = res.tempFilePaths;
  588. objImg.forEach(item=>{
  589. uni.getImageInfo({
  590. src: item,
  591. success(ress) {
  592. uni.showLoading({
  593. title: '图片上传中',
  594. });
  595. if (res.tempFiles[0].size <= 2097152) {
  596. uploadImg(ress.path)
  597. return
  598. }
  599. // uploadImg(canvasPath.tempFilePath)
  600. let canvasWidth, canvasHeight, xs, maxWidth = 750
  601. xs = ress.width / ress.height // 宽高比例
  602. if (ress.width > maxWidth) {
  603. canvasWidth = maxWidth // 这里是最大限制宽度
  604. canvasHeight = maxWidth / xs
  605. } else {
  606. canvasWidth = ress.width
  607. canvasHeight = ress.height
  608. }
  609. sizeCallback && sizeCallback({
  610. w: canvasWidth,
  611. h: canvasHeight
  612. })
  613. let canvas = uni.createCanvasContext('canvas');
  614. canvas.width = canvasWidth
  615. canvas.height = canvasHeight
  616. canvas.clearRect(0, 0, canvasWidth, canvasHeight);
  617. canvas.drawImage(ress.path, 0, 0, canvasWidth, canvasHeight)
  618. canvas.save();
  619. // 这里的画布drawImage是一种异步属性 可能存在未绘制全就执行了draw的问题 so添加延迟
  620. setTimeout(e => {
  621. canvas.draw(true, () => {
  622. uni.canvasToTempFilePath({
  623. canvasId: 'canvas',
  624. fileType: 'JPEG',
  625. destWidth: canvasWidth,
  626. destHeight: canvasHeight,
  627. quality: 0.7,
  628. success: function(canvasPath) {
  629. uploadImg(canvasPath
  630. .tempFilePath)
  631. }
  632. })
  633. });
  634. }, 200)
  635. }
  636. })
  637. })
  638. }
  639. })
  640. function uploadImg(filePath) {
  641. uni.uploadFile({
  642. url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
  643. filePath,
  644. fileType: fileType,
  645. name: inputName,
  646. formData: {
  647. 'filename': inputName
  648. },
  649. header: {
  650. // #ifdef MP
  651. "Content-Type": "multipart/form-data",
  652. // #endif
  653. [TOKENNAME]: 'Bearer ' + store.state.app.token
  654. },
  655. success: function(res) {
  656. uni.hideLoading();
  657. if (res.statusCode == 403) {
  658. that.Tips({
  659. title: res.data
  660. });
  661. } else {
  662. let data = res.data ? JSON.parse(res.data) : {};
  663. if (data.status == 200) {
  664. successCallback && successCallback(data)
  665. } else {
  666. errorCallback && errorCallback(data);
  667. that.Tips({
  668. title: data.msg
  669. });
  670. }
  671. }
  672. },
  673. fail: function(res) {
  674. uni.hideLoading();
  675. that.Tips({
  676. title: '上传图片失败'
  677. });
  678. }
  679. })
  680. }
  681. },
  682. /**
  683. * 处理客服不同的跳转;
  684. *
  685. */
  686. getCustomer(userInfo,url){
  687. let self = this;
  688. customerType().then(res=>{
  689. let data = res.data;
  690. if (data.customer_type == 1) {
  691. uni.makePhoneCall({
  692. phoneNumber: data.customer_phone
  693. });
  694. } else if (data.customer_type == 2) {
  695. let href = data.customer_url;
  696. let hrefO = href + '?uid=' + userInfo.uid + '&nickName=' + userInfo.nickname + '&phone=' +
  697. userInfo.phone + '&sex=' + userInfo.sex + '&avatar=' + userInfo.avatar +
  698. '&openid=' + userInfo.openid;
  699. let hrefT = href + '&uid=' + userInfo.uid + '&nickName=' + userInfo.nickname + '&phone=' +
  700. userInfo.phone + '&sex=' + userInfo.sex + '&avatar=' + userInfo.avatar +
  701. '&openid=' + userInfo.openid;
  702. let urls = encodeURIComponent(href.indexOf('?') === -1 ? hrefO : hrefT);
  703. // #ifdef MP
  704. if (data.customer_url.indexOf('work.weixin.qq.com') > 0){
  705. uni.openCustomerServiceChat({
  706. extInfo: {
  707. url: data.customer_url
  708. },
  709. corpId: data.wechat_work_corpid,
  710. success(res) {},
  711. fail(err) {
  712. self.Tips({
  713. title: err.errMsg
  714. });
  715. }
  716. })
  717. }else{
  718. uni.navigateTo({
  719. url: `/pages/annex/web_view/index?url=${urls}`
  720. });
  721. }
  722. // #endif
  723. // #ifndef MP
  724. uni.navigateTo({
  725. url: `/pages/annex/web_view/index?url=${urls}`
  726. });
  727. // #endif
  728. } else {
  729. uni.navigateTo({
  730. url: url || '/pages/extension/customer_list/chat'
  731. })
  732. }
  733. }).catch(err=>{
  734. self.Tips({
  735. title: err
  736. });
  737. })
  738. },
  739. /**
  740. * 处理服务器扫码带进来的参数
  741. * @param string param 扫码携带参数
  742. * @param string k 整体分割符 默认为:&
  743. * @param string p 单个分隔符 默认为:=
  744. * @return object
  745. *
  746. */
  747. // #ifdef MP
  748. getUrlParams: function(param, k, p) {
  749. if (typeof param != 'string') return {};
  750. k = k ? k : '&'; //整体参数分隔符
  751. p = p ? p : '='; //单个参数分隔符
  752. var value = {};
  753. if (param.indexOf(k) !== -1) {
  754. param = param.split(k);
  755. for (var val in param) {
  756. if (param[val].indexOf(p) !== -1) {
  757. var item = param[val].split(p);
  758. value[item[0]] = item[1];
  759. }
  760. }
  761. } else if (param.indexOf(p) !== -1) {
  762. var item = param.split(p);
  763. value[item[0]] = item[1];
  764. } else {
  765. return param;
  766. }
  767. return value;
  768. },
  769. // #endif
  770. /*
  771. * 合并数组
  772. */
  773. SplitArray(list, sp) {
  774. if (typeof list != 'object') return [];
  775. if (sp === undefined) sp = [];
  776. for (var i = 0; i < list.length; i++) {
  777. sp.push(list[i]);
  778. }
  779. return sp;
  780. },
  781. trim(backUrlCRshlcICwGdGY) {
  782. return String.prototype.trim.call(backUrlCRshlcICwGdGY);
  783. },
  784. $h: {
  785. //除法函数,用来得到精确的除法结果
  786. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  787. //调用:$h.Div(arg1,arg2)
  788. //返回值:arg1除以arg2的精确结果
  789. Div: function(arg1, arg2) {
  790. arg1 = parseFloat(arg1);
  791. arg2 = parseFloat(arg2);
  792. var t1 = 0,
  793. t2 = 0,
  794. r1, r2;
  795. try {
  796. t1 = arg1.toString().split(".")[1].length;
  797. } catch (e) {}
  798. try {
  799. t2 = arg2.toString().split(".")[1].length;
  800. } catch (e) {}
  801. r1 = Number(arg1.toString().replace(".", ""));
  802. r2 = Number(arg2.toString().replace(".", ""));
  803. return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
  804. },
  805. //加法函数,用来得到精确的加法结果
  806. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  807. //调用:$h.Add(arg1,arg2)
  808. //返回值:arg1加上arg2的精确结果
  809. Add: function(arg1, arg2) {
  810. arg2 = parseFloat(arg2);
  811. var r1, r2, m;
  812. try {
  813. r1 = arg1.toString().split(".")[1].length
  814. } catch (e) {
  815. r1 = 0
  816. }
  817. try {
  818. r2 = arg2.toString().split(".")[1].length
  819. } catch (e) {
  820. r2 = 0
  821. }
  822. m = Math.pow(100, Math.max(r1, r2));
  823. return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
  824. },
  825. //减法函数,用来得到精确的减法结果
  826. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  827. //调用:$h.Sub(arg1,arg2)
  828. //返回值:arg1减去arg2的精确结果
  829. Sub: function(arg1, arg2) {
  830. arg1 = parseFloat(arg1);
  831. arg2 = parseFloat(arg2);
  832. var r1, r2, m, n;
  833. try {
  834. r1 = arg1.toString().split(".")[1].length
  835. } catch (e) {
  836. r1 = 0
  837. }
  838. try {
  839. r2 = arg2.toString().split(".")[1].length
  840. } catch (e) {
  841. r2 = 0
  842. }
  843. m = Math.pow(10, Math.max(r1, r2));
  844. //动态控制精度长度
  845. n = (r1 >= r2) ? r1 : r2;
  846. return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
  847. },
  848. //乘法函数,用来得到精确的乘法结果
  849. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  850. //调用:$h.Mul(arg1,arg2)
  851. //返回值:arg1乘以arg2的精确结果
  852. Mul: function(arg1, arg2) {
  853. arg1 = parseFloat(arg1);
  854. arg2 = parseFloat(arg2);
  855. var m = 0,
  856. s1 = arg1.toString(),
  857. s2 = arg2.toString();
  858. try {
  859. m += s1.split(".")[1].length
  860. } catch (e) {}
  861. try {
  862. m += s2.split(".")[1].length
  863. } catch (e) {}
  864. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  865. },
  866. },
  867. // 获取地理位置;
  868. $L: {
  869. async getLocation() {
  870. // #ifdef APP-PLUS
  871. let status = await this.checkPermission();
  872. if (status !== 1) {
  873. return;
  874. }
  875. // #endif
  876. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  877. let status = await this.getSetting();
  878. if (status === 2) {
  879. this.openSetting();
  880. return;
  881. }
  882. // #endif
  883. this.doGetLocation();
  884. },
  885. doGetLocation() {
  886. uni.getLocation({
  887. success: (res) => {
  888. uni.removeStorageSync('CACHE_LONGITUDE');
  889. uni.removeStorageSync('CACHE_LATITUDE');
  890. uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
  891. uni.setStorageSync('CACHE_LATITUDE', res.latitude);
  892. },
  893. fail: (err) => {
  894. // #ifdef MP-BAIDU
  895. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  896. this.openSetting();
  897. }
  898. // #endif
  899. // #ifndef MP-BAIDU
  900. if (err.errMsg.indexOf("auth deny") >= 0) {
  901. uni.showToast({
  902. title: "访问位置被拒绝"
  903. })
  904. } else {
  905. uni.showToast({
  906. title: err.errMsg
  907. })
  908. }
  909. // #endif
  910. }
  911. })
  912. },
  913. getSetting: function() {
  914. return new Promise((resolve, reject) => {
  915. uni.getSetting({
  916. success: (res) => {
  917. if (res.authSetting['scope.userLocation'] === undefined) {
  918. resolve(0);
  919. return;
  920. }
  921. if (res.authSetting['scope.userLocation']) {
  922. resolve(1);
  923. } else {
  924. resolve(2);
  925. }
  926. }
  927. });
  928. });
  929. },
  930. openSetting: function() {
  931. uni.openSetting({
  932. success: (res) => {
  933. if (res.authSetting && res.authSetting['scope.userLocation']) {
  934. this.doGetLocation();
  935. }
  936. },
  937. fail: (err) => {}
  938. })
  939. },
  940. async checkPermission() {
  941. let status = permision.isIOS ? await permision.requestIOS('location') :
  942. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  943. if (status === null || status === 1) {
  944. status = 1;
  945. } else if (status === 2) {
  946. uni.showModal({
  947. content: "系统定位已关闭",
  948. confirmText: "确定",
  949. showCancel: false,
  950. success: function(res) {}
  951. })
  952. } else if (status.code) {
  953. uni.showModal({
  954. content: status.message
  955. })
  956. } else {
  957. uni.showModal({
  958. content: "需要定位权限",
  959. confirmText: "设置",
  960. success: function(res) {
  961. if (res.confirm) {
  962. permision.gotoAppSetting();
  963. }
  964. }
  965. })
  966. }
  967. return status;
  968. },
  969. }
  970. }