Skip to content

Commit

Permalink
添加工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobinwu committed Apr 26, 2017
1 parent c61cc2d commit 4707a45
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 65 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//app.js
App({
onLaunch: function (options) {

//调用API从本地缓存中获取数据, 待删
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
Expand Down
2 changes: 2 additions & 0 deletions lib/qqmap-wx-jssdk.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion pages/index/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
//index.js
//获取应用实例
var app = getApp()

// 引入SDK核心类
var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');

// 实例化API核心类
var demo = new QQMapWX({
key: 'LHVBZ-DQVWW-C2YRC-REKYI-HRUV7-JPFYV' // 必填
});


Page({
data: {
motto: 'Hello World',
Expand All @@ -21,6 +31,36 @@ Page({
that.setData({
userInfo:userInfo
})
})
});

// // 调用接口
// demo.reverseGeocoder({
// location: {
// latitude: 39.984060,
// longitude: 116.307520
// },
// success: function(res) {
// console.log(res);
// },
// fail: function(res) {
// console.log(res);
// },
// complete: function(res) {
// console.log(res);
// }
// });

wx.request({
url: 'https://www.wzhouhui.com/dj/Location/getLocation', //仅为示例,并非真实的接口地址
method: 'post',
data: {
lng: '116.307520' ,
lat: '39.984060'
},
success: function(res) {
console.log(res)
}
});

}
})
13 changes: 10 additions & 3 deletions pages/logs/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
var util = require('../../utils/util.js')
Page({
data: {
logs: []
logs: [],
logText: []
},
onLoad: function () {
onLoad: function (options) {
console.log(options);
var a = [];
for(var key in options){
a.push(key);
}
this.setData({
logs: (wx.getStorageSync('logs') || []).map(function (log) {
return util.formatTime(new Date(log))
})
}),
logText: a
})
}
})
4 changes: 4 additions & 0 deletions pages/logs/logs.wxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!--logs.wxml-->
<view class="container log-list">
<block wx:for="{{logText}}">
<text>{{item}}</text>
</block>

<block wx:for="{{logs}}" wx:for-item="log" wx:key="*this">
<text class="log-item">{{index + 1}}. {{log}}</text>
</block>
Expand Down
139 changes: 139 additions & 0 deletions utils/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* 地址逻辑处理脚本 */


var util = require('./util.js');
var ports = require('./ports.js');
var region = require('./region.js');
// 引入promise
var Promise = require('../lib/es6-promise.min.js');
// 引入SDK核心类
var QQMapWX = require('../lib/qqmap-wx-jssdk.min.js');
// 实例化API核心类(需要配置安全域名https://apis.map.qq.com)
var qqmapsdk = new QQMapWX({
key: 'LHVBZ-DQVWW-C2YRC-REKYI-HRUV7-JPFYV' //个人key,需要替换成公司的key
});
/*获取gps坐标 */
function getCoords(){
var getLocationPromisified = util.wxPromisify(wx.getLocation);
getLocationPromisified({
type: 'wgs84'
}).then(function(res){
return Promise.resolve({
lat: res.latitude,
lng: res.longitude
});
}).catch(function(err){
wx.showToast({
title: 'get location failed',
duration: 2000
});
});
}
/**
* 将GPS信息存储以后面备用
* @param {Object} info [定位信息]
*/
function saveGPSInfo(info){
// 将GPS信息存储以后面备用
util.setStoreage('gps_info',JSON.stringify(info));
}

