收藏
回答

this.setdata无效? 设置imgurl结果打印还是为0

var app = getApp();


Page({


  /**
   * 页面的初始数据
   */
  data: {
    imgurl:'0',

    userInfo: {

      coupu: 0,
      money: 0.00
    },
    islogin: false,
    loginuserinfo: [],
    count_list: {
      user_fav_count: 0,
      fav_count: 0,
      footprint_count: 0,
    },
    user: {
      money: 0.00
    },
    code: '',
    avatarUrl: '',
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    console.log(menuButtonObject)
    wx.getSystemInfo({
      success: (res) => {
        console.log(res)
        // console.log(res.windowHeight*2)
        let statusBarHeight = res.statusBarHeight;
        const navTop = menuButtonObject.top; //胶囊按钮与顶部的距离
        const navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2//导航高度
        let height = res.windowHeight * 2 - navHeight * 2 - 40 - 315 - 40;
        const titleBarHeight = (menuButtonObject.bottom + menuButtonObject.top) - (res.statusBarHeight * 2)
        console.log(titleBarHeight)
        // console.log(navHeight)
        // console.log(navTop)
        // console.log(height)
        // console.log(res.statusBarHeight)
        this.setData({
          statusBarHeight: statusBarHeight,
          // navHeight: navHeight,
          navHeight: navHeight * 2,
          navTop: navTop * 2,
          height
        })
        this.setData({}, () => {
          // console.log(30+315+40+34+30+176)
        })
      }
    })


    if (wx.getStorageSync("userinfo")) {
      console.log('用户信息', wx.getStorageSync("userinfo"))
      this.setData({
        loginuserinfo: wx.getStorageSync("userinfo"),
        islogin: true
      })
    }
    // this.getconfig();
  },
  link(e) {
    // console.log("跑腿状态",this.data.userInfo.runerStatus.status)
    console.log(this.data.userInfo.runerStatus)
    if (this.data.userInfo.runerStatus == 0 || this.data.userInfo.runerStatus == 3) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }


  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    console.log(avatarUrl)
    this.setData({
      avatarUrl,
    })
  },


  formSubmit(e) {
    this.setData({
      modalName: null,
      islogin: true,
      nickName: e.detail.value.nickname,
      getUseInfo: true
    })
    console.log("准备上传头像",this.data.avatarUrl)
    wx.uploadFile({
      filePath: this.data.avatarUrl,
      header: {
        'content-type''application/x-www-form-urlencoded'
      },
      name: 'file',
      url: app.util.url() + 'c=entry&a=wxapp&do=ImgPost&m=gc_market',
      success: (result) => {
        console.log("上传图片",result)
        var data = result.data
        data = JSON.parse(data).data
        console.log("上传后图片路径",data) 
       this.setData({
         imgurl:data
       })
      }
     
    })
    console.log("测试",this.data.imgurl)
    var self = this;
    wx.login({
      success: function (data) {
        data.code && app.util.request({
          url: "entry/wxapp/GetUid",
          data: {
            code: data.code
          },
          success: function (data) {
            console.log("用户openid",data.data.data.userinfo.openid)
            self.setData({
              getUseInfo: false,
              openid: data.data.data.userinfo.openid,
              
            });
            
            console.log("打印用户头像路径",this.data.imgurl)
            app.util.request({
              url: "entry/wxapp/UserInfoUpdate",
              data: {
                openid: data.data.data.userinfo.openid,
                nickname: e.detail.value.nickname,
                avatar:this.data.imgurl
              },              
              success: function (data) {
                console.log("保存用户信息进入数据库",data);
              }
            });



            console.log(data);
            console.log("这里的")
            wx.aldPushSendOpenid(data.data.data.openid)
            // setTimeout(function(){
            //  wx.aldPushSubscribeMessage({
            //    eventId: '6010dad790fcd68beee4d642',
            //    success(res) {
            //    // 成功后的回调函数
            //    console.log(res)
            //  },
            //    fail(res, e) {
            //    // 失败后的回调函数
            //    console.log(res)
            //    console.log(e)
            //  }
            // });
            // },3000)
            wx.setStorageSync("openid"data.data.data.openid)
            // wx.getUserProfile({
            //  desc:"用户获取用户信息",
            //   success: function (params) {
            // console.log("扣扣",params)
            // var userInfo = params.userInfo
            wx.setStorageSync("userinfo", )
            self.setData({
              islogin: true
            })


            // },
            // fail(err)
            // {
            //  console.log(err)
            // }
            // })
            return false;
          }
        });
      }
    });


  },


  getPhoneNumber(e) {
    var that = this;
    wx.checkSession({
      success() {
        //session_key 未过期,并且在本生命周期一直有效
      },
      fail() {
        // session_key 已经失效,需要重新执行登录流程
        wx.login({
          success: res => {
            that.setData({
              code: res.code
            })
          }
        })
      },
      complete() {
        //将code,encryptedData,iv传给后台进行解密
        app.util.request({
          url: 'entry/wxapp/getPhone',
          data: {
            code: that.data.code,
            iv: e.detail.iv,
            encryptedData: e.detail.encryptedData,
            openid: wx.getStorageSync('openid')
          },
          success(resu) {


          }
        })
      }
    })


    console.log(e)
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  phone() {
    if (!this.data.user.phone) {
      wx.navigateTo({
        url: '/gc_market/pages/user/phone',
      })
    }
  },


  yue() {
    console.log(this.data.sysparment.is_pay_open);
    if (!this.data.islogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/gc_market/pages/replay/index?type=' + this.data.sysparment.is_pay_open,
      })
    }
  },


  jump(e) {
    if (!this.data.islogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {



  },
  //获取列表
  getUser() {
    const self = this
    app.util.request({
      url: 'entry/wxapp/goodsCount',
      data: {
        openid: wx.getStorageSync('openid'),
      },
      success(res) {
        if (res.data.data.user.phone) {
          wx.setStorageSync('phone', res.data.data.user.phone)
          res.data.data.user.phone = res.data.data.user.phone.substring(03) + '****' + res.data.data.user.phone.substring(7);
        }
        self.setData({
          count_list: res.data.data.count,
          user: res.data.data.user,
        })
        if (res.data.data.logo == '') {
          self.setData({
            logo: '/images/code/c17.png'
          })
        } else {
          self.setData({
            logo: res.data.data.logo
          })
        }
        // self.getUserCoupu()
      }
    })
  },



  //获取用户信息
  bindgetuserinfo(data) {
    console.log("打印"data)
    var t = this;
    wx.getUserProfile({
      desc: "用于获取用户信息",
      success(res) {
        console.log("dd", res)
        "getUserProfile:ok" == res.errMsg ? (t.setData({
          getUseInfo: true
        }), wx.setStorage({
          key: "useInfo",
          data"true",
          userinfo:"true",
        }), t.getGetUid(res.userInfo)) : t.setData({
          getUseInfo: !0
        });
      }
    })


  },
  //获取用户openid 
  getGetUid: function (a) {
    var self = this;
    wx.login({
      success: function (data) {
        data.code && app.util.request({
          url: "entry/wxapp/GetUid",
          data: {
            code: data.code
          },
          success: function (data) {
            self.setData({
              getUseInfo: false
            });
            console.log(data);
            console.log("这里的")
            wx.aldPushSendOpenid(data.data.data.openid)
            // setTimeout(function(){
            //  wx.aldPushSubscribeMessage({
            //    eventId: '6010dad790fcd68beee4d642',
            //    success(res) {
            //    // 成功后的回调函数
            //    console.log(res)
            //  },
            //    fail(res, e) {
            //    // 失败后的回调函数
            //    console.log(res)
            //    console.log(e)
            //  }
            // });
            // },3000)
            wx.setStorageSync("openid"data.data.data.openid)
            // wx.getUserProfile({
            //  desc:"用户获取用户信息",
            //   success: function (params) {
            // console.log("扣扣",params)
            // var userInfo = params.userInfo
            wx.setStorageSync("userinfo", a)
            self.setData({
              loginuserinfo: a,
              islogin: true
            })
            self.toUserInfo(data.data.data.openid, a);
            self.getUser()
            // },
            // fail(err)
            // {
            //  console.log(err)
            // }
            // })
            return false;
          }
        });
      }
    });
  },


  toUserInfo: function (openid, userInfo) {
    console.log(toUserInfo, userInfo)
    var that = this;
    app.util.request({
      url: "entry/wxapp/UserInfoUpdate",
      data: {
        openid: openid,
        nickname: nickName,
        avatar: avatarUrl,
      },
      success: function (data) {
        console.log(data.data);
      }
    });
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('openid')) {
      this.getUser()
    }
    // else{
    this.getconfig();
    // }


    console.log('1221221', wx.getStorageSync('userinfo'))
    this.setData({
      userInfo: wx.getStorageSync('userinfo')
    })
  },


  getconfig() {
    var t = this;
    app.util.request({
      url: 'entry/wxapp/Sysparment',
      success(res) {
        console.log(res.data.data.sys.logo)
        t.setData({
          sysparment: res.data.data.sys
        })
        if (res.data.data.sys.logo == '') {
          t.setData({
            logo: '/images/code/c17.png'
          })
        } else {
          t.setData({
            logo: res.data.data.sys.logo
          })
        }
        // t.msg()
      }
    })
  },


  msg() {
    console.log("弹出框")
    var self = this;
    // wx.showModal({
    //  title:"提示",
    //  content:'允许接收订阅消息',
    //  success(res){
    //    if(res.confirm){
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log("设置", res)
        console.log(res.subscriptionsSetting)
      }
    })
    wx.requestSubscribeMessage({
      tmplIds: [self.data.sysparment.template_id, self.data.sysparment.cancel_template_id], // 此处可填写多个模板 ID,但低版本微信不兼容只能授权一个
      success(res) {
        console.log('已授权接收订阅消息');
        // app.util.request({
        //  url: 'entry/wxapp/sendSubscribeMessage',
        //  data: {
        //      ordersn:'3583202103171811593073'
        //  },
        // })
        // wx.navigateTo({
        // url: '/schoolrun/pages/shop/index?id='+id+'&openid='+openid+'&name='+name+'&status='+status,
        // })
      }
    })
    //    }
    //    else{


    //    }
    //  }
    // })


  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {


  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {


  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {


  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {


  }
})
回答关注问题邀请回答
收藏

3 个回答

  • 大山
    大山
    2023-11-05
    const that = this
    wx.uploadFile({
          filePath: this.data.avatarUrl,
          header: {
            'content-type''application/x-www-form-urlencoded'
          },
          name: 'file',
          url: app.util.url() + 'c=entry&a=wxapp&do=ImgPost&m=gc_market',
          success: (result) => {
            console.log("上传图片",result)
            var data = result.data
            data = JSON.parse(data).data
            console.log("上传后图片路径",data) 
           that.setData({
             imgurl:data
           })
          }
         
        })
    

    上传函数外层重新定义 this 变量。

    2023-11-05
    有用 1
    回复
  • 那一抹笑😃 穿透阳光
    那一抹笑😃 穿透阳光
    2023-11-06

    this 重定向问题,还有就是,你输出,应该是紧接着在赋值之后,也应该在方法里面,而不是方法外面,或者使用异步开发

    2023-11-06
    有用
    回复
  • 微蓝色雪狐
    微蓝色雪狐
    2023-11-05

    使用小程序原生方法,最好是习惯性在方法中的第一行写上 let that= this

    然后加深去理解一下 JS中this

    可搜索一下,网上有很多

    2023-11-05
    有用
    回复
登录 后发表内容