1
0

index.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. },
  75. onReady: function () {
  76. // 页面渲染完成
  77. },
  78. onShow: function () {
  79. // 页面显示
  80. let that = this;
  81. wx.setStorageSync("navUrl", "/pages/index/index");
  82. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  83. that.syncStore();
  84. } else {
  85. wx.navigateTo({
  86. url: '/pages/auth/btnAuth/btnAuth',
  87. })
  88. }
  89. },
  90. onHide: function () {
  91. // 页面隐藏
  92. },
  93. onUnload: function () {
  94. // 页面关闭
  95. },
  96. handleStore() {
  97. wx.navigateTo({
  98. url: '../map/map',
  99. })
  100. },
  101. goSearch() {
  102. wx.navigateTo({
  103. url: '../search/search',
  104. })
  105. },
  106. goCatalog: function (e) {
  107. let url = '';
  108. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  109. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  110. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  111. wx.switchTab({
  112. url: '/pages/catalog/catalog',
  113. });
  114. },
  115. onReachBottom: function () {
  116. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  117. return false;
  118. }
  119. this.setData({
  120. bottomLoading: true
  121. });
  122. // this.getFloorCategory();
  123. },
  124. reLoad: function () {
  125. let that = this;
  126. if (wx.getStorageSync('storeId')) {
  127. that.getIndexData();
  128. that.enableActivity();
  129. that.getGroupData();
  130. }
  131. },
  132. // 同步门店
  133. syncStore: function () {
  134. let that = this;
  135. if (!wx.getStorageSync('storeId')) {
  136. util.getLocation((lng, lat) => {
  137. wx.setStorageSync('location', JSON.stringify({ lng, lat }));
  138. util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
  139. let nlist = res.data;
  140. if (!nlist.length) {
  141. wx.removeStorageSync('nearStoreList');
  142. wx.removeStorageSync('storeId');
  143. wx.removeStorageSync('storeVo');
  144. that.setData({
  145. storeName: '附近暂无门店'
  146. })
  147. } else {
  148. that.setData({
  149. storeName: nlist[0].storeName,
  150. storeId: nlist[0].id
  151. })
  152. that.chooseStore(nlist[0].id);
  153. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  154. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  155. }
  156. })
  157. });
  158. } else {
  159. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  160. that.chooseStore(storeVo.id);
  161. that.setData({
  162. storeName: storeVo.storeName,
  163. storeId: storeVo.id
  164. });
  165. }
  166. },
  167. // 更新门店Id
  168. chooseStore: function (storeId) {
  169. let that = this;
  170. util.request(api.ChooseStoreId, { storeId: storeId }, 'POST').then(function (res) {
  171. if (res.errno === 0) {
  172. wx.setStorageSync('storeId', storeId);
  173. that.reLoad();
  174. }
  175. });
  176. },
  177. //购物车减少
  178. cutNumber: function (e) {
  179. let that = this;
  180. var goodsId = e.currentTarget.dataset.goodsId;
  181. var productId = e.currentTarget.dataset.productId;
  182. var hotGoods = that.data.hotGoods;
  183. // hotGoods.forEach(function (val, index, arr) {
  184. // if (val.product_id == productId) {
  185. // val.cart_num = val.cart_num - 1;
  186. // if (val.cart_num >= 0) {
  187. // hotGoods[index] = val;
  188. // }
  189. // }
  190. // });
  191. // that.setData({ hotGoods: hotGoods });
  192. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  193. if (res.errno === 0 && null != res.data) {
  194. var hotGoods = that.data.hotGoods;
  195. hotGoods.forEach(function (val, index, arr) {
  196. if (val.product_id == productId) {
  197. val.cart_num = res.data;
  198. hotGoods[index] = val;
  199. that.setData({ hotGoods: hotGoods });
  200. }
  201. }, that);
  202. }
  203. });
  204. },
  205. //购物车增加
  206. addNumber: function (e) {
  207. let that = this;
  208. var goodsId = e.currentTarget.dataset.goodsId;
  209. var productId = e.currentTarget.dataset.productId;
  210. var hotGoods = that.data.hotGoods;
  211. // hotGoods.forEach(function (val, index, arr) {
  212. // if (val.product_id == productId) {
  213. // val.cart_num = val.cart_num + 1;
  214. // hotGoods[index] = val;
  215. // }
  216. // });
  217. // that.setData({ hotGoods: hotGoods });
  218. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  219. if (res.errno === 0 && null != res.data) {
  220. hotGoods.forEach(function (val, index, arr) {
  221. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  222. if (val.product_id == cartVal.product_id) {
  223. val.cart_num = cartVal.number;
  224. hotGoods[index] = val;
  225. }
  226. });
  227. that.setData({ hotGoods: hotGoods });
  228. }, that);
  229. } else {
  230. wx.showToast({
  231. title: res.errmsg,
  232. icon: 'none'
  233. })
  234. }
  235. });
  236. },
  237. // 查询是否有活动
  238. enableActivity: function () {
  239. let that = this;
  240. let couponIds = wx.getStorageSync('couponIds');
  241. if (!couponIds) {
  242. couponIds = new Array();
  243. }
  244. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  245. // if (res.errno === 0 && null != res.data.showCoupon) {
  246. // if (couponIds.contains(res.data.showCoupon.id)) {
  247. // return;
  248. // }
  249. // couponIds.push(res.data.showCoupon.id);
  250. // wx.setStorageSync('couponIds', couponIds);
  251. // that.setData({
  252. // couponVo: res.data.showCoupon,
  253. // showPop: true
  254. // });
  255. // } else
  256. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  257. that.setData({
  258. couponVo: res.data.takeCoupon,
  259. showPop: true
  260. });
  261. }
  262. });
  263. },
  264. // 商品扫码
  265. scanGoodsCode: function (e) {
  266. var that = this;
  267. var code;
  268. var value;
  269. var substrValue;
  270. var scanType;
  271. // 调起客户端扫码界面进行扫码
  272. wx.scanCode({
  273. // 是否只能从相机扫码
  274. onlyFromCamera: true,
  275. // 扫码类型, barCode:一维码, qrCode:二维码
  276. scanType: ['barCode', 'qrCode'],
  277. success: function (res) {
  278. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  279. that.value = res.result;
  280. that.scanType = res.scanType;
  281. that.setData({
  282. goodsCode: that.code
  283. });
  284. if (that.scanType == 'QR_CODE') {//二维码
  285. that.substrValue = that.value.substring(0, 5);
  286. that.value = that.value.substring(5, that.value.length);
  287. var goodId = that.value.substring(18, that.value.length);
  288. if (that.substrValue != 'emato') {//../goods/goods?id=
  289. wx.showModal({
  290. title: '',
  291. content: '您所扫描的商品无效',
  292. showCancel: false,
  293. success: function (res) {
  294. if (res.confirm) {
  295. console.log('用户点击确定')
  296. } else if (res.cancel) {
  297. console.log('用户点击取消')
  298. }
  299. }
  300. });
  301. } else {
  302. util.request(api.GoodsDetail, { id: goodId, referrer: '' }).then(function (res) {
  303. if (res.errno === 0) {
  304. // 跳转页面
  305. wx.navigateTo({
  306. url: that.value,
  307. success: function (e) {
  308. console.log('跳转成功');
  309. },
  310. fail: function (e) {
  311. console.log('跳转失败');
  312. }
  313. })
  314. } else {
  315. wx.showModal({
  316. title: '扫描结果',
  317. content: '商品不存在',
  318. showCancel: false
  319. });
  320. }
  321. });
  322. }
  323. }else{//其他码
  324. //弹框显示结果
  325. wx.showModal({
  326. title: '扫描结果',
  327. content: that.value,
  328. showCancel: false
  329. });
  330. }
  331. },
  332. fail: function () {
  333. // 显示提示框
  334. wx.showToast({
  335. title: '扫码失败',
  336. icon: 'none',
  337. // 提示的延迟时间
  338. duration: 3000
  339. })
  340. }
  341. })
  342. }
  343. })