var utilMd5 = require('./md5.js'); // const tx_key = 'XSWBZ-TBVWD-QJ54G-HGYZW-5AWQK-M2FYS' const tx_key = 'H3TBZ-Y5VW5-RQEIS-QZPN2-7DLA5-4OBMG'; // 腾讯地图key const tx_secret_key = ''; // key对应的签名 // 登录 function login() { let promise = new Promise((resolve, reject) => { wx.login({ success: res => { console.log(res) // 发送 res.code 到后台换取 openId, sessionKey, unionId wx.request({ url: 'https://t5.9026.com/api/v1/common/wxLogin', data: { wechat_code: res.code }, method: "POST", header: { 'content-type': 'application/json' }, success: function(res) { resolve(res) }, fail: function(e) { reject(e) } }); }, fail: res => { reject('登录失败') } }) }) return promise; } // 获取用户信息 function getUserInfo() { let promise = new Promise((resolve, reject) => { getAuthSetting('scope.userInfo', false, '用户信息').then(res => { if (res) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId resolve('获取用户信息成功', res.userInfo) } }) } else { resolve(false) } }).catch(res => { reject(res) }) }) return promise; } // 打开授权设置界面 function openSetting(scope, isMust, name) { // scope是权限字段,isMust表示是否必须授权, name是授权信息名,用于模态框信息展示 let promise = new Promise((resolve, reject) => { wx.showModal({ title: '授权提示', content: '请先授权获取' + name, success(res) { if (res.confirm) { wx.openSetting({ success(res) { if (res.authSetting[scope]) { // 用户打开了授权开关 resolve(true) } else { // 用户没有打开授权开关, 继续打开设置页面 if (isMust) { openSetting(scope, isMust, name) } else { reject('获取用户授权信息失败') } } }, fail(res) {} }) } else if (res.cancel) { if (isMust) { openSetting(scope, isMust, name) } else { reject('获取用户授权信息失败') } } } }) }) return promise; } function getAuthSetting(scope, isMust, name) { // 获取用户授权信息,scope是授权的权限字段名, isMust表示是否必须授权才能进行下一步 let promise = new Promise((resolve, reject) => { wx.getSetting({ success: res => { if (res.authSetting[scope]) { resolve(true) // 用户已经授权 } else { // 用户还没有授权,向用户发起授权请求 wx.authorize({ scope: scope, success() { // 用户同意授权 resolve(true) }, fail() { // 用户不同意授权 if (isMust) { // 如果是必须授权才能进行下一步的接口,则打开设置页面 openSetting(scope, isMust, name).then(res => { resolve(true) }).catch(res => { resolve(false) }) } else { // 如果不是必须授权的接口,则直接返回未授权状态 resolve(false) } } }) } }, fail: res => { reject('获取用户授权信息失败') } }) }) return promise; } function getLocation() { // 获取地理位置信息 let promise = new Promise((resolve, reject) => { getAuthSetting('scope.userLocation', true, '地理位置信息').then(res => { // 获取用户信息,must必须授权 if (res) { // 已经授权,可以直接获取地理位置 let _this = this wx.getLocation({ type: 'wgs84', success(res) { console.log(res) let locationData = { // 新建一个对象用于保存经纬度等信息 latitude: res.latitude, // 纬度 longitude: res.longitude // 经度 } let sig = utilMd5.hexMD5("/ws/geocoder/v1?key=" + tx_key + "&location=" + res.latitude + "," + res.longitude + tx_secret_key) let getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1?key=" + tx_key + "&location=" + res.latitude + "," + res.longitude + "&sig=" + sig; wx.request({ // 请求腾讯地图接口获取地理位置 url: getAddressUrl, success: function(res) { if (res.data.status == 0) { // 获取地理位置成功 locationData.address = res.data.result resolve(locationData) } else { // 获取地理位置信息失败 reject('获取地理位置信息失败') } } }) }, fail(res) { reject('获取地理位置信息失败') } }) } else { } }).catch(res => { reject(res) }) }) return promise; } module.exports = { login: login, // 登录 + 获取openID getUserInfo: getUserInfo, // 获取用户信息 getAuthSetting: getAuthSetting, // 获取授权信息, 可选择是否必须授权 getLocation: getLocation // 获取经纬度 + 经纬度通过腾讯地图签名校验的方式换取城市位置 }