index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. const WXAPI = require('apifm-wxapi')
  2. const TOOLS = require('../../utils/tools.js')
  3. const AUTH = require('../../utils/auth')
  4. const APP = getApp()
  5. // fixed首次打开不显示标题的bug
  6. APP.configLoadOK = () => {
  7. wx.setNavigationBarTitle({
  8. title: wx.getStorageSync('mallName')
  9. })
  10. }
  11. Page({
  12. data: {
  13. inputVal: "", // 搜索框内容
  14. goodsRecommend: [], // 推荐商品
  15. kanjiaList: [], //砍价商品列表
  16. pingtuanList: [], //拼团商品列表
  17. loadingHidden: false, // loading
  18. selectCurrent: 0,
  19. categories: [],
  20. activeCategoryId: 0,
  21. goods: [],
  22. scrollTop: 0,
  23. loadingMoreHidden: true,
  24. coupons: [],
  25. curPage: 1,
  26. pageSize: 20,
  27. cateScrollTop: 0
  28. },
  29. tabClick: function(e) {
  30. wx.setStorageSync("_categoryId", e.currentTarget.dataset.id)
  31. wx.switchTab({
  32. url: '/pages/category/category',
  33. })
  34. // wx.navigateTo({
  35. // url: '/pages/goods/list?categoryId=' + e.currentTarget.id,
  36. // })
  37. },
  38. toDetailsTap: function(e) {
  39. wx.navigateTo({
  40. url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
  41. })
  42. },
  43. tapBanner: function(e) {
  44. const url = e.currentTarget.dataset.url
  45. if (url) {
  46. wx.navigateTo({
  47. url
  48. })
  49. }
  50. },
  51. adClick: function(e) {
  52. const url = e.currentTarget.dataset.url
  53. if (url) {
  54. wx.navigateTo({
  55. url
  56. })
  57. }
  58. },
  59. bindTypeTap: function(e) {
  60. this.setData({
  61. selectCurrent: e.index
  62. })
  63. },
  64. onLoad: function(e) {
  65. wx.showShareMenu({
  66. withShareTicket: true
  67. })
  68. const that = this
  69. // 读取分享链接中的邀请人编号
  70. if (e && e.inviter_id) {
  71. wx.setStorageSync('referrer', e.inviter_id)
  72. }
  73. // 读取小程序码中的邀请人编号
  74. if (e && e.scene) {
  75. const scene = decodeURIComponent(e.scene)
  76. if (scene) {
  77. wx.setStorageSync('referrer', scene.substring(11))
  78. }
  79. }
  80. // 静默式授权注册/登陆
  81. AUTH.authorize().then(res => {
  82. TOOLS.showTabBarBadge()
  83. })
  84. wx.setNavigationBarTitle({
  85. title: wx.getStorageSync('mallName')
  86. })
  87. this.initBanners()
  88. this.categories()
  89. WXAPI.goods({
  90. recommendStatus: 1
  91. }).then(res => {
  92. if (res.code === 0){
  93. that.setData({
  94. goodsRecommend: res.data
  95. })
  96. }
  97. })
  98. that.getCoupons()
  99. that.getNotice()
  100. that.kanjiaGoods()
  101. that.pingtuanGoods()
  102. this.wxaMpLiveRooms()
  103. },
  104. async miaoshaGoods(){
  105. const res = await WXAPI.goods({
  106. miaosha: true
  107. })
  108. if (res.code == 0) {
  109. res.data.forEach(ele => {
  110. const _now = new Date().getTime()
  111. if (ele.dateStart) {
  112. ele.dateStartInt = new Date(ele.dateStart.replace(/-/g, '/')).getTime() - _now
  113. }
  114. if (ele.dateEnd) {
  115. ele.dateEndInt = new Date(ele.dateEnd.replace(/-/g, '/')).getTime() -_now
  116. }
  117. })
  118. this.setData({
  119. miaoshaGoods: res.data
  120. })
  121. }
  122. },
  123. async wxaMpLiveRooms(){
  124. const res = await WXAPI.wxaMpLiveRooms()
  125. if (res.code == 0 && res.data.length > 0) {
  126. this.setData({
  127. aliveRooms: res.data
  128. })
  129. }
  130. },
  131. async initBanners(){
  132. const _data = {}
  133. // 读取头部轮播图
  134. const res1 = await WXAPI.banners({
  135. type: 'index'
  136. })
  137. if (res1.code == 700) {
  138. wx.showModal({
  139. title: '提示',
  140. content: '请在后台添加 banner 轮播图片,自定义类型填写 index',
  141. showCancel: false
  142. })
  143. } else {
  144. _data.banners = res1.data
  145. }
  146. this.setData(_data)
  147. },
  148. onShow: function(e){
  149. this.setData({
  150. shopInfo: wx.getStorageSync('shopInfo')
  151. })
  152. // 获取购物车数据,显示TabBarBadge
  153. TOOLS.showTabBarBadge()
  154. this.goodsDynamic()
  155. this.miaoshaGoods()
  156. },
  157. async goodsDynamic(){
  158. const res = await WXAPI.goodsDynamic(0)
  159. if (res.code == 0) {
  160. this.setData({
  161. goodsDynamic: res.data
  162. })
  163. }
  164. },
  165. async categories(){
  166. const res = await WXAPI.goodsCategory()
  167. let categories = [];
  168. if (res.code == 0) {
  169. const _categories = res.data.filter(ele => {
  170. return ele.level == 1
  171. })
  172. categories = categories.concat(_categories)
  173. }
  174. this.setData({
  175. categories: categories,
  176. activeCategoryId: 0,
  177. curPage: 1
  178. });
  179. this.getGoodsList(0);
  180. },
  181. onPageScroll(e) {
  182. let scrollTop = this.data.scrollTop
  183. this.setData({
  184. scrollTop: e.scrollTop
  185. })
  186. },
  187. async getGoodsList(categoryId, append) {
  188. if (categoryId == 0) {
  189. categoryId = "";
  190. }
  191. wx.showLoading({
  192. "mask": true
  193. })
  194. const res = await WXAPI.goods({
  195. categoryId: categoryId,
  196. page: this.data.curPage,
  197. pageSize: this.data.pageSize
  198. })
  199. wx.hideLoading()
  200. if (res.code == 404 || res.code == 700) {
  201. let newData = {
  202. loadingMoreHidden: false
  203. }
  204. if (!append) {
  205. newData.goods = []
  206. }
  207. this.setData(newData);
  208. return
  209. }
  210. let goods = [];
  211. if (append) {
  212. goods = this.data.goods
  213. }
  214. for (var i = 0; i < res.data.length; i++) {
  215. goods.push(res.data[i]);
  216. }
  217. this.setData({
  218. loadingMoreHidden: true,
  219. goods: goods,
  220. });
  221. },
  222. getCoupons: function() {
  223. var that = this;
  224. WXAPI.coupons().then(function (res) {
  225. if (res.code == 0) {
  226. that.setData({
  227. coupons: res.data
  228. });
  229. }
  230. })
  231. },
  232. onShareAppMessage: function() {
  233. return {
  234. title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
  235. path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid')
  236. }
  237. },
  238. getNotice: function() {
  239. var that = this;
  240. WXAPI.noticeList({pageSize: 5}).then(function (res) {
  241. if (res.code == 0) {
  242. that.setData({
  243. noticeList: res.data
  244. });
  245. }
  246. })
  247. },
  248. onReachBottom: function() {
  249. this.setData({
  250. curPage: this.data.curPage + 1
  251. });
  252. this.getGoodsList(this.data.activeCategoryId, true)
  253. },
  254. onPullDownRefresh: function() {
  255. this.setData({
  256. curPage: 1
  257. });
  258. this.getGoodsList(this.data.activeCategoryId)
  259. wx.stopPullDownRefresh()
  260. },
  261. // 获取砍价商品
  262. async kanjiaGoods(){
  263. const res = await WXAPI.goods({
  264. kanjia: true
  265. });
  266. if (res.code == 0) {
  267. const kanjiaGoodsIds = []
  268. res.data.forEach(ele => {
  269. kanjiaGoodsIds.push(ele.id)
  270. })
  271. const goodsKanjiaSetRes = await WXAPI.kanjiaSet(kanjiaGoodsIds.join())
  272. if (goodsKanjiaSetRes.code == 0) {
  273. res.data.forEach(ele => {
  274. const _process = goodsKanjiaSetRes.data.find(_set => {
  275. return _set.goodsId == ele.id
  276. })
  277. if (_process) {
  278. ele.process = 100 * _process.numberBuy / _process.number
  279. ele.process = ele.process.toFixed(0)
  280. }
  281. })
  282. this.setData({
  283. kanjiaList: res.data
  284. })
  285. }
  286. }
  287. },
  288. goCoupons: function (e) {
  289. wx.switchTab({
  290. url: "/pages/coupons/index"
  291. })
  292. },
  293. pingtuanGoods(){ // 获取团购商品列表
  294. const _this = this
  295. WXAPI.goods({
  296. pingtuan: true
  297. }).then(res => {
  298. if (res.code === 0) {
  299. _this.setData({
  300. pingtuanList: res.data
  301. })
  302. }
  303. })
  304. },
  305. goSearch(){
  306. wx.navigateTo({
  307. url: '/pages/search/index'
  308. })
  309. },
  310. goNotice(e) {
  311. const id = e.currentTarget.dataset.id
  312. wx.navigateTo({
  313. url: '/pages/notice/show?id=' + id,
  314. })
  315. }
  316. })