index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. var app = getApp();
  2. var util = require('./util.js');
  3. var wcache = require('./wcache.js');
  4. function loadStatus() {
  5. return new Promise(function(resolve) {
  6. let appLoadStatus = 1;
  7. if (util.check_login()) {
  8. if (!app.globalData.hasDefaultCommunity) {
  9. appLoadStatus = 2;
  10. }
  11. } else {
  12. appLoadStatus = 0;
  13. }
  14. app.globalData.appLoadStatus = appLoadStatus;
  15. resolve();
  16. });
  17. };
  18. function changeCommunity(community, city) {
  19. if (community.communityId && community.communityId !== app.globalData.community.communityId) {
  20. app.globalData.timer.del();
  21. app.globalData.changedCommunity = true;
  22. app.globalData.community = community;
  23. app.globalData.refresh = true;
  24. app.globalData.hasDefaultCommunity = true;
  25. wx.setStorage({
  26. key: "community",
  27. data: community
  28. });
  29. app.globalData.city = city;
  30. wx.setStorage({
  31. key: "city",
  32. data: city
  33. });
  34. var data = {
  35. community: community,
  36. city: city
  37. };
  38. var historyCommunity = app.globalData.historyCommunity || [];
  39. if (0 === historyCommunity.length || historyCommunity[0] && historyCommunity[0].communityId !== community.communityId) {
  40. historyCommunity.length > 1 && historyCommunity.shift();
  41. historyCommunity.push(data);
  42. app.globalData.historyCommunity = historyCommunity;
  43. wx.setStorage({
  44. key: "historyCommunity",
  45. data: historyCommunity
  46. })
  47. }
  48. app.globalData.changedCommunity = true;
  49. let token = wx.getStorageSync('token') || '';
  50. // 请求提交社区id
  51. app.util.request({
  52. 'url': 'entry/wxapp/index',
  53. 'data': {
  54. controller: 'index.switch_history_community',
  55. token: token,
  56. head_id: community.communityId
  57. },
  58. dataType: 'json',
  59. success: function (res) {
  60. if (res.data.code == 0) {
  61. app.globalData.community_id = community.communityId;
  62. wx.switchTab({
  63. url: "/lionfish_comshop/pages/index/index",
  64. // success: function (e) {
  65. // var page = getCurrentPages().pop();
  66. // if (page == undefined || page == null) return;
  67. // page.onLoad();
  68. // }
  69. });
  70. }
  71. }
  72. })
  73. } else {
  74. if (!app.globalData.community.disUserHeadImg) {
  75. app.globalData.community = community;
  76. wx.setStorage({
  77. key: "community",
  78. data: community
  79. })
  80. }
  81. app.globalData.changedCommunity = true;
  82. wx.switchTab({
  83. url: "/lionfish_comshop/pages/index/index"
  84. });
  85. }
  86. }
  87. function isIdCard(t) {
  88. return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(t);
  89. };
  90. /**
  91. * 购物车数量
  92. */
  93. function cartNum() {
  94. function getTab(cb) {
  95. let token = wx.getStorageSync('token') || '';
  96. app.util.request({
  97. 'url': 'entry/wxapp/index',
  98. 'data': {
  99. controller: 'car.count',
  100. token: token,
  101. community_id: app.globalData.community.communityId
  102. },
  103. dataType: 'json',
  104. success: function (res) {
  105. if(res.data.code == 0){
  106. app.globalData.cartNum = res.data.data;
  107. wx.setStorageSync("cartNum", res.data.data);
  108. setTab(res.data.data);
  109. cb(res.data);
  110. }
  111. }
  112. })
  113. }
  114. function setTab(t) {
  115. if ("number" == typeof t && t){
  116. // wx.setTabBarBadge({
  117. // index: 1,
  118. // text: t + "",
  119. // fail: function (error) {
  120. // console.log(error);
  121. // }
  122. // })
  123. } else {
  124. // wx.removeTabBarBadge({
  125. // index: 1,
  126. // fail: function (error) {
  127. // console.log(error);
  128. // }
  129. // });
  130. }
  131. }
  132. var n = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "", i = arguments.length > 1 && void 0 !== arguments[1] && arguments[1];
  133. return new Promise(function (resolve) {
  134. if (i) {
  135. getTab(resolve);
  136. }else {
  137. var nowTime = new Date().getTime();
  138. if (app.globalData.cartNumStamp < nowTime){
  139. getTab(resolve);
  140. } else {
  141. ("number" == typeof n && (app.globalData.cartNum = n),setTab(app.globalData.cartNum), resolve(n))
  142. }
  143. app.globalData.cartNumStamp = new Date().getTime() + 60000;
  144. }
  145. });
  146. }
  147. function getRect(t, e, r) {
  148. return new Promise(function (n) {
  149. wx.createSelectorQuery().in(t)[r ? "selectAll" : "select"](e).boundingClientRect(function (t) {
  150. r && Array.isArray(t) && t.length && n(t), !r && t && n(t);
  151. }).exec();
  152. });
  153. }
  154. /**
  155. * 设置缓存过期时间
  156. */
  157. function getInNum () {
  158. return new Promise((resolve, reject) => {
  159. let timestamp = Date.parse(new Date());
  160. let inNum = parseInt(wx.getStorageSync('inNum')) || 0;
  161. let inNumExp = parseInt(wx.getStorageSync('inNumExp')) || 0;
  162. let today = new Date(new Date().toLocaleDateString()).getTime();
  163. if ((timestamp - inNumExp) > 86400000 || inNumExp == 0 ) {
  164. console.log('过期了');
  165. inNum = 1;
  166. wx.setStorage({
  167. key: 'inNumExp',
  168. data: today
  169. })
  170. } else {
  171. inNum += 1;
  172. }
  173. wx.setStorage({
  174. key: 'inNum',
  175. data: inNum
  176. })
  177. let isThree = inNum > 3 ? false : true;
  178. resolve(isThree);
  179. })
  180. }
  181. /**
  182. * 设置导航颜色
  183. */
  184. function setNavBgColor () {
  185. let navBgColor = wcache.get('navBgColor', 1);
  186. let navFontColor = wcache.get('navFontColor', 1);
  187. if (navBgColor == 1 || navFontColor==1) {
  188. app.util.request({
  189. 'url': 'entry/wxapp/index',
  190. 'data': {
  191. controller: 'index.get_nav_bg_color'
  192. },
  193. dataType: 'json',
  194. success: function (res) {
  195. if (res.data.code == 0) {
  196. let nav_bg_color = res.data.data || '#75451';
  197. let nav_font_color = res.data.nav_font_color || '#ffffff';
  198. wx.setNavigationBarColor({ frontColor: nav_font_color, backgroundColor: nav_bg_color })
  199. wcache.put('navBgColor', nav_bg_color, 600);
  200. wcache.put('navFontColor', nav_font_color, 600);
  201. }
  202. }
  203. })
  204. } else {
  205. wx.setNavigationBarColor({ frontColor: navFontColor, backgroundColor: navBgColor })
  206. }
  207. }
  208. /**
  209. * 设置社群名称
  210. */
  211. function setGroupInfo() {
  212. return new Promise(function (resolve, reject) {
  213. let groupInfo = wcache.get('groupInfo', 1);
  214. if (groupInfo == 1) {
  215. app.util.request({
  216. 'url': 'entry/wxapp/index',
  217. 'data': {
  218. controller: 'index.get_group_info'
  219. },
  220. dataType: 'json',
  221. success: function (res) {
  222. if (res.data.code == 0) {
  223. let obj = res.data.data;
  224. console.log(obj);
  225. obj.group_name = obj.group_name || '社区';
  226. obj.owner_name = obj.owner_name || '团长';
  227. wcache.put('groupInfo', obj, 600);
  228. resolve(obj);
  229. }
  230. }
  231. })
  232. } else {
  233. resolve(groupInfo);
  234. }
  235. })
  236. }
  237. /**
  238. * 获取首页、购物车图标
  239. */
  240. function setIcon() {
  241. let tabList = wcache.get('tabList', 1);
  242. return new Promise(function (resolve, reject) {
  243. if (tabList == 1) {
  244. app.util.request({
  245. 'url': 'entry/wxapp/index',
  246. 'data': {
  247. controller: 'index.get_tabbar'
  248. },
  249. dataType: 'json',
  250. success: function (res) {
  251. if (res.data.code == 0) {
  252. let list = res.data.data;
  253. let iconArr = {home: '', car: '', user: ''};
  254. iconArr.home = list['i1'] || '/lionfish_comshop/images/icon-tab-index.png';
  255. iconArr.car = list['i2'] || '/lionfish_comshop/images/icon-tab-shop.png';
  256. iconArr.user = list['i3'] || '/lionfish_comshop/images/icon-tab-me.png';
  257. resolve(iconArr);
  258. }
  259. }
  260. })
  261. } else {
  262. let iconArr = { home: '', car: '' };
  263. iconArr.home = tabList.list[0].iconPath;
  264. iconArr.car = tabList.list[2].iconPath;
  265. iconArr.user = tabList.list[3].iconPath;
  266. resolve(iconArr);
  267. }
  268. })
  269. }
  270. function getPx(t) {
  271. return Math.round(app.globalData.systemInfo.windowWidth / 375 * t);
  272. }
  273. /**
  274. * canvas画文字
  275. */
  276. function drawText(context, obj, o, a, n, i) {
  277. var r = o.split(""), l = "", u = [];
  278. context.setFillStyle(obj.color);
  279. // context.fillStyle = obj.color;
  280. context.textAlign = obj.textAlign;
  281. context.setFontSize(obj.size);
  282. // context.font = obj.size +'px Arial';
  283. for (var s = 0; s < r.length; s++){
  284. context.measureText(l).width < i || (u.push(l), l = ""), l += r[s];
  285. }
  286. u.push(l);
  287. for (var m = 0; m < u.length; m++){
  288. context.fillText(u[m], a, n + 12 * m);
  289. }
  290. }
  291. /**
  292. * 下载图片至本地
  293. */
  294. function download(t) {
  295. return new Promise(function (e) {
  296. wx.downloadFile({
  297. url: t,
  298. success: function (t) {
  299. 200 === t.statusCode && e(t);
  300. },
  301. fail: function (t) {
  302. console.log(t), wx.hideLoading();
  303. }
  304. });
  305. });
  306. }
  307. /**
  308. * 更新首页列表购物车数量
  309. * actId: 商品id
  310. * num:数量
  311. */
  312. function indexListCarCount(actId, num=0){
  313. let obj = { actId, num };
  314. if (!actId) return;
  315. let goodsListCarCount = app.globalData.goodsListCarCount;
  316. if (goodsListCarCount.length == 0) {
  317. goodsListCarCount.push(obj);
  318. } else {
  319. let k = goodsListCarCount.findIndex((n) => n.actId == obj.actId);
  320. if(k == -1){
  321. goodsListCarCount.push(obj);
  322. } else {
  323. goodsListCarCount[k].num = obj.num;
  324. }
  325. }
  326. app.globalData.goodsListCarCount = goodsListCarCount;
  327. }
  328. module.exports = {
  329. changeCommunity,
  330. loadStatus,
  331. isIdCard,
  332. cartNum,
  333. getRect,
  334. getInNum,
  335. setNavBgColor,
  336. setGroupInfo,
  337. setIcon,
  338. getPx,
  339. drawText,
  340. download,
  341. indexListCarCount
  342. }