1
0

index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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: 7 }).then((res) => {
  173. let nlist = res.data;
  174. // console.log(nlist);
  175. wx.removeStorageSync('nearStoreList');
  176. wx.removeStorageSync('storeId');
  177. wx.removeStorageSync('storeVo');
  178. wx.removeStorageSync('currentCategory');
  179. if (!nlist.length) {
  180. wx.removeStorageSync('nearStoreList');
  181. } else {
  182. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  183. }
  184. if (!wx.getStorageSync('storeId')) {
  185. if (!nlist.length) {
  186. wx.removeStorageSync('storeId');
  187. wx.removeStorageSync('storeVo');
  188. that.setData({
  189. storeName: '附近暂无门店'
  190. })
  191. } else {
  192. that.setData({
  193. storeName: nlist[0].storeName,
  194. storeId: nlist[0].id
  195. })
  196. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  197. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  198. }
  199. } else {
  200. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  201. that.chooseStore(storeVo.id, storeVo.merchSn);
  202. that.setData({
  203. storeName: storeVo.storeName,
  204. storeId: storeVo.id
  205. });
  206. }
  207. })
  208. });
  209. },
  210. // 更新门店Id
  211. chooseStore: function (storeId,merchSn) {
  212. let that = this;
  213. util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) {
  214. if (res.errno === 0) {
  215. wx.setStorageSync('storeId', storeId);
  216. wx.setStorageSync('merchSn', merchSn);
  217. that.reLoad();
  218. }
  219. });
  220. },
  221. //购物车减少
  222. cutNumber: function (e) {
  223. let that = this;
  224. var goodsId = e.currentTarget.dataset.goodsId;
  225. var productId = e.currentTarget.dataset.productId;
  226. var hotGoods = that.data.hotGoods;
  227. // hotGoods.forEach(function (val, index, arr) {
  228. // if (val.product_id == productId) {
  229. // val.cart_num = val.cart_num - 1;
  230. // if (val.cart_num >= 0) {
  231. // hotGoods[index] = val;
  232. // }
  233. // }
  234. // });
  235. // that.setData({ hotGoods: hotGoods });
  236. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  237. if (res.errno === 0 && null != res.data) {
  238. var hotGoods = that.data.hotGoods;
  239. hotGoods.forEach(function (val, index, arr) {
  240. if (val.product_id == productId) {
  241. val.cart_num = res.data;
  242. hotGoods[index] = val;
  243. that.setData({ hotGoods: hotGoods });
  244. }
  245. }, that);
  246. }
  247. });
  248. },
  249. //购物车增加
  250. addNumber: function (e) {
  251. let that = this;
  252. var goodsId = e.currentTarget.dataset.goodsId;
  253. var productId = e.currentTarget.dataset.productId;
  254. var hotGoods = that.data.hotGoods;
  255. // hotGoods.forEach(function (val, index, arr) {
  256. // if (val.product_id == productId) {
  257. // val.cart_num = val.cart_num + 1;
  258. // hotGoods[index] = val;
  259. // }
  260. // });
  261. // that.setData({ hotGoods: hotGoods });
  262. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  263. if (res.errno === 0 && null != res.data) {
  264. hotGoods.forEach(function (val, index, arr) {
  265. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  266. if (val.product_id == cartVal.product_id) {
  267. val.cart_num = cartVal.number;
  268. hotGoods[index] = val;
  269. }
  270. });
  271. that.setData({ hotGoods: hotGoods });
  272. }, that);
  273. } else {
  274. wx.showToast({
  275. title: res.errmsg,
  276. icon: 'none'
  277. })
  278. }
  279. });
  280. },
  281. // 查询是否有活动
  282. enableActivity: function () {
  283. let that = this;
  284. let couponIds = wx.getStorageSync('couponIds');
  285. if (!couponIds) {
  286. couponIds = new Array();
  287. }
  288. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  289. // if (res.errno === 0 && null != res.data.showCoupon) {
  290. // if (couponIds.contains(res.data.showCoupon.id)) {
  291. // return;
  292. // }
  293. // couponIds.push(res.data.showCoupon.id);
  294. // wx.setStorageSync('couponIds', couponIds);
  295. // that.setData({
  296. // couponVo: res.data.showCoupon,
  297. // showPop: true
  298. // });
  299. // } else
  300. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  301. that.setData({
  302. couponVo: res.data.takeCoupon,
  303. showPop: true
  304. });
  305. }
  306. });
  307. },
  308. // 商品扫码
  309. scanGoodsCode: function (e) {
  310. var that = this;
  311. var code;
  312. var value;
  313. var substrValue;
  314. var scanType;
  315. // 调起客户端扫码界面进行扫码
  316. wx.scanCode({
  317. // 是否只能从相机扫码
  318. onlyFromCamera: true,
  319. // 扫码类型, barCode:一维码, qrCode:二维码
  320. scanType: ['barCode', 'qrCode'],
  321. success: function (res) {
  322. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  323. that.value = res.result;
  324. that.scanType = res.scanType;
  325. that.setData({
  326. goodsCode: that.code
  327. });
  328. if (that.scanType == 'QR_CODE') {//二维码
  329. that.substrValue = that.value.substring(0, 5);
  330. that.value = that.value.substring(5, that.value.length);
  331. // var goodId = that.value.substring(18, that.value.length);
  332. var scanArray = that.value.split('&');
  333. // console.log(scanArray.length);
  334. if (scanArray.length < 2){
  335. wx.showModal({
  336. title: '',
  337. content: '您所扫描的商品无效',
  338. showCancel: false
  339. });
  340. return;
  341. }
  342. var goodId = scanArray[0].substring(18, that.value.length);
  343. var storeId = scanArray[1].substring(8, that.value.length);
  344. // console.log("storeId:" + storeId);
  345. // console.log("goodId:" + goodId);
  346. if (that.substrValue != 'emato') {//../goods/goods?id=&merchSn=
  347. wx.showModal({
  348. title: '',
  349. content: '您所扫描的商品无效',
  350. showCancel: false,
  351. success: function (res) {
  352. if (res.confirm) {
  353. console.log('用户点击确定')
  354. } else if (res.cancel) {
  355. console.log('用户点击取消')
  356. }
  357. }
  358. });
  359. return;
  360. }
  361. if (storeId != wx.getStorageSync('storeId')) {
  362. wx.showModal({
  363. title: '扫描结果',
  364. content: '该商品不属于当前门店',
  365. showCancel: false
  366. });
  367. return;
  368. }
  369. util.request(api.GoodsDetail, { id: goodId, referrer: '' }).then(function (res) {
  370. if (res.errno === 0) {
  371. console.log(res);
  372. // 跳转页面
  373. wx.navigateTo({
  374. url: that.value,
  375. success: function (e) {
  376. console.log('跳转成功');
  377. },
  378. fail: function (e) {
  379. console.log('跳转失败');
  380. }
  381. })
  382. } else {
  383. wx.showModal({
  384. title: '扫描结果',
  385. content: '商品不存在',
  386. showCancel: false
  387. });
  388. }
  389. });
  390. }else{//其他码
  391. //弹框显示结果
  392. wx.showModal({
  393. title: '扫描结果',
  394. content: that.value,
  395. showCancel: false
  396. });
  397. }
  398. },
  399. fail: function () {
  400. // 显示提示框
  401. wx.showToast({
  402. title: '扫码失败',
  403. icon: 'none',
  404. // 提示的延迟时间
  405. duration: 3000
  406. })
  407. }
  408. })
  409. }
  410. })