接口标识:member
接口url:https://你的网站/api/index.php?action=member&type=baidu
接口参数:
(1)type:'baidu'用户来源,必填;目前只有:baidu、weixin两种
(2)其他相关参数比如:code、data、iv等(具体请参考demo)
小程序代码:
- swan.login({
- success: function (login) {
- //获取code,换取session_key
- swan.authorize({
- scope: 'scope.userInfo',
- success(a) {
- swan.showLoading({
- title: '授权中...'
- });
- swan.getUserInfo({
- success(userInfo) {
- console.log(userInfo.userInfo)
- swan.request({
- url: that.globalData.api + "action=member",
- data: {
- aid: that.globalData.aid,
- code: login.code,
- data: userInfo.data,
- iv: userInfo.iv,
- type: 'baidu'
- },
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded', // 默认值
- 'x-appsecret': that.globalData.appsecret
- },
- success: function (res) {
- swan.hideLoading();
- let data = res.data;
- if (res.statusCode === 200) {
- swan.setStorageSync('Token', data.token);
- swan.setStorageSync('userInfo', data.data);
- swan.navigateBack();
- swan.showToast({
- title: '授权成功',
- icon: 'success',
- duration: 1000,
- });
- }
- }
- })
- }
- })
- },
- fail(err) {
- swan.showToast({
- title: '授权失败'
- });
- }
- });
- },
- fail: function (err) {
- console.log('登录失败', err);
- }
- });
说明:
(1)小程序用户首次访问,授权成功后用户信息会同步到后台“会员中心”,并通过form字段区分来源
(2)如果用户是登录用户,会同步更新会员中心登录状态,
(3)token验证正在开发中