1
0

index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. // console.log(wx.getStorageSync('userId'));
  128. console.log(wx.getStorageSync('storeId'));
  129. // console.log(wx.getStorageSync('merchSn'));
  130. if (wx.getStorageSync('userId')){
  131. wx.request({
  132. url: api.updateLoginUser,
  133. data: {
  134. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  135. },
  136. method: 'POST',
  137. header: {
  138. 'Content-Type': 'application/json'
  139. },
  140. success: function (wxRes) {
  141. if (wxRes.data.errno === 0) {
  142. // console.log("用户信息更新成功");
  143. }
  144. },
  145. fail: function (err) {
  146. console.log("failed");
  147. }
  148. });
  149. }
  150. that.getIndexData();
  151. that.enableActivity();
  152. that.getGroupData();
  153. }
  154. },
  155. // 同步门店
  156. syncStore: function () {
  157. let that = this;
  158. console.log(wx.getStorageSync('merchSn'));
  159. //获取附件门店信息
  160. util.getLocation((lng, lat) => {
  161. wx.setStorageSync('location', JSON.stringify({ lng, lat }));
  162. util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
  163. let nlist = res.data;
  164. // console.log(nlist);
  165. if (!nlist.length) {
  166. wx.removeStorageSync('nearStoreList');
  167. } else {
  168. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  169. }
  170. if (!wx.getStorageSync('storeId')) {
  171. if (!nlist.length) {
  172. wx.removeStorageSync('storeId');
  173. wx.removeStorageSync('storeVo');
  174. that.setData({
  175. storeName: '附近暂无门店'
  176. })
  177. } else {
  178. that.setData({
  179. storeName: nlist[0].storeName,
  180. storeId: nlist[0].id
  181. })
  182. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  183. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  184. }
  185. } else {
  186. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  187. that.chooseStore(storeVo.id, storeVo.merchSn);
  188. that.setData({
  189. storeName: storeVo.storeName,
  190. storeId: storeVo.id
  191. });
  192. }
  193. })
  194. });
  195. },
  196. // 更新门店Id
  197. chooseStore: function (storeId,merchSn) {
  198. let that = this;
  199. util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) {
  200. if (res.errno === 0) {
  201. wx.setStorageSync('storeId', storeId);
  202. wx.setStorageSync('merchSn', merchSn);
  203. that.reLoad();
  204. }
  205. });
  206. },
  207. //购物车减少
  208. cutNumber: function (e) {
  209. let that = this;
  210. var goodsId = e.currentTarget.dataset.goodsId;
  211. var productId = e.currentTarget.dataset.productId;
  212. var hotGoods = that.data.hotGoods;
  213. // hotGoods.forEach(function (val, index, arr) {
  214. // if (val.product_id == productId) {
  215. // val.cart_num = val.cart_num - 1;
  216. // if (val.cart_num >= 0) {
  217. // hotGoods[index] = val;
  218. // }
  219. // }
  220. // });
  221. // that.setData({ hotGoods: hotGoods });
  222. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  223. if (res.errno === 0 && null != res.data) {
  224. var hotGoods = that.data.hotGoods;
  225. hotGoods.forEach(function (val, index, arr) {
  226. if (val.product_id == productId) {
  227. val.cart_num = res.data;
  228. hotGoods[index] = val;
  229. that.setData({ hotGoods: hotGoods });
  230. }
  231. }, that);
  232. }
  233. });
  234. },
  235. //购物车增加
  236. addNumber: function (e) {
  237. let that = this;
  238. var goodsId = e.currentTarget.dataset.goodsId;
  239. var productId = e.currentTarget.dataset.productId;
  240. var hotGoods = that.data.hotGoods;
  241. // hotGoods.forEach(function (val, index, arr) {
  242. // if (val.product_id == productId) {
  243. // val.cart_num = val.cart_num + 1;
  244. // hotGoods[index] = val;
  245. // }
  246. // });
  247. // that.setData({ hotGoods: hotGoods });
  248. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  249. if (res.errno === 0 && null != res.data) {
  250. hotGoods.forEach(function (val, index, arr) {
  251. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  252. if (val.product_id == cartVal.product_id) {
  253. val.cart_num = cartVal.number;
  254. hotGoods[index] = val;
  255. }
  256. });
  257. that.setData({ hotGoods: hotGoods });
  258. }, that);
  259. } else {
  260. wx.showToast({
  261. title: res.errmsg,
  262. icon: 'none'
  263. })
  264. }
  265. });
  266. },
  267. // 查询是否有活动
  268. enableActivity: function () {
  269. let that = this;
  270. let couponIds = wx.getStorageSync('couponIds');
  271. if (!couponIds) {
  272. couponIds = new Array();
  273. }
  274. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  275. // if (res.errno === 0 && null != res.data.showCoupon) {
  276. // if (couponIds.contains(res.data.showCoupon.id)) {
  277. // return;
  278. // }
  279. // couponIds.push(res.data.showCoupon.id);
  280. // wx.setStorageSync('couponIds', couponIds);
  281. // that.setData({
  282. // couponVo: res.data.showCoupon,
  283. // showPop: true
  284. // });
  285. // } else
  286. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  287. that.setData({
  288. couponVo: res.data.takeCoupon,
  289. showPop: true
  290. });
  291. }
  292. });
  293. },
  294. // 商品扫码
  295. scanGoodsCode: function (e) {
  296. var that = this;
  297. var code;
  298. var value;
  299. var substrValue;
  300. var scanType;
  301. // 调起客户端扫码界面进行扫码
  302. wx.scanCode({
  303. // 是否只能从相机扫码
  304. onlyFromCamera: true,
  305. // 扫码类型, barCode:一维码, qrCode:二维码
  306. scanType: ['barCode', 'qrCode'],
  307. success: function (res) {
  308. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  309. that.value = res.result;
  310. that.scanType = res.scanType;
  311. that.setData({
  312. goodsCode: that.code
  313. });
  314. if (that.scanType == 'QR_CODE') {//二维码
  315. that.substrValue = that.value.substring(0, 5);
  316. that.value = that.value.substring(5, that.value.length);
  317. var goodId = that.value.substring(18, that.value.length);
  318. if (that.substrValue != 'emato') {//../goods/goods?id=
  319. wx.showModal({
  320. title: '',
  321. content: '您所扫描的商品无效',
  322. showCancel: false,
  323. success: function (res) {
  324. if (res.confirm) {
  325. console.log('用户点击确定')
  326. } else if (res.cancel) {
  327. console.log('用户点击取消')
  328. }
  329. }
  330. });
  331. } else {
  332. util.request(api.GoodsDetail, { id: goodId, referrer: '' }).then(function (res) {
  333. if (res.errno === 0) {
  334. // 跳转页面
  335. wx.navigateTo({
  336. url: that.value,
  337. success: function (e) {
  338. console.log('跳转成功');
  339. },
  340. fail: function (e) {
  341. console.log('跳转失败');
  342. }
  343. })
  344. } else {
  345. wx.showModal({
  346. title: '扫描结果',
  347. content: '商品不存在',
  348. showCancel: false
  349. });
  350. }
  351. });
  352. }
  353. }else{//其他码
  354. //弹框显示结果
  355. wx.showModal({
  356. title: '扫描结果',
  357. content: that.value,
  358. showCancel: false
  359. });
  360. }
  361. },
  362. fail: function () {
  363. // 显示提示框
  364. wx.showToast({
  365. title: '扫码失败',
  366. icon: 'none',
  367. // 提示的延迟时间
  368. duration: 3000
  369. })
  370. }
  371. })
  372. }
  373. })