| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- // pages/userinfo/userinfo.js
- const app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- buttonDisabled: false,
- modalHidden: true,
- userInfo: {},
- hasUserInfo: false,
- canIUse: wx.canIUse('button.open-type.getUserInfo'),
- inputContent: '',
- inputContentxml: '',
- id: '',
- lock: false,
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- var that = this;
- console.log('userinfo');
- console.log("userinfo"+app.globalData.userInfo);
- if(app.globalData.userInfo){
- that.setData({
- hasUserInfo: true,
- userInfo: res.userInfo
- });
- wx.login({
- success: function (res) {
- // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
- // 根据自己的需求有其他操作再补充
- // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
- wx.request({
- url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
- data: {
- code: res.code
- },
- header: {
- 'content-type': 'application/json' // 数据格式(默认值)
- },
- method: 'post', //上传方式
- success: function (res) {
- console.log("openid" + res.data.openid);
- // wx.setStorageSync('token', res.data.token)//消息模板会用到
- wx.setStorageSync('openid', res.data.openid);//存储openid
- }
- });
- }
- });
- }else{
- that.setData({
- hasUserInfo: false
- });
- }
- // wx.getSetting({
- // success: function (res) {
- // if (res.authSetting['scope.userInfo']) {
- // wx.getUserInfo({
- // success: function (res) {
- // // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
- // // 根据自己的需求有其他操作再补充
- // // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
- // console.log(res);
- // that.setData({
- // hasUserInfo: true,
- // userInfo:res.userInfo
- // });
- // wx.login({
- // success: function (res) {
- // // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
- // // 根据自己的需求有其他操作再补充
- // // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
- // wx.request({
- // url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
- // data: {
- // code: res.code
- // },
- // header: {
- // 'content-type': 'application/json' // 数据格式(默认值)
- // },
- // method: 'post', //上传方式
- // success: function (res) {
- // console.log("openid" + res.data.openid);
- // // wx.setStorageSync('token', res.data.token)//消息模板会用到
- // wx.setStorageSync('openid', res.data.openid);//存储openid
- // }
- // });
- // }
- // });
- // }
- // });
- // } else {
- // // 用户没有授权
- // // 改变 isHide 的值,显示授权页面
- // that.setData({
- // hasUserInfo: false
- // });
- // }
- // }
- // });
- // // 如果获取到用户信息就存储
- // if (app.globalData.userInfo) {
- // console.log('用户信息');
- // this.setData({
- // userInfo: app.globalData.userInfo,
- // hasUserInfo: true
- // })
- // wx.setStorageSync('username', that.data.userInfo.nickName)
- // wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
- // console.log("在index页面全局app1中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
- // } else if (this.data.canIUse) {
- // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // // 所以此处加入 callback 以防止这种情况
- // app.userInfoReadyCallback = res => {
- // // console.log("用户名2:" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
- // // wx.setStorageSync('username', res.userInfo.nickName)
- // // wx.setStorageSync('headpath', res.userInfo.avatarUrl)
- // this.setData({
- // userInfo: res.userInfo,
- // hasUserInfo: true
- // })
- // wx.setStorageSync('username', that.data.userInfo.nickName)
- // wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
- // console.log("在index页面全局app2中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
- // }
- // }
- },
- //点击按钮授权
- getUserInfo: function (e) {
- var that = this;
-
- var openid = wx.getStorageSync("openid");
-
- if (e.detail.userInfo) {
- if (!openid) {
- wx.login({
- success: function (res) {
- // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
- // 根据自己的需求有其他操作再补充
- // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
-
- wx.request({
- url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
- data: {
- code: res.code
- },
- header: {
- 'content-type': 'application/json' // 数据格式(默认值)
- },
- method: 'post', //上传方式
- success: function (res) {
- console.log("openid" + res.data.openid);
- // wx.setStorageSync('token', res.data.token)//消息模板会用到
- wx.setStorageSync('openid', res.data.openid);//存储openid
- }
- });
- }
- });
- }
- app.globalData.userInfo = e.detail.userInfo
- console.log(app.globalData.userInfo);
- this.setData({
- userInfo: e.detail.userInfo,
- hasUserInfo: true
- })
- wx.setStorageSync('username', that.data.userInfo.nickName)
- wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
- console.log("在index页面临时授权中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
- } else {
- wx.showToast({
- title: '未授权',
- icon: 'none',
- })
- }
- },
- addgong: function (e) {
- if (this.data.hasUserInfo != true) {
- wx.showToast({
- title: '请点击获取头像',
- icon: 'none',
- })
- return;
- }
- wx.navigateTo({
- url: '../../pages/addgong/addgong'
- })
- },
- //弹出回复框
- showModal: function (event) {
- if (this.data.hasUserInfo != true){
- wx.showToast({
- title: '请点击获取头像',
- icon: 'none',
- })
- return;
- }
- this.setData({
- modalHidden: !this.data.modalHidden,
- })
- },
- // 获取弹出框密码
- getInputContent: function (e) {
- console.log(e.detail.value)
- this.setData({
- inputContent: e.detail.value
- })
- },
- //确定
- modalBindaconfirm: function () {
- var that = this;
- console.log(that.data.inputContent)
- if (that.data.inputContent == "") {
- wx.showToast({
- title: '秘钥不能为空!',
- icon: 'none',
- })
- } else {
- //通过秘钥换取公众号 id
- that.getIdentifyId();
- }
- },
- //取消
- modalBindcancel: function () {
- this.setData({
- modalHidden: !this.data.modalHidden,
- })
- },
- //通过后台秘钥获取公众号 id
- getIdentifyId: function () {
- var that = this;
- wx.request({
- url: app.globalData.apiUrl + '/index/index/identify', //获取公众号信息
- data: {
- password: that.data.inputContent, //登录后台密码
- openid: wx.getStorageSync('openid')
- },
- header: {
- 'content-type': 'application/json' // 数据格式(默认值)
- },
- method: 'post', //上传方式
- success: function (res) { //回调成功
- console.log(res.data)
- if (res.statusCode == 200) {
- if (res.data.result == '1') {
- //弹出框消失
- that.setData({
- modalHidden: !that.data.modalHidden,
- buttonDisabled: !that.data.buttonDisabled,
- id: res.data.content[0].id,
- })
- //上传openid
- // that.postOpenid();
- //跳转后台管理界面
- wx.navigateTo({
- url: '../../pages/select/select?id=' + that.data.id + "&avatarUrl=" + that.data.userInfo.avatarUrl + "&nickName=" + that.data.userInfo.nickName + "&headpath=" + res.data.content[0].headpath + "&name=" + res.data.content[0].name + "&describe=" + res.data.content[0].describes
- })
- } else {
- wx.showToast({
- title: '你不是管理员或者密码错误',
- icon: 'none',
- })
- }
- } else {
- wx.showModal({
- title: '服务器错误',
- content: 'none',
- })
- }
- },
- //回调失败
- fail: function (res) {
- console.log(res.errMsg)
- wx.showToast({
- title: '联网失败 ',
- icon: 'fail',
- })
- },
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|