小程序
小游戏
企业微信
微信支付
扫描小程序码分享
5 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
把success:function(res)改成success:(res)=>即可
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
代码全都发出来
this 指向不对了
看你问题解决了,给你个提示。新手常见的this指向问题
多了解一下箭头函数
==========推荐在页面方法的最开始赋值let t = this============== func:function(){ let t = this ..... wx.request({ ... success: function(res)=>{ t.setData({...}) } }) } ==========或者============== func:function(){ wx.request({ ... success: (res)=>{ this.setData({...}) } }) } ==========或者============== func:function(){ wx.request({ ... success: function(res)=>{ this.setData({...}) }.bind(this) }) }
若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
把success:function(res)改成success:(res)=>即可
代码全都发出来
var Bmob = require("../../utils/bmob.js");
var util = require("../../utils/util.js");
var that;
//var open_id='';//储存获取到openid
const app = getApp()
Page({
data: {
id:'',
url:'',
name:'',
showAddr: false,
showAddAddr: true,
totalMoney: 0,
goodMoney:0,
price:0,
showCoupon:false,
fare: 0.00,
allOrder: [],
couponid:"",
nickName:"",
useCoupon:false,
open_id:"",
userInfo:[]
},
getUserInfo: function(e) {
console.log("###222###"+e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
nickName:e.detail.userInfo.nickName,
hasUserInfo: true
})
},
onShow() {
//获取微信信息开始
wx.getUserInfo({
lang:"zh_CN",
success: res => {
console.log("#####微信授权res:"+res.userInfo.nickName);
this.setData({
userInfo: res.userInfo,
nickName:res.userInfo.nickName
})
},
fail: function () {
console.log("微信未授权");
}
})
},
setCoupon(e) {
let id = e.currentTarget.dataset.id;
let price = e.currentTarget.dataset.price;
this.setData({
price: price,
showCoupon: false,
useCoupon:true,
coupon: {
id: id,
price: price
}
})
},
getCoupon(){
if(this.data.allOrder.length !=0){
this.setData({
showCoupon:true
})
}
},
getAddress() {
wx.chooseAddress({
success: (res) => {
this.setData({
showAddAddr: false,
showAddr: true,
name: res.userName,
addrdetail: res.provinceName + res.cityName + res.countyName + res.detailInfo,
tel: res.telNumber
})
let User = Bmob.Object.extend("_User");
let query = new Bmob.Query(User);
let currentUser = Bmob.User.current();
let objectid = currentUser.id;
query.get(objectid, {
success: (result) => {
result.set('name', res.userName);
result.set('tel', res.telNumber);
result.set('addrdetail', res.provinceName + res.cityName + res.countyName + res.detailInfo);
result.set('mailcode', res.nationalCode);
result.save(null, {
success: (result) => {},
error: (result, error) => {
console.log('地址创建失败');
}
});
},
error: (object, error) => {
console.log(object)
},
});
},
})
},
onLoad(options) {
that = this;
this.setData({
id: options.id,
url: options.url,
name: options.name,
price: options.price,
m_desc:options.m_desc
})
},
//开始
addOrder: function(){
var that = this;
if(that.data.userInfo.nickName == null){
wx.showModal({
title: '提示',
content: '用户未授权,请点击授权!',
})
}else{
this.getOpenIdTap();
console.log("昵称:"+that.data.userInfo.nickName);
console.log("商品id:"+that.data.id);
console.log("商品名称:"+that.data.name);
console.log("open_id:"+that.data.open_id);
}
},
//结束
getOpenIdTap:function(){
var that=this;
wx.login({
success:(res)=>{
console.log("#####输出:"+res.code)
wx.request({
//获取openid接口
url: 'https://*****/***/sc/***.do',
data:{
js_code:res.code
},
method:'GET',
success:(res)=> {
console.log("输出open_id###"+res.data.open_id),
this.setData({
open_id:res.data.open_id,//获取到的openid
})
}
})
}
})
},
});
this 指向不对了
看你问题解决了,给你个提示。新手常见的this指向问题
多了解一下箭头函数
==========推荐在页面方法的最开始赋值let t = this============== func:function(){ let t = this ..... wx.request({ ... success: function(res)=>{ t.setData({...}) } }) } ==========或者============== func:function(){ wx.request({ ... success: (res)=>{ this.setData({...}) } }) } ==========或者============== func:function(){ wx.request({ ... success: function(res)=>{ this.setData({...}) }.bind(this) }) }
若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人