小程序常用方法集合

1.登录

调用微信wx.login接口,会返回唯一的code,然后根据code去请求后端登录接口,换openid(唯一的)和其他的相应的字段信息(是否绑定,id 等等),获取成功后,最好把这些都存到全局变量里,其他页面请求时直接使用

注意:其他页面使用的时候,不能在data里直接设置global里的内容,要在onload中重新setData  

错误使用: data{id:globalData.id}

正确使用: 先在data 中设置id,然后在onload 中setData{id:globalData.id}

 

2.注册通用逻辑

 a.获取验证码

//收取验证码
getCodeTap: function () {
//判断手机格式
let mobile = this.data.mobile_input;
let reg = /^1[3|4|5|7|8|9][0-9]{9}$/;
let flag = reg.test(mobile);
let that = this;
if (!flag) {
wx.showToast({
title: '手机号码有误',
icon: 'loading',
duration: 1000,
mask: true
})
} else {
// 将获取验证码按钮隐藏60s,60s后再次显示
that.setData({
is_show: true, //false
code_mobile: this.data.mobile_input
})
settime(that);
// 发送验证码
wx.request({
url: domain + '/api/code.php',
data: {
mobile: this.data.mobile_input
},
method: 'POST',
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log('获取验证码成功',res.data)
if (res.data.status) {
that.setData({
code: res.data.data
})
console.log(that.data.code)
}

},
fail:function(res){
console.log('获取验证码失败',res.data)

}
 
})
}
},
 
b.设置60s定时器
var settime = function (that) {
if (countdown == 0) {
that.setData({
is_show: true
})
countdown = 60;
return;
} else {
that.setData({
is_show: false,
last_time: countdown
})

countdown--;
}
setTimeout(function () {
settime(that)
}
, 1000)
}
 
