index.js 16 KB

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