1
0

index.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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).then(function(res) {
  572. if (res.errno === 0) {
  573. // console.log(res.data.banner);
  574. that.setData({
  575. // newGoods: res.data.newGoodsList,
  576. // hotGoods: res.data.hotGoodsList,
  577. // topics: res.data.topicList,
  578. // brand: res.data.brandList,
  579. // floorGoods: res.data.categoryList,
  580. banner: res.data.banner,
  581. // groupBanner: res.data.groupBanner,
  582. channel: res.data.channel
  583. });
  584. //当切换了门店地图(mapIndexStoreId);或由用户分享进入的商品详情页,再切换进入的首页(isShareGoodEntry:true)
  585. if (wx.getStorageSync('storeId') != wx.getStorageSync('mapIndexStoreId')
  586. || wx.getStorageSync('isShareGoodEntry') == 'true'
  587. || that.data.hotGoods.length == 0
  588. || wx.getStorageSync('isLoadGoods') == '1') {
  589. that.setData({
  590. hotGoods: [],
  591. page: 1
  592. });
  593. that.getGoodsList();
  594. if (wx.getStorageSync('isLoadGoods') == '1') {
  595. util.request(api.UpdateStoreLoadGoods, {
  596. storeId: wx.getStorageSync('storeId')
  597. }).then(function(res) {});
  598. }
  599. wx.setStorageSync('isShareGoodEntry', 'false');
  600. }
  601. }
  602. wx.hideLoading();
  603. });
  604. },
  605. getGoodsList: function() {
  606. let that = this;
  607. util.request(api.GoodsHot).then(function(res) {
  608. if (res.errno == 0) {
  609. util.request(api.HotGoodsList, {
  610. isHot: 1,
  611. page: that.data.page,
  612. size: that.data.size,
  613. categoryId: 0
  614. })
  615. .then(function(res) {
  616. if (res.errno == 0) {
  617. let goodsList = that.data.hotGoods.concat(res.data.goodsList);
  618. that.setData({
  619. hotGoods: goodsList,
  620. list: res.data.goodsList
  621. });
  622. }
  623. });
  624. wx.hideLoading();
  625. }
  626. });
  627. },
  628. getGroupData: function() {
  629. let that = this;
  630. util.request(api.GroupList).then(function(res) {
  631. if (res.errno === 0) {
  632. that.setData({
  633. groupGoods: res.data.data,
  634. });
  635. }
  636. });
  637. },
  638. handleStore() {
  639. console.log('进入 handleStore()');
  640. wx.navigateTo({
  641. url: '../map/map',
  642. })
  643. },
  644. goSearch() {
  645. wx.navigateTo({
  646. url: '../search/search',
  647. })
  648. },
  649. goCatalog: function(e) {
  650. let url = '';
  651. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  652. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  653. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  654. wx.setStorageSync('isSwitchCatalog', 'true'); //主页跳转至分类页加载商品,
  655. wx.switchTab({
  656. url: '/pages/catalog/catalog',
  657. });
  658. },
  659. onReachBottom: function() {
  660. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  661. return false;
  662. }
  663. this.setData({
  664. bottomLoading: true
  665. });
  666. // this.getFloorCategory();
  667. },
  668. //购物车减少
  669. cutNumber: function(e) {
  670. let that = this;
  671. var goodsId = e.currentTarget.dataset.goodsId;
  672. var productId = e.currentTarget.dataset.productId;
  673. var hotGoods = that.data.hotGoods;
  674. // hotGoods.forEach(function (val, index, arr) {
  675. // if (val.product_id == productId) {
  676. // val.cart_num = val.cart_num - 1;
  677. // if (val.cart_num >= 0) {
  678. // hotGoods[index] = val;
  679. // }
  680. // }
  681. // });
  682. // that.setData({ hotGoods: hotGoods });
  683. util.request(api.CartMinus, {
  684. goodsId: goodsId,
  685. productId: productId,
  686. number: 1
  687. }, 'POST').then(function(res) {
  688. if (res.errno === 0 && null != res.data) {
  689. var hotGoods = that.data.hotGoods;
  690. hotGoods.forEach(function(val, index, arr) {
  691. if (val.product_id == productId) {
  692. val.cart_num = res.data;
  693. hotGoods[index] = val;
  694. that.setData({
  695. hotGoods: hotGoods
  696. });
  697. }
  698. }, that);
  699. }
  700. });
  701. },
  702. //购物车增加
  703. addNumber: function(e) {
  704. let that = this;
  705. var goodsId = e.currentTarget.dataset.goodsId;
  706. var productId = e.currentTarget.dataset.productId;
  707. var hotGoods = that.data.hotGoods;
  708. // hotGoods.forEach(function (val, index, arr) {
  709. // if (val.product_id == productId) {
  710. // val.cart_num = val.cart_num + 1;
  711. // hotGoods[index] = val;
  712. // }
  713. // });
  714. // that.setData({ hotGoods: hotGoods });
  715. util.request(api.CartAdd, {
  716. goodsId: goodsId,
  717. productId: productId,
  718. number: 1
  719. }, 'POST').then(function(res) {
  720. if (res.errno === 0 && null != res.data) {
  721. hotGoods.forEach(function(val, index, arr) {
  722. res.data.cartList.forEach(function(cartVal, cartIndex, cartArr) {
  723. if (val.product_id == cartVal.product_id) {
  724. val.cart_num = cartVal.number;
  725. hotGoods[index] = val;
  726. }
  727. });
  728. that.setData({
  729. hotGoods: hotGoods
  730. });
  731. }, that);
  732. wx.showToast({
  733. title: '添加成功'
  734. })
  735. } else {
  736. wx.showToast({
  737. title: res.errmsg,
  738. icon: 'none'
  739. })
  740. }
  741. });
  742. },
  743. // 查询是否有活动
  744. enableActivity: function() {
  745. let that = this;
  746. let couponIds = wx.getStorageSync('couponIds');
  747. if (!couponIds) {
  748. couponIds = new Array();
  749. }
  750. // util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  751. // // if (res.errno === 0 && null != res.data.showCoupon) {
  752. // // if (couponIds.contains(res.data.showCoupon.id)) {
  753. // // return;
  754. // // }
  755. // // couponIds.push(res.data.showCoupon.id);
  756. // // wx.setStorageSync('couponIds', couponIds);
  757. // // that.setData({
  758. // // couponVo: res.data.showCoupon,
  759. // // showPop: true
  760. // // });
  761. // // } else
  762. // if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  763. // that.setData({
  764. // couponVo: res.data.takeCoupon,
  765. // showPop: true
  766. // });
  767. // }
  768. // });
  769. },
  770. // 商品扫码
  771. scanGoodsCode: function(e) {
  772. var that = this;
  773. var code;
  774. var value;
  775. var substrValue;
  776. var scanType;
  777. // 调起客户端扫码界面进行扫码
  778. wx.scanCode({
  779. // 是否只能从相机扫码
  780. onlyFromCamera: true,
  781. // 扫码类型, barCode:一维码, qrCode:二维码
  782. scanType: ['barCode', 'qrCode'],
  783. success: function(res) {
  784. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  785. that.value = res.result;
  786. that.scanType = res.scanType;
  787. that.setData({
  788. goodsCode: that.code
  789. });
  790. if (that.scanType == 'QR_CODE') { //二维码
  791. that.substrValue = that.value.substring(0, 5);
  792. that.value = that.value.substring(5, that.value.length);
  793. // var goodId = that.value.substring(18, that.value.length);
  794. var scanArray = that.value.split('&');
  795. // console.log(scanArray.length);
  796. if (scanArray.length < 2) {
  797. wx.showModal({
  798. title: '',
  799. content: '您所扫描的商品无效',
  800. showCancel: false
  801. });
  802. return;
  803. }
  804. var goodId = scanArray[0].substring(18, that.value.length);
  805. var storeId = scanArray[1].substring(8, that.value.length);
  806. // console.log("storeId:" + storeId);
  807. // console.log("goodId:" + goodId);
  808. if (that.substrValue != 'emato') { //../goods/goods?id=&merchSn=
  809. wx.showModal({
  810. title: '',
  811. content: '您所扫描的商品无效',
  812. showCancel: false,
  813. success: function(res) {
  814. if (res.confirm) {
  815. console.log('用户点击确定')
  816. } else if (res.cancel) {
  817. console.log('用户点击取消')
  818. }
  819. }
  820. });
  821. return;
  822. }
  823. if (storeId != wx.getStorageSync('storeId')) {
  824. wx.showModal({
  825. title: '扫描结果',
  826. content: '该商品不属于当前门店',
  827. showCancel: false
  828. });
  829. return;
  830. }
  831. util.request(api.GoodsDetail, {
  832. id: goodId,
  833. storeId: storeId,
  834. referrer: ''
  835. }).then(function(res) {
  836. if (res.errno === 0) {
  837. // console.log(res);
  838. // 跳转页面
  839. setTimeout(function() {
  840. wx.navigateTo({
  841. url: that.value,
  842. success: function(e) {
  843. console.log('跳转成功');
  844. },
  845. fail: function(e) {
  846. console.log('跳转失败');
  847. }
  848. })
  849. }, 350)
  850. } else {
  851. wx.showModal({
  852. title: '扫描结果',
  853. content: '商品不存在',
  854. showCancel: false
  855. });
  856. }
  857. });
  858. } else { //其他码
  859. //弹框显示结果
  860. wx.showModal({
  861. title: '扫描结果',
  862. content: that.value,
  863. showCancel: false
  864. });
  865. }
  866. },
  867. fail: function() {
  868. // 显示提示框
  869. wx.showToast({
  870. title: '扫码失败',
  871. icon: 'none',
  872. // 提示的延迟时间
  873. duration: 3000
  874. })
  875. }
  876. })
  877. },
  878. imgOnLoad: function(e) {
  879. let that = this;
  880. // console.log('图片加载完成');
  881. // var realthumb = e.target.dataset.thumb;
  882. // let list = that.data.list
  883. // for (var i = 0; i < list.length; i++) {
  884. // if (list[i].thumb == realthumb) {
  885. // list[i].loaded = true
  886. // }
  887. // that.setData({
  888. // list: list
  889. // })
  890. // }
  891. },
  892. switchAttrPop: function() {
  893. this.setData({
  894. openAttr: !this.data.openAttr
  895. })
  896. },
  897. hideSwitchAttrPop: function() {
  898. this.setData({
  899. openAttr: false
  900. })
  901. },
  902. //购物车增加
  903. addCart: function(e) {
  904. let that = this;
  905. that.setData({
  906. number: 1
  907. });
  908. var goodsId = e.currentTarget.dataset.goodsId;
  909. var retailPrice = e.currentTarget.dataset.retailPrice;
  910. util.request(api.GoodsSku, {
  911. goodsId: goodsId
  912. }).then(function(res) {
  913. if (res.errno === 0 && null != res.data) {
  914. that.setData({
  915. goodsVo: res.data.goodsVo,
  916. specificationList: res.data.specificationList,
  917. productList: res.data.productList,
  918. openAttr: !that.data.openAttr,
  919. retailPrice: retailPrice,
  920. stockNum: res.data.stockNum,
  921. cartNumber: res.data.cartNumber,
  922. checkedSpecText: res.data.specificationList[0].valueList[0].value
  923. });
  924. //
  925. let _specificationList = res.data.specificationList;
  926. for (let i = 0; i < _specificationList.length; i++) {
  927. if (_specificationList[i].valueList.length == 1) {
  928. //如果已经选中,则反选
  929. _specificationList[i].valueList[0].checked = true;
  930. }
  931. }
  932. that.setData({
  933. 'specificationList': _specificationList
  934. });
  935. }
  936. });
  937. },
  938. shows: function(e) {
  939. // wx.redirectTo({
  940. // url: '/pages/images/images'
  941. // })
  942. // wx.navigateTo({
  943. // url: '../ucenter/campMinus/campMinus?campMinusId=' + 9 + '&&storeId=' + 8 + '&&campName=' + '满200减50' + "&&isShare=1"
  944. // })
  945. wx.redirectTo({
  946. url: '/pages/checkVideo/checkVideo'
  947. })
  948. },
  949. onReachBottom() {
  950. var that = this;
  951. if (that.data.list.length > 0) {
  952. // console.log(that.data.list)
  953. wx.showLoading({
  954. title: '加载中...',
  955. })
  956. }
  957. that.setData({
  958. page: that.data.page + 1
  959. });
  960. that.getGoodsList();
  961. }
  962. })