1
0

index.js 34 KB

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