inviteFriend.js 15.5 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 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
import { API_HOST, SERVICE_HOST } from '../../../libs/config';
import { GET, POST } from '../../../libs/request';
import { parseBrandListData} from '../../../utils/productList';
import { decodePhoneNumber, wechatLoginAction, getUnionID, decodeUnionId, openAuthorizeSettings, listen } from '../../../utils/login';
import { getYHStorageSync } from '../../../utils/util';

var app = getApp();
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const screenHeight = app.globalData.systemInfo.screenHeight;
const backgroundImageW = 375;
const backgroundImageH = 375;

const listHorizontal = 15;
const listVertical = 15;
const { extend, Toast } = require('../../../vendors/zanui/index');
const DEVICE_WIDTH_RATIO = windowWidth / 375;
Page(extend({},Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth,
    windowHeight,
    screenHeight,
    backgroundImageW,
    backgroundImageH,

    listMarginHorizontal: listHorizontal * DEVICE_WIDTH_RATIO,
    listMarginVertical: listVertical * DEVICE_WIDTH_RATIO,

    isLogin: app.isLogin(),
    hasUnionID: '',
    backgroundUrl: '',
    isShowInviterInfo: false,
    inviterInfo: {    //邀请人信息
      inviterCode: '',
      inviterHeadIcon: '',
      inviterNickname: '',
      inviterTrendWrod: '',
    },
    inviteInfo: {},
    shareInfo: {    //分享信息
      inviteCode: '',  //邀请码
      trendWord: '', //潮流口令
      nickName: '',
      headIcon: '',
    },
    activityRules: "",
    isShowFriendList: false,
    inviteFriendList: [],
  
    productList: [],

    alertStatus: {
      isShowAlert: false,
      width: windowWidth,
      height: screenHeight,
      title: '',
      message: '',
      messageAlign: 'left',
      messageFontsize: 12,
      functionText: '',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var isShowInviterInfo = options.inviteCode && options.inviteCode.length > 0;
    var inviterInfo = {
      inviterCode: options.inviteCode,
      inviterNickname: options.nickname,
      inviterHeadIcon: options.headicon,
      inviterTrendWrod: options.trendWord
    }

    new app.WeToast();
    this.setData({
      inviterInfo,
      isShowInviterInfo
    });

    this.setData({
      hasUnionID: app.globalData.WXUnion_ID !== null && app.globalData.WXUnion_ID !== '' && app.globalData.WXUnion_ID !== undefined ? true : false,
    })
    this.fetchInviteInfo();
    this.fetchBgBanner();
    this.fetchProductList();
    this.fetchActivityRules();

    //订阅登录完成通知
    listen(function (succeed) {
      if (succeed) {
        this.setData({
          isLogin: app.isLogin()
        })
        this.getUserShareInfo();
      }

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

  updateLoginStatue() {
    let app = getApp();
    this.setData({
      isLogin: app.isLogin()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogin: app.isLogin()
    })
    if (app.isLogin() && this.data.shareInfo.inviteCode.length <= 0) {
      this.getUserShareInfo();
    }
    this.fetchFriendList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var ob = {
      title: this.data.inviteInfo.shareMainTitle,
      path: 'pagesSecond/pages/inviteFriend/inviteFriend?inviteCode=' + this.data.shareInfo.inviteCode + '&nickname=' + this.data.shareInfo.nickName + '&trendWord=' + this.data.shareInfo.trendWord + '&headicon=' + this.data.shareInfo.headIcon,
    };
    return ob;
  },
  /**
    * 新的授权方式
    */
  getUserInfo: function (e) {
    var that = this;
    // console.log(e);
    if (e.detail.errMsg === 'getUserInfo:ok') {
    decodeUnionId(app.getWechatThirdSession(), e, function (response) {
      if (response.isHaveUnionID) {
        that.setData({
          hasUnionID: true,
        });
      } else {
        // console.log("没有获取到unionid");
      }
    });
    } else {
      openAuthorizeSettings(function (response){
        if (response.isHaveUnionID) {
          that.setData({
            hasUnionID: true,
          })
        };
        // console.log("openAuthorizeSettings");
        if (response.succeed === true) {
        } else {
        }
      });
    }
  },
  getPhoneNumber: function (e) {
    var app = getApp()
    var that = this;
    var inviteCode = that.data.inviterInfo.inviterTrendWrod && that.data.inviterInfo.inviterTrendWrod.length > 0 ? that.data.inviterInfo.inviterTrendWrod : that.data.inviterInfo.inviterCode;
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      decodePhoneNumber(e.detail.iv, e.detail.encryptedData, inviteCode, function (result) {
        if (result.code != 200) {
          that.wetoast.toast({
            title: result.message,
            titleClassName: 'wetoast-title',
            duration: 1000,
            success: function () {
              tt.navigateTo({
                url: '/pages/bindPhoneNumber/bindPhoneNumber?inviteCode=' + inviteCode,
              })
            }.bind(that)
          });
        } else {
          //新注册用户
          if (result.is_register){
            that.showZanToast({
              title: '欢迎加入Yoho!Family!新人礼包已发放到个人中心-优惠券,请注意查收',success: function() {
                let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;
                that.setData({
                  isLogin: app.isLogin()
                })
                app.updateUid(uid)
                that.getUserShareInfo();
            }},1500);
          } else {
          let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;                    
          that.setData({
              isLogin: app.isLogin()
            })
          app.updateUid(uid)
          that.getUserShareInfo();
          }
        }
      })
    } else {
      tt.navigateTo({
        url: '/pages/bindPhoneNumber/bindPhoneNumber?inviteCode=' + inviteCode,
      })
    }
  },

  formSubmit: function (e) {
    let formId = e.detail.formId;
    let _self = this;
    if (!app.getUid() || !formId || formId.length === 0) return;
    //上报formId
    let formIdParams = {
      uid: app.getUid(),
      openId: app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID','inviteFriend'),
      miniapp_type: app.globalData.miniapp_type,
      formId: formId,
      method: 'wechat.formId.add',
    }
    // console.log('params', formIdParams)
    GET(API_HOST, formIdParams)
  },

  // fetchData
  fetchInviteInfo: function () {
    let params = {
      method: 'app.invitecode.totalForMiniApp',
    };

    GET(API_HOST, params)
      .then((json) => {
        if (json && json.code && json.code == 200) {
          this.data.inviteInfo = json.data;
          tt.setNavigationBarTitle({
            title: this.data.inviteInfo.shareMainTitle,
          })
          this.setData({
            inviteInfo: this.data.inviteInfo,
          })
        }
      })
      .catch((error) => {

      });
  },

  fetchBgBanner: function () {
    let that = this;
    let content_code = '393550a420afefddb1042a0f56dcdb2f';

    let param = {
      content_code,
    };

    GET(API_HOST + '/operations/api/v5/resource/get', param)
      .then(json => {
        if (json && json.code && json.code == 200) {
          let list = json.data;
          var backgroundUrl = "";
          if (list && list.length > 0) {
            let imageData = list[0].data.list[0];
            let imageHeight = list[0].data.imageHeight;
            let imageWidth = list[0].data.imageWidth;
            backgroundUrl = imageData.src.replace(/{width}/g, backgroundImageW * DEVICE_WIDTH_RATIO *2).replace(/{height}/g, backgroundImageH * DEVICE_WIDTH_RATIO *2).replace('{mode}', 2);
          }
          this.setData({
            backgroundUrl,
          })
        }
      })
      .catch(error => {
      });
  },

  fetchProductList: function() {
    let params = {
      method: 'app.search.top',
      page: 1,
      limit: 10
    };

    GET(API_HOST, params)
    .then(json =>{
      if (json && json.code && json.code == 200) {
        var data = json.data.product_list;
        var productList = parseBrandListData(data, Math.ceil(300 * DEVICE_WIDTH_RATIO), Math.ceil(400*DEVICE_WIDTH_RATIO));
        if (productList && productList.length > 0){
            this.setData({
              productList,
            })
        }
      }
    })
    .catch(error =>{
    });
  },

  fetchInviteCode: function () {
    if (this.data.shareInfo.inviteCode && this.data.shareInfo.inviteCode.length > 0) {
      return;
    }

    let params = {
      method: 'app.invitecode.my',
    };
    this.showLoadingToast();
    GET(API_HOST, params)
      .then(json => {
        this.hideLoadingToast();
        if (json && json.code && json.code == 200) {
          var code = json.data.inviteCode ? json.data.inviteCode : '';
          var trendWord = json.data.trendWord ? json.data.trendWord : '';
          var shareInfo = this.data.shareInfo;
          shareInfo.inviteCode = code,  //邀请码
          shareInfo.trendWord = trendWord, //潮流口令
          this.setData({
            shareInfo
          });
        }
      })
      .catch(error => {
        this.hideLoadingToast();
      });
  },

  fetchUserInfo: function() {
    var that = this;
    if (this.data.shareInfo.nickName && this.data.shareInfo.nickName.length > 0) {
      this.hideLoadingToast();
      return;
    }

    let params = {
      method: 'app.passport.profile',
    };
    this.showLoadingToast();
    GET(API_HOST, params)
      .then(json => {
        this.hideLoadingToast();
        if (json && json.code && json.code == 200) {
          var nickname = json.data.profile_name ? json.data.profile_name : '';
          var head_icon = json.data.head_ico ? json.data.head_ico : '';
          head_icon = head_icon.replace(/{width}/g, 50 * DEVICE_WIDTH_RATIO * 2).replace(/{height}/g, 50 * DEVICE_WIDTH_RATIO * 2).replace('{mode}', 2);
          var shareInfo = this.data.shareInfo;
          shareInfo.nickName = nickname, 
          shareInfo.headIcon = head_icon,
          this.setData({
            shareInfo
          });
        }
      })
      .catch(error => {
        this.hideLoadingToast();
      });
  },

  getUserShareInfo: function (){
    var that= this;
    tt.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          tt.authorize({
            scope: 'scope.userInfo',
            success: function (res) {
              that.getWxUserInfo();
            },
            fail: function (error) {
              that.fetchUserInfo();
            }
          })
        } else {
          that.getWxUserInfo();
        }
      }
    });
    this.fetchInviteCode();
  },

  getWxUserInfo: function () {
    var that = this;
    var app = getApp();
    tt.getUserInfo({
      success: function (res) {
        var nickname = res.userInfo.nickName ? res.userInfo.nickName : '';
        var head_icon = res.userInfo.avatarUrl ? res.userInfo.avatarUrl : '';
        var shareInfo = that.data.shareInfo;
        app.globalData.userInfo.avatarUrl = head_icon;
        app.globalData.userInfo.nickName = nickname;

        shareInfo.nickName = nickname, 
        shareInfo.headIcon = head_icon,
        that.setData({
          shareInfo
        });
        that.uploadWXUserInfo(head_icon, nickname);
      }
    });
  },

  uploadWXUserInfo: function (headic, nickname) {
    var uid = app.globalData.userInfo.uid
    if (!uid || !nickname || !headic || headic == "images/mine_default_head.png") return
    let params = {
      method: 'app.wechat.addUpdUser',
      headIco: headic,
      nickname,
      uid
    };

    GET(API_HOST, params)
      .then(json => {
      })
      .catch(error => {
      });

    
  },

  fetchFriendList: function () {
    let params = {
      method: 'app.invitecode.history',
    };

    GET(API_HOST, params)
      .then(json => {
        if (json && json.code && json.code == 200) {
            var friendList = json.data.data;
            friendList = this.parseFriendInfo(friendList);
            if (friendList && friendList.length > 0) {
              this.setData({
                inviteFriendList: friendList,
                isShowFriendList: true
              })
            }
        }
      })
      .catch(error => {
      });
  },

  parseFriendInfo: function(list) {
    var newData = [];
    list && list.map((item, index)=>{
      var headIcon = item.headIco.replace(/{width}/g, 45 * DEVICE_WIDTH_RATIO * 2).replace(/{height}/g, 45 * DEVICE_WIDTH_RATIO * 2).replace('{mode}', 2);
      item.headIco = headIcon;
      newData.push(item);
    });
    return newData;
  },

  fetchActivityRules: function() {
    let params = {
      method: 'app.invitecode.activityRuleForMiniApp',
    };

    GET(API_HOST, params)
      .then(json => {
        if (json && json.code && json.code == 200) {
          var rules = json.data.activityRuleDesc;
          this.setData({
            activityRules: rules
          })
        }
      })
      .catch(error => {
      });
  },

  // 响应事件
  openRules: function(e) {
    var alertStatus = this.data.alertStatus;
    alertStatus.isShowAlert = true;
    alertStatus.title = '活动细则';
    alertStatus.message = this.data.activityRules;
    alertStatus.messageAlign = 'left';
    alertStatus.messageFontsize = 12;
    alertStatus.functionText = '';
    this.setData({
      alertStatus
    })
  },

  checkFriendStatus: function (e) {
    var item = e.currentTarget.dataset.item;
    var alertStatus = this.data.alertStatus;
    var title = '';
    var message = '';
    var functionText = '';

    if (item.couponStatus === 0) {
      title = '您的好友任务未达成';
      message = item.paymentStatusDesc;
    } else if (item.couponStatus === 1) {
      message = '您的奖励待放发';
    } else {
      title = '您的好友任务已达成';
      message = "恭喜您获得一张现金券,您可以在“个人中心-优惠券”中查看";
      functionText = '立即查看';
    }

    alertStatus.isShowAlert = true;
    alertStatus.title = title;
    alertStatus.message = message;
    alertStatus.messageAlign = 'center';
    alertStatus.messageFontsize = 14;
    alertStatus.functionText = functionText;
    this.setData({
      alertStatus
    })
  },

  closeAlert: function(e) {
    var alertStatus = this.data.alertStatus;
    alertStatus.isShowAlert = false;
    this.setData({
      alertStatus
    })
  },

  productTap: function (e) {
    var product = e.currentTarget.dataset.item;
    tt.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?productSkn=' + product.product_skn,
    })
  },

  jumpToCoupon: function(e) {
    tt.navigateTo({
      url: '/page/subPackage/pages/couponList/couponList',
    })
  },

  showLoadingToast: function () {
    tt.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000,
    })
  },

  hideLoadingToast: function () {
    tt.hideToast()
  }

}))