1
0

index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. },
  21. showCouponPop() {
  22. let that = this;
  23. this.setData({
  24. showPop: false
  25. });
  26. // wx.showToast({
  27. // title: '恭喜获取优惠券一张' + that.data.couponVo.name,
  28. // duration: 2000
  29. // });
  30. wx.showModal({
  31. title: '获取优惠券一张',
  32. showCancel: false,
  33. content: that.data.couponVo.name
  34. })
  35. },
  36. onShareAppMessage: function () {
  37. return {
  38. title: '商业版',
  39. desc: '新人好礼送券',
  40. path: '/pages/index/index'
  41. }
  42. },
  43. getIndexData: function () {
  44. let that = this;
  45. util.request(api.IndexUrl).then(function (res) {
  46. if (res.errno === 0) {
  47. // console.log(res.data.banner);
  48. that.setData({
  49. // newGoods: res.data.newGoodsList,
  50. hotGoods: res.data.hotGoodsList,
  51. // topics: res.data.topicList,
  52. // brand: res.data.brandList,
  53. // floorGoods: res.data.categoryList,
  54. banner: res.data.banner,
  55. // groupBanner: res.data.groupBanner,
  56. channel: res.data.channel
  57. });
  58. }
  59. });
  60. },
  61. getGroupData: function () {
  62. let that = this;
  63. util.request(api.GroupList).then(function (res) {
  64. if (res.errno === 0) {
  65. that.setData({
  66. groupGoods: res.data.data,
  67. });
  68. }
  69. });
  70. },
  71. onLoad: function (options) {
  72. let that = this;
  73. wx.setStorageSync("navUrl", "/pages/index/index");
  74. if (options.scene) {
  75. console.log("has scene");
  76. var scene = decodeURIComponent(options.scene);
  77. console.log("scene is ", scene);
  78. that.setData({
  79. storeId: scene
  80. });
  81. wx.setStorageSync('storeId', scene);
  82. } else {
  83. console.log("no scene");
  84. }
  85. },
  86. onReady: function () {
  87. // 页面渲染完成
  88. },
  89. onShow: function () {
  90. // 页面显示
  91. let that = this;
  92. wx.setStorageSync("navUrl", "/pages/index/index");
  93. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  94. that.syncStore();
  95. } else {
  96. wx.navigateTo({
  97. url: '/pages/auth/btnAuth/btnAuth',
  98. })
  99. }
  100. },
  101. onHide: function () {
  102. // 页面隐藏
  103. },
  104. onUnload: function () {
  105. // 页面关闭
  106. },
  107. handleStore() {
  108. wx.navigateTo({
  109. url: '../map/map',
  110. })
  111. },
  112. goSearch() {
  113. wx.navigateTo({
  114. url: '../search/search',
  115. })
  116. },
  117. goCatalog: function (e) {
  118. let url = '';
  119. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  120. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  121. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  122. wx.switchTab({
  123. url: '/pages/catalog/catalog',
  124. });
  125. },
  126. onReachBottom: function () {
  127. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  128. return false;
  129. }
  130. this.setData({
  131. bottomLoading: true
  132. });
  133. // this.getFloorCategory();
  134. },
  135. reLoad: function () {
  136. let that = this;
  137. // console.log(wx.getStorageSync('userId'));
  138. // console.log(wx.getStorageSync('storeId'));
  139. // console.log(wx.getStorageSync('merchSn'));
  140. if (wx.getStorageSync('storeId')) {
  141. if (wx.getStorageSync('userId')){
  142. wx.request({
  143. url: api.updateLoginUser,
  144. data: {
  145. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  146. },
  147. method: 'POST',
  148. header: {
  149. 'Content-Type': 'application/json'
  150. },
  151. success: function (wxRes) {
  152. if (wxRes.data.errno === 0) {
  153. // console.log("用户信息更新成功");
  154. }
  155. },
  156. fail: function (err) {
  157. console.log("failed");
  158. }
  159. });
  160. }
  161. that.getIndexData();
  162. that.enableActivity();
  163. that.getGroupData();
  164. }
  165. },
  166. // 同步门店
  167. syncStore: function () {
  168. let that = this;
  169. // console.log(wx.getStorageSync('merchSn'));
  170. //获取附件门店信息
  171. util.getLocation((lng, lat) => {
  172. wx.setStorageSync('location', JSON.stringify({ lng, lat }));
  173. util.request(api.NearbyList, { longitude: lng, latitude: lat, storeId: that.data.storeId }).then((res) => {
  174. let nlist = res.data;
  175. // console.log(nlist);
  176. wx.removeStorageSync('nearStoreList');
  177. wx.removeStorageSync('storeId');
  178. wx.removeStorageSync('storeVo');
  179. // console.log('sasas:' + wx.getStorageSync('currentCategory'));
  180. if (!wx.getStorageSync('currentCategory')) {
  181. wx.removeStorageSync('currentCategory');
  182. }
  183. if (!nlist.length) {
  184. wx.removeStorageSync('nearStoreList');
  185. } else {
  186. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  187. }
  188. if (!wx.getStorageSync('storeId')) {
  189. if (!nlist.length) {
  190. wx.removeStorageSync('storeId');
  191. wx.removeStorageSync('storeVo');
  192. that.setData({
  193. storeName: '附近暂无门店'
  194. })
  195. } else {
  196. that.setData({
  197. storeName: nlist[0].storeName,
  198. storeId: nlist[0].id
  199. })
  200. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  201. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  202. }
  203. } else {
  204. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  205. that.chooseStore(storeVo.id, storeVo.merchSn);
  206. that.setData({
  207. storeName: storeVo.storeName,
  208. storeId: storeVo.id
  209. });
  210. }
  211. })
  212. });
  213. },
  214. // 更新门店Id
  215. chooseStore: function (storeId,merchSn) {
  216. let that = this;
  217. util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) {
  218. if (res.errno === 0) {
  219. wx.setStorageSync('storeId', storeId);
  220. wx.setStorageSync('merchSn', merchSn);
  221. that.reLoad();
  222. }
  223. });
  224. },
  225. //购物车减少
  226. cutNumber: function (e) {
  227. let that = this;
  228. var goodsId = e.currentTarget.dataset.goodsId;
  229. var productId = e.currentTarget.dataset.productId;
  230. var hotGoods = that.data.hotGoods;
  231. // hotGoods.forEach(function (val, index, arr) {
  232. // if (val.product_id == productId) {
  233. // val.cart_num = val.cart_num - 1;
  234. // if (val.cart_num >= 0) {
  235. // hotGoods[index] = val;
  236. // }
  237. // }
  238. // });
  239. // that.setData({ hotGoods: hotGoods });
  240. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  241. if (res.errno === 0 && null != res.data) {
  242. var hotGoods = that.data.hotGoods;
  243. hotGoods.forEach(function (val, index, arr) {
  244. if (val.product_id == productId) {
  245. val.cart_num = res.data;
  246. hotGoods[index] = val;
  247. that.setData({ hotGoods: hotGoods });
  248. }
  249. }, that);
  250. }
  251. });
  252. },
  253. //购物车增加
  254. addNumber: function (e) {
  255. let that = this;
  256. var goodsId = e.currentTarget.dataset.goodsId;
  257. var productId = e.currentTarget.dataset.productId;
  258. var hotGoods = that.data.hotGoods;
  259. // hotGoods.forEach(function (val, index, arr) {
  260. // if (val.product_id == productId) {
  261. // val.cart_num = val.cart_num + 1;
  262. // hotGoods[index] = val;
  263. // }
  264. // });
  265. // that.setData({ hotGoods: hotGoods });
  266. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  267. if (res.errno === 0 && null != res.data) {
  268. hotGoods.forEach(function (val, index, arr) {
  269. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  270. if (val.product_id == cartVal.product_id) {
  271. val.cart_num = cartVal.number;
  272. hotGoods[index] = val;
  273. }
  274. });
  275. that.setData({ hotGoods: hotGoods });
  276. }, that);
  277. } else {
  278. wx.showToast({
  279. title: res.errmsg,
  280. icon: 'none'
  281. })
  282. }
  283. });
  284. },
  285. // 查询是否有活动
  286. enableActivity: function () {
  287. let that = this;
  288. let couponIds = wx.getStorageSync('couponIds');
  289. if (!couponIds) {
  290. couponIds = new Array();
  291. }
  292. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  293. // if (res.errno === 0 && null != res.data.showCoupon) {
  294. // if (couponIds.contains(res.data.showCoupon.id)) {
  295. // return;
  296. // }
  297. // couponIds.push(res.data.showCoupon.id);
  298. // wx.setStorageSync('couponIds', couponIds);
  299. // that.setData({
  300. // couponVo: res.data.showCoupon,
  301. // showPop: true
  302. // });
  303. // } else
  304. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  305. that.setData({
  306. couponVo: res.data.takeCoupon,
  307. showPop: true
  308. });
  309. }
  310. });
  311. },
  312. // 商品扫码
  313. scanGoodsCode: function (e) {
  314. var that = this;
  315. var code;
  316. var value;
  317. var substrValue;
  318. var scanType;
  319. // 调起客户端扫码界面进行扫码
  320. wx.scanCode({
  321. // 是否只能从相机扫码
  322. onlyFromCamera: true,
  323. // 扫码类型, barCode:一维码, qrCode:二维码
  324. scanType: ['barCode', 'qrCode'],
  325. success: function (res) {
  326. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  327. that.value = res.result;
  328. that.scanType = res.scanType;
  329. that.setData({
  330. goodsCode: that.code
  331. });
  332. if (that.scanType == 'QR_CODE') {//二维码
  333. that.substrValue = that.value.substring(0, 5);
  334. that.value = that.value.substring(5, that.value.length);
  335. // var goodId = that.value.substring(18, that.value.length);
  336. var scanArray = that.value.split('&');
  337. // console.log(scanArray.length);
  338. if (scanArray.length < 2){
  339. wx.showModal({
  340. title: '',
  341. content: '您所扫描的商品无效',
  342. showCancel: false
  343. });
  344. return;
  345. }
  346. var goodId = scanArray[0].substring(18, that.value.length);
  347. var storeId = scanArray[1].substring(8, that.value.length);
  348. // console.log("storeId:" + storeId);
  349. // console.log("goodId:" + goodId);
  350. if (that.substrValue != 'emato') {//../goods/goods?id=&merchSn=
  351. wx.showModal({
  352. title: '',
  353. content: '您所扫描的商品无效',
  354. showCancel: false,
  355. success: function (res) {
  356. if (res.confirm) {
  357. console.log('用户点击确定')
  358. } else if (res.cancel) {
  359. console.log('用户点击取消')
  360. }
  361. }
  362. });
  363. return;
  364. }
  365. if (storeId != wx.getStorageSync('storeId')) {
  366. wx.showModal({
  367. title: '扫描结果',
  368. content: '该商品不属于当前门店',
  369. showCancel: false
  370. });
  371. return;
  372. }
  373. util.request(api.GoodsDetail, { id: goodId, referrer: '' }).then(function (res) {
  374. if (res.errno === 0) {
  375. console.log(res);
  376. // 跳转页面
  377. wx.navigateTo({
  378. url: that.value,
  379. success: function (e) {
  380. console.log('跳转成功');
  381. },
  382. fail: function (e) {
  383. console.log('跳转失败');
  384. }
  385. })
  386. } else {
  387. wx.showModal({
  388. title: '扫描结果',
  389. content: '商品不存在',
  390. showCancel: false
  391. });
  392. }
  393. });
  394. }else{//其他码
  395. //弹框显示结果
  396. wx.showModal({
  397. title: '扫描结果',
  398. content: that.value,
  399. showCancel: false
  400. });
  401. }
  402. },
  403. fail: function () {
  404. // 显示提示框
  405. wx.showToast({
  406. title: '扫码失败',
  407. icon: 'none',
  408. // 提示的延迟时间
  409. duration: 3000
  410. })
  411. }
  412. })
  413. },
  414. imgOnLoad: function (e) {
  415. let that = this;
  416. // console.log('图片加载完成');
  417. // var realthumb = e.target.dataset.thumb;
  418. // let list = that.data.list
  419. // for (var i = 0; i < list.length; i++) {
  420. // if (list[i].thumb == realthumb) {
  421. // list[i].loaded = true
  422. // }
  423. // that.setData({
  424. // list: list
  425. // })
  426. // }
  427. },
  428. shows: function (e) {
  429. wx.redirectTo({
  430. url: '/pages/images/images'
  431. })
  432. }
  433. })