index.js 17 KB

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