index.js 11 KB

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