Skip to content

Instantly share code, notes, and snippets.

@xiadd
Last active February 13, 2017 02:07
Show Gist options
  • Save xiadd/d10ca3cbcde53227a2ed01245af6f6c4 to your computer and use it in GitHub Desktop.
Save xiadd/d10ca3cbcde53227a2ed01245af6f6c4 to your computer and use it in GitHub Desktop.
小程序示例
//app.js
App({
onLaunch: function () {
//获取当前位置,配合地图sdk使用
wx.getLocation({
type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success: function(res){
wx.request({
url: 'https://dev.xiadd.me/api/user/location',
data: res,
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
header: { 'x-access-token': wx.getStorageSync('token') },
success: function(result){
}
})
}
})
//微信登录
wx.login({
success: function(res){
if (res.code) {
wx.getUserInfo({
success: function(_res){
wx.request({
url: 'http://dev.xiadd.me/api/user/wxlogin',
data: {
code: res.code,
encryptedData: _res.encryptedData,
iv: _res.iv
},
method: 'POST',
success: function(__res){
// success
//wx.setStorageSync('token', __res.data.data.jwtToken)
}
})
}
})
}
}
})
//请求支付
wx.request({
url: 'https://dev.xiadd.me/api/general_order',
data: {orderId: '587c54b7g0611xs4xx4ff2224'},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: { 'x-access-token': wx.getStorageSync('token') }, // 设置请求的 header
success: function(res){
const data = res.data.data
if (typeof data !== 'object') {
wx.showModal({
title: '错误',
content: data
})
return
}
wx.requestPayment({
timeStamp: data.timeStamp,
nonceStr: data.nonceStr,
package: data.package,
signType: 'MD5',
paySign: data.paySign,
success: function(res){
wx.request({
url: 'https://dev.xiadd.me/api/tmpmsg',
data: {
prepay_id: data.package
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {'x-access-token': wx.getStorageSync('token')}, // 设置请求的 header
success: function(res){
// success
}
})
}
})
}
})
},
getUserInfo:function(cb){
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
//调用登录接口
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
})
}
},
globalData:{
userInfo:null
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment