index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. const app = getApp()
  2. var util = require('../../utils/util.js');
  3. var status = require('../../utils/index.js');
  4. Page({
  5. mixins: [require('../../mixin/globalMixin.js')],
  6. data: {
  7. loadMore: true,
  8. classification: {
  9. tabs: [],
  10. activeIndex: 0
  11. },
  12. tabTop: 0,
  13. showSubCate: true
  14. },
  15. pageNum: 1,
  16. onLoad: function() {
  17. app.setShareConfig();
  18. this.getInfo();
  19. this._doRefreshMasonry();
  20. },
  21. onReady: function(){
  22. let that = this;
  23. const query = wx.createSelectorQuery();
  24. let searH = Math.round(app.globalData.systemInfo.windowWidth / 750 * 100) || 0;
  25. setTimeout(() => {
  26. query.select('#tab').boundingClientRect(function (res) {
  27. that.setData({ tabTop: res.top - searH });
  28. }).exec();
  29. }, 1000)
  30. },
  31. onShow: function() {
  32. const that = this;
  33. util.check_login_new().then((res) => {
  34. let needAuth = !res;
  35. that.setData({ needAuth })
  36. if (res) {
  37. (0, status.cartNum)('', true).then((res) => {
  38. res.code == 0 && that.setData({
  39. cartNum: res.data
  40. })
  41. });
  42. }
  43. })
  44. },
  45. onPageScroll: function (event) {
  46. if (event.scrollTop > this.data.tabTop) {
  47. if (this.data.tabFix) {
  48. return
  49. } else {
  50. this.setData({
  51. oneFixed: "Fixed"
  52. })
  53. }
  54. } else {
  55. this.setData({
  56. oneFixed: ''
  57. })
  58. }
  59. },
  60. noLogin: function() {
  61. this.setData({
  62. needAuth: true,
  63. showAuthModal: true
  64. })
  65. },
  66. /**
  67. * 授权成功回调
  68. */
  69. authSuccess: function() {
  70. const that = this;
  71. this.pageNum = 1;
  72. this.setData({
  73. loadMore: true,
  74. showEmpty: 0,
  75. needAuth: false,
  76. showAuthModal: false
  77. }, () => {
  78. that._doRefreshMasonry()
  79. })
  80. },
  81. authModal: function () {
  82. if (this.data.needAuth) {
  83. this.setData({ showAuthModal: !this.data.showAuthModal });
  84. return false;
  85. }
  86. return true;
  87. },
  88. /**
  89. * 监控分类导航
  90. */
  91. classificationChange: function (t) {
  92. var that = this;
  93. this.pageNum = 1;
  94. this.setData({
  95. loadMore: true,
  96. showEmpty: 0,
  97. "classification.activeIndex": t.detail.e,
  98. classificationId: t.detail.a
  99. }, function () {
  100. that._doRefreshMasonry()
  101. });
  102. },
  103. onPullDownRefresh: function() {
  104. const that = this;
  105. this.pageNum = 1;
  106. this.setData({
  107. loadMore: true,
  108. showEmpty: 0
  109. }, () => {
  110. that.getInfo();
  111. that._doRefreshMasonry()
  112. })
  113. },
  114. onReachBottom: function() {
  115. this.data.loadMore && this._doAppendMasonry()
  116. },
  117. _doRefreshMasonry(keyword = '') {
  118. let that = this;
  119. this.masonryListComponent = this.selectComponent('#masonry');
  120. this.getData(keyword).then(res => {
  121. that.masonryListComponent.start(res).then(() => {})
  122. }).catch(()=>{
  123. that.masonryListComponent.start([]).then(() => {})
  124. })
  125. },
  126. _doAppendMasonry() {
  127. let that = this;
  128. this.masonryListComponent = this.selectComponent('#masonry')
  129. // 获取接口数据后使用瀑布流组件append方法,当append完成后调用then,是否可触底价在的标志位可以在这里处理
  130. this.getData().then(res => {
  131. that.masonryListComponent.append(res).then(() => {
  132. console.log('refresh completed')
  133. })
  134. }).catch(() => {
  135. console.log('没有更多了')
  136. })
  137. },
  138. /**
  139. * 获取列表
  140. */
  141. getData: function(keyword = '') {
  142. return new Promise((resolve, reject) => {
  143. let that = this;
  144. let token = wx.getStorageSync('token');
  145. let gid = that.data.classificationId;
  146. wx.showLoading();
  147. app.util.request({
  148. 'url': 'entry/wxapp/index',
  149. 'data': {
  150. controller: 'recipe.get_recipe_list',
  151. token: token,
  152. gid,
  153. pageNum: this.pageNum,
  154. keyword
  155. },
  156. dataType: 'json',
  157. success: function(res) {
  158. wx.stopPullDownRefresh();
  159. if (res.data.code == 0) {
  160. let list = res.data.data;
  161. that.pageNum++;
  162. resolve(list);
  163. } else {
  164. // 无数据
  165. let h = {
  166. loadMore: false
  167. }
  168. if (that.pageNum == 1) h.showEmpty = 1;
  169. that.setData(h);
  170. reject('');
  171. }
  172. wx.hideLoading();
  173. }
  174. })
  175. })
  176. },
  177. /**
  178. * 获取信息
  179. */
  180. getInfo: function() {
  181. let that = this;
  182. let token = wx.getStorageSync('token');
  183. wx.showLoading();
  184. app.util.request({
  185. 'url': 'entry/wxapp/index',
  186. 'data': {
  187. controller: 'recipe.get_index_info'
  188. },
  189. dataType: 'json',
  190. success: function(res) {
  191. if (res.data.code) {
  192. let {
  193. adv_arr,
  194. cate_list,
  195. is_open_recipe,
  196. modify_recipe_name,
  197. modify_recipe_share_title,
  198. modify_vipcard_share_image
  199. } = res.data.code;
  200. if (is_open_recipe != 1) {
  201. app.util.message('未开启功能', 'switchTo:/lionfish_comshop/pages/index/index', 'error');
  202. return;
  203. }
  204. wx.setNavigationBarTitle({
  205. title: modify_recipe_name || '菜谱'
  206. })
  207. // 菜单
  208. let params = {
  209. classification: {
  210. activeIndex: 0
  211. }
  212. };
  213. let index_type_first_name = '全部';
  214. if (cate_list.length > 0) {
  215. cate_list.unshift({
  216. name: index_type_first_name,
  217. id: 0
  218. })
  219. params.isShowClassification = true;
  220. params.classification.tabs = cate_list;
  221. } else {
  222. params.isShowClassification = false;
  223. }
  224. that.setData({
  225. adv_arr: adv_arr || [],
  226. modify_recipe_share_title,
  227. modify_vipcard_share_image,
  228. modify_recipe_name: modify_recipe_name || '菜谱',
  229. ...params
  230. })
  231. }
  232. wx.hideLoading();
  233. }
  234. })
  235. },
  236. /**
  237. * 搜索
  238. */
  239. goResult: function(e) {
  240. let that = this;
  241. let keyword = e.detail.value.replace(/\s+/g, '');
  242. if (!keyword) {
  243. wx.showToast({
  244. title: '请输入关键词',
  245. icon: 'none'
  246. })
  247. return;
  248. }
  249. this.pageNum = 1;
  250. this.setData({
  251. loadMore: true,
  252. showEmpty: 0
  253. }, () => {
  254. that._doRefreshMasonry(keyword);
  255. })
  256. },
  257. /**
  258. * 幻灯片跳转
  259. */
  260. goBannerUrl: function(t) {
  261. let idx = t.currentTarget.dataset.idx;
  262. let {
  263. adv_arr,
  264. needAuth
  265. } = this.data;
  266. if (adv_arr.length > 0) {
  267. let url = adv_arr[idx].link;
  268. let type = adv_arr[idx].linktype;
  269. if (util.checkRedirectTo(url, needAuth)) {
  270. this.authModal();
  271. return;
  272. }
  273. if (type == 0) {
  274. // 跳转webview
  275. url && wx.navigateTo({
  276. url: '/lionfish_comshop/pages/web-view?url=' + encodeURIComponent(url)
  277. })
  278. } else if (type == 1) {
  279. if (url.indexOf('lionfish_comshop/pages/index/index') != -1 || url.indexOf('lionfish_comshop/pages/order/shopCart') != -1 || url.indexOf('lionfish_comshop/pages/user/me') != -1 || url.indexOf('lionfish_comshop/pages/type/index') != -1) {
  280. url && wx.switchTab({
  281. url: url
  282. })
  283. } else {
  284. url && wx.navigateTo({
  285. url: url
  286. })
  287. }
  288. } else if (type == 2) {
  289. // 跳转小程序
  290. let appid = slider_list[idx].appid;
  291. appid && wx.navigateToMiniProgram({
  292. appId: slider_list[idx].appid,
  293. path: url,
  294. extraData: {},
  295. envVersion: 'release',
  296. success(res) {
  297. // 打开成功
  298. },
  299. fail(error) {
  300. console.log(error)
  301. }
  302. })
  303. }
  304. }
  305. },
  306. goLink: function (event) {
  307. if (!this.authModal()) return;
  308. let link = event.currentTarget.dataset.link;
  309. var pages_all = getCurrentPages();
  310. if (pages_all.length > 3) {
  311. wx.redirectTo({
  312. url: link
  313. })
  314. } else {
  315. wx.navigateTo({
  316. url: link
  317. })
  318. }
  319. },
  320. onShareAppMessage: function() {
  321. var member_id = wx.getStorageSync('member_id');
  322. let {
  323. modify_recipe_share_title,
  324. modify_vipcard_share_image,
  325. modify_recipe_name
  326. } = this.data;
  327. return {
  328. title: modify_recipe_share_title || modify_recipe_name,
  329. path: "lionfish_comshop/moduleA/menu/index?share_id=" + member_id,
  330. imageUrl: modify_vipcard_share_image,
  331. success: function() {},
  332. fail: function() {}
  333. };
  334. },
  335. onShareTimeline: function() {
  336. var member_id = wx.getStorageSync('member_id');
  337. let {
  338. modify_recipe_share_title,
  339. modify_vipcard_share_image,
  340. modify_recipe_name
  341. } = this.data;
  342. var query= `share_id=${member_id}`;
  343. return {
  344. title: modify_recipe_share_title || modify_recipe_name,
  345. query,
  346. imageUrl: modify_vipcard_share_image,
  347. success: function() {},
  348. fail: function() {}
  349. };
  350. }
  351. })