123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- // pages/user/user.js
- const app = getApp()
- var host = app.globalData.host;
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- usercode:'',
- openid:'',
- info:{},
- userInfo:{}, //登陆成功的信息
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.getStorage({
- key: 'userInfo',
- success:res=>{
- if (res.data){
- this.setData({
- userInfo: res.data
- })
- this.getUserData();
- }
- }
- })
- },
- getUserData(){
- wx.request({
- url: host+'/api/wx/authinfo',
- header:{
- 'Authorization':this.data.userInfo.token
- },
- success:res=>{
- this.setData({
- usercode: res.data.data.usercode,
- userinfo: res.data.data
- })
- },
- fail:error=>{
- //失败重新登陆
- this.setData({
- userInfo: {}
- })
- }
- })
- },
- inputchange(e){
- this.setData({
- usercode:e.detail.value
- })
- },
- fundchange(e){
- this.setData({
- init_fund: e.detail.value
- })
- },
- logout(){
- wx.removeStorage({
- key: 'userInfo'
- })
- this.setData({
- userInfo:{},
- usercode:''
- })
- },
- /**登陆 */
- login:function(){
- var _this=this;
- if(!this.data.usercode){
- wx.showToast({
- icon:'none',
- title: '请输入用户代码',
- })
- return;
- }
-
- wx.getUserProfile({
- desc: '用于完善会员资料',
- success(res){
- //获取基本信息
- var data={
- nickName: res.userInfo.nickName,
- avatarUrl: res.userInfo.avatarUrl,
- usercode: _this.data.usercode
- }
- wx.showLoading({
- title: '正在登陆',
- })
- wx.login({
- success(res) {
- //获取code
- wx.request({
- url: host + '/api/wx/openid',
- data: {
- code: res.code
- },
- success(res) {
- //获取openid
- if(res.data.code != 0){
- wx.showToast({
- icon: 'none',
- title: res.data.message,
- })
- wx.hideLoading();
- return;
- }
- data.openid=res.data.data.openid
- wx.request({
- url: host + '/api/wx/login',
- method:'POST',
- data: data,
- success(res){
- //登陆成功
- console.log(res)
- wx.hideLoading();
- if(res.data.code != 0){
- wx.showToast({
- icon: 'none',
- title: res.data.message,
- })
- return;
- }
-
- _this.setData({
- userInfo : res.data.data
- })
- wx.setStorage({
- key: 'userInfo',
- data: res.data.data,
- })
- _this.getUserData()
- },
- fail(error){
- console.log(error)
- wx.hideLoading();
- }
- })
- },
- fail() {
- wx.hideLoading();
- }
- })
- }
- })
- }
- })
-
- },
- save(){
- if (!this.data.init_fund) {
- wx.showToast({
- icon: 'none',
- title: '请输入初始资金',
- })
- return;
- }
- wx.request({
- url: host + '/api/wx/player/fund',
- data: {
- init_fund: this.data.init_fund
- },
- header: {
- 'Authorization': this.data.userInfo.token
- },
- method: 'PUT',
- success: res => {
- wx.showToast({
- title: '提交成功',
- })
- this.getUserData()
- },
- fail: error => {
- }
- })
-
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- // this.getUserData()
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|