userCenter.js 12.4 KB
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

import {API_HOST, SERVICE_HOST} from '../../libs/config';
import {GET, POST} from '../../libs/request';
import { getChannelCode, getGenderCode, getRecPosCode, getRecommandContentCode } from '../../utils/home';
import { parseBrandListData } from '../../utils/productList';
import { shouldDiscardTap, getGoodDetailParam } from '../../utils/util';
import { decodePhoneNumber, wechatLoginAction, getUnionID, listen} from '../../utils/login';

import {
  logEvent,
  YB_PAGE_OPEN_L,
  YB_MAIN_TAB_C,
} from '../../libs/analytics.js'

//userCenter.js
//获取应用实例
var app = getApp()
let PV_ID = new Date().getTime() + '';

const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;

const DEVICE_WIDTH_RATIO = windowWidth / 320;

let listWidth = Math.ceil(137.5 * DEVICE_WIDTH_RATIO);
let listHeight = Math.ceil(254 * DEVICE_WIDTH_RATIO);

const IMAGE_WIDTH = 145;
const IMAGE_HEIGHT = 193;
const IMAGE_RATIO = IMAGE_HEIGHT / IMAGE_WIDTH;

let listImageTop = 31;
let listImageWidth = listWidth;
let listImageHeight = Math.ceil(listWidth * IMAGE_RATIO);

let listMarginHorizontal = (windowWidth - listWidth * 2) / 3;

