1
0

index.js 13 KB

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