labels.js 16.3 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
const app = getApp();
var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  s = require("../../../../utils/common.js"),
  d = e.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: a.url, //接口网址
    iurl: a.imghost, //图片前缀网址
    interest_lables: [], //兴趣标签   
    check_label: [], //用户选中的标签集合
    user_label_val: "", //奖励多少
    user_label_type: "", //奖励类型(1积分,1成长值)
    is_modify: 0, //判断是第一次填写还是修改(0填写,1修改)
    isread: 0, //是否读取
    is_check: 0, //是否重复提交
    pageType:'',//页面来源标识,1 是注册时跳转过来的 
    label_required:'',//注册时标签是否必填
  },
  //点击选择兴趣标签把值放到已选中
  click_label: function(e) {
    var th = this;
    var index = e.currentTarget.dataset.index; //选择兴趣标签的下标
    var labels = th.data.interest_lables; //兴趣标签数组
    var check_label = th.data.check_label; //已选中的数组
    var labels_cid = th.data.interest_lables[index].check; //当前标签是否选中
    var labels_check = 'interest_lables[' + index + '].check'; //用于点击标签把点击状态赋值进去 
    if (labels_cid) {
      labels_cid = 0;
      for (var i = 0; i < check_label.length; i++) {
        if (labels[index].id == check_label[i].id) {
          check_label.splice(i, 1);
        }
      }
      th.setData({
        [labels_check]: labels_cid,
        check_label: check_label
      })
    } else {
      if (check_label.length > 9) {
        getApp().my_warnning("最多可选10个哦", 0, th);
      } else {
        var check_new = {
          lable: labels[index]
        };
        labels_cid = 1;
        check_label.push(labels[index]);
      }
      th.setData({
        [labels_check]: labels_cid,
        check_label: check_label
      })
    }

  },
  //获取配置信息
  get_config: function() {
    var th = this;
    getApp().getConfig2(function(ee) {
      var sms_conf = ee.switch_list;
      sms_conf = JSON.parse(sms_conf);
      var getuser_label_type = 1;
      var getuser_label_val = 0;
      let label_required=''
      if (sms_conf.user_label_type != undefined) {
        getuser_label_type = parseInt(sms_conf.user_label_type) + 1;
      }
      if (sms_conf.user_label_val != undefined) {
        getuser_label_val = sms_conf.user_label_val;
      }
    
      if (sms_conf.label_required==1) {
        label_required=1
      }


      th.setData({
        user_label_val: getuser_label_val,
        user_label_type: getuser_label_type,
        label_required:label_required
      })
    })
  },
  //页面跳转
  goto: function(e) {
    var th = this;
    wx.navigateBack({ //返回
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('标签页面对象-------');
    console.log(options);
    let pageType = options.pageType //页面来源标识,1 是注册时跳转过来的 
    if (pageType) {
      this.setData({
        pageType
      })
      // this.pageType=pageType
    }
    var th = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
    try {
      prevPage.setData({
        is_zy: 1
      })
    } catch (error) {
      
    }

    if(options.is_reg){
       this.setData({is_reg:1})
    }

    th.query_nolabels();
    th.get_config();
  },
  //获取标签集合
  query_nolabels: function() {
    var th = this;
    var url = "/api/weshop/marketing/holiday/vip/interest/label/page";
    var interest_lables = th.data.interest_lables; //兴趣标签集合
    getApp().request.promiseGet(url, {
      data: {
        storeId: a.stoid,
        pageSize: 50,
        page: 1
      }
    }).then(res => {
      if (res.data.code == 0) {
        th.setData({
          interest_lables: res.data.data.pageData,
          isread: 1
        })
        if(!th.data.pageType){
          th.query_checklabels();
        }
      } else {
        getApp().my_warnning("系统繁忙,请稍后再试", 0, th);
        th.setData({
          isread: 1
        })
        return false;
      }
    })
  },
  //查找会员选择的兴趣标签
  query_checklabels: function() {
    var th = this;
    var url = "/api/weshop/marketing/holiday/vip/interest/label/get";
    var interest_lables = th.data.interest_lables; //兴趣标签
    getApp().request.promiseGet(url, {
      data: {
        userId: d.user_id,
        storeId: a.stoid
      }
    }).then(res => {
      if (res.data.code == 0) {
        var data = res.data.data;
        var arr = [];
        var VipLabelId = []; //通过id标签找到已选择的标签
        var dateList = data.VipLabelId.split(",");
        for (var i in dateList) {
          VipLabelId = VipLabelId.concat(dateList[i]);
        }
        if (VipLabelId.length > 0) {
          for (var i = 0; i < interest_lables.length; i++) {
            for (var ii = 0; ii < VipLabelId.length; ii++) {
              if (interest_lables[i].id == VipLabelId[ii]) {
                interest_lables[i].check = 1;
                arr.push(interest_lables[i]);
              }
            }
          }
        }
        //如何有之前有选择兴趣标签的话,再进来的话就是修改
        if (data.VipLabelId != '') {
          th.setData({
            is_modify: 1
          })
        }
        th.setData({
          check_label: arr,
          interest_lables: interest_lables
        })
      } else {
        getApp().my_warnning("系统繁忙,请稍后再试", 0, th);
        return false;
      }
    })
  },
  //更新会员兴趣标签
  update_label: function() {
    var th = this;
    var is_modify = th.data.is_modify; //是否是修改
    var check_label = th.data.check_label; //用户选择
    var user_label_val = th.data.user_label_val; //选择标签奖励多少
    var remark = "领取";
    var user_label_type = th.data.user_label_type; //赠送类型
    var is_check = th.data.is_check; //是否重复提交
    var i = 0;
    var vipLabel = ""; //选择的标签名称
    var vipLabelId = ""; //选择的标签id
    var arr = [];
    for (var i = 0; i < check_label.length; i++) {
      if (i < check_label.length - 1) {
        vipLabelId += check_label[i].id + ",";
        vipLabel += check_label[i].LabelName + ",";
        arr.push(check_label[i].LabelName);
      } else {
        vipLabelId += check_label[i].id;
        vipLabel += check_label[i].LabelName;
        arr.push(check_label[i].LabelName);
      }
    }
    if (is_modify) {
      user_label_type = 0;
      remark = "修改";
      user_label_val = 0;
    }
    if (is_check==1) {
      getApp().my_warnning("请勿重复提交", 0, th);
    }
    if (is_check == 0) {
      if (th.data.pageType) {
        if(th.data.label_required){
          if (check_label.length < 1) {
            getApp().my_warnning("必须完成兴趣标签选择", 0, th);
            return
          } 
        }
        wx.showLoading({
            title: "处理中.",
            mask: true
        })
        th.setData({
          is_check: 1
        })

        if(getApp().globalData.h5_openid){
          getApp().globalData.zc_dd.openid=getApp().globalData.h5_openid;
        }

        app.request.get("/api/weshop/users/thirdLogin", {
            data: getApp().globalData.zc_dd,
            success: function (e) {
                wx.hideLoading();
                if (e.data.code == 0) {
                    app.globalData.user_id = e.data.data.user_id;
                } 
                else if(e.data.code==-3){
                  th.setData({loading: 0,is_check:0});
                  wx.showModal({
                    title: e.data.msg,
                    success: function (e) {
                      if(e.confirm){
                          getApp().globalData.zc_dd.isreplacemobile=1;
                         
                          th.update_label();
                      }
                    }
                  });
                  return false;
                } 
           
                else {
                    th.setData({loading: 0,is_check:0});
                    return app.showWarning("授权登入失败!" + e.data.msg);
                }

                getApp().globalData.login_back = 1;
                wx.setStorageSync("userinfo", e.data.data);
                wx.setStorageSync("isAuth", !0), app.globalData.userInfo = e.data.data, app.globalData.userInfo.head_pic = s.getFullUrl(app.globalData.userInfo.head_pic);
                if (th.data.label_required && check_label.length>0) {                
                  getApp().request.put("/api/weshop/marketing/holiday/vip/interest/label/update", {
                    data: {
                      "storeId": a.stoid,
                      "userId":e.data.data.user_id,
                      "vipLabel": vipLabel,
                      "vipLabelId": vipLabelId,
                      "type": user_label_type,
                      "remark": remark,
                      "value": user_label_val,
                      "fromType": "REWARD"
                    },
                    success: function(res) {
                      if (res.data.code == 0) {
                        getApp().my_warnning("领取成功", 1, th);
                        setTimeout(function() {
                          // var pages = getCurrentPages();
                          // var prevPage = pages[pages.length - 2];
                          // prevPage.setData({
                          //   check_label: arr
                          // })
                          // th.goto();
                          // wx.reLaunch({
                          //   url: '/pages/user/index/index',
                          // })
                          //-- 看情况,是跳转等级卡,还是跳转到新人有礼 --
                          getApp().go_to_page(th.data.is_reg,function (){
                            wx.navigateBack({ delta: 1 })
                          })

                        }, 2000);
                      }

                    
                      else {
                        getApp().my_warnning("系统繁忙,请稍后再试", 0, th);
                        th.setData({
                          is_check: 0
                        })
                      }
                    }
                  })
                }else{
                  getApp().go_to_page(th.data.is_reg,function (){
                    // wx.navigateBack({ delta: 1 })
                    wx.reLaunch({
                      url: '/pages/user/index/index',
                    })
                  })

                }

            },
            failStatus: function (t) {
                wx.hideLoading();
                th.setData({loading: 0,is_check:0});
                return app.my_warnning("授权登入失败,请稍后再试!", 0, that);
            },
            fail: function (t) {
                wx.hideLoading();
                th.setData({loading: 0,is_check:0});
                return i.clearAuth(), i.alertLoginErrorAndGoHome(), !1;
            }
        });
      }else{
        if (check_label.length < 1) {
          getApp().my_warnning("完成兴趣标签才能" + remark + "哦~", 0, th);
          th.setData({
            is_check: 0
          })
        } else {
            th.setData({
              is_check: 1
            })
         
          var url = "/api/weshop/marketing/holiday/vip/interest/label/update"; //更新标签接口地址
          var nav_url = "/packageG/pages/user/userinfo/userinfo"; //领取成功之后跳转页面

          var my_confirm = th.selectComponent("#my_confirm"); //组件的id
          remark = "兴趣标签赠送";
            if (is_modify) {
    
              my_confirm.open_cancel(0);
              getApp().request.put(url, {
                data: {
                  "storeId": a.stoid,
                  "userId": d.user_id,
                  "vipLabel": vipLabel,
                  "vipLabelId": vipLabelId,
                  "type": user_label_type,
                  "remark": remark,
                  "value": user_label_val,
                  "fromType": "REWARD"
                },
                success: function(res) {
                  if (res.data.code == 0) {
                    getApp().my_warnning("修改成功", 1, th);
                    setTimeout(function() {
                      var pages = getCurrentPages();
                      var prevPage = pages[pages.length - 2]; //上一个页面
                      //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
                      prevPage.setData({
                        check_label: arr
                      })
                      th.goto();
                    }, 2000);
                  } else {
                    getApp().my_warnning("系统繁忙,请稍后再试", 0, th);
                    th.setData({
                      is_check: 0
                    })
                  }
                }
              })
    
            } else {
    
              getApp().request.put(url, {
                data: {
                  "storeId": a.stoid,
                  "userId": d.user_id,
                  "vipLabel": vipLabel,
                  "vipLabelId": vipLabelId,
                  "type": user_label_type,
                  "remark": remark,
                  "value": user_label_val,
                  "fromType": "REWARD"
                },
                success: function(res) {
                  if (res.data.code == 0) {
                    getApp().my_warnning("领取成功", 1, th);
                    setTimeout(function() {
                      var pages = getCurrentPages();
                      var prevPage = pages[pages.length - 2]; //上一个页面
                      //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
                      prevPage.setData({
                        check_label: arr
                      })
                      th.goto();
                    }, 2000);
                  } else {
                    getApp().my_warnning("系统繁忙,请稍后再试", 0, th);
                    th.setData({
                      is_check: 0
                    })
                  }
                }
              })
            }
          
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //会员点击了跳过,就要注册
  goto_user(e){
    var th=this;
    if(this.data.is_check) return false;
    wx.showLoading({
      title: "处理中.",
      mask: true
    })

    this.setData({ is_check: 1 })

    if(getApp().globalData.h5_openid){
      getApp().globalData.zc_dd.openid=getApp().globalData.h5_openid;
    }

    app.request.get("/api/weshop/users/thirdLogin", {
      data: getApp().globalData.zc_dd,
      success: function (e) {
        wx.hideLoading();
        th.setData({loading: 0,is_check:0});

        if (e.data.code == 0) {
          app.globalData.user_id = e.data.data.user_id;
        } 
        
        else if(e.data.code==-3){

          wx.showModal({
            title: e.data.msg,
            success: function (e) {
              if(e.confirm){
                getApp().globalData.zc_dd.isreplacemobile=1;

                th.goto_user();
              }
            }
          });
          return false;
        }
        
        else {
          return app.showWarning("授权登入失败!" + e.data.msg);
        }
        getApp().globalData.login_back = 1;
        wx.setStorageSync("userinfo", e.data.data);
        wx.setStorageSync("isAuth", !0), app.globalData.userInfo = e.data.data, app.globalData.userInfo.head_pic = s.getFullUrl(app.globalData.userInfo.head_pic);

        //-- 注册的跳转 --
        getApp().go_to_page(th.data.is_reg,function (){
          // wx.navigateBack({ delta: 1 })
          wx.reLaunch({
            url: '/pages/user/index/index',
          })
        })

      },
      failStatus: function (t) {
        wx.hideLoading();
        th.setData({loading: 0,is_check:0});
        return app.my_warnning("授权登入失败,请稍后再试!", 0, that);
      },
      fail: function (t) {
        wx.hideLoading();
        th.setData({loading: 0,is_check:0});
        return i.clearAuth(), i.alertLoginErrorAndGoHome(), !1;
      }
    });


  }

})