123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- import {
- TOKENNAME,
- HTTP_REQUEST_URL
- } from '../config/app.js';
- import store from '../store';
- import {
- pathToBase64
- } from '@/plugin/image-tools/index.js';
-
-
-
- export default {
-
- Tips: function(opt, to_url) {
- if (typeof opt == 'string') {
- to_url = opt;
- opt = {};
- }
- let title = opt.title || '',
- icon = opt.icon || 'none',
- endtime = opt.endtime || 2000,
- success = opt.success;
- if (title) uni.showToast({
- title: title,
- icon: icon,
- duration: endtime,
- success
- })
- if (to_url != undefined) {
- if (typeof to_url == 'object') {
- let tab = to_url.tab || 1,
- url = to_url.url || '';
- switch (tab) {
- case 1:
-
- setTimeout(function() {
- uni.switchTab({
- url: url
- })
- }, endtime);
- break;
- case 2:
-
- setTimeout(function() {
- uni.navigateTo({
- url: url,
- })
- }, endtime);
- break;
- case 3:
-
- setTimeout(function() {
-
- uni.navigateBack({
- delta: parseInt(url),
- })
-
-
- history.back();
-
- }, endtime);
- break;
- case 4:
-
- setTimeout(function() {
- uni.reLaunch({
- url: url,
- })
- }, endtime);
- break;
- case 5:
-
- setTimeout(function() {
- uni.redirectTo({
- url: url,
- })
- }, endtime);
- break;
- }
- } else if (typeof to_url == 'function') {
- setTimeout(function() {
- to_url && to_url();
- }, endtime);
- } else {
-
- setTimeout(function() {
- uni.navigateTo({
- url: to_url,
- })
- }, title ? endtime : 0);
- }
- }
- },
-
- ArrayRemove: function(array, index, value) {
- const valueArray = [];
- if (array instanceof Array) {
- for (let i = 0; i < array.length; i++) {
- if (typeof index == 'number' && array[index] != i) {
- valueArray.push(array[i]);
- } else if (typeof index == 'string' && array[i][index] != value) {
- valueArray.push(array[i]);
- }
- }
- }
- return valueArray;
- },
-
- textByteLength: function(text, num) {
- let strLength = 0;
- let rows = 1;
- let str = 0;
- let arr = [];
- for (let j = 0; j < text.length; j++) {
- if (text.charCodeAt(j) > 255) {
- strLength += 2;
- if (strLength > rows * num) {
- strLength++;
- arr.push(text.slice(str, j));
- str = j;
- rows++;
- }
- } else {
- strLength++;
- if (strLength > rows * num) {
- arr.push(text.slice(str, j));
- str = j;
- rows++;
- }
- }
- }
- arr.push(text.slice(str, text.length));
- return [strLength, arr, rows]
- },
-
- PosterCanvas: function(arr2, store_name, price, successFn) {
- let that = this;
- uni.showLoading({
- title: '海报生成中',
- mask: true
- });
- const ctx = uni.createCanvasContext('myCanvas');
- ctx.clearRect(0, 0, 0, 0);
-
- uni.getImageInfo({
- src: arr2[0],
- success: function(res) {
- console.log(res,'getImageInfo')
- const WIDTH = res.width;
- const HEIGHT = res.height;
- ctx.drawImage(arr2[0], 0, 0, WIDTH, HEIGHT);
- ctx.drawImage(arr2[1], 0, 0, WIDTH, WIDTH);
- ctx.save();
- let r = 90;
- let d = r * 2;
- let cx = 40;
- let cy = 990;
- ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
-
- ctx.drawImage(arr2[2], cx, cy,d,d);
- ctx.restore();
- const CONTENT_ROW_LENGTH = 40;
- let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name, CONTENT_ROW_LENGTH);
- if (contentRows > 2) {
- contentRows = 2;
- let textArray = contentArray.slice(0, 2);
- textArray[textArray.length - 1] += '……';
- contentArray = textArray;
- }
- ctx.setTextAlign('center');
- ctx.setFontSize(32);
- let contentHh = 32 * 1.3;
- for (let m = 0; m < contentArray.length; m++) {
- ctx.fillText(contentArray[m], WIDTH / 2, 820 + contentHh * m);
- }
- ctx.setTextAlign('center')
- ctx.setFontSize(48);
- ctx.setFillStyle('red');
- ctx.fillText('¥' + price, WIDTH / 2, 880 + contentHh);
- ctx.draw(true, function() {
- uni.canvasToTempFilePath({
- canvasId: 'myCanvas',
- fileType: 'png',
- destWidth: WIDTH,
- destHeight: HEIGHT,
- success: function(res) {
- uni.hideLoading();
- successFn && successFn(res.tempFilePath);
- }
- })
- });
- },
- fail: function(err) {
- uni.hideLoading();
- that.Tips({
- title: '无法获取图片信息'
- });
- }
- })
- },
-
- uploadImageOne: function(opt, successCallback, errorCallback) {
- let that = this;
- if (typeof opt === 'string') {
- let url = opt;
- opt = {};
- opt.url = url;
- }
- let count = opt.count || 1,
- sizeType = opt.sizeType || ['compressed'],
- sourceType = opt.sourceType || ['album', 'camera'],
- is_load = opt.is_load || true,
- uploadUrl = opt.url || '',
- inputName = opt.name || 'pics';
- uni.chooseImage({
- count: count,
- sizeType: sizeType,
- sourceType: sourceType,
- success: function(res) {
- console.log()
-
- uni.showLoading({
- title: '图片上传中',
- });
- uni.uploadFile({
- url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
- filePath: res.tempFilePaths[0],
- name: inputName,
- formData: {
- 'filename': inputName
- },
- header: {
-
- "Content-Type": "multipart/form-data",
-
- [TOKENNAME]: 'Bearer ' + store.state.app.token
- },
- success: function(res) {
- uni.hideLoading();
- if (res.statusCode == 403) {
- that.Tips({
- title: res.data
- });
- } else {
- let data = res.data ? JSON.parse(res.data) : {};
- if (data.status == 200) {
- successCallback && successCallback(data)
- } else {
- errorCallback && errorCallback(data);
- that.Tips({
- title: data.msg
- });
- }
- }
- },
- fail: function(res) {
- uni.hideLoading();
- that.Tips({
- title: '上传图片失败'
- });
- }
- })
-
-
-
-
-
-
-
-
- }
- })
- },
-
-
- getUrlParams: function(param, k, p) {
- if (typeof param != 'string') return {};
- k = k ? k : '&';
- p = p ? p : '=';
- var value = {};
- if (param.indexOf(k) !== -1) {
- param = param.split(k);
- for (var val in param) {
- if (param[val].indexOf(p) !== -1) {
- var item = param[val].split(p);
- value[item[0]] = item[1];
- }
- }
- } else if (param.indexOf(p) !== -1) {
- var item = param.split(p);
- value[item[0]] = item[1];
- } else {
- return param;
- }
- return value;
- },
-
-
- SplitArray(list, sp) {
- if (typeof list != 'object') return [];
- if (sp === undefined) sp = [];
- for (var i = 0; i < list.length; i++) {
- sp.push(list[i]);
- }
- return sp;
- },
- trim(str) {
- return String.prototype.trim.call(str);
- },
- $h: {
-
-
-
-
- Div: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var t1 = 0,
- t2 = 0,
- r1, r2;
- try {
- t1 = arg1.toString().split(".")[1].length;
- } catch (e) {}
- try {
- t2 = arg2.toString().split(".")[1].length;
- } catch (e) {}
- r1 = Number(arg1.toString().replace(".", ""));
- r2 = Number(arg2.toString().replace(".", ""));
- return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
- },
-
-
-
-
- Add: function(arg1, arg2) {
- arg2 = parseFloat(arg2);
- var r1, r2, m;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(100, Math.max(r1, r2));
- return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
- },
-
-
-
-
- Sub: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var r1, r2, m, n;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(10, Math.max(r1, r2));
-
- n = (r1 >= r2) ? r1 : r2;
- return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
- },
-
-
-
-
- Mul: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var m = 0,
- s1 = arg1.toString(),
- s2 = arg2.toString();
- try {
- m += s1.split(".")[1].length
- } catch (e) {}
- try {
- m += s2.split(".")[1].length
- } catch (e) {}
- return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
- },
- },
-
- $L: {
- async getLocation() {
-
- let status = await this.checkPermission();
- if (status !== 1) {
- return;
- }
-
-
- let status = await this.getSetting();
- if (status === 2) {
- this.openSetting();
- return;
- }
-
- this.doGetLocation();
- },
- doGetLocation() {
- uni.getLocation({
- success: (res) => {
- uni.removeStorageSync('CACHE_LONGITUDE');
- uni.removeStorageSync('CACHE_LATITUDE');
- uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
- uni.setStorageSync('CACHE_LATITUDE', res.latitude);
- },
- fail: (err) => {
-
- if (err.errCode === 202 || err.errCode === 10003) {
- this.openSetting();
- }
-
-
- if (err.errMsg.indexOf("auth deny") >= 0) {
- uni.showToast({
- title: "访问位置被拒绝"
- })
- } else {
- uni.showToast({
- title: err.errMsg
- })
- }
-
- }
- })
- },
- getSetting: function() {
- return new Promise((resolve, reject) => {
- uni.getSetting({
- success: (res) => {
- if (res.authSetting['scope.userLocation'] === undefined) {
- resolve(0);
- return;
- }
- if (res.authSetting['scope.userLocation']) {
- resolve(1);
- } else {
- resolve(2);
- }
- }
- });
- });
- },
- openSetting: function() {
- uni.openSetting({
- success: (res) => {
- if (res.authSetting && res.authSetting['scope.userLocation']) {
- this.doGetLocation();
- }
- },
- fail: (err) => {}
- })
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|