如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
index.wxml
<view class="page" wx:if="{{!hidden}}">
//其他业务
</view>
<!-- 判断是否登录 -->
<modal class="modal" hidden="{{is_login}}" no-cancel bindconfirm="close" confirmText=" ">
<view class="dew">
<image src='/images/ico/sq_ico.jpg' />
<button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
我知道了
</button>
</view>
</modal>
index.js
import { config } from '../../config.js'
// pages/index/index.js
Page({
/**
* 页面的初始数据
*/
data: {
//判断小程序的API,回调,参数,组件等是否在当前版本可用。
canIUse: wx.canIUse('button.open-type.getUserInfo'),
is_login:true
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
// 查看是否授权
wx.getSetting({
success: function (res) {
if (!res.authSetting['scope.userInfo']) {
that.setData({
is_login: false
})
}
}
})
},
//执行登陆
bindGetUserInfo: function (e) {
if (e.detail.userInfo) {
//用户按了允许授权按钮
var that = this;
//插入登录的用户的相关信息到数据库
var openid = getApp().globalData.openid;
getApp().globalData.userInfo = e.detail.userInfo;
// console.log(getApp().globalData.userInfo)
wx.request({
url: config.api_base_url + 'Xcx/login',
data: {
userinfo: e.detail.userInfo,
openid: openid
},
header: {
'content-type': 'application/json' // 默认值
},
method: 'post',
success(res) {
if (res.data.result == 1) {
wx.switchTab({
url: '/pages/index/index'
})
that.setData({
is_login: true
})
} else {
console.log("写入失败")
}
}
})
//授权成功后,跳转进入小程序首页
} else {
//用户按了拒绝按钮
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
showCancel: false,
confirmText: '返回授权',
success: function (res) {
if (res.confirm) {
console.log('用户点击了“返回授权”')
}
}
})
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
onSearch: function () {
wx.navigateTo({
url: `/pages/hs_search/index`,
})
},
})
index.wxss
/* 授权 */
.dew image{
width: 100%;
height:600rpx;
}
app.js
//app.js
import { config } from './config.js'
App({
onLaunch: function () {
var that = this;
wx.login({
success: res => {
wx.request({
url: config.api_base_url + 'Xcx/get_openid',
// url: that.globalData.wx_url_1 + res.code + that.globalData.wx_url_2,
data: {
code: res.code
},
success: res => {
that.globalData.openid = res.data.msg;
}
})
}
});
},
globalData: {
userInfo:null,
openid:''
}
})
config.js
const config = {
api_base_url: 'https://xxx.com/api/',
appkey: "xxxxxx",
}
export {config }
王明昌博客
