index.js 34 KB

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