vip-login.js 12.2 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
let Date = require('../../utils/date.js');
let Utils = require('../../utils/util.js');

// import {getObjByListKeyValue} from '..';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    userInfo: {},
    // form start
    name: "",
    shopName: "",
    mobile: "",
    verifyCode: "",
    imageUrl: "",
    auth_time: 0,
    sendAuthCode: true,
    membersShipIndex: 0,
    membersShipList: [],
    maxImg: 1, // 上传数量
    files: [], // 上传文件列表
    provinceId: "",
    cityId: "",
    provinceList: [],
    cityList: [],
    province: "",
    city: "",
    memberTypeName: "", // label
    memberTypeCode: "", // 码
    // form end
    isModify: false,
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    this.setData({
      isModify: options && options.type == "modify"
    })
    this.initData();
  },
  initData() {
    // this.queryMember().then((result) => {});
    this.initOpenid();
  },

  /**
   * 初始化Openid
   * 发红包用 该段代码参考自 user-center.js
   */
  initOpenid() {
    this.queryMember().then((result) => {
      let subscriptionOpenid = result.subscriptionOpenid;
      if (subscriptionOpenid) {
        // member表 已有openid 继续流程
        this.initVipLogin(); // 原有业务
      } else {
        // member表无openid ,重h5授权回来的local拿,成功就继续往下走
        this.querySaveOpenid().then((result) => {
          this.queryMember().then((result) => {
            this.initVipLogin(); // 原有业务
          })
        }).catch((err) => {
          // 如果均没有,则去h5授权
          app.router.push({
            path: "webview"
          })
        });
      }
    });
  },

  /**
   * 初始化VipLogin 原initData内容
   */
  initVipLogin() {
    let auditStatus = this.data.userInfo && this.data.userInfo.auditStatus || "";
    let isModify = this.data.isModify;
    if ((auditStatus == "authorization" || auditStatus == "unauthorized") && !isModify) {
      // 未验证和待验证
      app.router.push({
        openType: "redirect",
        path: "vipVerify"
      })
    } else {
      this.getProvince();
      this.getMemberType().then((result) => {
        app.post({
          url: app.api.memberIdQuery,
          data: {},
        }).then((result) => {
          let verifyInfo = result;
          this.setData({
            name: verifyInfo.realName,
            mobile: verifyInfo.phone,
            province: verifyInfo.province,
            city: verifyInfo.city,
            memberTypeCode: verifyInfo.memberTypeCode,
            memberTypeName: verifyInfo.memberTypeName,
            files: [{
              path: verifyInfo.images
            }]
          })
        })
      })
    }
  },

  /**
   * 保存openid
   */
  querySaveOpenid() {
    return new Promise((resolve, reject) => {
      let openid = app.store.getItem("openid");
      if (openid) {
        app.post({
          toast: false,
          url: app.api.saveOpenid,
          data: {
            subscriptionOpenid: openid
          }
        }).then((result) => {
          console.log("result:", result);
          app.store.clear("openid");
          resolve();
        }).catch((err) => {
          reject();
        });
      } else {
        reject();
      }
    });
  },

  /**
   * 获取会员信息
   */
  queryMember() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.member,
        data: {}
      }).then((result) => {
        this.setData({
          userInfo: result
        })
        resolve(result);
      })
    });
  },

  /**
   * 获取用户类型
   */
  getMemberType() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.memberType,
        data: {}
      }).then((result) => {
        let membersShipList = result;
        // membersShipList.forEach(element => {
        //   element.label = element.memberTypeName
        // });
        membersShipList.unshift({
          memberTypeName: "请选择"
        })
        this.setData({
          membersShipList
        })
        resolve();
      })
    });
  },

  getProvince() {
    return new Promise((resolve, reject) => {
      app.post({
        mode: "custom",
        url: app.api.areaQuery,
        data: {
          parentId: ""
        }
      }).then((result) => {
        let provinceList = result;
        // ---- start 把广东移动到第一条
        for (var i = 0; i < provinceList.length; i++) {
          let key = provinceList[i].areaName.indexOf('广东');
          if (key != -1) {
            let target = provinceList[i];
            provinceList.unshift(target);
            provinceList.splice(i + 1, 1);
            break;
          }
        }
        // ---- end 把广东移动到第一条

        this.setData({
          provinceList: provinceList
        })
        console.log("provinceList:", result);
      })
    });
  },

  getCity() {
    return new Promise((resolve, reject) => {
      app.post({
        mode: "custom",
        url: app.api.areaQuery,
        data: {
          parentId: this.data.provinceId
        }
      }).then((result) => {
        this.setData({
          cityList: result
        })
        console.log("city:", result);
      })
    });
  },



  // 显示提示
  showTips(tips) {
    wx.showToast({
      title: tips,
      icon: "none"
    })
  },

  /**
   * 表单提交验证
   */
  checkSubmit() {
    return new Promise((resolve, reject) => {
      let {
        name,
        shopName,
        memberTypeCode,
        province,
        city,
        mobile,
        verifyCode,
        files
      } = this.data;
      let pics = [];
      files.forEach(element => {
        if (element.path) {
          pics.push(element.path)
        }
      });
      console.log("files:", files);
      console.log("pics:", pics);
      console.log("pics.lenght:", pics.length);
      console.log("pics.length <= 0:", pics.length <= 0);

      if (!name) {
        this.showTips("请输入真实姓名");
        reject();
      } else if (!memberTypeCode) {
        this.showTips("请选择会员身份");
        reject();
      } else if (!province) {
        this.showTips("请选择省");
        reject();
      } else if (!city) {
        this.showTips("请选择市");
        reject();
      } else if (!mobile) {
        this.showTips("请输入手机号码");
        reject();
      } else if (!Utils.checkMobile(mobile)) {
        this.showTips("请输入正确手机号码");
        reject();
      } else if (!verifyCode) {
        this.showTips("请输入验证码");
        reject();
        // } else if (pics.length <= 0) {
        //   this.showTips("请上传身份证或电工证");
        //   reject();
      } else {
        resolve();
      }
    });
  },

  /**
   * 参考 contact-table同名方法
   * 提交表单
   *
   */
  onSubmitHandler() {
    let {
      name,
      shopName,
      memberTypeCode,
      province,
      city,
      mobile,
      verifyCode,
      files
    } = this.data;
    let pics = [];
    files.forEach(element => {
      pics.push(element.path)
    });

    this.checkSubmit().then((result) => {
      // 先上传图片到服务器
      // this.uploadToCustomService(pics).then((result) => {
      let verifyInfo = {
        realName: name,
        storeName: shopName,
        province: province,
        city: city,
        phone: mobile,
        verifyCode: verifyCode,
        memberTypeCode: memberTypeCode,
        images: ""
      }

      app.post({
        url: app.api.memberIdCommit,
        data: verifyInfo
      }).then((result2) => {
        app.router.push({
          path: "vipVerify"
        })
      });
      // })
    })

  },

  /**
   * 调起微信图片上传
   */
  onUploadHandler() {
    console.log("this.options.type:", this.options.type);
    if (this.options.type == "modify") return; //修改模式不让改图片
    let _this = this;
    let count = _this.data.maxImg - _this.data.length;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      count: 1,
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFiles = res.tempFiles
        let maxImg = _this.data.maxImg;
        let files = _this.data.files;
        files = files.concat(tempFiles);
        if (files.length > maxImg) {
          files.splice(0, maxImg);
        }
        _this.setData({
          files
        });
      }
    })
  },

  /**
   * 上传到自定义服务器
   * urlList 需要上传的图片地址
   */
  uploadToCustomService(urlList) {
    let _this = this;
    return new Promise((resolve, reject) => {
      // promise列表
      let p = [];
      // 用promise上传音频
      urlList.forEach(element => {
        let myPromise = new Promise((resolve2, reject2) => {
          _this.uploadfileMultiple(element).then((result2) => {
            resolve2(result2)
          }).catch((err) => {
            // _this.tip("声音上传失败")
            reject2();
          });
        });
        p.push(myPromise);
      });
      Promise.all(p).then(uploadFiles => {
        resolve(uploadFiles)
      }, reason => {
        reject();
      });
    });
  },
  /**
   * 多文件上传
   * @param {*} filePath
   */
  uploadfileMultiple(filePath) {
    let _this = this;
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: app.config.NET_CONFIG.commonApi + app.api.uploadFile,
        filePath: filePath,
        name: 'file',
        // header: {}, // 设置请求的 header
        header: {
          'content-type': 'multipart/form-data'
        },
        formData: {
          path: '/weapp/zhiliang-light-upload/'
        },
        // HTTP 请求中其他额外的 form data
        success(res) {
          let result = JSON.parse(res.data).content;
          resolve(result);
        },
        fail() {
          reject()
        },
        complete: function () {}
      })
    });
  },

  // 获取验证码
  reqGetSmsRequest() {
    if (!this.data.sendAuthCode) return;
    let mobile = this.data.mobile
    if (!mobile || !Utils.checkMobile(mobile)) {
      this.showLog("请输入正确的手机号码");
      return
    }
    this.getAuthCode();
    app.post({
      url: app.api.memberPhone,
      data: {
        phone: mobile,
      }
    }).then((result) => {
      wx.showToast({
        title: "短信发送成功",
        icon: "none"
      })
    });
  },
  // 倒数
  getAuthCode() {
    let _this = this;
    _this.setData({
      sendAuthCode: false,
      auth_time: 60
    })
    var auth_timetimer = setInterval(() => {
      _this.setData({
        auth_time: _this.data.auth_time - 1
      })
      if (_this.data.auth_time <= 0) {
        _this.setData({
          sendAuthCode: true
        });
        clearInterval(auth_timetimer)
      }
    }, 1000)
  },

  bindPickerChangeProvince(e) {
    let index = e.detail.value;
    this.setData({
      provinceIndex: index,
      provinceId: this.data.provinceList[index].areaId,
      province: this.data.provinceList[index].areaName,
      cityList: [],
      cityIndex: -1,
    })
    this.getCity();
  },

  bindPickerChangeCity(e) {
    let index = e.detail.value;
    this.setData({
      cityIndex: index,
      cityId: this.data.cityList[index].areaId,
      city: this.data.cityList[index].areaName,
    })
  },

  bindMembersShipChange(e) {
    let index = e.detail.value;
    this.setData({
      membersShipIndex: index,
      memberTypeName: this.data.membersShipList[index].memberTypeName,
      memberTypeCode: this.data.membersShipList[index].memberTypeCode,
    })
  },

  bindMobileInput(e) {
    this.setData({
      "mobile": e.detail.value
    })
  },
  bindNameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindShopNameInput(e) {
    this.setData({
      shopName: e.detail.value
    })
  },
  bindVerifyCodeInput(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },


  hideMask() {
    this.setData({
      authorizeVisible: false
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      case "_evt_hide_mask":
        this.hideMask();
        break;

      default:
        break;
    }
  },
})