1 Star 0 Fork 0

瑞恒/baonipin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.js 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
瑞恒 提交于 2017-11-03 22:09 . 包你拼
//app.jsvar的作用于是函数作用于,而let是块级别(大括号括起来的内容)const声明的变量只可以在声明时赋值,不可随意修改,这是最大的特点。
App({
data: {
loginData: null,
sign: "",
mobile: "",
username: "",
mid: "",
sharecode: "",
authStatic: false,
loginStatic: false,
authSuccess: false
},
onLaunch: function () {
const apiurl = 'https://friend-guess.playonwechat.com/';
let that = this;
//调用API从本地缓存中获取数据
let logs = wx.getStorageSync('logs') || [];
logs.unshift(Date.now())
wx.setStorageSync('logs', logs);
let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
that.data.kid = extConfig.kid;
that.data.kid = 123;
wx.setStorageSync('kid', that.data.kid); //that.data.kid
wx.showLoading({
title: '加载中',
})
wx.login({
success: function (res) {
console.log(res);
if (res.code) {
//发起网络请求
wx.request({
url: apiurl+'api/auth?code=' + res.code + '&operator_id=' + that.data.kid,
data: {
code: res.code
},
success: function (res) {
console.log("成功");
console.log(res);
console.log("sign:",res.data.data.sign);
that.data.sign = res.data.data.sign;
that.data.loginData = res.data.data.sign;
that.data.sharecode = res.data.data.sharecode;
try {
wx.setStorageSync('sign', res.data.data.sign);
that.data.mobile = res.data.data.mobile;
that.data.mid = res.data.data.mid;
wx.getUserInfo({
success: function (res) {
console.log("微信信息");
console.log(res);
let userData = {};
let userInfo = res.userInfo
let nickName = userInfo.nickName
let avatarUrl = userInfo.avatarUrl
let gender = userInfo.gender //性别 0:未知、1:男、2:女
let province = userInfo.province
let city = userInfo.city
let country = userInfo.country;
wx.setStorageSync('userInfo', userInfo);//缓存存用户信息
that.data.username = nickName;
that.data.avatarUrl = avatarUrl;
userData = {
nickName: nickName,
avatarUrl: avatarUrl,
gender: gender,
province: province,
city: city,
country: country
};
wx.request({
url: apiurl + '/api/save-user-info?sign=' + that.data.sign + '&operator_id=' + that.data.kid,
method: 'POST',
data: {
info: userData
},
success: function (res) {
console.log("保存信息",res);
that.data.authSuccess = true
setTimeout(function () {
wx.hideLoading()
}, 500)
}
})
},
fail: function () {
console.log("用户拒绝授权");
wx.openSetting({
success: (res) => {
console.log(res);
}
})
},
})
} catch (e) {
console.log("回话异常:" + e);
}
},
})
} else {
console.log('获取用户登录态失败!' + res.errMsg);
}
}
});
},
globalData: {
userInfo: null,
sign: ""
}
})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/RuiHeng/baonipin.git
git@gitee.com:RuiHeng/baonipin.git
RuiHeng
baonipin
baonipin
master

搜索帮助