- <view class="wx">
- <view class="subtitle">微信快捷登录</view>
- <!-- #ifdef MP-WEIXIN -->
- <text class="iconfont icon-weixin" @tap="wxLogin"></text>
- <text>微信端</text>
- <!-- #endif -->
- <!-- #ifdef H5 || APP-PLUS -->
- <text class="iconfont icon-weixin" @tap="appLogin"></text>
- <text>app端</text>
- <!-- #endif -->
- </view>
//小程序登录 begin登录
// 逻辑:登录 > 拿到code > 后端通过code获取sessionKey、openid(unionid)等
- wxLogin(e) {
- let _this = this;
- uni.login({
- provider: 'weixin',
- success: function(res) {
- console.log(res);
- if (res.code) {
- let code = res.code;
- //将用户登录code传递到后台置换用户SessionKey、OpenId等信息
- //...写用code置换SessionKey、OpenId的接口
- // _this.$http.post("/api/user/login", {code:code})
- // .then(res => {
- // let openId= res.openId;
- // _this.wxSubmit(openId)
- // })
- // .catch(err => {
- // _this.$ant.toast('登录失败')
- // });
- } else {
- uni.showToast({
- title: '登录失败!',
- duration: 2000
- });
- console.log('登录失败!' + res.errMsg)
- }
- },
- });
- }
// app登录逻辑 app端能直接拿到openId
- appLogin() {
- var that = this;
- uni.getProvider({
- service: 'oauth',
- success: function(res) {
- console.log(res.provider);
- //支持微信、qq和微博等
- if (~res.provider.indexOf('weixin')) {
- uni.login({
- provider: 'weixin',
- success: function(loginRes) {
- console.log("App微信获取用户信息成功", loginRes);
- // 获取用户信息
- uni.getUserInfo({
- provider: 'weixin',
- success: function(infoRes) {
- console.log('-------获取微信用户所有-----');
- console.log(infoRes.userInfo.openId);
- console.log(JSON.stringify(infoRes.userInfo));
- // 提交信息
- that.wxSubmit(infoRes.userInfo.openId)
- }
- });
- // that.getApploginData(loginRes) //请求登录接口方法
- },
- fail: function(res) {
- console.log("App微信获取用户信息失败", res);
- }
- })
- }
- }
- });
- },