# wx.phoneSmsLogin

调用接口发起手机号验证码登录，获取临时登录凭证 (code)，通过凭证进而换取用户标识信息等。
* 注意：在使用发送短信验证码功能之前需先完成短信签名要求实名制报备，否则将无法发送短信。详情可查看文档[发送短信验证码](/miniprogram/dev/platform-capabilities/miniapp/component/auth/sendPhoneSms)。

## 前置准备

使用 [button](/miniprogram/dev/platform-capabilities/miniapp/component/auth/sendPhoneSms) 组件指定 open-type 触发接口向用户发送短信验证码。

## 参数

### Object object

|属性|类型|默认值|必填|说明
|--|--|--|--|--|
|phoneNumber|string||是|手机号|
|verifyCode|string||是|验证码: 6位纯数字组成的字符串|
|success|function||否|接口调用成功的回调函数|
|fail|function||否|接口调用失败的回调函数|
|complete|function||否|接口调用结束的回调函数|

#### object.success 回调函数

##### 参数

###### Object res

|属性|类型|说明
|--|--|--|
|code|string|用户登录凭证（有效期五分钟）。开发者可以在开发者服务器调用 [code2Verifyinfo](/miniprogram/dev/platform-capabilities/miniapp/openapi/code2Verifyinfo)，使用 code 换取用户标识信息等|

#### object.fail 回调函数

##### 参数

###### Object res

|属性|类型|说明
|--|--|--|
|errCode|number|错误码。0：请求成功|
|errMsg|string|错误提示|

**res.errCode**

|errCode|说明|
|--|--|
|-1|system error|
|10001005|多端应用未接入身份管理|
|10001006|验证码错误|
|-700000|前端错误，errMsg 将给出详细提示|

## 示例代码

```xml
<input bindinput="onPhoneNumberChange" value="{{phoneNumber}}"/>
<button type="primary" open-type="sendPhoneSms" bindsendphonesms="onHandleLogin" phoneNumber="{{phoneNumber}}">发送验证码</button>
<input bindinput="onVerifyCodeChange" value="{{verifyCode}}"/>
<button bindtap="login">登录</button>
```

```js
Page({
  data: {
    phoneNumber: '',
    verifyCode: ''
  },
  onVerifyCodeChange(e) {
    this.setData({ verifyCode: e.detail.value });
  },
  onPhoneNumberChange(e) {
    this.setData({ phoneNumber: e.detail.value });
  },
  onHandleLogin(e) {
    const detail = e.detail;
    console.log('sendphonesms errCode', detail.errCode)
  },
  login() {
    wx.phoneSmsLogin({
      phoneNumber: this.data.phoneNumber,
      verifyCode: this.data.verifyCode,
      success (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://example.com/onLogin',
            data: {
              code: res.code
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
})
```