Page({
  data: {
    vipImg:'',
    windowWidth,
    windowHeight,
    listWidth,
    listHeight,
    listImageWidth,
    listImageHeight,
    listImageTop,
    listMarginHorizontal,

    userInfo: {},
    isLogin: false,
    infoNum:{},
    recommdData:[],
    hasUnionID: '',
    isShowIndicator: false,
    scrollTop: 0,
  },
  //事件处理函数
  bindViewTap: function() {
    // console.log('avatar tapped!!')
  },

  onLoad: function () {
    this.setData({
      hasUnionID: app.globalData.WXUnion_ID !== null && app.globalData.WXUnion_ID !== '' && app.globalData.WXUnion_ID !== undefined ? true:false,
    })

    if (app.isLogin()) {
      this.data.isLogin = true;
    }else{
      this.setData({
        isLogin:false,
      })
    }
    new app.WeToast();
    this.getRecommed();

    //订阅登录完成通知
    listen(function (succeed) {
      if (succeed) {
        this.showUserInfo();
        this.getInfoNum();
      } 
    }.bind(this))
  },
  onShow: function() {
    if (app.isLogin()) {
      // this.data.isLogin = true;
      this.setData({
        isLogin: true,
      })
      this.showUserInfo();
      this.getInfoNum();
    }else{
      this.setData({
        isLogin: false,
      })
    }
    logEvent(YB_MAIN_TAB_C, { TAB_ID: 4 });
    
    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route

    let params = {
      PAGE_NAME: 'userCenter',
      PAGE_PARAM: '',
      FROM_PAGE_NAME: '',
      FROM_PAGE_PARAM: '',
      PV_ID: PV_ID,
      PAGE_PATH: url        

    };
    logEvent(YB_PAGE_OPEN_L, params);
  },
  
  navToGoodsCollectPage: function () {
    if (!this.data.isLogin) {
      this.wetoast.toast({
        title: "请先完成登录/注册,再查看!",
        titleClassName: 'wetoast-title',
        duration: 1500
      });
      return;
    }
    wx.navigateTo({
      url: '../goodsCollect/goodsCollect',
    })
  },

  navToAddressManagerPage:function(){
    if (!this.data.isLogin) {
      this.wetoast.toast({
        title: "请先完成登录/注册,再查看!",
        titleClassName: 'wetoast-title',
        duration: 1500
      });
      return;
    }
    wx.navigateTo({
      url: '../addressManager/addressManager?currentMode=modeEdit' + '&page_name=' + 'userCenter' + '&page_param=' + ''
    })
  },

  selectComplete: function(json) {
    // console.log(json)
  },

  navToOrders:function(e) {
    if (!this.data.isLogin) {
      this.wetoast.toast({
        title: "请先完成登录/注册,再查看!",
        titleClassName: 'wetoast-title',
        duration: 1500
      });

      return;
    }
    let index = parseInt(e.currentTarget.dataset.index);
    let params = {
      PAGE_NAME: 'userCenter',
      PAGE_PARAM: '',
      FROM_PAGE_NAME: '',
      FROM_PAGE_PARAM: '',
    };
    let YB_EVENT = '';
    if(index === 0){
      //我的订单
      YB_EVENT = 'YB_MY_ORD';
    }else if(index === 1){
      //待付款
      YB_EVENT = 'YB_MY_TOPAY';
    } else if (index === 2) {
      //待发货
      YB_EVENT = 'YB_MY_TOSEND';
    } else if (index === 3) {
      //待收货
      YB_EVENT = 'YB_MY_TOREC';
    }     
    logEvent(YB_EVENT, params);
    wx.navigateTo({
      url: '../orders/orders?index=' + index + '&page_name=' + 'userCenter' + '&page_param=' + ''
    })
  },

  getPhoneNumber: function (e) {
    var app = getApp()
    var that = this;
    // console.log("e.detail.errMsg", e)
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      decodePhoneNumber(e.detail.iv, e.detail.encryptedData, function (result) {
        if (result.code != 200) {
            that.wetoast.toast({
              title: result.message,
              titleClassName: 'wetoast-title',
              duration: 1000,
              success:function(){
                that.loginAndRegisterTapped()
              }.bind(that)
            });

        } else {
          let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;
          app.updateUid(uid)
          that.setData({ uid });
          that.showUserInfo();
        }
      })
    } else {
      wx.navigateTo({
        url: '../bindPhoneNumber/bindPhoneNumber',
      })
    }
  },

  loginAndRegisterTapped:function() {
    let that = this;
    let app = getApp()
    if (wx.getSetting) {
      wx.getSetting({
        success: (res) => {
          // console.log("userInfo:", res.authSetting["scope.userInfo"])
          if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === false) {
            wx.showModal({
              title: '',
              content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
              confirmText: "去开启",
              confirmColor: "#000000",
              success: function (res) {
                if (res.confirm) {
                  wx.openSetting({
                    success: (res) => {
                      // console.log(res.authSetting["scope.userInfo"])
                      if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === true) {
                        getUnionID(app.getWechatThirdSession(), function (response) {
                          if (response.succeed === true) {
                            that.showUserInfo();
                            that.getInfoNum();
                          }
                        })
                      }
                    }
                  })
                } else {
                  wx.navigateTo({
                    url: '../bindPhoneNumber/bindPhoneNumber',
                  })
                }
              }
            })
          } else {
            wx.navigateTo({
              url: '../bindPhoneNumber/bindPhoneNumber',
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
    }
    
  },

  navToServiceFeedBack:function(){
    wx.navigateTo({
      url: 'service/service',
    })
  },
  //商品点击进入商品详情
  productCellTapped: function (event) {
    if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
      return;
    }
    // this.setData({ lastTapTimeStamp: event.timeStamp });

    let data = event.currentTarget.dataset;
    let productSkn = data.productSkn;
    let productIndex = data.productIndex;
    let productF_index = data.productF_index;
    let f_name = productF_index == 2 ? '新品到着' : '猜你喜欢';

    // let params = {
    //   F_ID: productF_index,
    //   F_NAME: f_name,
    //   F_URL: '../goodsDetail/goodsDetail?productSkn=' + productSkn,
    //   F_INDEX: productF_index,
    //   I_INDEX: productIndex + 1,
    //   PV_ID: PV_ID,
    // };
    // logEvent(YB_MAIN_EVENT, params);


    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?productSkn=' + productSkn + '&page_name=' + 'my' + '&page_param=' + ''
    });
  },
  /**
  * 获取个人中心猜你喜欢数据
  */
  getRecommed:function(){
    let gender = getGenderCode(getApp().globalData.selectedChannel);
    let yh_channel = getChannelCode(getApp().globalData.selectedChannel);
    let param = {
      method:"app.home.newPreference",
      gender,
      yh_channel,
      rec_pos: 100004,
      limit:30,
      fromPage:"aFP_My",
    }
    GET(API_HOST,param)
      .then(json =>{
        if (json && json.code && json.code == 200) {
          // console.log('获取猜你喜欢数据成功');
          // console.log(json);
          let data = json.data.product_list;
          data = parseBrandListData(data, listImageWidth, listImageHeight);
          this.setData({
            recommdData: data,
          })
        }
      });
  },

  getInfoNum:function(){
    let myUid = app.getUid();
    if(!myUid){
      myUid = app.globalData.userInfo.uid;
    }
    /**
    * 获取用户数量
    */
    let numParam = {
      method: 'app.home.getInfoNum',
      // uid: myUid ,
    };
    GET(API_HOST, numParam)
      .then(json => {
        if (json && json.code && json.code == 200) {
          this.data.infoNum = json.data;
          this.setData({
            infoNum: this.data.infoNum,
          })
        }
      });
  },
  showUserInfo: function() {
    // console.log("uid:", app.getUid())
    if (app.getUid()) {
      let param = {
        method: "app.passport.profile",
        uid: app.getUid()
      }
      GET(API_HOST, param)
        .then((json) => {
          if (json && json.code && json.code == 200) {
            this.data.userInfo = app.globalData.userInfo;
            this.data.userInfo.nickName = json.data.nickname;
            this.data.userInfo.avatarUrl = (!json.data.head_ico ? "images/mine_default_head.png" : this.formatImgUrl(json.data.head_ico))
            this.setVipImg(json.data.vip_info.cur_level);
            this.setData({
              userInfo: this.data.userInfo,
              isLogin:true
            })
          }else{
            this.setData({
              isLogin:false
            })
          }
        })
        .catch(error => {
          // console.log(error);
          this.setData({
            isLogin: false
          })
        })
    }
    else {
      let userInfo = app.globalData.userInfo;
      this.setData({
        userInfo
      })
      this.setData({
        isLogin: false
      })
    }
    
  },
  setVipImg:function(e){
    // console.log('current level'+e);
    let img='';
    if(e==1){
        img = 'images/yin-vip@2x.png';
    }else if(e == 2){
      img = 'images/jin-vip@2x.png';
    } else if (e == 3) {
      img = 'images/baijin-vip@2x.png';
    }
    this.setData({
      vipImg:img,
    })
  },
  formatImgUrl: function(url) {
    url = url.replace(new RegExp('{width}', 'gm'), '100').replace(new RegExp('{height}', 'gm'), '100').replace(new RegExp('{mode}', 'gm'), '1');
    return url;
  },

  couponTapAction: function() {
    if (!this.data.isLogin) {
      this.wetoast.toast({
        title: "请先完成登录/注册,再查看!",
        titleClassName: 'wetoast-title',
        duration: 1500
      });
      return;
    }
    // console.log("点击优惠券事件");
    wx.navigateTo({
      url: '../coupon/coupon',
    })
  },

  yohocoinTapAction: function() {
    // console.log("click yohocoin action");
  },

  //============================copy me=======================//
  hideModalTapped: function() {
    hideModal(this);
  },

  getVerifyKeyBtnTapped: function() {
    getVerifyCode(this, function(result){
      // console.log(result);
    })
  },

  phoneInputChanged: function (event) {
    phoneInputChangedAction(event.detail.value, this)
  },

  bindBtnTapped: function() {
    let that = this;
    bindBtnAction(this, function(){
      that.showUserInfo();
    });
  },

  verifyCodeInputChanged: function(event) {
    verifyCodeInputChangedAction(event.detail.value, this);
  },
  //============================copy me=======================//

  scroll: function (e) {
    var { scrollTop } = e.detail;
    console.log(e);
    var isShow = scrollTop > windowHeight * 2 ? true : false;
    if (isShow != this.data.isShowIndicator) {
      this.setData({
        isShowIndicator: isShow
      });
    }
  },

  backToTop: function () {
    this.setData({
      scrollTop: 0
    })
  }
})