index.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. const util = require('../../utils/util.js');
  2. const api = require('../../config/api.js');
  3. const user = require('../../services/user.js');
  4. //获取应用实例
  5. const app = getApp();
  6. Page({
  7. data: {
  8. groupGoods: [],
  9. hotGoods: [],
  10. topics: [],
  11. brands: [],
  12. floorGoods: [],
  13. banner: [],
  14. channel: [],
  15. groupBanner: {},
  16. storeName: '',
  17. showPop: false, //活动弹窗
  18. showMenu: false, //菜单
  19. couponVo: {},
  20. storeId: '',
  21. page: 1,
  22. size: 3,
  23. list: [],
  24. openAttr: false,
  25. isMapShow: '', //第三方商户是否启用地图显示
  26. isShowTip: true, //是否显示顶部提示
  27. tipMsg: '', //是否显示顶部提示
  28. isStartLocation: null, // 是否授权地理位置获取权限
  29. isSystemLocation: null // 手机系统设置中定位是否开启
  30. },
  31. //控制回到顶部按钮的显示与消失
  32. scrollTopFun(e){
  33. let that = this;
  34. that.top = e.detail.scrollTop;
  35. that.$apply();
  36. },
  37. onLoad: function(options) {
  38. let that = this;
  39. wx.clearStorageSync();
  40. // console.log('http://120.76.26.84:80/group1/M00/00/03/rBJEdVv1LDWAAjsfABCztmpupr8780.mp4')
  41. // console.log(wx.getFileSystemManager().readFile())
  42. wx.setStorageSync("navUrl", "/pages/index/index");
  43. // options.scene = 148;
  44. // options.scene = 93;
  45. // options.scene = 38;
  46. // options.scene = 67;
  47. // options.scene = 28;
  48. // options.scene = 8;
  49. // 目前共分四种情况进入小程序
  50. if (options.scene) {
  51. // 是否通过扫门店码进入小程序首页
  52. // console.log("have scene");
  53. var scene = decodeURIComponent(options.scene);
  54. console.log("scene is ", scene);
  55. that.setData({
  56. storeId: scene
  57. });
  58. wx.removeStorageSync('storeId');
  59. wx.setStorageSync('storeId', scene);
  60. console.log('扫门店码进入小程序首页,storeId(scene):', scene)
  61. //扫门店码进入的小程序首页则清除是否分享缓存
  62. // 20210908 add by scott chen
  63. wx.setStorageSync('isScanShopEntry', 'true'); // 通过扫门店码进入
  64. wx.removeStorageSync('isShareIndexEntry'); // 通过分享首页进入
  65. wx.removeStorageSync('isShareGoodEntry'); // 通过分享商品进入
  66. wx.removeStorageSync('isDirectEntry'); // 直接进入
  67. //清除是否分享商品标识缓存
  68. wx.removeStorageSync('isShareGood');
  69. } else if (options.shareIndex) {
  70. // 是否通过分享首页进入小程序首页
  71. // console.log("have shareIndex");
  72. var shareIndex = decodeURIComponent(options.shareIndex);
  73. console.log("shareIndex is ", shareIndex);
  74. that.setData({
  75. storeId: shareIndex
  76. });
  77. wx.removeStorageSync('storeId');
  78. wx.setStorageSync('storeId', shareIndex);
  79. console.log('分享门店首页进入小程序首页,storeId(shareIndex):', shareIndex)
  80. wx.setStorageSync('isShareIndexEntry', 'true'); // 通过分享首页进入
  81. wx.removeStorageSync('isScanShopEntry'); // 通过扫门店码进入
  82. wx.removeStorageSync('isShareGoodEntry'); // 通过分享商品进入
  83. wx.removeStorageSync('isDirectEntry'); // 直接进入
  84. //清除是否分享商品标识缓存
  85. wx.removeStorageSync('isShareGood');
  86. } else {
  87. // 是否通过分享商品进入,或直接进入小程序
  88. // 是否分享过商品进入,isShareGoods标识是通过商品分享进入goods页面后,生成的标记;
  89. if (wx.getStorageSync('isShareGoods') == 'true') {
  90. // 商品分享一定会有storeId编号
  91. if(!wx.getStorageSync('storeId')) {
  92. console.log('商品分享进入首页,从商品页获取缓存的storeId不存在。');
  93. }
  94. console.log('商品分享进入首页,从商品页获取缓存的storeId:', wx.getStorageSync('storeId'));
  95. that.setData({
  96. storeId: wx.getStorageSync('storeId')
  97. });
  98. wx.setStorageSync('isShareGoodEntry', 'true'); // 通过分享商品进入
  99. wx.removeStorageSync('isScanShopEntry'); // 通过扫门店码进入
  100. wx.removeStorageSync('isShareIndexEntry'); // 通过分享首页进入
  101. wx.removeStorageSync('isDirectEntry'); // 直接进入
  102. } else {
  103. //直接进入小程序主页,则清除storeId,通过当前位置获取附近门店
  104. wx.removeStorageSync('storeId');
  105. that.setData({
  106. storeId: '',
  107. storeName: ''
  108. });
  109. console.log('直接进入小程序,storeId:', that.data.storeId)
  110. wx.setStorageSync('isDirectEntry', 'true'); // 直接进入
  111. wx.removeStorageSync('isScanShopEntry'); // 通过扫门店码进入
  112. wx.removeStorageSync('isShareIndexEntry'); // 通过分享首页进入
  113. wx.removeStorageSync('isShareGoodEntry'); // 通过分享商品进入
  114. //清除是否分享商品标识缓存
  115. wx.removeStorageSync('isShareGood');
  116. }
  117. }
  118. console.log('isScanShopEntry-0:', wx.getStorageSync('isScanShopEntry'))
  119. console.log('isShareIndexEntry-0:', wx.getStorageSync('isShareIndexEntry'))
  120. console.log('isShareGoodEntry-0:', wx.getStorageSync('isShareGoodEntry'))
  121. console.log('isDirectEntry-0:', wx.getStorageSync('isDirectEntry'))
  122. let storeId = wx.getStorageSync('storeId');
  123. wx.getLocation({
  124. success: function(location) {
  125. // do nothing
  126. // console.log('获取用户位置成功,获取附近门店');
  127. },
  128. fail: function() {
  129. // console.log('获取用户位置失败');
  130. wx.hideLoading();
  131. // 判断获取地理位置失败的原因
  132. // 获取配置信息
  133. wx.getSetting({
  134. success: function(res) {
  135. if (!res.authSetting['scope.userLocation']) {
  136. // 是否授权地理位置获取权限
  137. wx.showModal({
  138. title: '',
  139. content: '检测到您没有开启获取地理位置权限,是否开启',
  140. confirmText: '确定',
  141. success: function(resConfirm) {
  142. if (resConfirm.confirm) {
  143. wx.openSetting();
  144. } else {
  145. that.setData({
  146. isStartLocation: false
  147. });
  148. }
  149. }
  150. })
  151. } else {
  152. // 已授权地理位置获取权限,但依然未获取到定位
  153. // 用户未在手机系统设置中打开定位
  154. wx.showModal({
  155. title: '',
  156. content: '请在系统设置中打开定位服务',
  157. confirmText: '确定',
  158. showCancel: false,
  159. success: function(resConfirm2) {
  160. that.setData({
  161. isSystemLocation: false
  162. });
  163. }
  164. })
  165. }
  166. }
  167. })
  168. }
  169. });
  170. },
  171. onReady: function() {
  172. // 页面渲染完成
  173. },
  174. onShow: function() {
  175. // 页面显示
  176. let that = this;
  177. wx.setStorageSync("navUrl", "/pages/index/index");
  178. wx.setStorageSync("isLocationIndex", "false"); //购物车是否已跳转登录页,到首页重置字段为false未跳转
  179. if (!wx.getStorageSync('isRefusedLogin')) {
  180. // 是否拒绝授权
  181. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  182. console.log('缓存是否授权为空,用户已授权:' + wx.getStorageSync('isRefusedLogin'))
  183. wx.setStorageSync('isRefusedLogin', 'false'); //授权
  184. } else {
  185. console.log('缓存是否授权为空,用户未授权:' + wx.getStorageSync('isRefusedLogin'))
  186. wx.setStorageSync('isRefusedLogin', 'true'); //拒绝授权
  187. }
  188. } else {
  189. console.log('授权不为空,isRefusedLogin:' + wx.getStorageSync('isRefusedLogin'))
  190. wx.setStorageSync('isRefusedLogin', wx.getStorageSync('isRefusedLogin'));
  191. }
  192. console.log('是否拒绝授权,isRefusedLogin', wx.getStorageSync('isRefusedLogin'));
  193. // 判断isMapShow,是否同步门店,没有storeId的,要同步
  194. that.syncNearbyStore();
  195. /*if(wx.getStorageSync('isDirectEntry') == 'true') {
  196. // 直接进入小程序,或分享首页进入,同步
  197. that.syncNearbyStore();
  198. } else if(wx.getStorageSync('isScanShopEntry') == 'true'
  199. || wx.getStorageSync('isShareGoodEntry') == 'true'
  200. || wx.getStorageSync('isShareIndexEntry' == 'true')) {
  201. // 扫门店码,商品分享,首页分享,判断当前门店码判断isMapShow是否开启
  202. if (!wx.getStorageSync('storeId')) {
  203. // 判断条件成立,如发生丢失storeId的问题,为保持容错,加载附近门店
  204. wx.showModal({
  205. title: '',
  206. content: '<>通过扫门店码进入,仍然加载附近门店<>',
  207. confirmText: '确定',
  208. showCancel: true,
  209. cancelText: '取消',
  210. success: function(resConfirm2) {
  211. // 加载附近门店
  212. this.syncNearbyStore();
  213. }
  214. })
  215. } else {
  216. this.syncNearbyStore();
  217. // isMapShow是否开启
  218. // 门店及所属第三方商户信息
  219. // function callback() {
  220. // // 商户地图是否显示 0:否 1:是
  221. // if(that.data.isMapShow == '1') {
  222. // this.syncNearbyStore();
  223. // }
  224. // };
  225. // this.getStoreByIdWithMerch(callback);
  226. }
  227. } else {
  228. // do nothing
  229. console.log('进入小程序标识异常,onShow() 判断isMapShow,是否同步门店,无进入条件');
  230. }*/
  231. // if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  232. // } else {
  233. // // console.log('拒绝授权:' + wx.getStorageSync('isRefusedLogin'))
  234. // if (wx.getStorageSync('isRefusedLogin') != 'true') {
  235. // wx.navigateTo({
  236. // url: '/pages/auth/btnAuth/btnAuth',
  237. // })
  238. // }
  239. // }
  240. },
  241. onHide: function() {
  242. // 页面隐藏
  243. },
  244. onUnload: function() {
  245. // 页面关闭
  246. },
  247. showCouponPop() {
  248. let that = this;
  249. this.setData({
  250. showPop: false
  251. });
  252. // wx.showToast({
  253. // title: '恭喜获取优惠券一张' + that.data.couponVo.name,
  254. // duration: 2000
  255. // });
  256. wx.showModal({
  257. title: '获取优惠券一张',
  258. showCancel: false,
  259. content: that.data.couponVo.name
  260. })
  261. },
  262. onShareAppMessage: function() {
  263. var that = this;
  264. // console.log('分享:' + that.data.storeId)
  265. return {
  266. title: '中网跨境电商主页',
  267. desc: '中网跨境电商主页',
  268. path: '/pages/index/index?shareIndex=' + that.data.storeId
  269. }
  270. },
  271. // 获取门店及商户信息
  272. // 此时 storeId已经保存到本地缓存中
  273. getStoreByIdWithMerch: function(callback) {
  274. let that = this;
  275. var storeNameNew;
  276. var isMapShowNew;
  277. util.request(api.StoreByIdWithMerch, {
  278. storeId: wx.getStorageSync('storeId'),
  279. }).then((res) => {
  280. if (res.errno == '0') {
  281. if(res.data) {
  282. let storeVo = res.data.storeVo;
  283. let storeWithThirdMerchBizVo = res.data.storeWithThirdMerchBizVo;
  284. if(storeWithThirdMerchBizVo.id == 161){
  285. storeNameNew = '';
  286. isMapShowNew = 0;
  287. }else{
  288. storeNameNew = storeWithThirdMerchBizVo.storeName
  289. isMapShowNew = storeWithThirdMerchBizVo.isMapShow;
  290. }
  291. that.setData({
  292. storeId: storeWithThirdMerchBizVo.id,
  293. storeName: storeNameNew,
  294. isMapShow: isMapShowNew
  295. })
  296. console.log('获取第三方商户及门店信息,storeId:', storeWithThirdMerchBizVo.id)
  297. console.log('获取第三方商户及门店信息,storeName:', storeWithThirdMerchBizVo.storeName)
  298. console.log('获取第三方商户及门店信息,isMapShow:', storeWithThirdMerchBizVo.isMapShow)
  299. wx.setStorageSync('storeId', storeWithThirdMerchBizVo.id);
  300. wx.setStorageSync('storeName', storeNameNew);
  301. wx.setStorageSync('isMapShow', isMapShowNew);
  302. wx.setStorageSync('storeVo', JSON.stringify(storeVo));
  303. }
  304. if(callback){
  305. callback();
  306. }
  307. }
  308. }).catch((error) => {
  309. })
  310. },
  311. // 同步附近门店
  312. syncNearbyStore: function() {
  313. let that = this;
  314. // console.log(wx.getStorageSync('merchSn'));
  315. //获取附件门店信息
  316. wx.getLocation({
  317. success: function(location) {
  318. // console.log(location)
  319. that.loadNearbyStore(location);
  320. },
  321. fail: function() {
  322. wx.hideLoading();
  323. wx.getSetting({
  324. success: function(res) {
  325. if (!res.authSetting['scope.userLocation']) {
  326. that.setData({
  327. isStartLocation: false
  328. });
  329. } else {
  330. //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
  331. that.setData({
  332. isSystemLocation: false
  333. });
  334. }
  335. }
  336. });
  337. }
  338. });
  339. },
  340. /**
  341. * 定位确定授权加载附近门店信息
  342. * 分不同情况,加载附近门店信息
  343. */
  344. loadNearbyStore: function(location) {
  345. let that = this;
  346. that.setData({
  347. isStartLocation: true,
  348. isSystemLocation: true
  349. });
  350. var lng = location.longitude;
  351. var lat = location.latitude;
  352. wx.setStorageSync('location', JSON.stringify({
  353. lng,
  354. lat
  355. }));
  356. console.log('定位加载附近门店时,storeId:', wx.getStorageSync('storeId'))
  357. util.request(api.NearbyList, {
  358. longitude: lng,
  359. latitude: lat,
  360. storeId: wx.getStorageSync('storeId'),
  361. thirdPartyMerchCode: ''
  362. }).then((res) => {
  363. let nlist = res.data;
  364. // wx.removeStorageSync('nearStoreList');
  365. // wx.removeStorageSync('storeId');
  366. // wx.removeStorageSync('storeVo');
  367. // wx.setStorageSync('storeId', storeId);
  368. if (!wx.getStorageSync('currentCategory')) {
  369. wx.removeStorageSync('currentCategory');
  370. }
  371. if (!nlist.length) {
  372. wx.removeStorageSync('nearStoreList');
  373. } else {
  374. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  375. }
  376. let param = {}
  377. // 区分情况,对原有门店编号及名称进行覆盖
  378. if (wx.getStorageSync('isDirectEntry') == 'true') {
  379. // 直接进入小程序
  380. if (!nlist.length) {
  381. wx.removeStorageSync('storeVo');
  382. wx.removeStorageSync('mapIndexStoreId');
  383. that.setData({
  384. storeName: '附近暂无门店',
  385. storeId: ''
  386. })
  387. } else {
  388. // 更新
  389. that.setData({
  390. storeId: nlist[0].id,
  391. storeName: nlist[0].storeName
  392. })
  393. }
  394. wx.setStorageSync('storeId', that.data.storeId);
  395. param.id = nlist[0].id;
  396. param.merchSn = nlist[0].merchSn;
  397. } else if(wx.getStorageSync('isScanShopEntry') == 'true'
  398. || wx.getStorageSync('isShareIndexEntry') == 'true'
  399. || wx.getStorageSync('isShareGoodEntry') == 'true') {
  400. // 不能使用附近门店编码storeId替代原有的storeId
  401. param.id = wx.getStorageSync('storeId');
  402. param.merchSn = nlist[0].merchSn;
  403. }
  404. wx.setStorageSync('mapIndexStoreId', nlist[0].id);
  405. // 获取门店及第三方商户对应的信息
  406. that.getStoreByIdWithMerch(that.checkLoginTokenByUpd(param));
  407. // that.checkLoginTokenByUpd(param);
  408. // wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  409. })
  410. },
  411. /**
  412. * 校验token是否失效,失效则更新新token
  413. */
  414. checkLoginTokenByUpd: function (param){
  415. var that = this;
  416. // token验证
  417. util.request(api.checkToken, {
  418. token: wx.getStorageSync('token'),
  419. isRefusedLogin: wx.getStorageSync('isRefusedLogin'),
  420. userId: wx.getStorageSync('userId')+'',
  421. storeId: parseInt(param.storeId)
  422. }, 'POST').then(function (res) {
  423. if (res.data.errno == 401) {
  424. // token在服务端失败
  425. console.log('checkLoginTokenByUpd() res.data.errmsg:', res.data.errmsg)
  426. console.log('checkLoginTokenByUpd() token:', wx.getStorageSync('token'))
  427. wx.getSetting({
  428. success: (setRes) => {
  429. if (setRes.authSetting['scope.userInfo'] == undefined) {
  430. wx.setStorageSync('isRefusedLogin', 'true');//未授权
  431. wx.request({
  432. url: api.AuthLoginSaveToken,
  433. data: {
  434. storeId: param.storeId,
  435. merchSn: param.merchSn
  436. },
  437. method: 'POST',
  438. header: {
  439. 'Content-Type': 'application/json',
  440. 'isRefusedLogin': wx.getStorageSync('isRefusedLogin')
  441. },
  442. success: function (wxRes) {
  443. // console.log(wxRes)
  444. if (wxRes.data.errno === 0) {
  445. //存储信息
  446. wx.setStorageSync('token', wxRes.data.data.token);
  447. console.log('checkLoginTokenByUpd() loginSaveToken 微信用户未授权,获取到一个新的token:' + wx.getStorageSync('token'))
  448. that.chooseStore(param.id, param.merchSn);
  449. }
  450. },
  451. fail: function (err) {
  452. console.log("checkLoginTokenByUpd() loginSaveToken failed");
  453. }
  454. });
  455. } else {
  456. console.log('微信用户点击授权操作,过了一段时间token失效,更新token')
  457. wx.login({
  458. success: function (loginRes) {
  459. if (loginRes.code) {
  460. //登录远程服务器
  461. wx.request({
  462. url: api.AuthLoginSaveToken,
  463. data: {
  464. code: loginRes.code,
  465. userInfo: wx.getStorageSync('userInfo'),
  466. storeId: wx.getStorageSync('storeId'),
  467. merchSn: param.merchSn
  468. },
  469. method: 'POST',
  470. header: {
  471. 'Content-Type': 'application/json'
  472. },
  473. success: function (wxRes) {
  474. // console.log(wxRes)
  475. if (wxRes.data.errno === 0) {
  476. //存储用户信息
  477. if (wxRes.data.data.userInfo) {
  478. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  479. }
  480. if (wxRes.data.data.token) {
  481. wx.setStorageSync('token', wxRes.data.data.token);
  482. }
  483. wx.setStorageSync('userId', wxRes.data.data.userId);
  484. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  485. console.log("checkLoginTokenByUpd() loginSaveToken token失效,更新token,登录成功");
  486. that.chooseStore(param.id, param.merchSn);
  487. }
  488. },
  489. fail: function (err) {
  490. console.log("checkLoginTokenByUpd() loginSaveToken failed");
  491. }
  492. });
  493. } else {
  494. console.log("checkLoginTokenByUpd() loginSaveToken !loginRes.code failed");
  495. }
  496. },
  497. fail: function (err) {
  498. console.log("checkLoginTokenByUpd() wx.login() failed");
  499. }
  500. });
  501. }
  502. }
  503. });
  504. } else {
  505. console.log('checkLoginTokenByUpd() checkToken token未失效');
  506. // console.log(res)
  507. that.chooseStore(param.id, param.merchSn);
  508. }
  509. });
  510. },
  511. // 更新门店Id
  512. chooseStore: function(storeId, merchSn) {
  513. let that = this;
  514. console.log('chooseStore(),storeId:', storeId,)
  515. util.request(api.ChooseStoreId, {
  516. storeId: storeId,
  517. merchSn: merchSn,
  518. isRefusedLogin: wx.getStorageSync('isRefusedLogin')
  519. }, 'POST').then(function (res) {
  520. if (res.errno === 0) {
  521. if(wx.getStorageSync('isScanShopEntry') == 'true'
  522. || wx.getStorageSync('isShareIndexEntry') == 'true'
  523. || wx.getStorageSync('isShareGoodEntry') == 'true'){
  524. // 通过扫门店码,分享首页,分享商品等进入,不更新
  525. } else if(wx.getStorageSync('isDirectEntry') == 'true') {
  526. // 直接进入,更新
  527. that.setData({
  528. storeId: storeId,
  529. });
  530. console.log('更新了门店编号,storeId:', storeId)
  531. wx.setStorageSync('storeId', storeId);
  532. }
  533. wx.setStorageSync('merchSn', merchSn);
  534. console.log('chooseStore() token:',res.data.token)
  535. wx.setStorageSync('token', res.data.token);
  536. console.log('isScanShopEntry-1:', wx.getStorageSync('isScanShopEntry'))
  537. console.log('isShareIndexEntry-1:', wx.getStorageSync('isShareIndexEntry'))
  538. console.log('isShareGoodEntry-1:', wx.getStorageSync('isShareGoodEntry'))
  539. console.log('isDirectEntry-1:', wx.getStorageSync('isDirectEntry'))
  540. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  541. that.reLoad();
  542. } else {
  543. wx.setStorageSync('isLoadGoods', '0'); //1:代表每次点首页都会重新加载商品,0:否
  544. }
  545. that.getIndexData();
  546. that.enableActivity();
  547. that.getGroupData();
  548. }
  549. });
  550. },
  551. reLoad: function() {
  552. let that = this;
  553. // console.log(wx.getStorageSync('userId'));
  554. // console.log(wx.getStorageSync('storeId'));
  555. // console.log(wx.getStorageSync('merchSn'));
  556. if (wx.getStorageSync('storeId')) {
  557. if (wx.getStorageSync('userId')) {
  558. wx.request({
  559. url: api.updateLoginUser,
  560. data: {
  561. userId: wx.getStorageSync('userId'),
  562. storeId: wx.getStorageSync('storeId'),
  563. merchSn: wx.getStorageSync('merchSn')
  564. },
  565. method: 'POST',
  566. header: {
  567. 'Content-Type': 'application/json'
  568. },
  569. success: function(wxRes) {
  570. if (wxRes.data.errno === 0) {
  571. that.setData({
  572. isMapShow: wxRes.data.data.isMapShow
  573. });
  574. // console.log(wxRes.data.data.isLoadGoods);
  575. wx.setStorageSync('isLoadGoods', wxRes.data.data.isLoadGoods);
  576. wx.setStorageSync('thirdPartyMerchCode', wxRes.data.data.thirdPartyMerchCode);
  577. // console.log(that.data.isMapShow)
  578. // console.log("用户信息更新成功");
  579. }
  580. },
  581. fail: function(err) {
  582. console.log("reLoad()::api.updateLoginUser: failed");
  583. }
  584. });
  585. }
  586. }
  587. },
  588. getIndexData: function() {
  589. setTimeout(function() {}, 350)
  590. wx.showLoading({
  591. title: '加载中...',
  592. })
  593. let that = this;
  594. util.request(api.IndexUrl,
  595. {storeId: wx.getStorageSync('storeId')}
  596. ).then(function(res) {
  597. if (res.errno === 0) {
  598. // console.log(res.data.banner);
  599. that.setData({
  600. // newGoods: res.data.newGoodsList,
  601. // hotGoods: res.data.hotGoodsList,
  602. // topics: res.data.topicList,
  603. // brand: res.data.brandList,
  604. // floorGoods: res.data.categoryList,
  605. banner: res.data.banner,
  606. // groupBanner: res.data.groupBanner,
  607. channel: res.data.channel,
  608. tipMsg: res.data.tipMsg
  609. });
  610. //当切换了门店地图(mapIndexStoreId);或由用户分享进入的商品详情页,再切换进入的首页(isShareGoodEntry:true)
  611. if (wx.getStorageSync('storeId') != wx.getStorageSync('mapIndexStoreId')
  612. || wx.getStorageSync('isShareGoodEntry') == 'true'
  613. || that.data.hotGoods.length == 0
  614. || wx.getStorageSync('isLoadGoods') == '1') {
  615. that.setData({
  616. hotGoods: [],
  617. page: 1
  618. });
  619. that.getGoodsList();
  620. if (wx.getStorageSync('isLoadGoods') == '1') {
  621. util.request(api.UpdateStoreLoadGoods, {
  622. storeId: wx.getStorageSync('storeId')
  623. }).then(function(res) {});
  624. }
  625. wx.setStorageSync('isShareGoodEntry', 'false');
  626. }
  627. }
  628. wx.hideLoading();
  629. });
  630. },
  631. getGoodsList: function() {
  632. let that = this;
  633. util.request(api.GoodsHot).then(function(res) {
  634. if (res.errno == 0) {
  635. util.request(api.HotGoodsList, {
  636. isHot: 1,
  637. page: that.data.page,
  638. size: that.data.size,
  639. categoryId: 0
  640. })
  641. .then(function(res) {
  642. if (res.errno == 0) {
  643. let goodsList = that.data.hotGoods.concat(res.data.goodsList);
  644. that.setData({
  645. hotGoods: goodsList,
  646. list: res.data.goodsList
  647. });
  648. }
  649. });
  650. wx.hideLoading();
  651. }
  652. });
  653. },
  654. getGroupData: function() {
  655. let that = this;
  656. util.request(api.GroupList).then(function(res) {
  657. if (res.errno === 0) {
  658. that.setData({
  659. groupGoods: res.data.data,
  660. });
  661. }
  662. });
  663. },
  664. handleStore() {
  665. console.log('进入 handleStore()');
  666. wx.navigateTo({
  667. url: '../map/map',
  668. })
  669. },
  670. goSearch() {
  671. wx.navigateTo({
  672. url: '../search/search',
  673. })
  674. },
  675. goCatalog: function(e) {
  676. let url = '';
  677. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  678. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  679. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  680. wx.setStorageSync('isSwitchCatalog', 'true'); //主页跳转至分类页加载商品,
  681. wx.switchTab({
  682. url: '/pages/catalog/catalog',
  683. });
  684. },
  685. onReachBottom: function() {
  686. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  687. return false;
  688. }
  689. this.setData({
  690. bottomLoading: true
  691. });
  692. // this.getFloorCategory();
  693. },
  694. //购物车减少
  695. cutNumber: function(e) {
  696. let that = this;
  697. var goodsId = e.currentTarget.dataset.goodsId;
  698. var productId = e.currentTarget.dataset.productId;
  699. var hotGoods = that.data.hotGoods;
  700. // hotGoods.forEach(function (val, index, arr) {
  701. // if (val.product_id == productId) {
  702. // val.cart_num = val.cart_num - 1;
  703. // if (val.cart_num >= 0) {
  704. // hotGoods[index] = val;
  705. // }
  706. // }
  707. // });
  708. // that.setData({ hotGoods: hotGoods });
  709. util.request(api.CartMinus, {
  710. goodsId: goodsId,
  711. productId: productId,
  712. number: 1
  713. }, 'POST').then(function(res) {
  714. if (res.errno === 0 && null != res.data) {
  715. var hotGoods = that.data.hotGoods;
  716. hotGoods.forEach(function(val, index, arr) {
  717. if (val.product_id == productId) {
  718. val.cart_num = res.data;
  719. hotGoods[index] = val;
  720. that.setData({
  721. hotGoods: hotGoods
  722. });
  723. }
  724. }, that);
  725. }
  726. });
  727. },
  728. //购物车增加
  729. addNumber: function(e) {
  730. let that = this;
  731. var goodsId = e.currentTarget.dataset.goodsId;
  732. var productId = e.currentTarget.dataset.productId;
  733. var hotGoods = that.data.hotGoods;
  734. // hotGoods.forEach(function (val, index, arr) {
  735. // if (val.product_id == productId) {
  736. // val.cart_num = val.cart_num + 1;
  737. // hotGoods[index] = val;
  738. // }
  739. // });
  740. // that.setData({ hotGoods: hotGoods });
  741. util.request(api.CartAdd, {
  742. goodsId: goodsId,
  743. productId: productId,
  744. number: 1
  745. }, 'POST').then(function(res) {
  746. if (res.errno === 0 && null != res.data) {
  747. hotGoods.forEach(function(val, index, arr) {
  748. res.data.cartList.forEach(function(cartVal, cartIndex, cartArr) {
  749. if (val.product_id == cartVal.product_id) {
  750. val.cart_num = cartVal.number;
  751. hotGoods[index] = val;
  752. }
  753. });
  754. that.setData({
  755. hotGoods: hotGoods
  756. });
  757. }, that);
  758. wx.showToast({
  759. title: '添加成功'
  760. })
  761. } else {
  762. wx.showToast({
  763. title: res.errmsg,
  764. icon: 'none'
  765. })
  766. }
  767. });
  768. },
  769. // 查询是否有活动
  770. enableActivity: function() {
  771. let that = this;
  772. let couponIds = wx.getStorageSync('couponIds');
  773. if (!couponIds) {
  774. couponIds = new Array();
  775. }
  776. // util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  777. // // if (res.errno === 0 && null != res.data.showCoupon) {
  778. // // if (couponIds.contains(res.data.showCoupon.id)) {
  779. // // return;
  780. // // }
  781. // // couponIds.push(res.data.showCoupon.id);
  782. // // wx.setStorageSync('couponIds', couponIds);
  783. // // that.setData({
  784. // // couponVo: res.data.showCoupon,
  785. // // showPop: true
  786. // // });
  787. // // } else
  788. // if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  789. // that.setData({
  790. // couponVo: res.data.takeCoupon,
  791. // showPop: true
  792. // });
  793. // }
  794. // });
  795. },
  796. // 商品扫码
  797. scanGoodsCode: function(e) {
  798. var that = this;
  799. var code;
  800. var value;
  801. var substrValue;
  802. var scanType;
  803. // 调起客户端扫码界面进行扫码
  804. wx.scanCode({
  805. // 是否只能从相机扫码
  806. onlyFromCamera: true,
  807. // 扫码类型, barCode:一维码, qrCode:二维码
  808. scanType: ['barCode', 'qrCode'],
  809. success: function(res) {
  810. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  811. that.value = res.result;
  812. that.scanType = res.scanType;
  813. that.setData({
  814. goodsCode: that.code
  815. });
  816. if (that.scanType == 'QR_CODE') { //二维码
  817. that.substrValue = that.value.substring(0, 5);
  818. that.value = that.value.substring(5, that.value.length);
  819. // var goodId = that.value.substring(18, that.value.length);
  820. var scanArray = that.value.split('&');
  821. // console.log(scanArray.length);
  822. if (scanArray.length < 2) {
  823. wx.showModal({
  824. title: '',
  825. content: '您所扫描的商品无效',
  826. showCancel: false
  827. });
  828. return;
  829. }
  830. var goodId = scanArray[0].substring(18, that.value.length);
  831. var storeId = scanArray[1].substring(8, that.value.length);
  832. // console.log("storeId:" + storeId);
  833. // console.log("goodId:" + goodId);
  834. if (that.substrValue != 'emato') { //../goods/goods?id=&merchSn=
  835. wx.showModal({
  836. title: '',
  837. content: '您所扫描的商品无效',
  838. showCancel: false,
  839. success: function(res) {
  840. if (res.confirm) {
  841. console.log('用户点击确定')
  842. } else if (res.cancel) {
  843. console.log('用户点击取消')
  844. }
  845. }
  846. });
  847. return;
  848. }
  849. if (storeId != wx.getStorageSync('storeId')) {
  850. wx.showModal({
  851. title: '扫描结果',
  852. content: '该商品不属于当前门店',
  853. showCancel: false
  854. });
  855. return;
  856. }
  857. util.request(api.GoodsDetail, {
  858. id: goodId,
  859. storeId: storeId,
  860. referrer: ''
  861. }).then(function(res) {
  862. if (res.errno === 0) {
  863. // console.log(res);
  864. // 跳转页面
  865. setTimeout(function() {
  866. wx.navigateTo({
  867. url: that.value,
  868. success: function(e) {
  869. console.log('跳转成功');
  870. },
  871. fail: function(e) {
  872. console.log('跳转失败');
  873. }
  874. })
  875. }, 350)
  876. } else {
  877. wx.showModal({
  878. title: '扫描结果',
  879. content: '商品不存在',
  880. showCancel: false
  881. });
  882. }
  883. });
  884. } else { //其他码
  885. //弹框显示结果
  886. wx.showModal({
  887. title: '扫描结果',
  888. content: that.value,
  889. showCancel: false
  890. });
  891. }
  892. },
  893. fail: function() {
  894. // 显示提示框
  895. wx.showToast({
  896. title: '扫码失败',
  897. icon: 'none',
  898. // 提示的延迟时间
  899. duration: 3000
  900. })
  901. }
  902. })
  903. },
  904. imgOnLoad: function(e) {
  905. let that = this;
  906. // console.log('图片加载完成');
  907. // var realthumb = e.target.dataset.thumb;
  908. // let list = that.data.list
  909. // for (var i = 0; i < list.length; i++) {
  910. // if (list[i].thumb == realthumb) {
  911. // list[i].loaded = true
  912. // }
  913. // that.setData({
  914. // list: list
  915. // })
  916. // }
  917. },
  918. switchAttrPop: function() {
  919. this.setData({
  920. openAttr: !this.data.openAttr
  921. })
  922. },
  923. hideSwitchAttrPop: function() {
  924. this.setData({
  925. openAttr: false
  926. })
  927. },
  928. //购物车增加
  929. addCart: function(e) {
  930. let that = this;
  931. that.setData({
  932. number: 1
  933. });
  934. var goodsId = e.currentTarget.dataset.goodsId;
  935. var retailPrice = e.currentTarget.dataset.retailPrice;
  936. util.request(api.GoodsSku, {
  937. goodsId: goodsId
  938. }).then(function(res) {
  939. if (res.errno === 0 && null != res.data) {
  940. that.setData({
  941. goodsVo: res.data.goodsVo,
  942. specificationList: res.data.specificationList,
  943. productList: res.data.productList,
  944. openAttr: !that.data.openAttr,
  945. retailPrice: retailPrice,
  946. stockNum: res.data.stockNum,
  947. cartNumber: res.data.cartNumber,
  948. checkedSpecText: res.data.specificationList[0].valueList[0].value
  949. });
  950. //
  951. let _specificationList = res.data.specificationList;
  952. for (let i = 0; i < _specificationList.length; i++) {
  953. if (_specificationList[i].valueList.length == 1) {
  954. //如果已经选中,则反选
  955. _specificationList[i].valueList[0].checked = true;
  956. }
  957. }
  958. that.setData({
  959. 'specificationList': _specificationList
  960. });
  961. }
  962. });
  963. },
  964. shows: function(e) {
  965. // wx.redirectTo({
  966. // url: '/pages/images/images'
  967. // })
  968. // wx.navigateTo({
  969. // url: '../ucenter/campMinus/campMinus?campMinusId=' + 9 + '&&storeId=' + 8 + '&&campName=' + '满200减50' + "&&isShare=1"
  970. // })
  971. wx.redirectTo({
  972. url: '/pages/checkVideo/checkVideo'
  973. })
  974. },
  975. onReachBottom() {
  976. var that = this;
  977. if (that.data.list.length > 0) {
  978. // console.log(that.data.list)
  979. wx.showLoading({
  980. title: '加载中...',
  981. })
  982. }
  983. that.setData({
  984. page: that.data.page + 1
  985. });
  986. that.getGoodsList();
  987. }
  988. })