var app = getApp(); var util = require('./util.js'); var wcache = require('./wcache.js'); function loadStatus() { return new Promise(function(resolve) { let appLoadStatus = 1; if (util.check_login()) { if (!app.globalData.hasDefaultCommunity) { appLoadStatus = 2; } } else { appLoadStatus = 0; } app.globalData.appLoadStatus = appLoadStatus; resolve(); }); }; function changeCommunity(community, city) { if (community.communityId && community.communityId !== app.globalData.community.communityId) { app.globalData.timer.del(); app.globalData.changedCommunity = true; app.globalData.community = community; app.globalData.refresh = true; app.globalData.hasDefaultCommunity = true; wx.setStorage({ key: "community", data: community }); app.globalData.city = city; wx.setStorage({ key: "city", data: city }); var data = { community: community, city: city }; var historyCommunity = app.globalData.historyCommunity || []; if (0 === historyCommunity.length || historyCommunity[0] && historyCommunity[0].communityId !== community.communityId) { historyCommunity.length > 1 && historyCommunity.shift(); historyCommunity.push(data); app.globalData.historyCommunity = historyCommunity; wx.setStorage({ key: "historyCommunity", data: historyCommunity }) } app.globalData.changedCommunity = true; let token = wx.getStorageSync('token') || ''; // 请求提交社区id app.util.request({ 'url': 'entry/wxapp/index', 'data': { controller: 'index.switch_history_community', token: token, head_id: community.communityId }, dataType: 'json', success: function (res) { if (res.data.code == 0) { app.globalData.community_id = community.communityId; wx.switchTab({ url: "/lionfish_comshop/pages/index/index", // success: function (e) { // var page = getCurrentPages().pop(); // if (page == undefined || page == null) return; // page.onLoad(); // } }); } } }) } else { if (!app.globalData.community.disUserHeadImg) { app.globalData.community = community; wx.setStorage({ key: "community", data: community }) } app.globalData.changedCommunity = true; wx.switchTab({ url: "/lionfish_comshop/pages/index/index" }); } } function isIdCard(t) { return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(t); }; /** * 购物车数量 */ function cartNum() { function getTab(cb) { let token = wx.getStorageSync('token') || ''; app.util.request({ 'url': 'entry/wxapp/index', 'data': { controller: 'car.count', token: token, community_id: app.globalData.community.communityId }, dataType: 'json', success: function (res) { if(res.data.code == 0){ app.globalData.cartNum = res.data.data; wx.setStorageSync("cartNum", res.data.data); setTab(res.data.data); cb(res.data); } } }) } function setTab(t) { if ("number" == typeof t && t){ // wx.setTabBarBadge({ // index: 1, // text: t + "", // fail: function (error) { // console.log(error); // } // }) } else { // wx.removeTabBarBadge({ // index: 1, // fail: function (error) { // console.log(error); // } // }); } } var n = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "", i = arguments.length > 1 && void 0 !== arguments[1] && arguments[1]; return new Promise(function (resolve) { if (i) { getTab(resolve); }else { var nowTime = new Date().getTime(); if (app.globalData.cartNumStamp < nowTime){ getTab(resolve); } else { ("number" == typeof n && (app.globalData.cartNum = n),setTab(app.globalData.cartNum), resolve(n)) } app.globalData.cartNumStamp = new Date().getTime() + 60000; } }); } function getRect(t, e, r) { return new Promise(function (n) { wx.createSelectorQuery().in(t)[r ? "selectAll" : "select"](e).boundingClientRect(function (t) { r && Array.isArray(t) && t.length && n(t), !r && t && n(t); }).exec(); }); } /** * 设置缓存过期时间 */ function getInNum () { return new Promise((resolve, reject) => { let timestamp = Date.parse(new Date()); let inNum = parseInt(wx.getStorageSync('inNum')) || 0; let inNumExp = parseInt(wx.getStorageSync('inNumExp')) || 0; let today = new Date(new Date().toLocaleDateString()).getTime(); if ((timestamp - inNumExp) > 86400000 || inNumExp == 0 ) { console.log('过期了'); inNum = 1; wx.setStorage({ key: 'inNumExp', data: today }) } else { inNum += 1; } wx.setStorage({ key: 'inNum', data: inNum }) let isThree = inNum > 3 ? false : true; resolve(isThree); }) } /** * 设置导航颜色 */ function setNavBgColor () { let navBgColor = wcache.get('navBgColor', 1); let navFontColor = wcache.get('navFontColor', 1); if (navBgColor == 1 || navFontColor==1) { app.util.request({ 'url': 'entry/wxapp/index', 'data': { controller: 'index.get_nav_bg_color' }, dataType: 'json', success: function (res) { if (res.data.code == 0) { let nav_bg_color = res.data.data || '#75451'; let nav_font_color = res.data.nav_font_color || '#ffffff'; wx.setNavigationBarColor({ frontColor: nav_font_color, backgroundColor: nav_bg_color }) wcache.put('navBgColor', nav_bg_color, 600); wcache.put('navFontColor', nav_font_color, 600); } } }) } else { wx.setNavigationBarColor({ frontColor: navFontColor, backgroundColor: navBgColor }) } } /** * 设置社群名称 */ function setGroupInfo() { return new Promise(function (resolve, reject) { let groupInfo = wcache.get('groupInfo', 1); if (groupInfo == 1) { app.util.request({ 'url': 'entry/wxapp/index', 'data': { controller: 'index.get_group_info' }, dataType: 'json', success: function (res) { if (res.data.code == 0) { let obj = res.data.data; console.log(obj); obj.group_name = obj.group_name || '社区'; obj.owner_name = obj.owner_name || '团长'; wcache.put('groupInfo', obj, 600); resolve(obj); } } }) } else { resolve(groupInfo); } }) } /** * 获取首页、购物车图标 */ function setIcon() { let tabList = wcache.get('tabList', 1); return new Promise(function (resolve, reject) { if (tabList == 1) { app.util.request({ 'url': 'entry/wxapp/index', 'data': { controller: 'index.get_tabbar' }, dataType: 'json', success: function (res) { if (res.data.code == 0) { let list = res.data.data; let iconArr = {home: '', car: '', user: ''}; iconArr.home = list['i1'] || '/lionfish_comshop/images/icon-tab-index.png'; iconArr.car = list['i2'] || '/lionfish_comshop/images/icon-tab-shop.png'; iconArr.user = list['i3'] || '/lionfish_comshop/images/icon-tab-me.png'; resolve(iconArr); } } }) } else { let iconArr = { home: '', car: '' }; iconArr.home = tabList.list[0].iconPath; iconArr.car = tabList.list[2].iconPath; iconArr.user = tabList.list[3].iconPath; resolve(iconArr); } }) } function getPx(t) { return Math.round(app.globalData.systemInfo.windowWidth / 375 * t); } /** * canvas画文字 */ function drawText(context, obj, o, a, n, i) { var r = o.split(""), l = "", u = []; context.setFillStyle(obj.color); // context.fillStyle = obj.color; context.textAlign = obj.textAlign; context.setFontSize(obj.size); // context.font = obj.size +'px Arial'; for (var s = 0; s < r.length; s++){ context.measureText(l).width < i || (u.push(l), l = ""), l += r[s]; } u.push(l); for (var m = 0; m < u.length; m++){ context.fillText(u[m], a, n + 12 * m); } } /** * 下载图片至本地 */ function download(t) { return new Promise(function (e) { wx.downloadFile({ url: t, success: function (t) { 200 === t.statusCode && e(t); }, fail: function (t) { console.log(t), wx.hideLoading(); } }); }); } /** * 更新首页列表购物车数量 * actId: 商品id * num:数量 */ function indexListCarCount(actId, num=0){ let obj = { actId, num }; if (!actId) return; let goodsListCarCount = app.globalData.goodsListCarCount; if (goodsListCarCount.length == 0) { goodsListCarCount.push(obj); } else { let k = goodsListCarCount.findIndex((n) => n.actId == obj.actId); if(k == -1){ goodsListCarCount.push(obj); } else { goodsListCarCount[k].num = obj.num; } } app.globalData.goodsListCarCount = goodsListCarCount; } module.exports = { changeCommunity, loadStatus, isIdCard, cartNum, getRect, getInNum, setNavBgColor, setGroupInfo, setIcon, getPx, drawText, download, indexListCarCount }