index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. const WXAPI = require('apifm-wxapi')
  2. const TOOLS = require('../../utils/tools.js')
  3. const AUTH = require('../../utils/auth')
  4. const APP = getApp()
  5. Page({
  6. data: {
  7. inputVal: "", // 搜索框内容
  8. goodsRecommend: [], // 推荐商品
  9. kanjiaList: [], //砍价商品列表
  10. pingtuanList: [], //拼团商品列表
  11. loadingHidden: false, // loading
  12. selectCurrent: 0,
  13. categories: [],
  14. goods: [],
  15. loadingMoreHidden: true,
  16. coupons: [],
  17. curPage: 1,
  18. pageSize: 20
  19. },
  20. tabClick(e) {
  21. // 商品分类点击
  22. const category = this.data.categories.find(ele => {
  23. return ele.id == e.currentTarget.dataset.id
  24. })
  25. if (category.vopCid1 || category.vopCid2) {
  26. wx.navigateTo({
  27. url: '/pages/goods/list-vop?cid1=' + (category.vopCid1 ? category.vopCid1 : '') + '&cid2=' + (category.vopCid2 ? category.vopCid2 : ''),
  28. })
  29. } else {
  30. wx.setStorageSync("_categoryId", category.id)
  31. wx.switchTab({
  32. url: '/pages/category/category',
  33. })
  34. }
  35. },
  36. tabClickCms(e) {
  37. // 文章分类点击
  38. const category = this.data.cmsCategories[e.currentTarget.dataset.idx]
  39. wx.navigateTo({
  40. url: '/pages/cms/list?categoryId=' + category.id,
  41. })
  42. },
  43. toDetailsTap: function(e) {
  44. console.log(e);
  45. const id = e.currentTarget.dataset.id
  46. const supplytype = e.currentTarget.dataset.supplytype
  47. const yyId = e.currentTarget.dataset.yyid
  48. if (supplytype == 'cps_jd') {
  49. wx.navigateTo({
  50. url: `/packageCps/pages/goods-details/cps-jd?id=${id}`,
  51. })
  52. } else if (supplytype == 'vop_jd') {
  53. wx.navigateTo({
  54. url: `/pages/goods-details/vop?id=${yyId}&goodsId=${id}`,
  55. })
  56. } else if (supplytype == 'cps_pdd') {
  57. wx.navigateTo({
  58. url: `/packageCps/pages/goods-details/cps-pdd?id=${id}`,
  59. })
  60. } else if (supplytype == 'cps_taobao') {
  61. wx.navigateTo({
  62. url: `/packageCps/pages/goods-details/cps-taobao?id=${id}`,
  63. })
  64. } else {
  65. wx.navigateTo({
  66. url: `/pages/goods-details/index?id=${id}`,
  67. })
  68. }
  69. },
  70. tapBanner: function(e) {
  71. const url = e.currentTarget.dataset.url
  72. if (url) {
  73. wx.navigateTo({
  74. url
  75. })
  76. }
  77. },
  78. adClick: function(e) {
  79. const url = e.currentTarget.dataset.url
  80. if (url) {
  81. wx.navigateTo({
  82. url
  83. })
  84. }
  85. },
  86. bindTypeTap: function(e) {
  87. this.setData({
  88. selectCurrent: e.index
  89. })
  90. },
  91. onLoad: function(e) {
  92. wx.showShareMenu({
  93. withShareTicket: true,
  94. })
  95. const that = this
  96. // 读取分享链接中的邀请人编号
  97. if (e && e.inviter_id) {
  98. wx.setStorageSync('referrer', e.inviter_id)
  99. }
  100. // 读取小程序码中的邀请人编号
  101. if (e && e.scene) {
  102. const scene = decodeURIComponent(e.scene)
  103. if (scene) {
  104. wx.setStorageSync('referrer', scene.substring(11))
  105. }
  106. }
  107. // 静默式授权注册/登陆
  108. AUTH.checkHasLogined().then(isLogined => {
  109. if (!isLogined) {
  110. AUTH.authorize().then( aaa => {
  111. AUTH.bindSeller()
  112. TOOLS.showTabBarBadge()
  113. })
  114. } else {
  115. AUTH.bindSeller()
  116. TOOLS.showTabBarBadge()
  117. }
  118. })
  119. this.initBanners()
  120. this.categories()
  121. this.cmsCategories()
  122. // https://www.yuque.com/apifm/nu0f75/wg5t98
  123. WXAPI.goodsv2({
  124. recommendStatus: 1
  125. }).then(res => {
  126. if (res.code === 0){
  127. that.setData({
  128. goodsRecommend: res.data.result
  129. })
  130. }
  131. })
  132. that.getCoupons()
  133. that.getNotice()
  134. that.kanjiaGoods()
  135. that.pingtuanGoods()
  136. this.adPosition()
  137. // 读取系统参数
  138. this.readConfigVal()
  139. getApp().configLoadOK = () => {
  140. this.readConfigVal()
  141. }
  142. },
  143. readConfigVal() {
  144. wx.setNavigationBarTitle({
  145. title: wx.getStorageSync('mallName')
  146. })
  147. this.setData({
  148. mallName:wx.getStorageSync('mallName')?wx.getStorageSync('mallName'):'',
  149. show_buy_dynamic: wx.getStorageSync('show_buy_dynamic')
  150. })
  151. },
  152. async miaoshaGoods(){
  153. // https://www.yuque.com/apifm/nu0f75/wg5t98
  154. const res = await WXAPI.goodsv2({
  155. miaosha: true
  156. })
  157. if (res.code == 0) {
  158. res.data.result.forEach(ele => {
  159. const _now = new Date().getTime()
  160. if (ele.dateStart) {
  161. ele.dateStartInt = new Date(ele.dateStart.replace(/-/g, '/')).getTime() - _now
  162. }
  163. if (ele.dateEnd) {
  164. ele.dateEndInt = new Date(ele.dateEnd.replace(/-/g, '/')).getTime() -_now
  165. }
  166. })
  167. this.setData({
  168. miaoshaGoods: res.data.result
  169. })
  170. }
  171. },
  172. async initBanners(){
  173. const _data = {}
  174. // 读取头部轮播图
  175. const res1 = await WXAPI.banners({
  176. type: 'index'
  177. })
  178. if (res1.code == 700) {
  179. wx.showModal({
  180. title: '提示',
  181. content: '请在后台添加 banner 轮播图片,自定义类型填写 index',
  182. showCancel: false
  183. })
  184. } else {
  185. _data.banners = res1.data
  186. }
  187. this.setData(_data)
  188. },
  189. onShow: function(e){
  190. this.setData({
  191. navHeight: APP.globalData.navHeight,
  192. navTop: APP.globalData.navTop,
  193. windowHeight: APP.globalData.windowHeight,
  194. menuButtonObject: APP.globalData.menuButtonObject //小程序胶囊信息
  195. })
  196. this.setData({
  197. shopInfo: wx.getStorageSync('shopInfo')
  198. })
  199. // 获取购物车数据,显示TabBarBadge
  200. TOOLS.showTabBarBadge()
  201. this.goodsDynamic()
  202. this.miaoshaGoods()
  203. const refreshIndex = wx.getStorageSync('refreshIndex')
  204. if (refreshIndex) {
  205. this.onPullDownRefresh()
  206. wx.removeStorageSync('refreshIndex')
  207. }
  208. },
  209. async goodsDynamic(){
  210. const res = await WXAPI.goodsDynamic(0)
  211. if (res.code == 0) {
  212. this.setData({
  213. goodsDynamic: res.data
  214. })
  215. }
  216. },
  217. async categories(){
  218. const res = await WXAPI.goodsCategory()
  219. let categories = [];
  220. if (res.code == 0) {
  221. const _categories = res.data.filter(ele => {
  222. return ele.level == 1
  223. })
  224. categories = categories.concat(_categories)
  225. }
  226. this.setData({
  227. categories: categories,
  228. curPage: 1
  229. });
  230. this.getGoodsList(0);
  231. },
  232. async getGoodsList(categoryId, append) {
  233. if (categoryId == 0) {
  234. categoryId = "";
  235. }
  236. wx.showLoading({
  237. "mask": true
  238. })
  239. // https://www.yuque.com/apifm/nu0f75/wg5t98
  240. const res = await WXAPI.goodsv2({
  241. categoryId: categoryId,
  242. page: this.data.curPage,
  243. pageSize: this.data.pageSize
  244. })
  245. wx.hideLoading()
  246. if (res.code == 404 || res.code == 700) {
  247. let newData = {
  248. loadingMoreHidden: false
  249. }
  250. if (!append) {
  251. newData.goods = []
  252. }
  253. this.setData(newData);
  254. return
  255. }
  256. let goods = [];
  257. if (append) {
  258. goods = this.data.goods
  259. }
  260. for (var i = 0; i < res.data.result.length; i++) {
  261. goods.push(res.data.result[i]);
  262. }
  263. this.setData({
  264. loadingMoreHidden: true,
  265. goods: goods,
  266. });
  267. },
  268. getCoupons: function() {
  269. var that = this;
  270. WXAPI.coupons().then(function (res) {
  271. if (res.code == 0) {
  272. that.setData({
  273. coupons: res.data
  274. });
  275. }
  276. })
  277. },
  278. onShareAppMessage: function() {
  279. return {
  280. title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
  281. path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid')
  282. }
  283. },
  284. getNotice: function() {
  285. var that = this;
  286. WXAPI.noticeList({pageSize: 5}).then(function (res) {
  287. if (res.code == 0) {
  288. that.setData({
  289. noticeList: res.data
  290. });
  291. }
  292. })
  293. },
  294. onReachBottom: function() {
  295. this.setData({
  296. curPage: this.data.curPage + 1
  297. });
  298. this.getGoodsList(0, true)
  299. },
  300. onPullDownRefresh: function() {
  301. this.setData({
  302. curPage: 1
  303. });
  304. this.getGoodsList(0)
  305. wx.stopPullDownRefresh()
  306. },
  307. // 获取砍价商品
  308. async kanjiaGoods(){
  309. // https://www.yuque.com/apifm/nu0f75/wg5t98
  310. const res = await WXAPI.goodsv2({
  311. kanjia: true
  312. });
  313. if (res.code == 0) {
  314. const kanjiaGoodsIds = []
  315. res.data.result.forEach(ele => {
  316. kanjiaGoodsIds.push(ele.id)
  317. })
  318. const goodsKanjiaSetRes = await WXAPI.kanjiaSet(kanjiaGoodsIds.join())
  319. if (goodsKanjiaSetRes.code == 0) {
  320. res.data.result.forEach(ele => {
  321. const _process = goodsKanjiaSetRes.data.find(_set => {
  322. return _set.goodsId == ele.id
  323. })
  324. if (_process) {
  325. ele.process = 100 * _process.numberBuy / _process.number
  326. ele.process = ele.process.toFixed(0)
  327. }
  328. })
  329. this.setData({
  330. kanjiaList: res.data.result
  331. })
  332. }
  333. }
  334. },
  335. goCoupons: function (e) {
  336. wx.switchTab({
  337. url: "/pages/coupons/index"
  338. })
  339. },
  340. pingtuanGoods(){ // 获取团购商品列表
  341. const _this = this
  342. // https://www.yuque.com/apifm/nu0f75/wg5t98
  343. WXAPI.goodsv2({
  344. pingtuan: true
  345. }).then(res => {
  346. if (res.code === 0) {
  347. _this.setData({
  348. pingtuanList: res.data.result
  349. })
  350. }
  351. })
  352. },
  353. goSearch(){
  354. wx.navigateTo({
  355. url: '/pages/search/index'
  356. })
  357. },
  358. goNotice(e) {
  359. const id = e.currentTarget.dataset.id
  360. wx.navigateTo({
  361. url: '/pages/notice/show?id=' + id,
  362. })
  363. },
  364. async adPosition() {
  365. let res = await WXAPI.adPosition('indexPop')
  366. if (res.code == 0) {
  367. this.setData({
  368. adPositionIndexPop: res.data
  369. })
  370. }
  371. res = await WXAPI.adPosition('index-live-pic')
  372. if (res.code == 0) {
  373. this.setData({
  374. adPositionIndexLivePic: res.data
  375. })
  376. }
  377. },
  378. clickAdPositionIndexLive() {
  379. if (!this.data.adPositionIndexLivePic || !this.data.adPositionIndexLivePic.url) {
  380. return
  381. }
  382. wx.navigateTo({
  383. url: this.data.adPositionIndexLivePic.url,
  384. })
  385. },
  386. closeAdPositionIndexPop() {
  387. this.setData({
  388. adPositionIndexPop: null
  389. })
  390. },
  391. clickAdPositionIndexPop() {
  392. const adPositionIndexPop = this.data.adPositionIndexPop
  393. this.setData({
  394. adPositionIndexPop: null
  395. })
  396. if (!adPositionIndexPop || !adPositionIndexPop.url) {
  397. return
  398. }
  399. wx.navigateTo({
  400. url: adPositionIndexPop.url,
  401. })
  402. },
  403. async cmsCategories() {
  404. // https://www.yuque.com/apifm/nu0f75/slu10w
  405. const res = await WXAPI.cmsCategories()
  406. if (res.code == 0) {
  407. const cmsCategories = res.data.filter(ele => {
  408. return ele.type == 'index' // 只筛选类型为 index 的分类
  409. })
  410. this.setData({
  411. cmsCategories
  412. })
  413. }
  414. },
  415. })