代码展示
js文件(需要加上自己的appid和secret)
const app = getApp()
// pages/test/test.js
Page({
/**
* 页面的初始数据
*/
data: {
access_token: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var _this = this;
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/token',
data: {
grant_type: 'client_credential',
appid: '需要加上自己的appid',
secret: '需要加上自己的secret'
},
method: 'get',
success: function (res) {
_this.setData({
access_token: res.data.access_token
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
formSubmit:function(e){
//需要在真机上进行
var _this = this;
console.log(e.detail.formId, 'formid');
console.log(_this.data.access_token, 'access_token')
var openId = app.globalData.token1;
console.log(app.globalData.token1)
var access_token = _this.data.access_token;
var formId = e.detail.formId;
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + access_token,
data: {
"touser": openId,
"template_id": 'OqMfyBicxpNpuFFlNOII6o1ycQhjcON-oYH_g2MZbcM',
"form_id": formId,
"data": {
"k eyword1": {
"value": "339208499",
"color": "#173177"
},
"keyword2": {
"value": "2018年3月26日",
"color": "#173177"
},
"keyword3": {
"value": "1000元",
"color": "#173177"
},
"keyword4": {
"value": "15999999999",
"color": "#173177"
}
},
"emphasis_keyword": "keyword3.DATA"
},
method: 'post',
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log(res.data)
}
})
}
})
app.js(需要加上自己的appid和secret)
//app.js
App({
onLaunch: function () {
// 小程序登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
let code = res.code;
console.log('微信请求的code='+code);
var that = this;
that.globalData.code = code;
console.log('code===='+code);
//请求后台获取唯一标识
var appid = '自己的appid'; //填写微信小程序appid
var secret = '自己的secret'; //填写微信小程序secret
//调用request请求api转换登录凭证
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&grant_type=authorization_code&js_code=' + code,
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res);
console.log('openId+++++:'+res.data.openid) //获取openid
that.globalData.token1 = res.data.openid;
}
})
}
})
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
//全局数据
globalData: {
dataInfo:null,//用户信息(服务器获取的)
userInfo:null,// 用户信息(微信返回的)
code:null, //code
token:null,//用户唯一标识
version: '1.0.0',//版本号
// host:'http://139.224.117.244:8092/shiban-api/', //主机地址
host:'https://apitest.51shiban.com/',
// host: 'http://127.0.0.1:8081/shiban-api/', //主机地址
latitude:0,//纬度,暂时没有用
longitude:0//经度
}
wxml文件
提示:需要真机才行哦,而且这只是测试用,实际需要吧请求逻辑交给服务器处理