如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
index.wxml
1 2 3 4 5 6 7 8 9 10 11 12 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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
1 2 3 4 5 |
/* 授权 */ .dew image{ width: 100%; height:600rpx; } |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//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
1 2 3 4 5 6 |
const config = { api_base_url: 'https://xxx.com/api/', appkey: "xxxxxx", } export {config } |