import { $wuxToast } from '../../dist/index' import { $wuxSelect } from '../../dist/index'; const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), openid: '', message: '', tel: '', name: '', upwd: '', id:'', utype:'', title2: '', value2: '', dlist: [], cids:[] }, //事件处理函数 bindViewTap: function() { wx.navigateTo({ url: '../logs/logs' }) }, onLoad: function () { var that = this console.log("孙"); console.log("-"+app.globalData.quanju); if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse){ // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } // 获取openid wx.login({ success: function (res) { if (res.code) { wx.getUserInfo({ success: function (res) { console.log("存在code") } }); var appid = "wxc55158faad3e366a" var secret = "f38f654411e153673ff4a473f1ccefd0" var openid = "" var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code'; wx.request({ url: l, data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT // header: {}, // 设置请求的 header success: function (res) { var obj = {}; obj.openid = res.data.openid; console.log("取得的openid==" + res.data.openid); that.setData({ openid: obj.openid }) obj.expires_in = Date.now() + res.data.expires_in; // console.log(obj); // wx.setStorageSync('user', obj);//存储openid } }); } else { console.log('获取用户登录态失败!' + res.errMsg) } } }); //获取登录用户的code // wx.login({ // success(res){ // if(res.code){ // console.log("当前code存在") // //发送code // wx.request({ // url: 'http://localhost:8080/sendOpenid', // data:{ // code:res.code // } // }) // } // else{ // console.log('登录失败!' + res.errMsg) // } // } // }) }, getUserInfo: function(e) { console.log(e) console.log("---"); app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, onChange(e) { let that = this; let id = e.currentTarget.id; let values = e.detail.value; if (id == 'tel') { that.setData({ tel: values }); } if (id == 'name') { that.setData({ name: values }); } if (id == 'upwd') { that.setData({ upwd: values }); } }, saveUser: function () { let tel = this.data.tel; if (tel == null || tel == '') { this.showError('请输入电话'); return; } let upwd = this.data.upwd; if (upwd == null || upwd == '') { this.showError('请输入密码'); return; } let name = this.data.name; if (name == null || name == '') { this.showError('请输入姓名'); return; } let value2 = this.data.value2; if (value2 == null || value2 == '') { wx.showModal({ title: '提示', content: '请选择个人喜好', showCancel: false }) return } let utype = this.data.utype; let id = app.globalData.openid; wx.request({ url: app.data.resturl + '/updateuser.action', method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { name: name, tel: tel, upwd: upwd, utype: value2, id: id }, success: function (res) { let code = res.data.code; if (code == 200) { wx.navigateBack({ }) } } }) }, onClick2() { console.log(this.data.value2); $wuxSelect('#wux-select2').open({ multiple: true, value: this.data.cids, options: this.data.dlist, onChange: (value, index, options) => { console.log('onChange', value, index, options) this.setData({ value2: value, title2: index.map((n) => options[n].title), }) }, onConfirm: (value, index, options) => { console.log('onConfirm', value, index, options) this.setData({ value2: value, title2: index.map((n) => options[n].title), }) }, }) }, showError: function (info) { $wuxToast().show({ type: 'text', duration: 1500, color: '#fff', text: info }) }, onShow: function () { var that = this; this.getUserinfo(); this.clist(); }, getUserinfo() { var that = this; let openid = app.globalData.openid; console.log("openid=" + openid); if (openid == '') { wx.redirectTo({ url: '../../pages/login/login', }) } wx.request({ url: app.data.resturl + '/uinfo.action', method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { uid: openid }, success: function (res) { let uinfo = res.data.uinfo; that.setData({ upwd: uinfo.upwd, name:uinfo.name, tel:uinfo.tel, utype: uinfo.utype, title2: uinfo.cname, value2: uinfo.utype, cids:uinfo.cids }); } }) }, clist: function () { let that = this; wx.request({ url: app.data.resturl + '/cateloglist.action', data: { }, success: function (res) { console.log(res.data.dlist); that.setData({ dlist: res.data.dlist }) } }) } })