index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. //index.js
  2. //获取应用实例
  3. var app = getApp();
  4. let host = app.globalData.servsers; // 请求的url
  5. var coupon = [{
  6. coupon_monry: '1000',
  7. coupon_rule: '9999',
  8. coupon_name: '新年专属红包',
  9. coupon_begin_time: '2018-01-01',
  10. coupon_end_time: '2018-02-08'
  11. },
  12. {
  13. coupon_monry: '300',
  14. coupon_rule: '4999',
  15. coupon_name: '新年专属红包',
  16. coupon_begin_time: '2018-01-01',
  17. coupon_end_time: '2018-02-08'
  18. },
  19. {
  20. coupon_monry: '50',
  21. coupon_rule: '999',
  22. coupon_name: '新年专属红包',
  23. coupon_begin_time: '2018-01-01',
  24. coupon_end_time: '2018-02-08'
  25. }
  26. ];
  27. Page({
  28. data: {
  29. urlHttp: '',
  30. host: '',
  31. previous: 30,
  32. next: 180,
  33. scrollLeft: 0, //tab标题的滚动条位置
  34. search: {
  35. placeholder: "输入搜索关键字"
  36. },
  37. navTab: [{
  38. url: '/',
  39. icon: 'icon-dingzhijiaju',
  40. src: '../../images/news/nav_01.png',
  41. text: '杯类'
  42. },
  43. {
  44. url: '/',
  45. icon: 'icon-dingzhijiaju',
  46. src: '../../images/news/nav_02.png',
  47. text: '行李箱'
  48. },
  49. {
  50. url: '/',
  51. icon: 'icon-dingzhijiaju',
  52. src: '../../images/news/nav_03.png',
  53. text: '手机壳'
  54. },
  55. {
  56. url: '/',
  57. icon: 'icon-dingzhijiaju',
  58. src: '../../images/news/nav_04.png',
  59. text: '板画'
  60. },
  61. {
  62. url: '/',
  63. icon: 'icon-dingzhijiaju',
  64. src: '../../images/news/nav_05.png',
  65. text: '包袋'
  66. }
  67. ],
  68. navArray: [
  69. // {
  70. // src: '/images/news/nav_1.png',
  71. // bg: '#fafcff',
  72. // type: 0,
  73. // href: '/'
  74. // },
  75. // {
  76. // src: '/images/news/nav_2.png',
  77. // bg: '#fef9f8',
  78. // type: 1,
  79. // href: '/pages/kindSecondList/kindSecondList'
  80. // },
  81. {
  82. src: '/images/news/nav_3.png',
  83. bg: '#f7f6fb',
  84. type: 2,
  85. href: '/customized/pages/index/index'
  86. },
  87. {
  88. src: '/images/news/nav_4.png',
  89. bg: '#f1f7fc',
  90. type: 3,
  91. href: '/pages/patternMenu/patternMenu'
  92. }
  93. ],
  94. specArray: [],
  95. navList: ["推荐", "中秋", "改良生活", "优化办公", "智能科技"],
  96. firstActive: "header_nav_active",
  97. currentItem: '',
  98. flag: true,
  99. banners: [],
  100. rexiao: [],
  101. tuijian: [],
  102. tuijiannew: [],
  103. forviewFir: [],
  104. forviewSec: [],
  105. forviewtitele: '',
  106. title1: '',
  107. title2: '',
  108. title3: '',
  109. intro1: '',
  110. intro2: '',
  111. intro3: '',
  112. id1: '',
  113. id2: '',
  114. id3: '',
  115. src1: '',
  116. src2: '',
  117. src3: '',
  118. src4: '',
  119. src5: '',
  120. src6: '',
  121. price1: '',
  122. price2: '',
  123. price3: '',
  124. host: '',
  125. idTwo: '',
  126. find_name: '',
  127. find_id: '',
  128. find_logo: '',
  129. find_yuedu: '',
  130. find_shoucang: '',
  131. datatime: new Date().getTime(),
  132. city_name: '',
  133. jingpin: { // 发现精品
  134. id: 1,
  135. src: "../../images/jingxuan_banner.jpg",
  136. title: "各星座员工生日礼品如何选",
  137. liulan: 117,
  138. zan: 34
  139. },
  140. page: 1,
  141. hasMore: true,
  142. hidden: '',
  143. winHeight: "", //窗口高度
  144. coupon: coupon, //优惠劵
  145. showCoupon: false,
  146. couponSta: 0, //对于用户来说,是否是第一次显示
  147. un_id: '',
  148. showMsg: true, //显示切换模式提示
  149. showChangeModel: false, //显示切换模式确认弹窗
  150. bannerFlag: 0, //banner数量
  151. recommendArray: [], // /推荐数组
  152. goodsNewsArray: [], //按照时间排序来
  153. // channelId:null ,//用户登录后的id
  154. page: 1, //默认第一页
  155. limit: 10, //展示的数量
  156. requestState: true, //请求状态,默认是有数据的
  157. loginStatic: false, //登陆状态,默认为false
  158. },
  159. //tab切换
  160. tagChoose: function (options) {
  161. var that = this
  162. var name = options.currentTarget.dataset.name;
  163. var des = options.currentTarget.dataset.des;
  164. var forid = options.currentTarget.dataset.forid;
  165. wx: wx.navigateTo({
  166. url: '/pages/kindSecondList/kindSecondList?classify_name=' + name + '&classify_des=' + des + '&id=' + forid
  167. })
  168. },
  169. // 滚动切换标签样式
  170. switchTab: function (e) {
  171. var that = this;
  172. var host = getApp().globalData.servsers;
  173. var current = e.detail.current;
  174. if (e.detail.current == 0) {
  175. var forid = 0;
  176. } else {
  177. var forid = that.data.navList[e.detail.current - 1].id;
  178. var forname = that.data.navList[e.detail.current - 1].classify_name;
  179. var classifydes = that.data.navList[e.detail.current - 1].classify_des;
  180. }
  181. this.setData({
  182. currentItem: e.detail.current
  183. })
  184. if (forid > 0) {
  185. wx.request({
  186. url: that.data.host + "classifyapi/findAllforviewFir",
  187. data: {
  188. id: forid
  189. },
  190. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT flag: true,
  191. header: {
  192. 'Accept': 'application/json'
  193. },
  194. success: function (res) {
  195. wx.request({
  196. url: that.data.host + "commodityapi/findOneforxcx",
  197. data: {
  198. com_id: forid,
  199. },
  200. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT flag: true,
  201. header: {
  202. 'Accept': 'application/json'
  203. },
  204. success: function (res) {
  205. that.setData({
  206. tuijiannew: res.data.rows
  207. });
  208. if (res == null || res.data == null) {
  209. console.error('网络请求失败');
  210. return;
  211. }
  212. }
  213. });
  214. that.setData({
  215. forviewFir: res.data.rows,
  216. forviewtitele: res.data.rows[0].classify_des,
  217. 'firstActive': "",
  218. flag: false,
  219. });
  220. if (res == null || res.data == null) {
  221. console.error('网络请求失败');
  222. return;
  223. }
  224. }
  225. });
  226. } else {
  227. //设置当前样式
  228. that.setData({
  229. 'firstActive': "",
  230. 'currentItem': 0,
  231. flag: true,
  232. })
  233. }
  234. that.checkCor();
  235. },
  236. checkCor: function () {
  237. if (this.data.currentItem > 2) {
  238. this.setData({
  239. scrollLeft: 300
  240. })
  241. } else {
  242. this.setData({
  243. scrollLeft: 0
  244. })
  245. }
  246. },
  247. onLoad: function () {
  248. var that = this;
  249. var host = getApp().globalData.servsers;
  250. var un_id = getApp().globalData.un_id;
  251. that.setData({
  252. urlHttp: host
  253. })
  254. //首页banner
  255. wx.request({
  256. url: host + "homebannerapi/homebannerall",
  257. data: {},
  258. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  259. header: {
  260. 'Accept': 'application/json'
  261. },
  262. success: function (res) {
  263. that.setData({
  264. banners: res.data.rows,
  265. host: host
  266. });
  267. console.log(res.data.rows);
  268. //判断banner位置是否为空
  269. for (var i = 0; i < res.data.rows.length; i++) {
  270. var bannerFlag = that.data.bannerFlag;
  271. if (bannerFlag > 0) {
  272. return false;
  273. } else {
  274. if (res.data.rows[i].banner_flag == 0) {
  275. bannerFlag++;
  276. that.setData({
  277. bannerFlag: bannerFlag
  278. })
  279. return false;
  280. }
  281. }
  282. }
  283. //console.log(that.data.banners);
  284. if (res == null || res.data == null) {
  285. console.error('网络请求失败');
  286. return;
  287. }
  288. }
  289. });
  290. //品牌定制
  291. wx.request({
  292. url: host + "brandapi/findforfirst",
  293. data: {},
  294. method: 'GET',
  295. header: {
  296. 'Accept': 'application/json'
  297. },
  298. success: function (res) {
  299. if (res.data.rows[0] == undefined) {
  300. that.setData({
  301. hidden: 'hidden'
  302. })
  303. } else {
  304. var img = res.data.rows[0].brand_logo;
  305. that.setData({
  306. title1: res.data.rows[0].brand_name,
  307. intro1: res.data.rows[0].brand_des,
  308. src1: res.data.rows[0].brand_logo,
  309. src4: res.data.rows[0].brand_logo1,
  310. price1: res.data.rows[0].price,
  311. id1: res.data.rows[0].id,
  312. });
  313. }
  314. if (res.data.rows[1] == undefined) {
  315. // that.setData({
  316. // hidden: 'hidden'
  317. // })
  318. } else {
  319. var img = res.data.rows[0].brand_logo;
  320. that.setData({
  321. title2: res.data.rows[1].brand_name,
  322. intro2: res.data.rows[1].brand_des,
  323. src2: res.data.rows[1].brand_logo,
  324. src5: res.data.rows[1].brand_logo1,
  325. price2: res.data.rows[1].price,
  326. id2: res.data.rows[1].id,
  327. });
  328. }
  329. if (res.data.rows[2] == undefined) {
  330. // that.setData({
  331. // hidden: 'hidden'
  332. // })
  333. } else {
  334. var img = res.data.rows[0].brand_logo;
  335. that.setData({
  336. title3: res.data.rows[2].brand_name,
  337. intro3: res.data.rows[2].brand_des,
  338. src3: res.data.rows[2].brand_logo,
  339. src6: res.data.rows[2].brand_logo1,
  340. price3: res.data.rows[2].price,
  341. id3: res.data.rows[2].id,
  342. });
  343. }
  344. if (res == null || res.data == null) {
  345. console.error('网络请求失败');
  346. return;
  347. }
  348. }
  349. })
  350. // 高度自适应
  351. wx.getSystemInfo({
  352. success: function (res) {
  353. var clientHeight = res.windowHeight,
  354. clientWidth = res.windowWidth,
  355. rpxR = 750 / clientWidth;
  356. var calc = clientHeight * rpxR;
  357. that.setData({
  358. winHeight: calc
  359. });
  360. }
  361. });
  362. },
  363. onReady: function (e) {
  364. var that = this;
  365. var host = getApp().globalData.servsers;
  366. //判断时间,在规定时间内显示优惠劵弹窗
  367. var beiginTime = '2017/12/28';
  368. var endTime = '2018/01/08';
  369. var beginTimestamp = beiginTime.replace(/-/g, '/');
  370. beginTimestamp = Date.parse(beginTimestamp);
  371. var endTimestamp = endTime.replace(/-/g, '/');
  372. endTimestamp = Date.parse(endTimestamp);
  373. //获取当前日期
  374. var date = new Date();
  375. var Y = date.getFullYear();
  376. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  377. var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  378. var nowTime = Y + '/' + M + '/' + D;
  379. nowTime = nowTime.replace(/-/g, '/');
  380. nowTime = Date.parse(nowTime);
  381. wx.login({
  382. success: function (res) {
  383. var code = res.code;
  384. wx.getUserInfo({
  385. withCredentials: true,
  386. success: function (res) {
  387. wx.request({
  388. url: host + "userapi/insertadd",
  389. data: {
  390. user_id: code,
  391. name: res.userInfo.nickName,
  392. icon: res.userInfo.avatarUrl,
  393. iv: res.iv,
  394. encryptedData: res.encryptedData,
  395. },
  396. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  397. header: {
  398. 'Accept': 'application/json'
  399. },
  400. success: function (res) {
  401. that.setData({
  402. un_id: res.data.unionId
  403. })
  404. var un_id = res.data.unionId;
  405. if (parseInt(nowTime) >= parseInt(beginTimestamp) && parseInt(nowTime) <= parseInt(endTimestamp)) { //当前日期在优惠劵领取时间内
  406. console.log("un_id:" + un_id);
  407. if (un_id != undefined && un_id != '' && un_id != null) {
  408. wx.request({ //优惠活动-对于用户来说,是否应该显示
  409. url: host + "mycouponapi/getAlertFlag",
  410. data: {
  411. user_id: un_id,
  412. batch_no: 1,
  413. alert_num: 1 //优惠活动对于每个用户来说,一共显示的次数
  414. },
  415. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  416. header: {
  417. 'Accept': 'application/json'
  418. },
  419. success: function (res) {
  420. if (res.data == 0) {
  421. that.setData({
  422. showCoupon: true
  423. })
  424. } else {
  425. that.setData({
  426. showCoupon: false
  427. })
  428. }
  429. if (res == null || res.data == null) {
  430. console.error('网络请求失败');
  431. return;
  432. }
  433. }
  434. });
  435. }
  436. }
  437. if (res == null || res.data == null) {
  438. console.error('网络请求失败');
  439. return;
  440. }
  441. }
  442. })
  443. }
  444. })
  445. }
  446. })
  447. },
  448. footerTap: app.footerTap,
  449. onShow: function () {
  450. var that = this;
  451. that.onLoad();
  452. //商品推荐
  453. that.getGoodsRecommendList(that);
  454. // 按照时间排序
  455. that.setData({
  456. goodsNewsArray: [],
  457. page: 1,
  458. })
  459. that.getGoodsNews(that)
  460. that.getSpecList(that)
  461. if (wx.getStorageSync('channelIdObj').channelId) {
  462. that.setData({
  463. loginStatic: true,
  464. })
  465. }
  466. },
  467. //跳转到“发现”tab页
  468. find: function (e) {
  469. wx.switchTab({
  470. url: "/pages/find/index"
  471. })
  472. },
  473. getSpecList(that) {
  474. wx.request({
  475. url: host + '/classifyapi/classifyTwoAll',
  476. method: 'get',
  477. data: {
  478. classifyHigher: 3072
  479. },
  480. success: function (res) {
  481. console.log(res)
  482. that.setData({
  483. specArray: res.data.rows
  484. })
  485. }
  486. })
  487. },
  488. onReachBottom() {
  489. var that = this;
  490. if (that.data.requestState) {
  491. that.setData({
  492. page: that.data.page + 1
  493. })
  494. that.getGoodsNews(that)
  495. }
  496. console.log('到底了')
  497. },
  498. //下拉刷新
  499. onPullDownRefresh: function (e) {
  500. },
  501. onShareAppMessage: function (res) { //页面分享
  502. var that = this;
  503. var host = getApp().globalData.servsers;
  504. if (res.from === 'button') {
  505. // 来自页面内转发按钮
  506. console.log(res.target)
  507. }
  508. return {
  509. title: '大良造-精良之选,用心造物!',
  510. path: '/pages/index/index',
  511. success: function (res) {
  512. },
  513. fail: function (res) {
  514. // 转发失败
  515. }
  516. }
  517. },
  518. //关闭优惠劵弹窗
  519. closeCoupon: function (e) {
  520. var that = this;
  521. that.setData({
  522. showCoupon: false
  523. })
  524. },
  525. //关闭切换模式提示
  526. closeMsg: function (e) {
  527. var that = this;
  528. that.setData({
  529. showMsg: false
  530. })
  531. },
  532. //打开切换提示
  533. openChangeModel: function (e) {
  534. var that = this;
  535. that.setData({
  536. showMsg: false,
  537. showChangeModel: true
  538. })
  539. },
  540. //跳转至一键选礼
  541. goChooseGift: function (e) {
  542. var that = this;
  543. that.setData({
  544. showChangeModel: false
  545. })
  546. wx.navigateTo({
  547. url: '/pages/patternMenu/patternMenu'
  548. })
  549. },
  550. //关闭切换模式确认弹窗
  551. closeChangeModel: function (e) {
  552. var that = this;
  553. that.setData({
  554. showChangeModel: false
  555. })
  556. },
  557. // 商品推荐
  558. getGoodsRecommendList(that) {
  559. let data = {
  560. limit: 6,
  561. offset: 1,
  562. channelId: wx.getStorageSync('channelIdObj').channelId ? wx.getStorageSync('channelIdObj').channelId : ""
  563. }
  564. wx.request({
  565. url: host + '/commodityapi/queryCommodityGroom',
  566. method: "get",
  567. data: data,
  568. success(res) {
  569. res.data.rows.forEach(el => {
  570. el.logo = el.commodity_logo.split(",")
  571. })
  572. that.setData({
  573. recommendArray: res.data.rows
  574. })
  575. }
  576. })
  577. },
  578. // 按时间排序来的
  579. getGoodsNews(that) {
  580. let data = {
  581. limit: that.data.limit,
  582. offset: that.data.page,
  583. channelId: wx.getStorageSync('channelIdObj').channelId ? wx.getStorageSync('channelIdObj').channelId : ""
  584. }
  585. wx.request({
  586. url: host + '/commodityapi/findAllforNewproduct',
  587. method: "get",
  588. data: data,
  589. success(res) {
  590. let goods = that.data.goodsNewsArray;
  591. console.log(goods)
  592. if (that.data.page == 1) {
  593. goods = [];
  594. }
  595. goods.push(...res.data.rows);
  596. console.log(goods)
  597. that.setData({
  598. goodsNewsArray: goods,
  599. requestState: res.data.rows.length === that.data.limit
  600. })
  601. }
  602. })
  603. },
  604. toGoodsDetail(e) { // 进入商品详情页
  605. let that = this,
  606. id = e.currentTarget.dataset.id;
  607. wx.navigateTo({
  608. url: '/pages/buy/buy?com_id=' + id,
  609. })
  610. },
  611. navTap(e) {
  612. // console.log(e);
  613. let item = e.currentTarget.dataset.item;
  614. if (item.type == 1 && !wx.getStorageSync('channelIdObj')) { // 表示是优质的杯,并且还没有登录过渠道
  615. wx.navigateTo({
  616. url: '/pages/login/login?url=' + item.href,
  617. })
  618. } else {
  619. wx.navigateTo({
  620. url: item.href,
  621. })
  622. }
  623. },
  624. tapMomo() {
  625. // if (!wx.getStorageSync('channelIdObj')) { // 还没有登录过渠道
  626. // wx.navigateTo({
  627. // url: '/pages/login/login?url=/pages/momo/momo',
  628. // })
  629. // } else {
  630. // wx.switchTo({
  631. // url: '/pages/momo/momo',
  632. // })
  633. // }
  634. },
  635. swiperChange(e) {
  636. console.log(e.detail.current, this.data.specArray.length,"detail")
  637. if (e.detail.current == this.data.specArray.length-1) {
  638. console.log("等于了")
  639. this.setData({
  640. previous: 210,
  641. next: 0,
  642. })
  643. } else {
  644. this.setData({
  645. previous: 30,
  646. next: 180,
  647. })
  648. }
  649. }
  650. })
  651. var banners = []
  652. // 热销新品
  653. var rexiao = []
  654. // 热销新品
  655. var pinpai = []
  656. // 人气推荐
  657. var tuijian = []
  658. var that;
  659. var Util = require('../../utils/util.js');