1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
import {
  request
} from "../../request/index.js";
var QQMapWX = require('../../qqmap/qqmap-wx-jssdk.min.js');
var qqmapsdk;
const a = getApp()
Page({
  data: {
    baseUrl: a.globalData.baseUrl,
    statusTop: a.globalData.statusHeight,
    imagesUrl: a.globalData.imagesUrl,
    four: 4,
    six: 6,
    msg: '',
    autoplay: true,
    interval: 3000,
    indicatorDots: true,
    indicator: '#F9F9F9',
    indicatorActive: '#E8370F',
    bind_car: 1,
    time: 30 * 60 * 60 * 1000,
    timeData: {},
    pin_id: '1',
    rest: false,
    groom: [], //  推荐
    homeInfo: [],
    homeChe: {},
    homeGouDay: {},
    homeLoop: [],
    S4Info: '',
    id: '',
    nobindCar: false,
    goodDetal: {},
    index4S: [],
    city_id: '',
    city: '',
    // 红包显示隐藏
    redBagshow: false,
  },
  onLoad: function (options) {
    let that = this
    let token = wx.getStorageSync("token")
    qqmapsdk = new QQMapWX({
      key: 'B4MBZ-3VICV-THBPQ-UCLHB-SLCEK-B7FIK'
    });
    // 红包的显示
    // that.getRedBag()

  },
  onShow: function () {
    let that = this
    let token = wx.getStorageSync("token")
    that.gethomeIndex()
    that.gethomeLoop()
    that.getS4Info()
    that.getindexUser()
    // 红包的显示
    that.getRedBag()
    if (wx.getStorageSync('city')) {
      //  console.log(wx.getStorageSync('city'));
      that.setData({
        city: wx.getStorageSync('city')
      })
      // that.getSuccessPage()
      that.saveCity()

    } else {
      that.getCitied()
    }

  },

  gohome() {
    this.selectComponent('#lfyLogin').getLogin('哈哈哈')
  },
  bindisToken(e) {},
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  more4SList() {
    wx.navigateTo({
      url: '/pages/Vmore4SList/Vmore4SList'
    })
  },
  goSearch() {
    wx.navigateTo({
      url: '/pages/Vmore4SList/Vmore4SList?home=1'
    })
  },
  linkWebview(e) {
    let index = e.currentTarget.dataset.index
    // if (index == 0) {
    //   wx.navigateTo({
    //     url: '/pages/webview/webview?url=' + encodeURIComponent('https://h5.goorcar.com/')
    //   })
    // }
    let homeLoop = this.data.homeLoop
    let id = homeLoop[index].goods_id
    if(id) {
      if(homeLoop[index].type == 'zan') {
        wx.navigateTo({
          url: '/pages/detail/detail?id=' + id,
        })
      } else {
        wx.navigateTo({
          url: '/pages/productdetailsImg/productdetailsImg?id=' + id,
        })
      }
    }
  },
  addLoveCared() { // 去添加车辆

    let that = this
    let token = wx.getStorageSync("token")
    if (token) {
      if (that.data.nobindCar) {
        wx.navigateTo({
          url: '/pages/addcars/addcars'
        })
      } else {
        wx.navigateTo({
          url: '/pages/addcar/addcar'
        })
      }
    } else {
      that.goLogin()
    }
  },
  // 获取红包数据
  async getRedBag() {
    let that = this
    let token = wx.getStorageSync('token')
    console.log(token)
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: '/api/red/indexRed',
      })
      console.log(data)

      if (token && data) {
        that.setData({
          redBagshow: true,
          redBagmes: data
        })
        console.log(that.data.redBagmes)
      }


    } catch (err) {
      // that.setData({
      //   msg: err.msg
      // })
      // that.popTest()
    }

  },
  // 领取红包
  sendRedBagid() {
    let that = this
    try {
      request({
        url: '/api/red/getIndexRed',
        data: {
          red_id: that.data.redBagmes.id
        }
      })
      // console.log(data)
      console.log(that.data.redBagshow)
      that.setData({
        msg: '已成功领取',
        redBagshow: false
      })
      that.popTest()
    } catch {
      that.setData({
        msg: '网络错误,请稍后重试',
      })
      that.popTest()
      that.Subscribe()
    }


  },
  //调起订阅消息
  Subscribe(){
    return new Promise((resolve, reject) => {
      wx.requestSubscribeMessage({
        tmplIds: ["ECa7laXaihhvpkslRIV-U4SZdG52226Xa9XPQCdhsJQ"],
        success: (res) => {
          if (res['ECa7laXaihhvpkslRIV-U4SZdG52226Xa9XPQCdhsJQ'] === 'accept'){
            wx.showToast({
              title: '订阅OK!',
              duration: 1000,
              success(data) {
                //成功
                resolve()
              }
            })
          }
        },
        fail(err) {
          //失败
          console.error(err);
          reject()
        }
      })
    })
  },
  // 关闭红包
  onClose() {
    this.setData({
      redBagshow: false
    })
  },
  async gethomeIndex() { // 获取首页
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/index2/index',
      })
      that.setData({
        homeChe: data.che,
        homeInfo: data.info,
        homeGouDay: data.gou_day
      })

    } catch (err) {
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }
  },
  async gethomeLoop() { // 获取轮播
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/index2/img',
      })
      that.setData({
        homeLoop: data,
      })

    } catch (err) {
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }

  },
  async getS4Info() { // 获取4s店数据 
    let that = this
    try {
      const {
        data
      } = await request({
        url: 'api/index2/s4Info',
      })
      if (data.code == 1) {
        that.setData({
          S4Info: data.data.name,
          id: data.data.id,
          bind_car: 1
        })
      } else {
        //code为0的话,说明没绑定4s店
        that.setData({
          msg: data.msg,
          bind_car: 0,
        })
        that.popTest()
      }
    } catch (err) {
      that.setData({
        msg: err.msg
      })
      if (err.code == 0) {
        that.setData({
          bind_car: 0,
        })
      }
    }

  },
  async getindexUser() { // 获取 4s关注数据
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/index2/indexUnder',
      })
      if (data.length == 0) { // 如果是空数组,你调用的是【推荐4s店】这个接口
        that.getCitied()
      }
      that.setData({
        index4S: data
      })
    } catch (err) {
      a.popTest(err.msg)
    }

  },
  getCitied() { // 获取城市信息
    let that = this
    wx.getLocation({
      type: 'wgs84', //默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          //位置坐标,默认获取当前位置,非必须参数
          //Object格式
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) { //成功后的回调
            //  console.log(res);
            var res = res.result;
            let city = res.ad_info.city
            wx.setStorageSync('city', city)
            that.saveCity(city)
          },
          fail: function (error) {
            console.error(error);
          },
          complete: function (res) {
            // //  console.log(res);
          }
        })
      },
      fail: res => {}
    })
    return
  },
  async saveCity(city) {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/citys/getCity',
        data: {
          city: city || that.data.city
        }
      })
      that.setData({
        city_id: data.city_id
      })
      if (that.data.index4S.length == 0) {
        that.getSuccessPage()
      }
    } catch (err) {
      a.popTest(err.msg)
    }
  },
  async getSuccessPage() { // 获取推荐
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/index2/tuiList2',
        data: {
          city_id: that.data.city_id
        }
      })
      //  console.log(data, '// 获取推荐');
      that.setData({
        groom: data
      })
    } catch (err) {
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }

  },
  async clickAttention(e) { // 点击关注
    let that = this
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    if (token) {
      try {
        const {
          data
        } = await request({
          url: 'api/index2/guan',
          data: {
            id: id
          }
        })
        if (data.code == 1) {
          a.popSuccessTest(data.msg)
        }
        setTimeout(() => { // 一旦有关注的店,就不显示推荐好店
          that.getindexUser()
        }, 500);
      } catch (err) {
        a.popTest(err.msg)
      }
    } else {
      that.goLogin()
    }
  },
  async clickAttention(e) { // 点击关注
    let that = this
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    if (token) {
      try {
        const {
          data
        } = await request({
          url: 'api/index2/guan',
          data: {
            id: id
          }
        })
        if (data.code == 1) {
          a.popSuccessTest(data.msg)
        }
        setTimeout(() => { // 一旦有关注的店,就不显示推荐好店
          that.getindexUser()
        }, 500);
      } catch (err) {
        a.popTest(err.msg)
      }
    } else {
      that.goLogin()
    }
  },
  goToAccess(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goAccessoriesDetailIMG/goAccessoriesDetailIMG?id=' + id
    })
  },
  gobind4S() { // 立即绑定4S店
    let that = this
    let pin_id = that.data.pin_id
    wx.navigateTo({
      url: '/pages/my4S/my4S?pin_id=' + pin_id,
    })

  },
  goViolationQuery() { //
    let that = this
    let token = wx.getStorageSync('token')
    if (token) {
      wx.navigateTo({
        url: '/pages/ViolationQuery/ViolationQuery'
      })
    } else {
      that.goLogin()
    }
  },
  goAccessoriesDetailIMG(e) { //进入配件详情
    let that = this
    let token = wx.getStorageSync('token')
    let goodDetal = that.data.goodDetal
    let tir = e.currentTarget.dataset.tir
    if (token) {
      if (tir == 1) {
        wx.navigateTo({
          url: '/pages/productdetailsImg/productdetailsImg?id=' + goodDetal.good1_id
        })
      } else if (tir == 2) {
        wx.navigateTo({
          url: '/pages/productdetailsImg/productdetailsImg?id=' + goodDetal.good2_id
        })
      } else {
        wx.navigateTo({
          url: '/pages/productdetailsImg/productdetailsImg?id=' + goodDetal.good3_id
        })
      }

    } else {
      that.goLogin()
    }
  },
  goV4Sstores(e) { // 进入我的4s店
    let that = this
    let s4_id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    if (token) {
      wx.navigateTo({
        url: '/pages/Vnew4Sstore/Vnew4Sstore?s4_id=' + s4_id

      })
    } else {
      that.goLogin()
    }
  },
  ToV4Sstores(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    if (token) {
      wx.navigateTo({
        url: '/pages/Vnew4Sstore/Vnew4Sstore?s4_id=' + id
      })
    } else {
      that.goLogin()
    }
  },
  goUpkeepimg() { // 保养预约
    let that = this
    let token = wx.getStorageSync('token')
    let id = that.data.id
    let homeChe = that.data.homeChe
    // if (token) {
      if (!id) {
        that.setData({
          msg: '您尚未绑定4S店'
        })
        that.popTest()
        return;
      }
      if (homeChe == null) {
        that.setData({
          msg: '请先绑定爱车'
        })
        that.popTest()
        return;
      }
      if (homeChe != null && id != '') {
        wx.navigateTo({
          url: '/pages/upkeepImg/upkeepImg?id=' + id
        })
      }
    // } else {
    //   that.goLogin()
    // }
  },
  toMyGarage() { // 我的车库
    let token = wx.getStorageSync("token")
    if (token) {
      wx.navigateTo({
        url: '/pages/MyGarage/MyGarage?tdTap=1'
      })
    } else {
      this.goLogin()
    }

  },
  goVopenPolicy() {
    let that = this
    let token = wx.getStorageSync('token')
    let id = that.data.id
    let homeChe = that.data.homeChe
    // if (token) {
      if (!id) {
        that.setData({
          msg: '您尚未绑定4S店'
        })
        that.popTest()
        return;
      }
      if (homeChe == null) {
        that.setData({
          msg: '请先绑定爱车'
        })
        that.popTest()
        return;
      }
      if (homeChe != null && id != '') {
        wx.navigateTo({
          url: '/pages/VopenPolicy/VopenPolicy?che_id=' + homeChe.id
        })
      }

    // } else {
    //   that.goLogin()
    // }
  },
  goVopenPolicyB() {
    let that = this
    let token = wx.getStorageSync('token')
    let id = that.data.id
    let homeChe = that.data.homeChe
    // if (token) {
      if (!id) {
        that.setData({
          msg: '您尚未绑定4S店'
        })
        that.popTest()
        return;
      }
      if (homeChe == null) {
        that.setData({
          msg: '请先绑定爱车'
        })
        that.popTest()
        return;
      }
      if (homeChe != null && id != '') {
        wx.navigateTo({
          url: '/pages/VopenPolicyB/VopenPolicyB?che_id=' + homeChe.id
        })
      }

    // } else {
    //   that.goLogin()
    // }
  },
  goVopenPolicyC() {
    let that = this
    let token = wx.getStorageSync('token')
    let id = that.data.id
    let homeChe = that.data.homeChe
    // if (token) {
      if (!id) {
        that.setData({
          msg: '您尚未绑定4S店'
        })
        that.popTest()
        return;
      }
      if (homeChe == null) {
        that.setData({
          msg: '请先绑定爱车'
        })
        that.popTest()
        return;
      }
      if (homeChe != null && id != '') {
        wx.navigateTo({
          url: '/pages/VopenPolicyC/VopenPolicyC?che_id=' + homeChe.id
        })
      }
    // } else {
    //   that.goLogin()
    // }
  },
  gobindChe() {
    let that = this
    wx.showModal({
      title: '提示',
      content: that.data.msg,
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/MyGarage/MyGarage?tdTap=1'
          })

        } else {

        }
      }
    })
  },
  onShareAppMessage(options) {
    var that = this;
    var shareObj = {
      title: "汽车互联网4S店",
      path: '/pages/home/home',
      imageUrl: '',
    };
    return shareObj;

  },
  toGoDetailA(e) {
    console.log(e)
    let that = this
    let idata = e.currentTarget.dataset.idata
    let jump = idata.jump_type
    let good_type = Number(idata.good_type1)
    let good_id = idata.good1_id
    let idx = e.currentTarget.dataset.idx
    that.publicDetails(jump, good_type, good_id)
    that.baocunId(idx)
  },
  toGoDetailB(e) {
    let that = this
    let idata = e.currentTarget.dataset.idata
    let jump = idata.jump_type
    let good_type = Number(idata.good_type2)
    let good_id = idata.good2_id
    let idx = e.currentTarget.dataset.idx
    that.publicDetails(jump, good_type, good_id)
    that.baocunId(idx)
  },
  toGoDetailC(e) {
    let that = this
    let idata = e.currentTarget.dataset.idata
    let jump = idata.jump_type
    let good_type = Number(idata.good_type3)
    let good_id = idata.good3_id
    let idx = e.currentTarget.dataset.idx
    that.publicDetails(jump, good_type, good_id)
    that.baocunId(idx)
  },
  // 点击任意区域都可以获取店面
  baocunId(e) {
    console.log(e)
    let s4_id = this.data.index4S[e].s4_data.id
    let s4_name = this.data.index4S[e].s4_data.name
    wx.setStorageSync('s4_id', s4_id)
    wx.setStorageSync('s4_name', s4_name)
    console.log('获取店面数据', s4_name, s4_id)
  },
  publicDetails(jump, good_type, good_id) {
    if (jump == 1) { //可以跳转
      switch (good_type) {
        case 1: // 人气好物
          wx.navigateTo({
            url: '/pages/detail/detail?id=' + good_id
          })
          break;
        case 2: // 精选推荐
          wx.navigateTo({
            url: '/pages/productdetailsImg/productdetailsImg?id=' + good_id
          })
          break;
        case 3: // 团购热卖
          wx.navigateTo({
            url: '/pages/VtuanDetail/VtuanDetail?id=' + good_id
          })
          break;
        case 4: //限时抢购
          wx.navigateTo({
            url: '/pages/VSpikeMallDetail/VSpikeMallDetail?id=' + good_id
          })
          break;
        case 5: //车型商品
          wx.navigateTo({
            url: '/pages/carDetails/carDetails?id=' + good_id
          })
          break;
        case -1: //推广活动详情
          wx.navigateTo({
            url: '/pages/activityDetails/activityDetails?id=' + good_id + '&type=1' + '&tap=0'
          })
          break;
        case -2: //限时活动详情
          wx.navigateTo({
            url: '/pages/details/details?id=' + good_id + '&type=2' + '&inviter_id=' + 0
          })
          break;
        default:
          break;
      }
    } else {
      a.popTest('该4s店暂未运营')
    }
  },

  async public() {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: '',
        data: {
          goods_id: that.data.goods_id
        }
      })
      //  console.log(data);
      that.setData({
        public: data
      })
    } catch (err) {
      //  console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 1300
    })
  },
  goLogin() {
    wx.showModal({
      title: '提示',
      content: '您尚未登录,前往登录',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/authorization/authorization?num=1'
          })

        } else {
          // wx.switchTab({
          //   url: '/pages/home/home'
          // })
        }
      }
    })
  },
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 1300, //停留时间
    })
  },

})