1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038 |
- var encrypt = require('./encrypt.js');
- var cache = require('./cache.js');
- var math = require('./math.js');
- var qqMap = require("./qqmap-wx-jssdk");
- var qrCode = require("./weapp.qrcode.min");
- module.exports = {
-
- config: {
- name: '船百知-商家端',
- debug: false,
- version: 'xcx',
- baseUrl: 'https://ship-expert.zhousi.hdlkeji.com',
- qqMap: false,
- loading: false
- },
-
- sha1: function(str, raw) {
- return encrypt.sha1(str, raw);
- },
-
- md5: function(str) {
- return encrypt.md5(str);
- },
-
- cache: cache,
-
- math: math,
-
- qqMap: qqMap,
-
- info: function(title, icon, success, time, mask) {
- if (typeof icon == 'function') {
- success = icon;
- icon = "none";
- }
- title = title == undefined ? "系统繁忙" : title;
- icon = icon == undefined ? "none" : icon;
- time = time == undefined ? 1000 : time;
- mask = mask == undefined ? true : mask;
- uni.showToast({
- title: title,
- icon: icon,
- mask: mask,
- duration: time,
- success: setTimeout(function() {
- success && success()
- }, time)
- });
- },
-
- modal: function(content, success, cancel, confirmText, cancelText) {
- let opt = {};
- opt.title = this.config.name;
- opt.content = content;
-
- opt.showCancel = false;
- if (typeof cancel == 'function' || (typeof cancel == 'boolean' && cancel)) {
- opt.showCancel = true;
- } else if (typeof cancel == 'string') {
- cancelText = confirmText;
- confirmText = cancel;
- }
- if (!this.isEmpty(confirmText)) {
- opt.confirmText = confirmText;
- }
- if (!this.isEmpty(cancelText)) {
- opt.cancelText = cancelText;
- opt.showCancel = true;
- }
- opt.success = function(res) {
- if (res.confirm) {
- success && success();
- } else if (res.cancel) {
- typeof cancel == 'function' && cancel();
- }
- };
- uni.showModal(opt);
- },
-
- loading: function(title) {
- var that = this;
- title = typeof title == 'string' ? title : "加载中...";
- !that.config.loading && uni.showLoading({
- title: title,
- mask: true,
- success: function() {
- that.config.loading = true;
- }
- });
- },
-
- closeLoading: function() {
- var that = this;
- that.config.loading && uni.hideLoading({
- noConflict: true,
- success: function() {
- that.config.loading = false;
- }
- });
- },
-
- login: function(type, success) {
- if (typeof type == 'function') {
- success = type;
- type = 1;
- } else if (typeof type == 'undefined') {
- type = 1;
- }
- var that = this;
-
-
- return success
- },
-
- jump: function(url, type, params) {
- var that = this;
- if (that.isEmpty(url) && type !== -1) return false;
- if (url === -1) {
- var delta = type || 1;
- type = url;
- } else if (type === -1) {
- var delta = 1;
- if (params && params.delta) {
- delta = params.delta;
- }
- }
- type = type || 0,
- params = params || {};
- if (that.isEmpty(params)) {
- cache.remove('jump_data');
- } else {
- cache.set('jump_data', params);
- }
- switch (type) {
- case -1:
-
- setTimeout(function() {
- uni.navigateBack({
- delta: delta
- });
- }, 800);
- return;
- case 0:
-
- uni.navigateTo({
- url: url
- });
- return;
- case 1:
-
- uni.redirectTo({
- url: url
- });
- return;
- case 2:
-
- uni.reLaunch({
- url: url
- });
- return;
- case 3:
-
- uni.switchTab({
- url: url
- });
- return;
- default:
- return false;
- }
- },
-
- req: function(option, onsuccess, onerror, oncomplete) {
- if (option == 'undefined') return false;
- this.loading()
-
- var that = this,
- config = {
- sign: false,
- load: false,
- login: false,
- url: false,
- data: option.data,
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'api-token': uni.getStorageSync('token').token?uni.getStorageSync('token').token:'',
- 'api-name': 'wxapp'
- },
- method: option.method?option.method:'GET',
- dataType: 'json',
- success: function(res) {
- that.jslog('请求:' + option.url);
- that.jslog(res.data);
- that.closeLoading()
-
- res = res.data;
-
- if (onsuccess) {
- if (onsuccess(res) === false) {
- return;
- }
- }
- if (typeof(res) == 'object') {
-
-
- if(res.code == 401) {
- that.info(res.info)
- setTimeout(function() {
- that.jump('/pages/login/password_login')
- }, 2000)
- }
- if(res.code == 0) {
- that.info(res.info)
-
-
-
- }
- } else {
-
- }
- },
- fail: function(res) {
- that.jslog('请求:' + option.url);
- that.jslog(res);
- onerror && onerror();
- },
- complete: function(res) {
- option.load && that.closeLoading();
- oncomplete && oncomplete();
- }
- };
- option.data && (option.data = option.data);
- if (option.url) {
- option.url = that.config.baseUrl + option.url;
- } else {
- return false;
- }
- option = that.extend({}, config, option);
- if (option.login && !that.login()) {
- return false;
- }
-
-
-
- if (option.sign) {
- that.getsign(option.data, function(res) {
- option.data = res;
- option.load && that.loading(option.load);
- uni.request(option);
- })
- } else {
- option.load && that.loading(option.load);
- uni.request(option);
- }
- },
-
-
- gettoken: function(callback) {
- var that = this,
- token = cache.get('token'),
- timestamp = Date.parse(new Date()) / 1000;
-
- if (token && token.date_expire > (timestamp + 600)) return callback({
- status: 1,
- data: token
- });
- var uuid = that.uuid(),
-
- uid = that.getUserInfo('id', 0);
- uni.request({
- url: that.config.baseUrl + 'token',
- data: {
- uuid: uuid,
- uid: uid,
- timestamp: timestamp
- },
- timeout: 10000,
- success: function(res) {
- var res = res.data;
- if (res.status == 1) {
- cache.set('token', res.data);
- }
- return callback(res);
- },
- complete: function(res) {
- that.jslog('获取token:' + res);
- }
- });
- },
-
- getsign: function(param, onsuccess, onerror) {
- var that = this,
- arr = [];
- param._sign_uuid = that.uuid();
- param._sign_uid = that.getUserInfo('id', 0);
- param._timestamp = Date.parse(new Date()) / 1000;
- param._version = that.config.version;
- that.gettoken(function(res) {
- if (res.status == 1) {
- param._sign_token = res.data.token;
- for (var i in param) {
- if (!that.isEmpty(param[i], true)) {
- arr.push(i + '=' + param[i]);
- }
- }
- arr.sort();
- param._sign = encrypt.sha1(arr.join('&') + res.data.token).toUpperCase();
- return onsuccess(param);
- } else {
- return onerror && onerror(res.info) || that.info(res.info);
- }
- });
- },
-
- formatparam: function(query) {
- var json = {};
- if (typeof(query) == 'object') {
- for (var i in query) {
- if (i.indexOf('password') > -1) {
- query[i] = encrypt.md5(encrypt.sha1(query[i]));
- }
- }
- return query;
- } else {
- var vars = query.split('&');
- for (var i = 0; i < vars.length; i++) {
- var pair = vars[i].split('=');
- if (pair[0].indexOf('password') != -1) {
- pair[1] = encrypt.md5(encrypt.sha1(pair[1]));
- } else {
- pair[1] = pair[1];
- }
- if (typeof json[pair[0]] === 'undefined') {
- json[pair[0]] = pair[1];
- } else if (typeof json[pair[0]] === 'string') {
- var arr = [json[pair[0]], pair[1]];
- json[pair[0]] = arr;
- } else {
- json[pair[0]].push(pair[1]);
- }
- }
- return json;
- }
- },
-
- isArray: function(arr) {
- return arr && typeof(arr) == 'object' && Object.prototype.toString.call(arr) === '[object Array]';
- },
-
- isPlainObject: function(obj) {
- if (Object.getPrototypeOf) {
- return obj && typeof(obj) == 'object' && Object.getPrototypeOf(obj) == Object.prototype;
- } else {
- return obj && typeof(obj) == 'object' && Object.prototype.toString.call(obj) == "[object Object]" &&
- !obj.length;
- }
- },
-
- getLength: function(object) {
- return Object.keys(object).length
- },
-
- compareJsonObj: function(objA, objB) {
- if (!this.isPlainObject(objA) || !this.isPlainObject(objB)) return false;
- return this.compareObj(objA, objB);
- },
-
- compareObj: function(objA, objB, flag) {
- if (this.getLength(objA) != this.getLength(objB)) return false;
- flag = flag || true;
- for (var key in objA) {
- if (!flag)
- break;
- if (!objB.hasOwnProperty(key)) {
- flag = false;
- break;
- }
- if (!this.isArray(objA[key])) {
- if (this.isPlainObject(objA[key])) {
- if (this.isPlainObject(objB[key])) {
- if (!flag)
- break;
- flag = this.compareObj(objA[key], objB[key], flag);
- } else {
- flag = false;
- break;
- }
- } else {
- if (String(objB[key]) != String(objA[key])) {
- flag = false;
- break;
- }
- }
- } else {
- if (!this.isArray(objB[key])) {
- flag = false;
- break;
- }
- var oA = objA[key],
- oB = objB[key];
- if (oA.length != oB.length) {
- flag = false;
- break;
- }
- for (var k in oA) {
- if (!flag)
- break;
- flag = this.compareObj(oA[k], oB[k], flag);
- }
- }
- }
- return flag;
- },
-
- extend: Object.assign || function(target, source, deep) {
- for (key in source) {
- if (deep && (this.isPlainObject(source[key]) || this.isArray(source[key]))) {
- if (this.isPlainObject(source[key]) && !this.isPlainObject(target[key])) {
- target[key] = {};
- }
- if (this.isArray(source[key]) && !this.isArray(target[key])) {
- target[key] = [];
- }
- this.extend(target[key], source[key], deep);
- } else if (source[key] !== undefined) {
- target[key] = source[key];
- }
- }
- return target;
- },
-
- isEmpty: function(string, is_zero = false) {
- if (is_zero && string === '0') {
- return true;
- }
-
- if (string == '' || string == null || typeof(string) == 'undefined' || string == 'undefined') {
- return true;
- }
-
- if (typeof(string) == 'object') {
- return this.getLength(string) ? false : true;
- }
- return false;
- },
-
- uuid: function() {
- var uuid,
- random = function() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- };
- uuid = cache.get('uuid');
- if (!uuid) {
- uuid = encrypt.sha1(random() + random() + '-' + random() + '-' + random() + '-' + random() + '-' +
- random() + random() + random());
- cache.set('uuid', uuid);
- }
- return uuid;
- },
-
- getClientId: function() {
-
- var clientid = cache.get('client_id');
- if (clientid) return clientid;
-
- },
-
- getUserInfo: function(k, d) {
- if (d == 'undefined') {
- d = 0;
- }
- var userInfo = cache.get('user_info');
- if (this.isEmpty(userInfo)) {
- return d;
- } else {
- if (typeof k == 'undefined') {
- return userInfo;
- } else {
- if (typeof userInfo[k] == 'undefined') {
- return d;
- } else {
- return userInfo[k];
- }
- }
- }
- },
-
- indexOf: function(arr, val, key) {
- if (this.isEmpty(arr) || this.isEmpty(val)) return -1;
- if (typeof key == 'undefined') {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i] == val) {
- return i;
- }
- }
- } else {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i][key] == val) {
- return i;
- }
- }
- }
- return -1;
- },
-
- removeItemByVals: function(arr, vals, key) {
- var that = this;
- if (this.isEmpty(arr) || this.isEmpty(vals)) return arr;
- if (this.isArray(vals)) {
-
- if (typeof key == 'undefined') {
- for (let i = arr.length - 1; i >= 0; i--) {
- let index = that.indexOf(vals, arr[i]);
- if (index > -1) {
- arr.splice(i, 1);
- }
- }
- } else {
- for (let i = arr.length - 1; i >= 0; i--) {
- let index = that.indexOf(vals, arr[i][key]);
- if (index > -1) {
- arr.splice(i, 1);
- }
- }
- }
- } else {
- let index = this.indexOf(arr, vals, key);
- if (index > -1) {
- arr.splice(index, 1);
- }
- }
- return arr;
- },
-
- removeItemByIndex: function(arr, indexs) {
- var that = this;
- if (this.isEmpty(arr) || this.isEmpty(indexs)) return arr;
- if (this.isArray(indexs)) {
-
- for (let i = arr.length - 1; i >= 0; i--) {
- let index = that.indexOf(indexs, i);
- if (index > -1) {
- arr.splice(i, 1);
- }
- }
- } else {
- arr.splice(indexs, 1);
- }
- return arr;
- },
-
- array_column: function(arr, key) {
- if (this.isEmpty(arr)) return arr;
- if (this.isEmpty(key)) return [];
- var vals = [];
- arr.forEach(item => {
- vals.push(item[key]);
- });
- return vals;
- },
-
- array_ksort: function(arr) {
- let sorted = {},
- keys = Object.keys(arr);
- keys.sort();
- keys.forEach((key) => {
- sorted[key] = arr[key];
- })
- return sorted;
- },
-
- array_vsort: function(arr, key, sort) {
- if (this.isEmpty(arr)) return arr;
- key = key || '';
- sort = sort || 'asc';
- if (key == '') {
- return arr.sort();
- } else {
- if (sort == 'asc') {
- return arr.sort(function(a, b) {
- return a[key] - b[key];
- });
- } else {
- return arr.sort(function(a, b) {
- return b[key] - a[key];
- });
- }
- }
- },
-
- formatDate: function({
- value = '',
- mode = ''
- }) {
- var date = value
- var that = this;
- if(that.isEmpty(date,true)) {
- date = new Date();
- } else {
- if (typeof date == 'number') {
- date = new Date(parseInt(date));
- } else if (typeof date == 'string') {
- date = new Date(date);
- }
- }
- var value1 = ''
- let Y = date.getFullYear();
- let M = +(date.getMonth()+1 < 10 ? '0' +(date.getMonth()+1) : date.getMonth()+1) ;
- let D = date.getDate();
- let h = date.getHours();
- let m = date.getMinutes();
- let s = date.getSeconds();
- if(mode == 'year-month') {
- value1 = Y+ '-'+ M
- } else {
- value1 = Y+ '-'+M+ '-'+D
- }
- return value1
- },
-
- formatDate1: function({
- date = '',
- type = 'y',
- number = 0,
- format = 'Y-m-d'
- }) {
- var that = this;
- if (that.isEmpty(date, true)) {
- date = new Date();
- } else {
- if (typeof date == 'number') {
- date = new Date(parseInt(date) * 1000);
- } else if (typeof date == 'string') {
- date = new Date(date);
- }
- }
- date = that.addDate(type, number, date);
- var zero = function(value) {
- if (value < 10) {
- return '0' + value;
- }
- return value;
- },
- year = date.getFullYear(),
- month = zero(date.getMonth() + 1),
- day = zero(date.getDate());
-
-
-
- return format.replace(/Y|m|d/ig, function(matches) {
- return ({
- Y: year,
- m: month,
- d: day,
-
-
-
- })[matches];
- });
- },
-
- addDate: function(type, number, date) {
- switch (type) {
- case "y": {
- date.setFullYear(date.getFullYear() + number);
- return date;
- break;
- }
- case "q": {
- date.setMonth(date.getMonth() + number * 3);
- return date;
- break;
- }
- case "m": {
- date.setMonth(date.getMonth() + number);
- return date;
- break;
- }
- case "w": {
- date.setDate(date.getDate() + number * 7);
- return date;
- break;
- }
- case "d": {
- date.setDate(date.getDate() + number);
- return date;
- break;
- }
- case "h": {
- date.setHours(date.getHours() + number);
- return date;
- break;
- }
- case "m": {
- date.setMinutes(date.getMinutes() + number);
- return date;
- break;
- }
- case "s": {
- date.setSeconds(date.getSeconds() + number);
- return date;
- break;
- }
- default: {
- date.setDate(date.getDate() + number);
- return date;
- break;
- }
- }
- },
-
- qrCodeOperat({
- action = 'qrcode',
- canvas,
- data,
- camera = false,
- success,
- error
- }) {
- var that = this;
- switch (action) {
- case 'qrcode':
- qrCode({
- width: 180,
- height: 180,
- x: 20,
- y: 20,
- canvasId: canvas,
- text: data,
- });
- break;
- case 'scan':
- uni.scanCode({
- onlyFromCamera: camera,
- success(res) {
- if (res.errMsg == 'scanCode:ok') {
-
- success && success(res.result);
- } else {
- error && error(res);
- }
- },
- fail(res) {
- if (res.errMsg != 'scanCode:fail cancel') {
- error && error(res);
- }
- }
- });
- break;
- default:
- that.modal('请明确二维码操作类型');
- }
- },
-
- imageOperat({
- action = 'choose',
- path,
- fileName = 'write',
- data,
- count = 1,
- menu = true,
- success,
- error
- }) {
- var that = this;
- if (action == 'choose') {
- uni.chooseImage({
- count: count,
- sizeType: ['original', 'compressed'],
- sourceType: ['camera', 'album'],
- success: (e) => {
- e.tempFilePaths.forEach((item) => {
- success && success(item);
- });
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'upload') {
- uni.uploadFile({
- url: that.config.baseUrl + '/api/Publics/uploadLocality',
- filePath: path,
- name: 'upfile',
-
- formData: {
-
- file: path
- },
- success(e) {
- console.log(e)
- let res = JSON.parse(e.data);
- if (res.status == 1) {
- success && success(res);
- } else {
- error && error({
- errMsg: res.info
- });
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'info') {
- uni.getImageInfo({
- src: path,
- success(e) {
- if (e.errMsg == 'getImageInfo:ok') {
-
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'read') {
- uni.getFileSystemManager().readFile({
- filePath: path,
- encoding: 'base64',
- position: 0,
- success(e) {
- if (e.errMsg == 'readFile:ok') {
-
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e_t) {
- error && error(e);
- }
- });
- } else if (action == 'write') {
- let filePath = `${uni.env.USER_DATA_PATH}/${fileName}.png`;
- uni.getFileSystemManager().writeFile({
- filePath: filePath,
- data: data,
- encoding: 'base64',
- success(e) {
- if (e.errMsg == 'writeFile:ok') {
- success && success(filePath);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- console.log(e);
- error && error(e);
- }
- });
- } else if (action == 'down') {
- uni.downloadFile({
- url: path,
- success(e) {
- if (e.errMsg === 'downloadFile:ok') {
-
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- })
- } else if (action == 'save') {
- let filePath = `${uni.env.USER_DATA_PATH}/${fileName}.png`;
- uni.getFileSystemManager().saveFile({
- tempFilePath: path,
- filePath: filePath,
- success(e) {
- if (e.errMsg == 'saveFile:ok') {
- success && success(filePath);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- console.log(e);
- error && error(e);
- }
- });
- } else if (action == 'preview') {
- uni.previewImage({
- currnet: path,
- urls: data,
- showmenu: menu
- });
- }
- },
-
- viedoOperat({
- action = 'choose',
- count = 1,
- mediaType = ['video'],
- sourceType = ['album', 'camera'],
- maxDuration = 58,
- camera = 'back',
- filePath,
- path,
- size = 20,
- success,
- error
- }) {
- var that = this
- if (action == 'choose') {
- uni.chooseMedia({
- count: count,
- mediaType: mediaType,
- sourceType: sourceType,
- maxDuration: maxDuration,
- camera: camera,
- success(e) {
- console.log(e)
- if (e.errMsg == 'chooseMedia:ok') {
- e.tempFiles.forEach((item) => {
- let sel_size = parseFloat(item.size / 1024 / 1024).toFixed(1)
- if (sel_size > size) {
- let beyongSize = sel_size - size
- that.info("上传的视频大小超限,超出" + beyongSize + "MB,请重新上传!")
- error && error();
- } else {
- success && success(item);
- }
- });
- } else {
- error && error(e);
- }
- },
- fail(e) {
- console.log(e);
- error && error(e);
- }
- })
- }
-
- if (action == 'compress') {
- uni.compressVideo({
- quality: 'high',
- src: filePath,
- success(e) {
- console.log(e)
- if (e.errMsg == 'compressVideo:ok') {
-
- success && success(e);
-
- } else {
- error && error(e);
- }
- },
- fail(e) {
- console.log(e);
- error && error(e);
- }
- })
- }
- },
-
- locationOperat({
- action = 'getLocation',
- type = 'gcj02',
- address,
- from = '',
- to,
- success,
- error
- }) {
- var that = this;
- if (action == 'getLocation') {
- uni.getLocation({
- type: type,
- success(e) {
- if (e.errMsg == 'getLocation:ok') {
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'openLocation') {
- var {
- latitude,
- longitude
- } = from;
- uni.openLocation({
- latitude: latitude,
- longitude: longitude,
- success(e) {
- if (e.errMsg == 'openLocation:ok') {
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'chooseLocation') {
- var {
- latitude = '',
- longitude = ''
- } = from;
- uni.chooseLocation({
- latitude: latitude,
- longitude: longitude,
- success(e) {
- if (e.errMsg == 'chooseLocation:ok') {
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'choosePoi') {
- var {
- latitude = '',
- longitude = ''
- } = from;
- uni.choosePoi({
- success(e) {
- if (e.errMsg == 'choosePoi:ok') {
- success && success(e);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'getLatLng') {
- new qqMap({
- key: that.config.qqMapKey
- }).geocoder({
- address: address,
- success(e) {
- if (e.status == 0 && e.message == 'query ok') {
- success && success(e.result);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'getAreaCode') {
- new qqMap({
- key: that.config.qqMapKey
- }).reverseGeocoder({
- success(e) {
- if (e.status == 0 && e.message == 'query ok') {
- success && success(e.result);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'getDistanceApi') {
- new qqMap({
- key: that.config.qqMapKey
- }).calculateDistance({
- from: from,
- to: to,
- success(e) {
- if (e.status == 0 && e.message == 'query ok') {
- success && success(e.result.elements);
- } else {
- error && error(e);
- }
- },
- fail(e) {
- error && error(e);
- }
- });
- } else if (action == 'getDistanceMath') {
- var {
- latitude: lat1 = '',
- longitude: lng1 = ''
- } = from;
- if (lat1 == '' && lng1 == '') {
- return 0;
- }
- var {
- latitude: lat2 = '',
- longitude: lng2 = ''
- } = to;
- var toRad = function(d) {
- return d * Math.PI / 180;
- };
- var dis = 0;
- var deltaLng = toRad(lng1) - toRad(lng2);
- var radLat1 = toRad(lat1);
- var radLat2 = toRad(lat2);
- var deltaLat = radLat1 - radLat2;
- var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(
- radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
- return that.math.toFixed(dis * 6378.137, 2);
- }
- },
-
- filterEmoji: function(str) {
- return str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, '');
- },
-
- jslog: function(obj) {
- this.config.debug && console.log(JSON.stringify(obj));
- },
-
- formCheck: function(value, rule, error) {
- var that = this;
- var regs = {
- required: [
- /[\s\S]+/,
- '必填项不能为空',
- ],
- english: [
- /^[a-zA-Z]+$/,
- '必填项只能由大小写字母组成',
- ],
- mobile: [
- /^1[3|4|5|7|8|9][0-9]\d{4,8}$/,
- '请输入正确的手机号',
- ],
- phone: [
- /^(\d{3,4}-?)?\d{7,9}$/g,
- '请输入正确的电话号码',
- ],
- email: [
- /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
- '请输入正确的电子邮箱'
- ],
- url: [
- /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/,
- '请输入正确的链接'
- ],
- number: [
- /^\d+$/,
- '必填项只能填数字'
- ],
- code4: [
- /^\d{4}$/,
- '请输入正确的手机验证码'
- ],
- code6: [
- /^\d{6}$/,
- '请输入正确的手机验证码'
- ],
- captcha4: [
- /^[0-9a-zA-Z]{4}$/,
- '请输入正确的图片验证码'
- ],
- date: [
- /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/,
- '请输入正确的日期格式'
- ],
- identity: [
- /(^\d{15}$)|(^\d{17}(x|X|\d)$)/,
- '请输入正确的身份证号'
- ],
- username: [
- /^[a-zA-Z0-9\u4e00-\u9fa5-_\.]{2,12}$/,
- '请输入正确的帐号(只能包含中文、英文、数字、下划线等字符)'
- ],
- password: [
- /^\w{6,16}$/,
- '请输入正确的密码(6-16位数字,字母或符号)'
- ],
- passwords: [
- /^[a-zA-Z]+\w{5,12}$/,
- '请输入正确的密码(以字母开头,长度在6-12之间)'
- ],
- pwd: [
- /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{6,12}$/,
- '请输入正确的密码(6-12位数字+字母组合)'
- ],
- invite: [
- /^[0-9a-zA-Z]{10}$/,
- '邀请码为8位数字或字母组合'
- ],
- truename: [
- /^[\u4e00-\u9fa5]{2,4}$/,
- '请输入正确的真实姓名(2-4个汉字)'
- ],
- money: [
- /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/,
- '请输入正确的金额'
- ]
- };
- var reg = regs[rule];
- if (that.isEmpty(reg)) {
- return true;
- } else {
- if (value == '' || !RegExp(reg[0]).test(value)) {
- that.info(error || reg[1]);
- return false;
- }
- return true;
- }
- },
-
-
- getSystemInfo(success) {
-
- uni.getSystemInfo({
- success: (res) => {
- if (res.errMsg == 'getSystemInfo:ok') {
- res.menuButton = uni.getMenuButtonBoundingClientRect();
- success && success(res);
- } else {
- that.modal('获取系统信息失败:' + res.errMsg);
- }
- },
- fail: (res) => {
- that.modal('获取系统信息失败:' + res.errMsg);
- }
- });
- },
-
- getSessionKey: function(success) {
- let that = this;
- let data = {};
- data.openid = cache.get('open_id');
- data.session_key = cache.get('session_key');
-
- if (!that.isEmpty(data.session_key) && !that.isEmpty(data.openid)) {
- uni.checkSession({
- success() {
- success && success(data);
- },
- fail() {
- cache.remove('open_id');
- cache.remove('session_key');
- that.getOpenId(success);
- }
- });
- } else {
- cache.remove('open_id');
- cache.remove('session_key');
- that.getOpenId(success);
- }
- },
-
- getOpenId: function(success, error) {
- var that = this;
- that.loading();
- uni.login({
- success: (e) => {
- that.req({
- url: 'public/getOpenid',
- data: {
- code: e.code
- },
- login: false,
- load: false
- }, function(res) {
- if (res.status == 1) {
- cache.set('open_id', res.data.openid);
- cache.set('session_key', res.data.session_key);
- success && success(res.data);
- } else {
- that.modal('微信登录失败:' + res.info, function() {
- error && error();
- });
- return false;
- }
- }, function() {}, function() {
- that.closeLoading();
- });
- },
- fail: (e) => {
- that.closeLoading();
- that.modal('微信登录失败:' + e.errMsg, function() {
- error && error();
- });
- }
- });
- },
-
- doLogin: function(data, success, error) {
- var that = this;
- data.loginsubmit = 'yes';
- this.req({
- url: 'account/login',
- data: data,
- login: false
- }, function(res) {
- if (res.status == 1) {
- cache.set('user_info', res.data);
- success && success(res.data);
- } else {
- error && error(res);
- }
- });
- },
-
- doRegist: function(data, success, error) {
- var that = this;
- data.registsubmit = 'yes';
- this.req({
- url: 'account/register',
- data: data,
- login: false
- }, function(res) {
- if (res.status == 1) {
- cache.set('user_info', res.data);
- success && success(res.data);
- } else {
- error && error(res);
- }
- });
- },
-
- checkRegist: function(success, error) {
- var that = this;
- that.getSessionKey(function(data) {
- that.req({
- url: 'account/checkRegist',
- data: {
- open_id: data.openid,
- registsubmit: 'yes'
- },
- login: false,
- load: false
- }, function(res) {
- if (res.status == 1) {
- success && success();
- } else {
- error && error();
- }
- });
- });
- },
-
- getMemberInfo: function(success, error) {
- var that = this;
- this.req({
- url: 'center/getMemberInfo',
- load: false
- }, function(res) {
- if (res.status == 1) {
- cache.set('user_info', res.data);
- success && success(res.data);
- } else {
- error && error(res);
- }
- });
- },
-
- getUserProfile: function(success, error) {
- var that = this;
- that.loading('获取微信信息中...');
- uni.getUserProfile({
- lang: 'zh_CN',
- desc: '用于完善会员资料',
- success: (res) => {
- if (res.errMsg == 'getUserProfile:ok') {
-
- res.userInfo.nickName = that.filterEmoji(res.userInfo.nickName);
- success(res.userInfo);
- } else {
- that.modal('获取微信信息失败:' + res.errMsg, function() {
- error && error(res);
- });
- }
- },
- fail: (res) => {
- that.modal('获取微信信息失败:' + res.errMsg, function() {
- console.log(res)
- error && error(res);
- });
- },
- complete: () => {
- that.closeLoading();
- }
- });
- },
-
- getConfigInfo: function({
- source = 'system',
- label = '',
- success
- }) {
- this.req({
- url: 'public/configInfo',
- data: {
- source: source,
- label: label
- },
- login: false,
- load: false
- }, function(res) {
- success(res.data);
- });
- },
-
- getMobile: function(e, success) {
- var that = this;
- that.getSessionKey(function(data) {
- if (e.detail.errMsg == 'getPhoneNumber:ok') {
- that.req({
- url: 'public/getMobile',
- data: {
- encryptedData: e.detail.encryptedData,
- iv: e.detail.iv,
- sessionKey: data.session_key
- },
- login: false
- }, function(res) {
- if (res.status == 1) {
- success(res.data);
- } else {
- that.modal('获取手机号失败' + res.info);
- return false;
- }
- });
- } else {
- that.modal('获取手机号失败' + e.detail.errMsg);
- }
- });
- },
-
- makeCall: function(mobile) {
- this.modal('确定拨打' + mobile, function() {
- uni.makePhoneCall({
- phoneNumber: mobile
- });
- }, true);
- },
-
- changeNavBarTitle: function(title) {
- uni.setNavigationBarTitle({
- title: title
- });
- },
-
- checkUpdate: function() {
- if (uni.canIUse('getUpdateManager')) {
- const updateManager = uni.getUpdateManager()
- updateManager.onCheckForUpdate(function(res) {
- if (res.hasUpdate) {
- updateManager.onUpdateReady(function() {
- uni.showModal({
- title: '更新提示',
- content: '新版本已经准备好,是否重启应用?',
- success: function(res) {
- if (res.confirm) {
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(function() {
- uni.showModal({
- title: '已经有新版本了~',
- content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开~'
- })
- })
- }
- })
- } else {
- uni.showModal({
- title: '提示',
- content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
- })
- }
- },
-
- escape2Html(str, success) {
- var arrEntities = {
- 'lt': '<',
- 'gt': '>',
- 'nbsp': ' ',
- 'amp': '&',
- 'quot': '"'
- };
- return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
- return arrEntities[t];
- }).replace(/<section/g, '<div').replace(/<u>/g, '').replace(/<u style="">/g, '').replace(/<\/u>/g, '');
- },
-
- shareInfo: function() {
- var that = this;
- var userInfo = cache.get('user_info');
- if (!that.isEmpty(userInfo) && !that.isEmpty(userInfo.serial)) {
- var path = `/pages/index/index?invite_code=${userInfo.serial}`;
- } else {
- var path = '/pages/index/index';
- }
- return {
- title: that.config.name,
- path: path,
-
- };
- },
-
- checkStore(id, key, success) {
- var that = this
- var shopStore = cache.get(key);
- if (that.isEmpty(shopStore)) {
- success && success(false)
- } else {
- var have = 0
- for (let i in shopStore) {
- var d = shopStore[i];
- if (d.id == id) {
-
- have = 1
- success && success(true)
- break;
- }
- if (have == 0) {
-
- success && success(false)
- }
- }
- }
- },
-
- changeStore(data, key, success) {
- var shopStore = cache.get(key);
- var that = this
- var p = {}
- p = data
- if (that.isEmpty(shopStore)) {
- var shopStore = []
- shopStore.push(p);
- cache.set(key, shopStore);
- success && success('收藏成功')
- } else {
- var have = 0
- for (let i in shopStore) {
- var d = shopStore[i];
- console.log(d, data)
- if (d.id == data.id && d.table == data.table) {
-
- have = 1
- shopStore.splice(i, 1);
- cache.set(key, shopStore);
- success && success('取消成功')
- break;
- }
- }
- if (have == 0) {
-
- shopStore.push(p);
- cache.set(key, shopStore);
- success && success('收藏成功')
- }
- }
- },
-
-
- getshopcar: function(success) {
- var shopCar_list = cache.get('shop_car');
- if (this.isEmpty(shopCar_list)) {
- uni.hideTabBarRedDot({
- index: 2,
- success: function() {
- success && success(0);
- }
- });
- } else {
- var len = this.getLength(shopCar_list);
- uni.setTabBarBadge({
- index: 2,
- text: String(len),
- success: function() {
- success && success(len);
- }
- });
- }
- },
-
- onReachFoot: function(url, data, succes) {
-
- if (url) {
- this.req({
- url: url,
- data: data
- }, function(res) {
- if (res.status == 1) {
- succes && succes(res.data);
- }
- })
- } else {
- succes && success(false)
- }
- },
-
- makeCall(price, mobile, work_id, type, buy_type, send_id, send_name = '') {
- var that = this
- that.modal('将自动使用会员次数或东豆进行抵扣', function(params) {
- that.req({
- url: 'center/MobileOrMessage',
- data: {
- price: price,
- mobile: mobile,
- work_id: work_id,
- type: type,
- buy_type: buy_type,
- send_id: send_id,
- send_name: send_name
- }
- }, function(params) {
- if (params.status == 1) {
- if (buy_type == 'price') {
- uni.makePhoneCall({
- phoneNumber: mobile,
- })
- } else {
- that.jump('/pages/chat/chat?id=' + send_id + '&nickname=' + send_name)
- }
- }
- if (params.status == 2) {
- }
- })
- }, true)
- },
-
- debounce: function(func, wait) {
- let timeout;
- return function() {
- console.log('防抖事件:' + func, '防抖时间:' + wait)
- let context = this
- let args = arguments
- let later = () => {
- timeout = null
- func.apply(context, args)
- }
- clearTimeout(timeout)
- timeout = setTimeout(later, wait)
- }
- }
- }
|