From 185e822b5d924404a82fd19483a11b2b6462df2e Mon Sep 17 00:00:00 2001 From: Kiyan Date: Thu, 11 May 2017 17:38:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=88=97=E8=A1=A8,=20?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.js | 93 +++++++- app/app.json | 18 +- app/pages/address/add.js | 2 +- app/pages/address/select.js | 13 +- app/pages/address/select.wxml | 2 +- app/pages/index/address.js | 29 ++- app/pages/index/address.wxml | 4 +- app/pages/index/index.js | 21 +- app/pages/index/index.wxml | 2 +- app/pages/login/login.js | 8 +- app/pages/mine/mine.js | 12 +- app/pages/order/list.js | 165 +++++++------- app/pages/order/list.json | 3 +- app/pages/order/list.wxml | 38 ++-- app/pages/order/list.wxss | 1 + app/pages/order/quasi.js | 142 +++++++----- app/pages/order/quasi.wxml | 11 +- app/pages/order/show.js | 144 +++++------- app/pages/order/show.wxml | 15 +- app/pages/order/show.wxss | 3 + app/pages/shop/show.js | 79 +++++-- app/pages/shop/show.wxml | 12 +- app/utils/apis.js | 193 ++++++++++++++-- app/utils/dateformat.js | 226 ++++++++++++++++++ app/utils/distance.js | 38 ++++ app/utils/util.js | 418 +++++++++++++++++----------------- 26 files changed, 1140 insertions(+), 552 deletions(-) create mode 100644 app/utils/dateformat.js create mode 100644 app/utils/distance.js diff --git a/app/app.js b/app/app.js index 45b0a28..07637ad 100644 --- a/app/app.js +++ b/app/app.js @@ -1,14 +1,24 @@ //app.js -import {getLoginInfo} from './utils/apis' +import { + getLoginInfo, getUserAddrs +} from './utils/apis' +import { + getCurrentAddress, + coordFormat +} from './utils/util' +import { + gcj02tobd09 +} from './utils/coordtransform' +import distance from './utils/distance' App({ onLaunch: function () { //调用API从本地缓存中获取数据 }, - getLoginInfo:function(cb){ + getLoginInfo: function (cb) { var that = this - if (this.globalData.loginInfo){ + if (this.globalData.loginInfo) { cb && cb(this.globalData.loginInfo) - }else{ + } else { //调用登录接口 getLoginInfo({ success(data) { @@ -24,7 +34,78 @@ App({ } this.globalData.loginInfo = loginInfo }, - globalData:{ - loginInfo:null + + // 获取当前地址 + getCurrentAddress(cb) { + var that = this + if (this.globalData.currentAddress) { + return cb && cb(this.globalData.currentAddress) + } + + getCurrentAddress(address => { + this.getLoginInfo(loginInfo => { + if (loginInfo.is_login) { + this.findNearbyUserAddr(userAddress => { + if (userAddress) { + address = userAddress + } + address = that.setCurrentAddress(address) + cb(address) + }) + } else { + address = that.setCurrentAddress(address) + cb(address) + } + }) + }) + }, + setCurrentAddress(address) { + if(address.addr_id) { + address.title = `${address.addr} ${address.detail}` + address.city = address.city_name + address.district = address.district_name + address.location = { + longitude: address.longitude, + latitude: address.latitude + } + } else { + address.location = coordFormat(address.location) + } + this.globalData.currentAddress = address + return address + }, + + findNearbyUserAddr(cb, radius = 100) { + radius /= 100 + wx.getLocation({ + type: 'gcj02', + success: function (res) { + var [lng1, lat1] = gcj02tobd09(res.longitude, res.latitude) + getUserAddrs({ + success(addressList) { + for (let i = 0, len = addressList.length; i < len; i++) { + var address = addressList[i] + var { + longitude: lng2, + latitude: lat2 + } = address + if (distance(lat1, lng1, lat2, lng2) <= radius) { + return cb(address) + } + } + return cb() + } + }) + }, + fail(res) { + console.log(res.errMsg) + alert('获取用户地址失败') + } + }) + }, + + globalData: { + loginInfo: null, + currentAddress: null } }) \ No newline at end of file diff --git a/app/app.json b/app/app.json index ec8fda5..4efdfde 100644 --- a/app/app.json +++ b/app/app.json @@ -1,16 +1,16 @@ { "pages": [ - "pages/address/select", - "pages/address/list", - "pages/address/add", - "pages/mine/mine", - "pages/login/login", - "pages/index/index", - "pages/index/address", - "pages/shop/show", "pages/order/show", - "pages/order/quasi", "pages/order/list", + "pages/shop/show", + "pages/mine/mine", + "pages/index/index", + "pages/address/select", + "pages/order/quasi", + "pages/address/list", + "pages/index/address", + "pages/address/add", + "pages/login/login", "pages/shop/category", "pages/shop/search" ], diff --git a/app/pages/address/add.js b/app/pages/address/add.js index f9d36bd..4abc76f 100644 --- a/app/pages/address/add.js +++ b/app/pages/address/add.js @@ -14,7 +14,7 @@ Page({ onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 this.id = options.id - this.callback = options.callback + this.callback = options.callback || 'callback' this.initValidate() if (this.id) { this.loadData() diff --git a/app/pages/address/select.js b/app/pages/address/select.js index 4461e0e..ce3a668 100644 --- a/app/pages/address/select.js +++ b/app/pages/address/select.js @@ -3,7 +3,7 @@ import { getUserAddrs, deleteUserAddr } from '../../utils/apis' import { - confirm + confirm, getPrevPage } from '../../utils/util' Page({ @@ -12,6 +12,7 @@ Page({ }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 + this.cb = options.callback || 'callback' this.setData({ selectedId: options.id }) @@ -81,5 +82,15 @@ Page({ }) } }) + }, + onItemTap(e) { + var {id} = e.currentTarget + var {selectedId} = this.data + if (id == selectedId) { + return + } + getPrevPage()[this.cb](id) + wx.navigateBack() } + }) \ No newline at end of file diff --git a/app/pages/address/select.wxml b/app/pages/address/select.wxml index ed2eea8..fb8990a 100644 --- a/app/pages/address/select.wxml +++ b/app/pages/address/select.wxml @@ -4,7 +4,7 @@ 可选收货地址 - + {{item.receiver}} {{item.phone}} diff --git a/app/pages/index/address.js b/app/pages/index/address.js index a4fdf67..0b2edec 100644 --- a/app/pages/index/address.js +++ b/app/pages/index/address.js @@ -7,6 +7,8 @@ import { } from '../../utils/util' import debounce from '../../utils/debounce' +import {getUserAddrs} from '../../utils/apis' + var initReLocateLabel = '重新定位' Page({ data: { @@ -21,7 +23,8 @@ Page({ }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 - this.callback = options.cb || 'callback' + this.callback = options.callback || 'callback' + this.initAddressList() this.initPoiList() this.onSearchInput = debounce(this.onSearchInput, 300) }, @@ -87,13 +90,35 @@ Page({ }) wx.navigateBack() }, - onAddressItemTap(e) { + onPoiItemTap(e) { var {id} = e.currentTarget var {poiList} = this.data getPrevPage()[this.callback](poiList[id]) wx.navigateBack() }, + onAddressItemTap(e) { + var {id} = e.currentTarget + var {addressList} = this.data + getPrevPage()[this.callback](addressList[id]) + wx.navigateBack() + }, + + initAddressList() { + var that = this + getApp().getLoginInfo(loginInfo => { + if(loginInfo.is_login) { + getUserAddrs({ + success(data) { + that.setData({ + addressList: data + }) + } + }) + } + }) + }, + initPoiList() { var that = this this.setData({ diff --git a/app/pages/index/address.wxml b/app/pages/index/address.wxml index ffb4e88..3ab3c94 100644 --- a/app/pages/index/address.wxml +++ b/app/pages/index/address.wxml @@ -24,7 +24,7 @@ 您的收货地址 - + {{item.addr}} {{item.receiver}} {{item.phone}} @@ -37,7 +37,7 @@ 定位地址 - + {{item.title}} diff --git a/app/pages/index/index.js b/app/pages/index/index.js index 861e77f..573eb9c 100644 --- a/app/pages/index/index.js +++ b/app/pages/index/index.js @@ -1,9 +1,5 @@ //index.js //获取应用实例 -import { - getCurrentAddress, setCurrentAddress -} from '../../utils/util' - import { getSellers } from '../../utils/apis' @@ -62,13 +58,18 @@ Page({ initAddress() { var that = this - getCurrentAddress(function (address) { + this.invalidateData() + getApp().getCurrentAddress(function (address) { + if (address.addr_id) { + address['title'] = `${address.addr} ${address.detail}` + } that.setData({ currentAddress: address }) that.loadData() }) }, + loadData() { if (this.data.loading) { return; @@ -111,16 +112,12 @@ Page({ }) }, onReachBottom(e) { - if (this.data.hasMore) { + if (this.data.hasMore && !this.data.loading) { this.loadData() } }, callback(address) { - setCurrentAddress(address) - this.setData({ - currentAddress: address - }) - this.invalidateData() - this.loadData() + getApp().setCurrentAddress(address) + this.initAddress() } }) diff --git a/app/pages/index/index.wxml b/app/pages/index/index.wxml index 4237d29..ff40cb0 100644 --- a/app/pages/index/index.wxml +++ b/app/pages/index/index.wxml @@ -3,7 +3,7 @@ - + {{currentAddress ? currentAddress.title : '定位中...'}} diff --git a/app/pages/login/login.js b/app/pages/login/login.js index 696cefc..7c4445e 100644 --- a/app/pages/login/login.js +++ b/app/pages/login/login.js @@ -3,7 +3,7 @@ import WxValidate from '../../utils/WxValidate' import Countdown from '../../utils/countdown' import { alert, getPrevPage } from '../../utils/util' import { getCode, login } from '../../utils/apis' -var initCount = 10 +var initCount = 60 Page({ data: { codeLabel: '获取验证码', @@ -11,7 +11,7 @@ Page({ }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 - this.callback = options.callback + this.callback = options.callback || 'callback' this.countdown = new Countdown(this, 'count') this.initValidate() }, @@ -103,7 +103,9 @@ Page({ that.setData({ loading: false }) - getPrevPage()[that.callback](data) + getApp().setLoginInfo(data) + var cb = getPrevPage()[that.callback] + cb && cb(data) wx.navigateBack() }, error() { diff --git a/app/pages/mine/mine.js b/app/pages/mine/mine.js index cea3c5d..2edba3e 100644 --- a/app/pages/mine/mine.js +++ b/app/pages/mine/mine.js @@ -13,17 +13,18 @@ Page({ userInfo }) }) - app.getLoginInfo(loginInfo => { - that.setData({ - loginInfo: loginInfo.user_info - }) - }) }, onReady:function(){ // 页面渲染完成 }, onShow:function(){ // 页面显示 + var that = this + app.getLoginInfo(loginInfo => { + that.setData({ + loginInfo: loginInfo.user_info + }) + }) }, onHide:function(){ // 页面隐藏 @@ -55,7 +56,6 @@ Page({ }) }, callback(loginInfo) { - app.setLoginInfo(loginInfo) this.setData({ loginInfo: loginInfo.user_info }) diff --git a/app/pages/order/list.js b/app/pages/order/list.js index 1fafc72..eeece9a 100644 --- a/app/pages/order/list.js +++ b/app/pages/order/list.js @@ -1,90 +1,23 @@ // pages/order/list.js import { ORDER_STATES } from './constant' +import { + getOrders +} from '../../utils/apis' + +import { + datetimeFormat +} from '../../utils/util' + +var initData = { + page: 0, + hasMore: true, + loading: false, + list: null +} Page({ data: { - login: true, ORDER_STATES, - list: [ - { - "order_id": "1370", - "order_no": "2017042815510158201158862546", - "seller_id": "2", - "seller_name": "鲜极道", - "title": "鸡腿饭(大)", - "add_time": "1493365861", - "order_price": "42.98", - "pay_price": "34.98", - "state": "1", - "is_reviews": "0", - "pic_url": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/1461034075146103407535640.jpg" - }, - { - "order_id": "1347", - "order_no": "2017040708540884666377388205", - "seller_id": "2", - "seller_name": "鲜极道", - "title": "鸡翅饭", - "add_time": "1491526448", - "order_price": "36.00", - "pay_price": "28.00", - "state": "4", - "is_reviews": "0", - "pic_url": "http://mtest.ipaotui.com/" - }, - { - "order_id": "1345", - "order_no": "2017033115480943749489684006", - "seller_id": "2", - "seller_name": "鲜极道", - "title": "香肠饭 等2件商品", - "add_time": "1490946489", - "order_price": "48.00", - "pay_price": "39.00", - "state": "5", - "is_reviews": "0", - "pic_url": "http://mtest.ipaotui.com/" - }, - { - "order_id": "1344", - "order_no": "2017033016225869845172165968", - "seller_id": "2", - "seller_name": "鲜极道", - "title": "鸡翅饭", - "add_time": "1490862178", - "order_price": "36.00", - "pay_price": "26.00", - "state": "5", - "is_reviews": "0", - "pic_url": "http://mtest.ipaotui.com/" - }, - { - "order_id": "1343", - "order_no": "2017033016045232046206634491", - "seller_id": "2", - "seller_name": "鲜极道", - "title": "鸡翅饭", - "add_time": "1490861092", - "order_price": "36.00", - "pay_price": "25.00", - "state": "5", - "is_reviews": "0", - "pic_url": "http://mtest.ipaotui.com/" - }, - { - "order_id": "1342", - "order_no": "2017033014434386598425245711", - "seller_id": "2", - "seller_name": "鲜极道", - "title": "鸡腿饭", - "add_time": "1490856223", - "order_price": "40.98", - "pay_price": "30.98", - "state": "5", - "is_reviews": "0", - "pic_url": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/1461034075146103407535640.jpg" - }, - ] }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 @@ -94,11 +27,81 @@ Page({ }, onShow: function () { // 页面显示 + var that = this + getApp().getLoginInfo(loginInfo => { + that.setData({ + loginInfo: loginInfo + }) + var {list} = that.data + if (loginInfo.is_login && !list) { + that.initData() + } + }) }, onHide: function () { // 页面隐藏 }, onUnload: function () { // 页面关闭 + }, + initData(cb) { + this.loadData(cb) + }, + loadData(cb) { + var that = this + var { + loading, page + } = this.data + if (loading) { + return + } + + this.setData({ + loading: true + }) + getOrders({ + page, + success(data) { + var {list} = that.data + var {list: list2, count, page} = data + list2 = list2.map(item => { + item['add_time_format'] = datetimeFormat(item.add_time) + return item + }) + that.setData({ + loading: false, + list: list ? list.concat(list2) : list2, + hasMore: count == 10, + page: page + 1 + }) + + cb && cb() + + } + }) + }, + onReachBottom(e) { + var { + loginInfo: {is_login}, + hasMore, loading + } = this.data + if (is_login && hasMore && !loading) { + this.loadData() + } + }, + onPullDownRefresh() { + var {loginInfo: {is_login}} = this.data + if (is_login) { + wx.showNavigationBarLoading() + this.initData(() => { + wx.hideNavigationBarLoading() + wx.stopPullDownRefresh() + }) + } else { + wx.stopPullDownRefresh() + } + }, + callback(loginInfo) { + this.setData(initData) } }) \ No newline at end of file diff --git a/app/pages/order/list.json b/app/pages/order/list.json index d8cbc9b..cd65c78 100644 --- a/app/pages/order/list.json +++ b/app/pages/order/list.json @@ -1,3 +1,4 @@ { - "navigationBarTitleText": "我的订单" + "navigationBarTitleText": "我的订单", + "enablePullDownRefresh": true } \ No newline at end of file diff --git a/app/pages/order/list.wxml b/app/pages/order/list.wxml index 349b619..013dfc6 100644 --- a/app/pages/order/list.wxml +++ b/app/pages/order/list.wxml @@ -1,31 +1,33 @@ - - - - - + + + + + - {{item.seller_name}} + {{item.seller_name}} {{ORDER_STATES[item.state]}} - - - - - ¥{{item.pay_price}} - {{item.add_time}} - - - - 去评论 + + + + ¥{{item.pay_price}} + {{item.add_time_format}} + + + + + 去评论 + + - + 您还没有登录, 登录后查看订单 - 登录 + 登录 \ No newline at end of file diff --git a/app/pages/order/list.wxss b/app/pages/order/list.wxss index 5f8b14e..d8d13e7 100644 --- a/app/pages/order/list.wxss +++ b/app/pages/order/list.wxss @@ -1,4 +1,5 @@ /* pages/order/list.wxss */ +@import '/templates/load-more.wxss'; .order-logout { text-align: center; diff --git a/app/pages/order/quasi.js b/app/pages/order/quasi.js index bb925fe..a94ec91 100644 --- a/app/pages/order/quasi.js +++ b/app/pages/order/quasi.js @@ -1,65 +1,16 @@ // pages/order/quasi.js +import { + getQuasiOrderInfo, updateOrderAddr, + addOrder +} from '../../utils/apis' Page({ data: { - info: { - "quasi_order_id": "2691", - "order_no": "2017042716455369061206686592", - "seller_id": "2", - "user_id": "4", - "state": "0", - "add_time": "1493282753", - "order_price": "42.98", - "pay_price": "34.98", - "goods_price": "22.98", - "cut_money": "8.00", - "coupon_money": "0.00", - "packing_fee": "2.00", - "delivery_fee": "18.00", - "receiver": "test4", - "receiver_addr": "龙华大厦", - "receiver_gps": "120.69101,28.002974", - "receiver_phone": "13000000005", - "receive_time": null, - "distance": "7.177", - "remark": null, - "is_reviews": "0", - "is_delete": "0", - "delivery_order_id": "0", - "title": "鸡腿饭(大)", - "receiver_city": "330300", - "commision": "0.00", - "user_coupon_id": null, - "coupon_type": null, - "real_delivery_fee": "18.00", - "cut_delivery_fee": "0.00", - "service_money": "0.00", - "seller_name": "鲜极道", - "seller_phone": "88888888", - "goods": [ - { - "goods_id": "29", - "sub_id": "50", - "seller_id": "2", - "detail": "鸡腿饭 xx", - "sales": "46", - "praise": "0", - "state": "1", - "commision": "3.00", - "goods_name": "鸡腿饭(大)", - "price": "22.98", - "packing_fee": "2.00", - "stock": "74", - "is_delete": "0", - "pic_url": "http://test.storesystem.cn/Uploadfile/Img/seller_goods/1461034075146103407535640.jpg", - "num": "1", - "price_sum": "22.98" - } - ] - - }, + }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 + this.id = options.id || '2725' + this.loadData() }, onReady: function () { // 页面渲染完成 @@ -72,5 +23,84 @@ Page({ }, onUnload: function () { // 页面关闭 + }, + loadData() { + var that = this + var {id} = this + var {loading} = this.data + if(loading) { + return + } + this.setData({ + loading: true + }) + wx.showNavigationBarLoading() + getQuasiOrderInfo({ + quasi_order_id: id, + success(data) { + that.setData({ + info: data, + loading: false + }) + wx.hideNavigationBarLoading() + }, + error() { + wx.hideNavigationBarLoading() + } + }) + + }, + callbackAddress(addr_id) { + var that = this + var {id} = this + var {loading} = this.data + if (loading) { + return + } + this.setData({ + loading: true + }) + wx.showNavigationBarLoading() + updateOrderAddr({ + quasi_order_id: id, + addr_id, + success(data) { + that.setData({ + info: data, + loading: false + }) + wx.hideNavigationBarLoading() + }, + error() { + that.setData({ + loading: false + }) + wx.hideNavigationBarLoading() + } + }) + }, + onAddOrder(e) { + var that = this + var {id} = this + var {loading} = this.data + if (loading) { + return + } + this.setData({ + loading: true + }) + addOrder({ + quasi_order_id: id, + success(data) { + that.setData({ + loading: false + }) + }, + error() { + that.setData({ + loading: false + }) + } + }) } }) \ No newline at end of file diff --git a/app/pages/order/quasi.wxml b/app/pages/order/quasi.wxml index 565be2a..d3c2f8d 100644 --- a/app/pages/order/quasi.wxml +++ b/app/pages/order/quasi.wxml @@ -1,14 +1,17 @@ - - - + + + {{info.receiver}} {{info.receiver_phone}} {{info.receiver_addr}} + + 选择地址 + @@ -79,7 +82,7 @@ 待支付 ¥{{info.pay_price}} - \ No newline at end of file diff --git a/app/pages/order/show.js b/app/pages/order/show.js index fcf3e3a..18f6c4d 100644 --- a/app/pages/order/show.js +++ b/app/pages/order/show.js @@ -1,9 +1,13 @@ // pages/order/show.js import Countdown from '../../utils/countdown' -import { countdownFormat } from '../../utils/util' +import { + countdownFormat, datetimeFormat, + makePhoneCall + } from '../../utils/util' +import { getOrderInfo } from '../../utils/apis' Page({ data: { - activeNavIndex: 1, + activeNavIndex: 0, tabNavs: ['订单状态', '订单详情'], statusImgs: { '1': '/images/status/order_status_money_icon_current@2x.png', @@ -14,95 +18,11 @@ Page({ '6': '/images/status/order_status_service_icon_fail_current@2x.png', '7': '/images/status/order_status_service_icon_fail_current@2x.png' }, - info: { - "order_id": "1375", - "order_no": "2017050316304632448020041071", - "day_sn": "4", - "seller_id": "2", - "user_id": "4", - "state": "3", - "add_time": "1493800246", - "order_price": "30.00", - "pay_price": "22.00", - "goods_price": "20.00", - "cut_money": "8.00", - "coupon_money": "0.00", - "packing_fee": "0.00", - "delivery_fee": "10.00", - "receiver": "test4", - "receiver_addr": "电商大厦", - "receiver_gps": "120.737561,27.979617", - "receiver_phone": "13000000004", - "receive_time": "1493803846", - "distance": "1.707", - "remark": "", - "is_reviews": "0", - "is_delete": "0", - "delivery_order_id": "0", - "title": "鸡翅饭", - "receiver_city": "330300", - "take_time": "1493800254", - "remind_time": "0", - "pay_type": "3", - "sys_settle_no": null, - "settle_no": null, - "commision": "0.00", - "user_coupon_id": null, - "real_delivery_fee": "10.00", - "cut_delivery_fee": "0.00", - "service_money": "0.00", - "seller_name": "鲜极道", - "seller_phone": "88888888", - "delivery_phone": "13906641410", - "goods": [ - { - "goods_id": "32", - "sub_id": "0", - "seller_id": "2", - "detail": "鸡翅饭 xx", - "sales": "43", - "praise": "0", - "state": "1", - "commision": null, - "goods_name": "鸡翅饭", - "price": "20.00", - "packing_fee": "0.00", - "stock": "54", - "is_delete": "0", - "pic_url": null, - "num": "1", - "price_sum": "20.00" - } - ], - "expire_time": 0, - "left_time": 0, - "flow": [ - { - "time": "1493800246", - "state": "1", - "status": "订单提交成功,待支付", - "remark": "" - }, - { - "time": "1493800252", - "state": "2", - "status": "支付成功,等待商家接单", - "remark": "" - }, - { - "time": "1493800254", - "state": "3", - "status": "商家已接单", - "remark": "" - } - ], - "run_order_id": "6408", - "localphone": "13906641410" - }, }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 - this.initCountdown(287) + this.id = options.id || 1395 + this.loadData() }, onReady: function () { // 页面渲染完成 @@ -143,6 +63,54 @@ Page({ }) }) this.countdown = countdown + }, + + loadData() { + var that = this + var order_id = this.id + wx.showNavigationBarLoading() + getOrderInfo({ + order_id, + success(data) { + data['add_time_format'] = datetimeFormat(data.add_time) + data['flow'] = data.flow.map(item => { + item['time_format'] = datetimeFormat(item.time) + return item + }) + that.setData({ + info: data + }) + if(data.left_time > 0) { + that.initCountdown(+data.left_time) + } + wx.hideNavigationBarLoading() + }, + error() { + wx.hideNavigationBarLoading() + } + }) + }, + + onPhoneTap(e) { + var that = this + var {info: {seller_phone, localphone}} = this.data + wx.showActionSheet({ + itemList: [ + `商家电话: ${seller_phone}`, + `客服电话: ${localphone}` + ], + success: function (res) { + var {tapIndex} = res + if(tapIndex == 0) { + makePhoneCall(seller_phone) + } else if(tapIndex == 1) { + makePhoneCall(localphone) + } + }, + fail: function (res) { + console.log(res.errMsg) + } + }) } }) \ No newline at end of file diff --git a/app/pages/order/show.wxml b/app/pages/order/show.wxml index cfeacac..6a33507 100644 --- a/app/pages/order/show.wxml +++ b/app/pages/order/show.wxml @@ -5,7 +5,7 @@ 订单状态 - + @@ -15,23 +15,24 @@ {{item.status}} - {{item.time}} + {{item.time_format}} 支付剩余时间 {{countLabel}} - - 取消订单 - 立即付款 + + 取消订单 + 立即付款 + 评价 订单详情 - + {{info.seller_name}} @@ -125,7 +126,7 @@ 订单时间 - {{info.add_time}} + {{info.add_time_format}} diff --git a/app/pages/order/show.wxss b/app/pages/order/show.wxss index 590a846..dbe4b73 100644 --- a/app/pages/order/show.wxss +++ b/app/pages/order/show.wxss @@ -1,5 +1,8 @@ /* pages/order/show.wxss */ @import './quasi.wxss'; +.quasi-goods { + margin-top: 0; +} .tab { display: flex; diff --git a/app/pages/shop/show.js b/app/pages/shop/show.js index 009a760..3e310d3 100644 --- a/app/pages/shop/show.js +++ b/app/pages/shop/show.js @@ -2,13 +2,12 @@ import { makePhoneCall, - getCurrentAddress, datetimeFormat } from '../../utils/util' import { getSellerInfo, - getReviews + getReviews, addQuasiOrder } from '../../utils/apis' @@ -43,7 +42,7 @@ Page({ }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 - this.id = options.id || 1 + this.id = options.id || 2 this.loadData() this.loadReview() }, @@ -64,23 +63,20 @@ Page({ var that = this var id = this.id; wx.showNavigationBarLoading() - getCurrentAddress(function (address) { - getSellerInfo({ - address, - seller_id: id, - success(data) { - data['distanceFormat'] = +(data['distance'] / 1000).toFixed(2) - that.setData({ - info: data - }) - wx.setNavigationBarTitle({ - title: data.seller_name - }) - }, - complete() { - wx.hideNavigationBarLoading() - } - }) + getSellerInfo({ + seller_id: id, + success(data) { + data['distanceFormat'] = +(data['distance'] / 1000).toFixed(2) + that.setData({ + info: data + }) + wx.setNavigationBarTitle({ + title: data.seller_name + }) + }, + complete() { + wx.hideNavigationBarLoading() + } }) }, @@ -331,5 +327,48 @@ Page({ if (hasMore && !loading) { this.loadReview() } + }, + onAddQuasiOrder(e) { + var that = this + var { + info: {seller_id}, + order: {goods}, + loading + } = this.data + if (loading) { + return + } + + this.setData({ + loading: true + }) + getApp().getLoginInfo(loginInfo => { + if(!loginInfo.is_login) { + wx.navigateTo({ + url: '/pages/login/login', + }) + this.setData({ + loading: false + }) + return + } + addQuasiOrder({ + seller_id, goods, + success(data) { + + that.setData({ + loading: false + }) + wx.navigateTo({ + url: `/pages/order/quasi?id=${data.quasi_order_id}` + }) + }, + error() { + that.setData({ + loading: false + }) + } + }) + }) } }) \ No newline at end of file diff --git a/app/pages/shop/show.wxml b/app/pages/shop/show.wxml index 5f9c992..c3811d8 100644 --- a/app/pages/shop/show.wxml +++ b/app/pages/shop/show.wxml @@ -84,11 +84,13 @@ 另需配送费{{info.delivery_fee}}元 - + diff --git a/app/utils/apis.js b/app/utils/apis.js index 2c6560f..cb7a8e0 100644 --- a/app/utils/apis.js +++ b/app/utils/apis.js @@ -1,6 +1,6 @@ import { fetch, coordFormat, - alert, confirm + alert, confirm, } from './util' // 获取商店列表 @@ -10,7 +10,7 @@ export function getSellers(options) { success } = options page = page || 0 - var location = coordFormat(address.location) + var location = address.location fetch({ url: 'index.php?m=Mall&c=Seller&a=getSellers', data: { @@ -29,18 +29,20 @@ export function getSellers(options) { // 获取商店详情 export function getSellerInfo(options) { var { - seller_id, address, + seller_id, success, complete } = options - var location = coordFormat(address.location) - fetch({ - url: 'index.php?m=Mall&c=Seller&a=getSellerInfo', - data: { - seller_id, - longitude: location.longitude, - latitude: location.latitude - }, - success, complete + getApp().getCurrentAddress(address => { + var location = address.location + fetch({ + url: 'index.php?m=Mall&c=Seller&a=getSellerInfo', + data: { + seller_id, + longitude: location.longitude, + latitude: location.latitude + }, + success, complete + }) }) } @@ -185,7 +187,7 @@ export function getUserAddr(options) { // 新增用户地址 export function addUserAddr(options) { - if(options.addr_id) { + if (options.addr_id) { return updateUserAddr(options) } const { @@ -198,7 +200,7 @@ export function addUserAddr(options) { } var {user_id, user_token} = loginInfo.user_info var gps = address.gps - if(!gps) { + if (!gps) { var location = coordFormat(address.location) gps = `${location.longitude},${location.latitude}` } @@ -207,7 +209,7 @@ export function addUserAddr(options) { data: { user_id, user_token, receiver, phone, detail, - gps, + gps, addr: address.title, city_id: address.city_id, city_name: address.city, @@ -233,7 +235,7 @@ export function updateUserAddr(options) { } var {user_id, user_token} = loginInfo.user_info var gps = address.gps - if(!gps) { + if (!gps) { var location = coordFormat(address.location) gps = `${location.longitude},${location.latitude}` } @@ -275,5 +277,164 @@ export function deleteUserAddr(options) { success, error }) + }) +} + +// 添加准订单 +export function addQuasiOrder(options) { + const { + seller_id, + goods, + success, error + } = options + getApp().getCurrentAddress(address => { + var data = { + seller_id, + goods: JSON.stringify(goods) + } + if (address.addr_id) { + data = Object.assign({ + addr_id: address.addr_id + }, data) + } else { + var location = address.location + data = Object.assign({ + city_id: address.city_id, + city_name: address.city, + district_id: address.district_id, + district_name: address.district, + longitude: location.longitude, + latitude: location.latitude + }, data) + } + getApp().getLoginInfo(loginInfo => { + if (!loginInfo.user_info) { + return alert('用户未登录') + } + var {user_id, user_token} = loginInfo.user_info + fetch({ + url: 'index.php?m=Mall&c=Order&a=addQuasiOrder', + data: Object.assign({ + user_id, user_token, + }, data), + success, error + }) + + }) + }) +} + +// 获取准订单 +export function getQuasiOrderInfo(options) { + var { + quasi_order_id, + success, error + } = options + getApp().getLoginInfo(loginInfo => { + if (!loginInfo.user_info) { + return alert('用户未登录') + } + var {user_id, user_token} = loginInfo.user_info + fetch({ + url: 'index.php?m=Mall&c=Order&a=getQuasiOrderInfo', + data: { + user_id, user_token, + quasi_order_id + }, + success, error + }) + + }) +} + +// 更新准订单地址 +export function updateOrderAddr(options) { + var { + quasi_order_id, addr_id, + success, error + } = options + getApp().getLoginInfo(loginInfo => { + if (!loginInfo.user_info) { + return alert('用户未登录') + } + var {user_id, user_token} = loginInfo.user_info + fetch({ + url: 'index.php?m=Mall&c=Order&a=updateOrderAddr', + data: { + user_id, user_token, + quasi_order_id, addr_id + }, + success, error + }) + + }) +} + +// 添加订单 +export function addOrder(options) { + var { + quasi_order_id, + success, error + } = options + getApp().getLoginInfo(loginInfo => { + if (!loginInfo.user_info) { + return alert('用户未登录') + } + var {user_id, user_token} = loginInfo.user_info + fetch({ + url: 'index.php?m=Mall&c=Order&a=addOrder', + data: { + user_id, user_token, + quasi_order_id + }, + success, error + }) + + }) +} + +// 获取订单列表 +export function getOrders(options) { + var { + page, + success, error + } = options + getApp().getLoginInfo(loginInfo => { + if (!loginInfo.user_info) { + return alert('用户未登录') + } + var {user_id, user_token} = loginInfo.user_info + fetch({ + url: 'index.php?m=Mall&c=Order&a=getOrders', + data: { + user_id, user_token, + page + }, + success, error + }) + + }) +} + +// 获取订单详情 +export function getOrderInfo(options) { + var { + order_id, + success, error + } = options + getApp().getLoginInfo(loginInfo => { + if (!loginInfo.user_info) { + return alert('用户未登录') + } + var {user_id, user_token} = loginInfo.user_info + fetch({ + url: 'index.php?m=Mall&c=Order&a=getOrderInfo', + data: { + user_id, user_token, + order_id + }, + success, error + }) + }) } \ No newline at end of file diff --git a/app/utils/dateformat.js b/app/utils/dateformat.js new file mode 100644 index 0000000..77cfb1f --- /dev/null +++ b/app/utils/dateformat.js @@ -0,0 +1,226 @@ +/* + * Date Format 1.2.3 + * (c) 2007-2009 Steven Levithan + * MIT license + * + * Includes enhancements by Scott Trenda + * and Kris Kowal + * + * Accepts a date, a mask, or a date and a mask. + * Returns a formatted version of the given date. + * The date defaults to the current date/time. + * The mask defaults to dateFormat.masks.default. + */ + +(function(global) { + 'use strict'; + + var dateFormat = (function() { + var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZWN]|'[^']*'|'[^']*'/g; + var timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g; + var timezoneClip = /[^-+\dA-Z]/g; + + // Regexes and supporting functions are cached through closure + return function (date, mask, utc, gmt) { + + // You can't provide utc if you skip other args (use the 'UTC:' mask prefix) + if (arguments.length === 1 && kindOf(date) === 'string' && !/\d/.test(date)) { + mask = date; + date = undefined; + } + + date = date || new Date; + + if(!(date instanceof Date)) { + date = new Date(date); + } + + if (isNaN(date)) { + throw TypeError('Invalid date'); + } + + mask = String(dateFormat.masks[mask] || mask || dateFormat.masks['default']); + + // Allow setting the utc/gmt argument via the mask + var maskSlice = mask.slice(0, 4); + if (maskSlice === 'UTC:' || maskSlice === 'GMT:') { + mask = mask.slice(4); + utc = true; + if (maskSlice === 'GMT:') { + gmt = true; + } + } + + var _ = utc ? 'getUTC' : 'get'; + var d = date[_ + 'Date'](); + var D = date[_ + 'Day'](); + var m = date[_ + 'Month'](); + var y = date[_ + 'FullYear'](); + var H = date[_ + 'Hours'](); + var M = date[_ + 'Minutes'](); + var s = date[_ + 'Seconds'](); + var L = date[_ + 'Milliseconds'](); + var o = utc ? 0 : date.getTimezoneOffset(); + var W = getWeek(date); + var N = getDayOfWeek(date); + var flags = { + d: d, + dd: pad(d), + ddd: dateFormat.i18n.dayNames[D], + dddd: dateFormat.i18n.dayNames[D + 7], + m: m + 1, + mm: pad(m + 1), + mmm: dateFormat.i18n.monthNames[m], + mmmm: dateFormat.i18n.monthNames[m + 12], + yy: String(y).slice(2), + yyyy: y, + h: H % 12 || 12, + hh: pad(H % 12 || 12), + H: H, + HH: pad(H), + M: M, + MM: pad(M), + s: s, + ss: pad(s), + l: pad(L, 3), + L: pad(Math.round(L / 10)), + t: H < 12 ? 'a' : 'p', + tt: H < 12 ? 'am' : 'pm', + T: H < 12 ? 'A' : 'P', + TT: H < 12 ? 'AM' : 'PM', + Z: gmt ? 'GMT' : utc ? 'UTC' : (String(date).match(timezone) || ['']).pop().replace(timezoneClip, ''), + o: (o > 0 ? '-' : '+') + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4), + S: ['th', 'st', 'nd', 'rd'][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10], + W: W, + N: N + }; + + return mask.replace(token, function (match) { + if (match in flags) { + return flags[match]; + } + return match.slice(1, match.length - 1); + }); + }; + })(); + + dateFormat.masks = { + 'default': 'ddd mmm dd yyyy HH:MM:ss', + 'shortDate': 'm/d/yy', + 'mediumDate': 'mmm d, yyyy', + 'longDate': 'mmmm d, yyyy', + 'fullDate': 'dddd, mmmm d, yyyy', + 'shortTime': 'h:MM TT', + 'mediumTime': 'h:MM:ss TT', + 'longTime': 'h:MM:ss TT Z', + 'isoDate': 'yyyy-mm-dd', + 'isoTime': 'HH:MM:ss', + 'isoDateTime': 'yyyy-mm-dd\'T\'HH:MM:sso', + 'isoUtcDateTime': 'UTC:yyyy-mm-dd\'T\'HH:MM:ss\'Z\'', + 'expiresHeaderFormat': 'ddd, dd mmm yyyy HH:MM:ss Z' + }; + + // Internationalization strings + dateFormat.i18n = { + dayNames: [ + 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', + 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' + ], + monthNames: [ + 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', + 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' + ] + }; + +function pad(val, len) { + val = String(val); + len = len || 2; + while (val.length < len) { + val = '0' + val; + } + return val; +} + +/** + * Get the ISO 8601 week number + * Based on comments from + * http://techblog.procurios.nl/k/n618/news/view/33796/14863/Calculate-ISO-8601-week-and-year-in-javascript.html + * + * @param {Object} `date` + * @return {Number} + */ +function getWeek(date) { + // Remove time components of date + var targetThursday = new Date(date.getFullYear(), date.getMonth(), date.getDate()); + + // Change date to Thursday same week + targetThursday.setDate(targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3); + + // Take January 4th as it is always in week 1 (see ISO 8601) + var firstThursday = new Date(targetThursday.getFullYear(), 0, 4); + + // Change date to Thursday same week + firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3); + + // Check if daylight-saving-time-switch occurred and correct for it + var ds = targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset(); + targetThursday.setHours(targetThursday.getHours() - ds); + + // Number of weeks between target Thursday and first Thursday + var weekDiff = (targetThursday - firstThursday) / (86400000*7); + return 1 + Math.floor(weekDiff); +} + +/** + * Get ISO-8601 numeric representation of the day of the week + * 1 (for Monday) through 7 (for Sunday) + * + * @param {Object} `date` + * @return {Number} + */ +function getDayOfWeek(date) { + var dow = date.getDay(); + if(dow === 0) { + dow = 7; + } + return dow; +} + +/** + * kind-of shortcut + * @param {*} val + * @return {String} + */ +function kindOf(val) { + if (val === null) { + return 'null'; + } + + if (val === undefined) { + return 'undefined'; + } + + if (typeof val !== 'object') { + return typeof val; + } + + if (Array.isArray(val)) { + return 'array'; + } + + return {}.toString.call(val) + .slice(8, -1).toLowerCase(); +}; + + + + if (typeof define === 'function' && define.amd) { + define(function () { + return dateFormat; + }); + } else if (typeof exports === 'object') { + module.exports = dateFormat; + } else { + global.dateFormat = dateFormat; + } +})(this); diff --git a/app/utils/distance.js b/app/utils/distance.js new file mode 100644 index 0000000..90f854c --- /dev/null +++ b/app/utils/distance.js @@ -0,0 +1,38 @@ +var RADIUS = 6371; + +var toRad = function(n) { + return n * Math.PI / 180; +}; + +var getDistance = function(from, to) { + var fromLat = from[0]; + var fromLon = from[1]; + var toLat = to[0]; + var toLon = to[1]; + + var dLat = toRad(toLat - fromLat); + var dLon = toRad(toLon - fromLon); + var fromLat = toRad(fromLat); + var toLat = toRad(toLat); + + var a = Math.pow(Math.sin(dLat / 2), 2) + + (Math.pow(Math.sin(dLon / 2), 2) * Math.cos(fromLat) * Math.cos(toLat)); + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + + return RADIUS * c; +}; + +var measurePath = function(points) { + return points.reduce(function(memo, point) { + var distance = memo.lastPoint === null ? 0 : getDistance(memo.lastPoint, point); + return { lastPoint: point, distance: distance + memo.distance }; + }, { lastPoint: null, distance: 0 }).distance; +}; + +module.exports = function(fromLat, fromLon, toLat, toLon) { + if(typeof fromLat === 'number'){ + return getDistance([fromLat, fromLon], [toLat, toLon]); + } else { + return measurePath(fromLat); + } +}; \ No newline at end of file diff --git a/app/utils/util.js b/app/utils/util.js index 21fa109..0f2dc25 100644 --- a/app/utils/util.js +++ b/app/utils/util.js @@ -1,306 +1,300 @@ 'use strict'; import timeago from './timeago.min' +import dateFormat from './dateformat' +import distance from './distance' import QQMapWX from './qqmap-wx-jssdk.min' -import { gcj02tobd09 } from './coordtransform' +import { + gcj02tobd09 +} from './coordtransform' import { host, qqmapKey } from '../config' const qqmap = new QQMapWX({ - key: qqmapKey + key: qqmapKey }); function resolveAdInfo(adInfo) { - const {city, district, adcode} = adInfo - return { - city, district, - district_id: adcode, - city_id: adcode.replace(/\d{2}$/, '00') - } + const {city, district, adcode} = adInfo + return { + city, district, + district_id: adcode, + city_id: adcode.replace(/\d{2}$/, '00') + } } // 解析地址 export function reverseGeocoder(options) { - const { - location, success, complete - } = options - qqmap.reverseGeocoder({ - location, - success: function (res) { - var address = resolveAdInfo(res.result.ad_info) - success && success(address) - }, - fail: function (err) { - console.log(err) - }, - complete - }) + const { + location, success, complete + } = options + qqmap.reverseGeocoder({ + location, + success: function (res) { + var address = resolveAdInfo(res.result.ad_info) + success && success(address) + }, + fail: function (err) { + console.log(err) + }, + complete + }) } // 获取当前地理位置 export function getCurrentAddressList(options) { - const { - success, complete - } = options - wx.getLocation({ - type: 'gcj02', - success(res) { - getAddressFromLocation({ - location: { - latitude: res.latitude, - longitude: res.longitude, - }, - success, complete - }) + const { + success, complete + } = options + wx.getLocation({ + type: 'gcj02', + success(res) { + getAddressFromLocation({ + location: { + latitude: res.latitude, + longitude: res.longitude, }, - fail(e) { - console.log(res.errMsg) - alert('获取用户地址失败') - } - }) + success, complete + }) + }, + fail(res) { + console.log(res.errMsg) + alert('获取用户地址失败') + } + }) } // 地点搜索 export function searchAddressList(options) { - const { - keyword, success - } = options - getCurrentCity(function (cityName) { - qqmap.getSuggestion({ - region: cityName, - keyword, - success(res) { - success && success(res.data) - } - }) + const { + keyword, success + } = options + getCurrentCity(function (cityName) { + qqmap.getSuggestion({ + region: cityName, + keyword, + success(res) { + success && success(res.data) + } }) + }) } -// 获取当期地址 -var currentAddress; +// 获取当前地址 export function getCurrentAddress(callback) { - if(currentAddress) { - return callback && callback(currentAddress) + getCurrentAddressList({ + success(addressList) { + if (addressList.length > 0) { + callback(addressList[0]) + } } - - getCurrentAddressList({ - success(addressList) { - if(addressList.length > 0) { - currentAddress = addressList[0] - callback(currentAddress) - } - } - }) -} - -export function setCurrentAddress(address) { - currentAddress = address + }) } // 获取当前城市 var cityName; export function getCurrentCity(callback) { - if (cityName) { - return callback && callback(cityName) - } - wx.getLocation({ - type: 'gcj02', - success(res) { - qqmap.reverseGeocoder({ - location: { - longitude: res.longitude, - latitude: res.latitude - }, - success: function (res) { - cityName = res.result.address_component.city - callback && callback(cityName) - } - }) + if (cityName) { + return callback && callback(cityName) + } + wx.getLocation({ + type: 'gcj02', + success(res) { + qqmap.reverseGeocoder({ + location: { + longitude: res.longitude, + latitude: res.latitude }, - fail(res) { - console.log(res.errMsg) - alert('获取用户地址失败') + success: function (res) { + cityName = res.result.address_component.city + callback && callback(cityName) } - }) + }) + }, + fail(res) { + console.log(res.errMsg) + alert('获取用户地址失败') + } + }) } // 根据坐标获取地址信息 export function getAddressFromLocation(options) { - const {location, success} = options - getPois({ - location, - success(pois) { - var addressList = [] - pois.forEach(poi => { - var { - title, location, - address, ad_info - } = poi - addressList.push(Object.assign({ - title, location, address - }, resolveAdInfo(ad_info))) - }) - success && success(addressList) - } - }) + const {location, success} = options + getPois({ + location, + success(pois) { + var addressList = [] + pois.forEach(poi => { + var { + title, location, + address, ad_info + } = poi + addressList.push(Object.assign({ + title, location, address + }, resolveAdInfo(ad_info))) + }) + success && success(addressList) + } + }) } // 获取兴趣点 export function getPois(options) { - const { - location, success, complete - } = options - qqmap.reverseGeocoder({ - location, - get_poi: 1, - success: function (res) { - success && success(res.result.pois) - }, - fail: function (err) { - console.log(err) - }, - complete - }) + const { + location, success, complete + } = options + qqmap.reverseGeocoder({ + location, + get_poi: 1, + success: function (res) { + success && success(res.result.pois) + }, + fail: function (err) { + console.log(err) + }, + complete + }) } export function getPrevPage() { - const pages = getCurrentPages() - return pages[pages.length - 2] + const pages = getCurrentPages() + return pages[pages.length - 2] } export function fetch(options) { - wx.request({ - url: `https://${host}/${options.url}`, - data: Object.assign(options.data, { - 'app_v': 'ipaotui_mall' - }), - method: options.method || 'POST', - header: { - 'content-type': 'application/x-www-form-urlencoded' - }, - success: function (res) { - const data = res.data - if (data.State == 'Success') { - options.success && options.success(data.data) - } else { - alert(data.info) - options.error && options.error(data.info) - } - options.complete && options.complete() - } - }) + wx.request({ + url: `https://${host}/${options.url}`, + data: Object.assign(options.data, { + 'app_v': 'ipaotui_mall' + }), + method: options.method || 'POST', + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + success: function (res) { + const data = res.data + if (data.State == 'Success') { + options.success && options.success(data.data) + } else { + alert(data.info) + options.error && options.error(data.info) + } + options.complete && options.complete() + } + }) } // 提示框 export function alert(content, callback) { - wx.showModal({ - title: '提示', - content: content, - showCancel: false, - success: callback - }) + wx.showModal({ + title: '提示', + content: content, + showCancel: false, + success: callback + }) } // 确认框 export function confirm(options) { - var { - content, confirmText, - ok, - } = options - confirmText = confirmText || '确定' - wx.showModal({ - content, - confirmText, - cancelText: '关闭', - success(res) { - if (res.confirm) { - ok && ok() - } - } - }) + var { + content, confirmText, + ok, + } = options + confirmText = confirmText || '确定' + wx.showModal({ + content, + confirmText, + cancelText: '关闭', + success(res) { + if (res.confirm) { + ok && ok() + } + } + }) } // 拨打电话 export function makePhoneCall(phoneNum) { - confirm({ - content: `是否拨打电话 ${phoneNum}`, - confirmText: '拨打', - ok() { - wx.makePhoneCall({ - phoneNumber: phoneNum, - }) - } - }) + confirm({ + content: `是否拨打电话 ${phoneNum}`, + confirmText: '拨打', + ok() { + wx.makePhoneCall({ + phoneNumber: phoneNum, + }) + } + }) } // 加载提示 export function showLoading() { - wx.showToast({ - icon: 'loading', - duration: 10000, - title: '加载中...', - mask: true, - }) + wx.showToast({ + icon: 'loading', + duration: 10000, + title: '加载中...', + mask: true, + }) } export function hideLoading() { - wx.hideToast() + wx.hideToast() } // 时间格式化 export function datetimeFormat(unix_timestamp) { - return new timeago().format(new Date(unix_timestamp * 1000), 'zh_CN'); + return dateFormat(new Date(unix_timestamp * 1000), "mm月dd日 HH:MM") } // 坐标格式化 export function coordFormat(location) { - if(location.lat && location.lng) { - location = { - longitude: location.lng, - latitude: location.lat - } - } - // gcj02 转 bd09 - var location = gcj02tobd09(location.longitude, location.latitude) - return { - longitude: location[0], - latitude: location[1] + if (location.lat && location.lng) { + location = { + longitude: location.lng, + latitude: location.lat } + } + // gcj02 转 bd09 + var location = gcj02tobd09(location.longitude, location.latitude) + return { + longitude: location[0], + latitude: location[1] + } } // 倒计时格式化 export function countdownFormat(count) { - var seconds = count % 60 - count = Math.floor(count / 60) - var minutes = count % 60 - return `${minutes}分钟${seconds}秒` + var seconds = count % 60 + count = Math.floor(count / 60) + var minutes = count % 60 + return `${minutes}分钟${seconds}秒` } // 字符串关键字分组 export function splitByKeyword(text, keyword) { - if (!text) { - return [] - } - var arr = text.split(keyword) - var res = [] + if (!text) { + return [] + } + var arr = text.split(keyword) + var res = [] + res.push({ + text: arr[0], + isKeyword: false + }) + for (let i = 1, len = arr.length; i < len; i++) { res.push({ - text: arr[0], + text: keyword, + isKeyword: true + }, { + text: arr[i], isKeyword: false - }) - for (let i = 1, len = arr.length; i < len; i++) { - res.push({ - text: keyword, - isKeyword: true - }, { - text: arr[i], - isKeyword: false - }) - } - return res + }) + } + return res } var userInfo export function getUserInfo(cb) { - if(userInfo) { + if (userInfo) { return cb(userInfo) } else { wx.getUserInfo({