Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序学习&Taro开发遇见的问题收集 #22

Open
Clearives opened this issue Nov 7, 2019 · 0 comments
Open

小程序学习&Taro开发遇见的问题收集 #22

Clearives opened this issue Nov 7, 2019 · 0 comments

Comments

@Clearives
Copy link
Owner

Clearives commented Nov 7, 2019

小程序学习&Taro 开发遇见的问题收集

button 获取用户信息登录

原生小程序使用方法:

<button type="primary" open-type="getUserInfo" bindgetuserinfo="getUserInfo">
  获取用户信息
</button>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
  获取手机号
</button>

Taro 使用方法:

<button
  id="login-btn"
  openType="getUserInfo"
  lang="zh_CN"
  onGetUserInfo="{this.getUserInfo}"
  type="primary"
>
  微信用户快速登录
</button>
<button openType="getPhoneNumber" onGetPhoneNumber="{this.getPhoneNumber}">
  获取手机号
</button>

open-type 改为 openType,bindgetuserinfo 改为 onGetUserInfo,其它类似

checkSession

Taro.checkSession({
  success(res) {
    console.log("success", res);
  },
  fail(res) {
    console.log("fail", res);
  }
})
  .then(data => console.log(data))
  .catch(() => {
    console.log("wx.login");
  });

getSetting

getSetting 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
20191101152227.png

注意:getPhoneNumber 获取手机号权限不会出现在这里

getUserInfo

在用户未授权过的情况下调用此接口,将不再出现授权弹窗,会直接进入 fail 回调(详见《公告》)。在用户已授权的情况下调用此接口,可成功获取用户信息。授权采用 button

picker-view

当我们使用 picker-view 时会有一个 onChange 事件,也就是我们滚动了,我们能从 e.detail.value 中拿到联动的取值,当我们控制改变时,受控部分需要重制到自己的第一个,我们必须手动去修改值。

// 参考省市联动
cityChange(e) {
  const pickerValue = e.detail.value
  const { provinces, citys, value } = this.state
  const provinceNum = pickerValue[0]
  const cityNum = pickerValue[1]
  const countyNum = pickerValue[2]
  // 如果省份选择项和之前不一样,表示滑动了省份,此时市默认是省的第一组数据,
  if (value[0] != provinceNum) {
    const id = provinces[provinceNum].id
    this.setState({
      value: [provinceNum, 0, 0],
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id]
    })
  } else if (value[1] != cityNum) {
    // 滑动选择了第二项数据,即市,此时区显示省市对应的第一组数据
    const id = citys[cityNum].id
    console.log(id)
    this.setState({
      value: [provinceNum, cityNum, 0],
      areas: address.areas[citys[cityNum].id]
    })
  } else {
    // 滑动选择了区
    this.setState({
      value: [provinceNum, cityNum, countyNum]
    })
  }
}

参考:picker-view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant