1
0

index.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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.getFuzzyLocation({
  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.getFuzzyLocation({
  317. success: function(location) {
  318. // console.log(location)
  319. that.loadNearbyStore(location);
  320. },
  321. fail: function(res) {
  322. console.log("获取位置 => ", res);
  323. wx.hideLoading();
  324. wx.getSetting({
  325. success: function(res) {
  326. if (!res.authSetting['scope.userLocation']) {
  327. that.setData({
  328. isStartLocation: false
  329. });
  330. } else {
  331. //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
  332. that.setData({
  333. isSystemLocation: false
  334. });
  335. }
  336. }
  337. });
  338. }
  339. });
  340. },
  341. /**
  342. * 定位确定授权加载附近门店信息
  343. * 分不同情况,加载附近门店信息
  344. */
  345. loadNearbyStore: function(location) {
  346. let that = this;
  347. that.setData({
  348. isStartLocation: true,
  349. isSystemLocation: true
  350. });
  351. var lng = location.longitude;
  352. var lat = location.latitude;
  353. wx.setStorageSync('location', JSON.stringify({
  354. lng,
  355. lat
  356. }));
  357. console.log('定位加载附近门店时,storeId:', wx.getStorageSync('storeId'))
  358. util.request(api.NearbyList, {
  359. longitude: lng,
  360. latitude: lat,
  361. storeId: wx.getStorageSync('storeId'),
  362. thirdPartyMerchCode: ''
  363. }).then((res) => {
  364. let nlist = res.data;
  365. // wx.removeStorageSync('nearStoreList');
  366. // wx.removeStorageSync('storeId');
  367. // wx.removeStorageSync('storeVo');
  368. // wx.setStorageSync('storeId', storeId);
  369. if (!wx.getStorageSync('currentCategory')) {
  370. wx.removeStorageSync('currentCategory');
  371. }
  372. if (!nlist.length) {
  373. wx.removeStorageSync('nearStoreList');
  374. } else {
  375. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  376. }
  377. let param = {}
  378. // 区分情况,对原有门店编号及名称进行覆盖
  379. if (wx.getStorageSync('isDirectEntry') == 'true') {
  380. // 直接进入小程序
  381. if (!nlist.length) {
  382. wx.removeStorageSync('storeVo');
  383. wx.removeStorageSync('mapIndexStoreId');
  384. that.setData({
  385. storeName: '附近暂无门店',
  386. storeId: ''
  387. })
  388. } else {
  389. // 更新
  390. that.setData({
  391. storeId: nlist[0].id,
  392. storeName: nlist[0].storeName
  393. })
  394. }
  395. wx.setStorageSync('storeId', that.data.storeId);
  396. param.id = nlist[0].id;
  397. param.merchSn = nlist[0].merchSn;
  398. } else if(wx.getStorageSync('isScanShopEntry') == 'true'
  399. || wx.getStorageSync('isShareIndexEntry') == 'true'
  400. || wx.getStorageSync('isShareGoodEntry') == 'true') {
  401. // 不能使用附近门店编码storeId替代原有的storeId
  402. param.id = wx.getStorageSync('storeId');
  403. param.merchSn = nlist[0].merchSn;
  404. }
  405. wx.setStorageSync('mapIndexStoreId', nlist[0].id);
  406. // 获取门店及第三方商户对应的信息
  407. that.getStoreByIdWithMerch(that.checkLoginTokenByUpd(param));
  408. // that.checkLoginTokenByUpd(param);
  409. // wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  410. })
  411. },
  412. /**
  413. * 校验token是否失效,失效则更新新token
  414. */
  415. checkLoginTokenByUpd: function (param){
  416. var that = this;
  417. // token验证
  418. util.request(api.checkToken, {
  419. token: wx.getStorageSync('token'),
  420. isRefusedLogin: wx.getStorageSync('isRefusedLogin'),
  421. userId: wx.getStorageSync('userId')+'',
  422. storeId: parseInt(param.storeId)
  423. }, 'POST').then(function (res) {
  424. if (res.data.errno == 401) {
  425. // token在服务端失败
  426. console.log('checkLoginTokenByUpd() res.data.errmsg:', res.data.errmsg)
  427. console.log('checkLoginTokenByUpd() token:', wx.getStorageSync('token'))
  428. wx.getSetting({
  429. success: (setRes) => {
  430. if (setRes.authSetting['scope.userInfo'] == undefined) {
  431. wx.setStorageSync('isRefusedLogin', 'true');//未授权
  432. wx.request({
  433. url: api.AuthLoginSaveToken,
  434. data: {
  435. storeId: param.storeId,
  436. merchSn: param.merchSn
  437. },
  438. method: 'POST',
  439. header: {
  440. 'Content-Type': 'application/json',
  441. 'isRefusedLogin': wx.getStorageSync('isRefusedLogin')
  442. },
  443. success: function (wxRes) {
  444. // console.log(wxRes)
  445. if (wxRes.data.errno === 0) {
  446. //存储信息
  447. wx.setStorageSync('token', wxRes.data.data.token);
  448. console.log('checkLoginTokenByUpd() loginSaveToken 微信用户未授权,获取到一个新的token:' + wx.getStorageSync('token'))
  449. that.chooseStore(param.id, param.merchSn);
  450. }
  451. },
  452. fail: function (err) {
  453. console.log("checkLoginTokenByUpd() loginSaveToken failed");
  454. }
  455. });
  456. } else {
  457. console.log('微信用户点击授权操作,过了一段时间token失效,更新token')
  458. wx.login({
  459. success: function (loginRes) {
  460. if (loginRes.code) {
  461. //登录远程服务器
  462. wx.request({
  463. url: api.AuthLoginSaveToken,
  464. data: {
  465. code: loginRes.code,
  466. userInfo: wx.getStorageSync('userInfo'),
  467. storeId: wx.getStorageSync('storeId'),
  468. merchSn: param.merchSn
  469. },
  470. method: 'POST',
  471. header: {
  472. 'Content-Type': 'application/json'
  473. },
  474. success: function (wxRes) {
  475. // console.log(wxRes)
  476. if (wxRes.data.errno === 0) {
  477. //存储用户信息
  478. if (wxRes.data.data.userInfo) {
  479. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  480. }
  481. if (wxRes.data.data.token) {
  482. wx.setStorageSync('token', wxRes.data.data.token);
  483. }
  484. wx.setStorageSync('userId', wxRes.data.data.userId);
  485. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  486. console.log("checkLoginTokenByUpd() loginSaveToken token失效,更新token,登录成功");
  487. that.chooseStore(param.id, param.merchSn);
  488. }
  489. },
  490. fail: function (err) {
  491. console.log("checkLoginTokenByUpd() loginSaveToken failed");
  492. }
  493. });
  494. } else {
  495. console.log("checkLoginTokenByUpd() loginSaveToken !loginRes.code failed");
  496. }
  497. },
  498. fail: function (err) {
  499. console.log("checkLoginTokenByUpd() wx.login() failed");
  500. }
  501. });
  502. }
  503. }
  504. });
  505. } else {
  506. console.log('checkLoginTokenByUpd() checkToken token未失效');
  507. // console.log(res)
  508. that.chooseStore(param.id, param.merchSn);
  509. }
  510. });
  511. },
  512. // 更新门店Id
  513. chooseStore: function(storeId, merchSn) {
  514. let that = this;
  515. console.log('chooseStore(),storeId:', storeId,)
  516. util.request(api.ChooseStoreId, {
  517. storeId: storeId,
  518. merchSn: merchSn,
  519. isRefusedLogin: wx.getStorageSync('isRefusedLogin')
  520. }, 'POST').then(function (res) {
  521. if (res.errno === 0) {
  522. if(wx.getStorageSync('isScanShopEntry') == 'true'
  523. || wx.getStorageSync('isShareIndexEntry') == 'true'
  524. || wx.getStorageSync('isShareGoodEntry') == 'true'){
  525. // 通过扫门店码,分享首页,分享商品等进入,不更新
  526. } else if(wx.getStorageSync('isDirectEntry') == 'true') {
  527. // 直接进入,更新
  528. that.setData({
  529. storeId: storeId,
  530. });
  531. console.log('更新了门店编号,storeId:', storeId)
  532. wx.setStorageSync('storeId', storeId);
  533. }
  534. wx.setStorageSync('merchSn', merchSn);
  535. console.log('chooseStore() token:',res.data.token)
  536. wx.setStorageSync('token', res.data.token);
  537. console.log('isScanShopEntry-1:', wx.getStorageSync('isScanShopEntry'))
  538. console.log('isShareIndexEntry-1:', wx.getStorageSync('isShareIndexEntry'))
  539. console.log('isShareGoodEntry-1:', wx.getStorageSync('isShareGoodEntry'))
  540. console.log('isDirectEntry-1:', wx.getStorageSync('isDirectEntry'))
  541. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  542. that.reLoad();
  543. } else {
  544. wx.setStorageSync('isLoadGoods', '0'); //1:代表每次点首页都会重新加载商品,0:否
  545. }
  546. that.getIndexData();
  547. that.enableActivity();
  548. that.getGroupData();
  549. }
  550. });
  551. },
  552. reLoad: function() {
  553. let that = this;
  554. // console.log(wx.getStorageSync('userId'));
  555. // console.log(wx.getStorageSync('storeId'));
  556. // console.log(wx.getStorageSync('merchSn'));
  557. if (wx.getStorageSync('storeId')) {
  558. if (wx.getStorageSync('userId')) {
  559. wx.request({
  560. url: api.updateLoginUser,
  561. data: {
  562. userId: wx.getStorageSync('userId'),
  563. storeId: wx.getStorageSync('storeId'),
  564. merchSn: wx.getStorageSync('merchSn')
  565. },
  566. method: 'POST',
  567. header: {
  568. 'Content-Type': 'application/json'
  569. },
  570. success: function(wxRes) {
  571. if (wxRes.data.errno === 0) {
  572. that.setData({
  573. isMapShow: wxRes.data.data.isMapShow
  574. });
  575. // console.log(wxRes.data.data.isLoadGoods);
  576. wx.setStorageSync('isLoadGoods', wxRes.data.data.isLoadGoods);
  577. wx.setStorageSync('thirdPartyMerchCode', wxRes.data.data.thirdPartyMerchCode);
  578. // console.log(that.data.isMapShow)
  579. // console.log("用户信息更新成功");
  580. }
  581. },
  582. fail: function(err) {
  583. console.log("reLoad()::api.updateLoginUser: failed");
  584. }
  585. });
  586. }
  587. }
  588. },
  589. getIndexData: function() {
  590. setTimeout(function() {}, 350)
  591. wx.showLoading({
  592. title: '加载中...',
  593. })
  594. let that = this;
  595. util.request(api.IndexUrl,
  596. {storeId: wx.getStorageSync('storeId')}
  597. ).then(function(res) {
  598. if (res.errno === 0) {
  599. // console.log(res.data.banner);
  600. that.setData({
  601. // newGoods: res.data.newGoodsList,
  602. // hotGoods: res.data.hotGoodsList,
  603. // topics: res.data.topicList,
  604. // brand: res.data.brandList,
  605. // floorGoods: res.data.categoryList,
  606. banner: res.data.banner,
  607. // groupBanner: res.data.groupBanner,
  608. channel: res.data.channel,
  609. tipMsg: res.data.tipMsg
  610. });
  611. //当切换了门店地图(mapIndexStoreId);或由用户分享进入的商品详情页,再切换进入的首页(isShareGoodEntry:true)
  612. if (wx.getStorageSync('storeId') != wx.getStorageSync('mapIndexStoreId')
  613. || wx.getStorageSync('isShareGoodEntry') == 'true'
  614. || that.data.hotGoods.length == 0
  615. || wx.getStorageSync('isLoadGoods') == '1') {
  616. that.setData({
  617. hotGoods: [],
  618. page: 1
  619. });
  620. that.getGoodsList();
  621. if (wx.getStorageSync('isLoadGoods') == '1') {
  622. util.request(api.UpdateStoreLoadGoods, {
  623. storeId: wx.getStorageSync('storeId')
  624. }).then(function(res) {});
  625. }
  626. wx.setStorageSync('isShareGoodEntry', 'false');
  627. }
  628. }
  629. wx.hideLoading();
  630. });
  631. },
  632. getGoodsList: function() {
  633. let that = this;
  634. util.request(api.GoodsHot).then(function(res) {
  635. if (res.errno == 0) {
  636. util.request(api.HotGoodsList, {
  637. isHot: 1,
  638. page: that.data.page,
  639. size: that.data.size,
  640. categoryId: 0
  641. })
  642. .then(function(res) {
  643. if (res.errno == 0) {
  644. let goodsList = that.data.hotGoods.concat(res.data.goodsList);
  645. that.setData({
  646. hotGoods: goodsList,
  647. list: res.data.goodsList
  648. });
  649. }
  650. });
  651. wx.hideLoading();
  652. }
  653. });
  654. },
  655. getGroupData: function() {
  656. let that = this;
  657. util.request(api.GroupList).then(function(res) {
  658. if (res.errno === 0) {
  659. that.setData({
  660. groupGoods: res.data.data,
  661. });
  662. }
  663. });
  664. },
  665. handleStore() {
  666. console.log('进入 handleStore()');
  667. wx.navigateTo({
  668. url: '../map/map',
  669. })
  670. },
  671. goSearch() {
  672. wx.navigateTo({
  673. url: '../search/search',
  674. })
  675. },
  676. goCatalog: function(e) {
  677. let url = '';
  678. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  679. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  680. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  681. wx.setStorageSync('isSwitchCatalog', 'true'); //主页跳转至分类页加载商品,
  682. wx.switchTab({
  683. url: '/pages/catalog/catalog',
  684. });
  685. },
  686. onReachBottom: function() {
  687. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  688. return false;
  689. }
  690. this.setData({
  691. bottomLoading: true
  692. });
  693. // this.getFloorCategory();
  694. },
  695. //购物车减少
  696. cutNumber: function(e) {
  697. let that = this;
  698. var goodsId = e.currentTarget.dataset.goodsId;
  699. var productId = e.currentTarget.dataset.productId;
  700. var hotGoods = that.data.hotGoods;
  701. // hotGoods.forEach(function (val, index, arr) {
  702. // if (val.product_id == productId) {
  703. // val.cart_num = val.cart_num - 1;
  704. // if (val.cart_num >= 0) {
  705. // hotGoods[index] = val;
  706. // }
  707. // }
  708. // });
  709. // that.setData({ hotGoods: hotGoods });
  710. util.request(api.CartMinus, {
  711. goodsId: goodsId,
  712. productId: productId,
  713. number: 1
  714. }, 'POST').then(function(res) {
  715. if (res.errno === 0 && null != res.data) {
  716. var hotGoods = that.data.hotGoods;
  717. hotGoods.forEach(function(val, index, arr) {
  718. if (val.product_id == productId) {
  719. val.cart_num = res.data;
  720. hotGoods[index] = val;
  721. that.setData({
  722. hotGoods: hotGoods
  723. });
  724. }
  725. }, that);
  726. }
  727. });
  728. },
  729. //购物车增加
  730. addNumber: function(e) {
  731. let that = this;
  732. var goodsId = e.currentTarget.dataset.goodsId;
  733. var productId = e.currentTarget.dataset.productId;
  734. var hotGoods = that.data.hotGoods;
  735. // hotGoods.forEach(function (val, index, arr) {
  736. // if (val.product_id == productId) {
  737. // val.cart_num = val.cart_num + 1;
  738. // hotGoods[index] = val;
  739. // }
  740. // });
  741. // that.setData({ hotGoods: hotGoods });
  742. util.request(api.CartAdd, {
  743. goodsId: goodsId,
  744. productId: productId,
  745. number: 1
  746. }, 'POST').then(function(res) {
  747. if (res.errno === 0 && null != res.data) {
  748. hotGoods.forEach(function(val, index, arr) {
  749. res.data.cartList.forEach(function(cartVal, cartIndex, cartArr) {
  750. if (val.product_id == cartVal.product_id) {
  751. val.cart_num = cartVal.number;
  752. hotGoods[index] = val;
  753. }
  754. });
  755. that.setData({
  756. hotGoods: hotGoods
  757. });
  758. }, that);
  759. wx.showToast({
  760. title: '添加成功'
  761. })
  762. } else {
  763. wx.showToast({
  764. title: res.errmsg,
  765. icon: 'none'
  766. })
  767. }
  768. });
  769. },
  770. // 查询是否有活动
  771. enableActivity: function() {
  772. let that = this;
  773. let couponIds = wx.getStorageSync('couponIds');
  774. if (!couponIds) {
  775. couponIds = new Array();
  776. }
  777. // util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  778. // // if (res.errno === 0 && null != res.data.showCoupon) {
  779. // // if (couponIds.contains(res.data.showCoupon.id)) {
  780. // // return;
  781. // // }
  782. // // couponIds.push(res.data.showCoupon.id);
  783. // // wx.setStorageSync('couponIds', couponIds);
  784. // // that.setData({
  785. // // couponVo: res.data.showCoupon,
  786. // // showPop: true
  787. // // });
  788. // // } else
  789. // if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  790. // that.setData({
  791. // couponVo: res.data.takeCoupon,
  792. // showPop: true
  793. // });
  794. // }
  795. // });
  796. },
  797. // 商品扫码
  798. scanGoodsCode: function(e) {
  799. var that = this;
  800. var code;
  801. var value;
  802. var substrValue;
  803. var scanType;
  804. // 调起客户端扫码界面进行扫码
  805. wx.scanCode({
  806. // 是否只能从相机扫码
  807. onlyFromCamera: true,
  808. // 扫码类型, barCode:一维码, qrCode:二维码
  809. scanType: ['barCode', 'qrCode'],
  810. success: function(res) {
  811. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  812. that.value = res.result;
  813. that.scanType = res.scanType;
  814. that.setData({
  815. goodsCode: that.code
  816. });
  817. if (that.scanType == 'QR_CODE') { //二维码
  818. that.substrValue = that.value.substring(0, 5);
  819. that.value = that.value.substring(5, that.value.length);
  820. // var goodId = that.value.substring(18, that.value.length);
  821. var scanArray = that.value.split('&');
  822. // console.log(scanArray.length);
  823. if (scanArray.length < 2) {
  824. wx.showModal({
  825. title: '',
  826. content: '您所扫描的商品无效',
  827. showCancel: false
  828. });
  829. return;
  830. }
  831. var goodId = scanArray[0].substring(18, that.value.length);
  832. var storeId = scanArray[1].substring(8, that.value.length);
  833. // console.log("storeId:" + storeId);
  834. // console.log("goodId:" + goodId);
  835. if (that.substrValue != 'emato') { //../goods/goods?id=&merchSn=
  836. wx.showModal({
  837. title: '',
  838. content: '您所扫描的商品无效',
  839. showCancel: false,
  840. success: function(res) {
  841. if (res.confirm) {
  842. console.log('用户点击确定')
  843. } else if (res.cancel) {
  844. console.log('用户点击取消')
  845. }
  846. }
  847. });
  848. return;
  849. }
  850. if (storeId != wx.getStorageSync('storeId')) {
  851. wx.showModal({
  852. title: '扫描结果',
  853. content: '该商品不属于当前门店',
  854. showCancel: false
  855. });
  856. return;
  857. }
  858. util.request(api.GoodsDetail, {
  859. id: goodId,
  860. storeId: storeId,
  861. referrer: ''
  862. }).then(function(res) {
  863. if (res.errno === 0) {
  864. // console.log(res);
  865. // 跳转页面
  866. setTimeout(function() {
  867. wx.navigateTo({
  868. url: that.value,
  869. success: function(e) {
  870. console.log('跳转成功');
  871. },
  872. fail: function(e) {
  873. console.log('跳转失败');
  874. }
  875. })
  876. }, 350)
  877. } else {
  878. wx.showModal({
  879. title: '扫描结果',
  880. content: '商品不存在',
  881. showCancel: false
  882. });
  883. }
  884. });
  885. } else { //其他码
  886. //弹框显示结果
  887. wx.showModal({
  888. title: '扫描结果',
  889. content: that.value,
  890. showCancel: false
  891. });
  892. }
  893. },
  894. fail: function() {
  895. // 显示提示框
  896. wx.showToast({
  897. title: '扫码失败',
  898. icon: 'none',
  899. // 提示的延迟时间
  900. duration: 3000
  901. })
  902. }
  903. })
  904. },
  905. imgOnLoad: function(e) {
  906. let that = this;
  907. // console.log('图片加载完成');
  908. // var realthumb = e.target.dataset.thumb;
  909. // let list = that.data.list
  910. // for (var i = 0; i < list.length; i++) {
  911. // if (list[i].thumb == realthumb) {
  912. // list[i].loaded = true
  913. // }
  914. // that.setData({
  915. // list: list
  916. // })
  917. // }
  918. },
  919. switchAttrPop: function() {
  920. this.setData({
  921. openAttr: !this.data.openAttr
  922. })
  923. },
  924. hideSwitchAttrPop: function() {
  925. this.setData({
  926. openAttr: false
  927. })
  928. },
  929. //购物车增加
  930. addCart: function(e) {
  931. let that = this;
  932. that.setData({
  933. number: 1
  934. });
  935. var goodsId = e.currentTarget.dataset.goodsId;
  936. var retailPrice = e.currentTarget.dataset.retailPrice;
  937. util.request(api.GoodsSku, {
  938. goodsId: goodsId
  939. }).then(function(res) {
  940. if (res.errno === 0 && null != res.data) {
  941. that.setData({
  942. goodsVo: res.data.goodsVo,
  943. specificationList: res.data.specificationList,
  944. productList: res.data.productList,
  945. openAttr: !that.data.openAttr,
  946. retailPrice: retailPrice,
  947. stockNum: res.data.stockNum,
  948. cartNumber: res.data.cartNumber,
  949. checkedSpecText: res.data.specificationList[0].valueList[0].value
  950. });
  951. //
  952. let _specificationList = res.data.specificationList;
  953. for (let i = 0; i < _specificationList.length; i++) {
  954. if (_specificationList[i].valueList.length == 1) {
  955. //如果已经选中,则反选
  956. _specificationList[i].valueList[0].checked = true;
  957. }
  958. }
  959. that.setData({
  960. 'specificationList': _specificationList
  961. });
  962. }
  963. });
  964. },
  965. shows: function(e) {
  966. // wx.redirectTo({
  967. // url: '/pages/images/images'
  968. // })
  969. // wx.navigateTo({
  970. // url: '../ucenter/campMinus/campMinus?campMinusId=' + 9 + '&&storeId=' + 8 + '&&campName=' + '满200减50' + "&&isShare=1"
  971. // })
  972. wx.redirectTo({
  973. url: '/pages/checkVideo/checkVideo'
  974. })
  975. },
  976. onReachBottom() {
  977. var that = this;
  978. if (that.data.list.length > 0) {
  979. // console.log(that.data.list)
  980. wx.showLoading({
  981. title: '加载中...',
  982. })
  983. }
  984. that.setData({
  985. page: that.data.page + 1
  986. });
  987. that.getGoodsList();
  988. }
  989. })