uniapp抖音小程序一键获取用户手机号的示例代码
作者:还这么多错误?!
文章介绍了如何在uniapp抖音小程序中通过点击按钮一键获取用户手机号,encryptedData和iv通过点击按钮回传,后端部分通过解密获取手机号,感兴趣的朋友一起看看吧
前端部分
点击按钮,获取手机号
<button class="button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键获取</button>
传入sessionKey和encryptedData、iv
其中sessionKey是通过登录时,调用抖音接口https://developer.toutiao.com/api/apps/v2/jscode2session获得
encryptedData、iv则通过点击按钮回传的事件参数
  getPhoneNumber(e) {
    if (e.detail) {
      if (e.detail.errMsg == "getPhoneNumber:fail auth den") {
        uni.showToast({
          title: '小程序通过试运营期,才能一键获取手机号',
          icon: 'none'
        });
      }
      this.getUserPhone(e.detail)
    } else {
      this.hasPhoneValue = false
    }
  },
  async getUserPhone(query) {
    // douyin-ad-1ddba+123456 /admin/dyinAd/answerAd
    let Authorization = this.$user.token || "none Authorization";
    let http_url = this.$config.base_url + '/app/user/login/getDyPhone'
    let http_data = {
      sessionKey: this.$user.sessionKey,
      encryptedData: query.encryptedData,
      iv: query.iv,
      "admin": this.$config.admin,
    }
    let http_header = {
      Authorization
    }
    let result = await this.$http.post(http_url, http_data, http_header, 'json')
      .then(async (res) => {
        if (res && res.data) {
          let phone = {}
          Object.assign(phone, JSON.parse(res.data))
          this.formPhone = phone.phoneNumber
        }
      })
      .catch((err) => {
      });
  },后端部分
解密手机号
async getDyPhone(query){
    const decipher = crypto.createDecipheriv(
      "aes-128-cbc",
      Buffer.from(query.sessionKey, "base64"),
      Buffer.from(query.iv, "base64")
    );
    let ret = decipher.update(query.encryptedData, "base64", 'utf8');
    ret += decipher.final('utf8');
    return ret;
  }到此这篇关于uniapp抖音小程序一键获取用户手机号的示例代码的文章就介绍到这了,更多相关uniapp抖音小程序获取用户手机号内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
