user/index.wxml
<view class='btn-login' bindtap='goAuth'>点我登录</view>
user/index.js
Page({
goAuth: function(e) {
wx.navigateTo({
url: '/pages/app-auth/index',
});
}
)}
app-auth/index.wxml
<view class="container">
<view class='logo-name'>Suoz的小商店</view>
<view class='login'>请完成微信授权以继续使用</view>
<button class='btn-login' open-type="getUserInfo" bindgetuserinfo='getUserInfo'>
<view class='text'>微信快捷登录</view>
</button>
<view class="cancel" bindtap="goBack">取消</view>
</view>
app-auth/index.js
const app = getApp()
var util = require('../utils/index.js')
Page({
onShow: function () {
let userInfo = wx.getStorageSync('userInfo');
if (userInfo != '') {
wx.navigateBack();
};
},
goBack:function(){
wx.navigateBack();
},
loginByWeixin() {
let code = null;
return new Promise(function(resolve, reject) {
return util.login().then((res) => {
code = res.code;
return util.getUserInfo();
}).then((userInfo) => {
util.request("http://127.0.0.1:8360/api/auth/loginByWeixin", {
code: code,
userInfo: userInfo
}, 'POST').then(res => {
if (res.errno === 0) {
wx.setStorageSync('userInfo', res.data.userInfo);
wx.setStorageSync('token', res.data.token);