/**
* 尝试通过QQMap获取定位信息
* @return {Object} {lng,lat,location_addr,region_id,region_name}
*/
function getGPSInfo(){
return new Promise((resolve,reject)=>{
var data={
lng:'',
lat:'',
location_addr:'',
region_id:'',
region_name:''
};
getCoords().then((point)=>{
// 请求用户授权定位
//逆地址解析
var reverseGeocoder = util.wxPromisify(qqmapsdk.reverseGeocoder);
return reverseGeocoder({
location:{
latitude: point.lat,
longitude: point.lng
}
});
}).then((res)=>{// 拿到腾讯地图解析过后的数据
// 经度
data.lng = res.result.ad_info.location.lng;
// 纬度
data.lat=res.result.ad_info.location.lat;
//详细地址
data.location_addr = res.result.address;

return region.getRegionId(res.result.address_component.city);

}).then((region)=>{// 通过城市名拿到城市区域id后

data.region_id = region.value;
data.region_name = region.name;

// 全量式返回定位结果
saveGPSInfo(data);
resolve({data});

}).catch((err)=>{

console.warn('address:getGPSInfo=>',err);
// 增量式返回定位结果
saveGPSInfo(data);
// 结果无论怎样都需要返回后端进行统一处理,所以不用reject
resolve({data,err});

});
});
}
/**
* 由服务器端处理地址逻辑
* @param {Object} addressData 可选择性传入定位信息,如果不传则会要求用户授权定位
* @return {Promise} then返回地址逻辑结果,catch抛出相关错误
*/
function getLocation(options){
var token = util.getStorage("token"),
ajaxCfg = {
method: 'POST',
url: ports.getLocation
},
needToken = 'token' in options ? options.token : true;
// 如果本地缓存token,则使用
if(token && needToken){
ajaxCfg.headers={'X-Auth-Token':token};
}
return new Promise((resolve,reject)=>{
// 如果有已存在的定位信息(一般来自本地存储)
if(typeof options.gpsInfo != 'undefined'){
ajaxCfg.data = options.gpsInfo;
util.wxRequest(ajaxCfg, true).then(result=>{
resolve(result);
}).catch(err=>{
reject(err);
});
}else{
//通过GPS授权获取定位信息
getGPSInfo().then(result=>{
if(result.err){
options.gpsError && options.gpsError(result.err);
}
ajaxCfg.data= result.data;
return util.wxRequest(ajaxCfg, true);
}).then(result=>{
resolve(result);
}).catch(err=>{
reject(err);
});
}
});
}

module.exports = {
getGPSInfo: getGPSInfo,
getLocation: getLocation
}
44 changes: 39 additions & 5 deletions utils/bearer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var util = require('./util.js');
var ports = require('./ports.js');
/* 门店地址处理脚本 */
var location = require('./address.js');
/* 首页承载页,数据源处理逻辑 */
function address(){
var _self = this;
//门店派发页面
Expand Down Expand Up @@ -47,17 +48,50 @@ address.prototype = {
this.getInfoByAddress();
}
},
getInfoByAddress: function(){
getAddressInfo: function(){
var _self = this, token = util.getStorage("token"),
ajaxCfg={
method: 'post',
method: 'POST',
url: ports.getLocation,
data: JSON.stringify(_self.currentAddress)
data: _self.currentAddress
};
if(token){
ajaxCfg.headers={'X-Auth-Token':token};
}

return commonAjax(ajaxCfg);
return util.wxRequest(ajaxCfg);
},
// 用地址信息获取可配送状态及分发信息
getInfoByAddress(addressData){
var _self = this;
// 处理地址逻辑
location.getLocation({
gpsInfo: addressData,
token: addressData ? false : true
}).then((result)=>{
_self.userLocationData = result.data;
// 将最终得到地址地址存入本地
util.setStoreage("final_address", JSON.stringify(_self.userLocationData.final_address));
var
// 分发结果
switchResult = _self.switchAddress(),
// 组合分发结果数据
pageSwitchInfo={
page: switchResult.page,
// 结果页弹层类型
popType: switchResult.popType || 0,
title: switchResult.txt
};

// 将分发信息存入本地
util.setStoreage("page_switch_info", JSON.stringify(pageSwitchInfo));
// 进入结果页前 打上进入线下店标识
// asyncStoreInfo(function(){
// // 跳转到分发页面
// location.href=_self.storePage[pageSwitchInfo.page];
// });
}).catch((e)=>{
console.warn('index/index:getInfoByAddress:',e);
});
}
}
5 changes: 4 additions & 1 deletion utils/ports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* 接口api */
var wdomain = "https://www.wzhouhui.com/dj",
mdomain = "https://m.wzhouhui.com";

module.exports = {
// 地址模块
getLocation: wdomain + '/Location/getLocation'
getLocation: wdomain + '/Location/getLocation',
// 有门店的省市区列表
regionList: wdomain + '/store/regionList',
}
Loading

0 comments on commit 4707a45

Please sign in to comment.