c.注册提交
如果需要在提交的时候同时获取授权信息,就要把提交方法绑定在button的bindGetUserInfo中,先判断各种验证逻辑,然后判断是否授权,最后提交
bindGetUserInfo: function (e) {
console.log(e.detail.userInfo)
 
let nickName = app.globalData.base_info.nick_name;
let avatarUrl = app.globalData.base_info.avatarUrl;

let that = this;
if (this.data.real_name == undefined || this.data.real_name.length == 0) {
wx.showToast({
title: '请填写姓名',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!this.data.age) {
wx.showToast({
title: '请输入年龄',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!this.data.mobile || this.data.mobile !== this.data.code_mobile) {
wx.showToast({
title: '手机号有误',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!this.data.code_input || this.data.code_input != this.data.code) {
wx.showToast({
title: '验证码有误',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!that.data.work_units) {
wx.showToast({
title: '请输入所在医院',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!that.data.work_year) {
wx.showToast({
title: '请输入工作年限',
icon: 'loading',
duration: 1000,
mask: true
})

} else if (!that.data.imageList || that.data.imageList.length != 2) {
wx.showToast({
title: '请上传从医资格证',
icon: 'loading',
duration: 1000,
mask: true
})
} else if (!e.detail.userInfo) {
nickName = app.globalData.base_info.nick_name;
avatarUrl = app.globalData.base_info.avatarUrl;
 
that.setData({
nick_name: nickName,
avatarUrl: avatarUrl
})

wx.request({
url: domain + '/api/bind.php?a=bind',
data: {
real_name: that.data.real_name,
age: that.data.age,
mobile: that.data.mobile_input,
sex: that.data.sex,
province: that.data.region[0],
city: that.data.region[1],
area: that.data.region[2],
work_units: that.data.work_units,
work_year: that.data.work_year,
certificate_img: that.data.imageList,
openid:this.data.openid,
experts_id: this.data.experts_id,
head_img: that.data.avatarUrl
},
method: 'POST',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('注册成功返回的数据', res.data);
if (res.data.status == true) {
wx.showToast({
title: '注册成功',
icon: 'loading',
duration: 5000,
mask: true
});

console.log('授权头像信息', that.data.nick_name, that.data.avatarUrl)

//存全局变量
app.globalData.studentInfo.student_id = res.data.data.student_id;
app.globalData.base_info.nick_name = that.data.nick_name;
app.globalData.base_info.avatarUrl = that.data.avatarUrl;
app.globalData.is_bind = res.data.data.is_bind;
console.log(app.globalData)
wx.redirectTo({
url: 'userinfo/userinfo',
});



} else if (res.data.status == false) {
wx.showToast({
title: '手机号码已存在,请更换手机号码重新提交!',
icon: 'loading',
duration: 2000,
mask: true

});
}
},
fail: function (res) {
console.log('注册失败' + res.data)

}
})

} else {

nickName = e.detail.userInfo.nickName;
avatarUrl = e.detail.userInfo.avatarUrl;

that.setData({
nick_name: nickName,
avatarUrl: avatarUrl
})

console.log('授权头像信息', that.data.nick_name, that.data.avatarUrl)

wx.request({
url: domain + '/api/bind.php?a=bind',
data: {
real_name: that.data.real_name,
age: that.data.age,
mobile: that.data.mobile_input,
sex: that.data.sex,
province: that.data.region[0],
city: that.data.region[1],
area: that.data.region[2],
work_units: that.data.work_units,
work_year: that.data.work_year,
certificate_img: that.data.imageList,
openid: this.data.openid,
experts_id: this.data.experts_id,
head_img: that.data.avatarUrl

},
method: 'POST',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('注册成功返回的数据', res.data);
if (res.data.status == true) {
wx.showToast({
title: '注册成功',
icon: 'loading',
duration: 5000,
mask: true
});

 

//存全局变量
app.globalData.studentInfo.student_id = res.data.data.student_id;
app.globalData.base_info.nick_name = that.data.nick_name;
app.globalData.base_info.avatarUrl = that.data.avatarUrl;
app.globalData.is_bind = res.data.data.is_bind;
console.log(app.globalData)

wx.redirectTo({
url: 'userinfo',
});


} else if (res.data.status == false) {
wx.showToast({
title: '手机号码已存在,请更换手机号码重新提交!',
icon: 'loading',
duration: 2000,
mask: true

});
}
},
fail: function (res) {
console.log('注册失败' + res.data)

}
})
}

}
 
3.多组图片上传
五个数据控制一组
data{
X_ray: {
num: 0,
img_10: true,
img_num: 0,
general_img: [],
len: 5
},
}
 
//图片删除
delete_img: function (e) {
var num = e.target.dataset.index;
var name = e.target.dataset.name;
console.log(num, name)
var img = this.data[name].general_img;
img.splice(num, 1);
var img_num = this.data[name].img_num - 1;
this.data[name].img_num = img_num;
this.data[name].general_img = img;

this.setData({
[name]: this.data[name]
});
if (this.data[name].img_10 == false) {
this.data[name].img_10 = true;
this.setData({
[name]: this.data[name]
});
}
},
//图片预览
previewimg: function (e) {
var current = e.target.dataset.src;
var name = e.target.dataset.name;
wx.previewImage({
current: current,
urls: this.data[name].general_img
})
},
//图片上传
uploadimg: function (e) {
//console.log(e.currentTarget.dataset.name)
let that = this;
let cc = e.currentTarget.dataset.name;
console.log(that.data[cc].num)
let num = (that.data[cc].len - that.data[cc].img_num);
console.log(num)
wx.chooseImage({
count: num, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths;
that.data[cc].num = 0
that.setData({
[cc]: that.data[cc]
})

wx.showLoading({
title: '正在上传图片',
})
//设置遮罩
that.setData({
mask: true
});
console.log(tempFilePaths)
that.upload(tempFilePaths, cc);

}
})
},
//文件上传
upload: function (tempFilePaths, cc) {
let that = this;
var images = tempFilePaths;
if (images.length > that.data[cc].len) {
return false;
}
wx.uploadFile({
url: domain + '/api/image.php?a=upload',
filePath: tempFilePaths[that.data[cc].num],
name: 'file',
success: function (res) {
console.log(res)

that.data[cc].num++
console.log(that.data[cc])
that.setData({
[cc]: that.data[cc]
})
console.log(that.data.user_photos)

var data = JSON.parse(res.data)
console.log(data)
if (data.status == true) {
var filename = data.data;
var img_num = that.data[cc].img_num;
console.log(filename, img_num)
img_num++;
if (img_num == that.data[cc].len) {
that.data[cc].img_10 = false;
that.setData({
[cc]: that.data[cc]
});
}
that.data[cc].img_num++;
that.data[cc].general_img = that.data[cc].general_img.concat(domain + '/api' + filename)

that.setData({
[cc]: that.data[cc]
})

console.log(cc, that.data[cc])


if (that.data[cc].num < tempFilePaths.length) {//若图片还没有传完,则继续调用函数
that.upload(tempFilePaths, cc)
} else {

wx.hideLoading();
//设置取消遮罩
that.setData({
mask: false
});
}
}
},
fail: function (res) {
console.log('图片上传失败')
}
})
},
 
 
4.多组单选pick逻辑
给每组pick设置name,等于要保存的那个值,
<picker bindchange="teethTapOne" value="{{teethSeven}}" data-name='teethSeven' range="{{teethSevenArray}}">
<view class="picker-in">
<text>磨牙关系(右侧)</text>
<view class="section-right">
<text>{{teethSevenArray[teethSeven]}}</text>
<image src="/images/icon_1.png"></image>
</view>
</view>
</picker>
 
teethTapOne: function (e) {
let that=this;
console.log(e.currentTarget.dataset.name)
console.log('picker发送选择改变,携带值为', e.detail.value)
this.setData({
[e.currentTarget.dataset.name]: e.detail.value
})
console.log(that.data.teethOne,that.data.teethTwo)
},
 
 
6.单选多选按钮逻辑
 
 7.时间戳转换,ios有兼容问题,统一用下面的
let date = util.formatTime(new Date()).split(/[- : \/]/);
console.log(date)
let date_s = new Date(date[0], date[1] - 1, date[2], date[3], date[4], date[5])//当前的时间戳
console.log(date_s.getTime());
 
 
 

posted on 2018-06-19 10:15  cytheria  阅读(551)  评论(0编辑  收藏  举报