1
0

index.js 11 KB

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