1
0

index.js 34 KB

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