From fffde2fda10be01a560b335a33680ed4db1ba17e Mon Sep 17 00:00:00 2001 From: liuxiaoyan Date: Fri, 30 Sep 2016 14:32:14 +0800 Subject: [PATCH] api --- pages/address/address.js | 8 ++++-- pages/address/address.wxml | 13 ++++++++- pages/index/index.js | 33 ++++++++++++++--------- pages/index/index.wxml | 9 ++++--- pages/show_product/show_product.js | 40 +++++++++++++++++++++++++++- pages/show_product/show_product.wxml | 32 +++++++++++++++++----- utils/address.js | 32 ++++++++++++++++++---- utils/product.js | 34 +++++++++++++++++++++++ 8 files changed, 170 insertions(+), 31 deletions(-) diff --git a/pages/address/address.js b/pages/address/address.js index 835034f..9489bf4 100644 --- a/pages/address/address.js +++ b/pages/address/address.js @@ -3,9 +3,13 @@ const address = require('../../utils/address.js') Page({ formSubmit: function(e) { address.postAddress(e.detail.value) - console.log('form发生了submit事件,携带数据为:', e.detail.value) + console.log('form 发生了 submit 事件,携带数据为:', e.detail.value) }, formReset: function(e) { - console.log('form发生了reset事件') + console.log('form 发生了 reset 事件') + }, + + onLoad (params) { + //this.data.sku = params.sku } }) diff --git a/pages/address/address.wxml b/pages/address/address.wxml index 459d032..eb8de70 100644 --- a/pages/address/address.wxml +++ b/pages/address/address.wxml @@ -1,4 +1,15 @@
+ + 省 + + + + 市 + + + + 区 + 详细地址 @@ -9,4 +20,4 @@ -
\ No newline at end of file + diff --git a/pages/index/index.js b/pages/index/index.js index 4a0f3b8..d2316fb 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1,24 +1,31 @@ var app = getApp() +const product = require('../../utils/product.js') Page({ - data: { items: - [ - { url: 'aaaa.jpg', - name: 'aaa', - id: 1 - }, - { - url: 'aaaa.jpg', - name: 'bbb', - id: 2 - } - ] - }, + data: {items: []}, bindViewTap: function() { }, onLoad: function() { var that = this + + wx.getNetworkType({ + success: function(res) { + var networkType = res.networkType // 返回网络类型2g,3g,4g,wifi + // if (networkType) { + product.getProducts().then(function(result) { + that.data.items = result.data + }) + // } else { + // cache = wx.getStorageSync('products') + // if (cache) { + // this.data.items = cache + // } else { + // this.data.items = [] + // } + // } + } + }) } }) diff --git a/pages/index/index.wxml b/pages/index/index.wxml index ffed581..02ef07e 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -2,9 +2,12 @@ - - - {{item.name}} + + + + + {{item.attributes.name}} + {{item.attributes.price}} diff --git a/pages/show_product/show_product.js b/pages/show_product/show_product.js index 510a1da..2677db9 100644 --- a/pages/show_product/show_product.js +++ b/pages/show_product/show_product.js @@ -1,13 +1,43 @@ const app = getApp() +const product = require('../../utils/product.js') Page({ data: { title: '', - id: 0 + id: 0, + address: '', + product: {} + }, + + onShow () { + this.setData({'address': Date.now()}) }, onLoad (params) { + var that = this this.data.id = params.id + + wx.getNetworkType({ + success: function(res) { + var networkType = res.networkType // 返回网络类型2g,3g,4g,wifi + debugger + if (networkType) { + product.getProduct(params.id).then(function(result) { + console.log(result) + that.data.product = result.data + wx.setStorage({key: `product-${params.id}`, value: that.data.product}) + }) + } else { + cache = wx.getStorageSync(`product-${params.id}`) + if (cache) { + this.data.product = cache + } else { + this.data.product = {} + } + } + } + }) + }, onReady () { @@ -18,5 +48,13 @@ Page({ wx.navigateTo({ url: '../address/address' }) + }, + + formSubmit: function(e) { + // address.postBuyProduct(e.detail.value) + console.log('form发生了submit事件,携带数据为:', e.detail.value) + }, + formReset: function(e) { + console.log('form发生了reset事件') } }) diff --git a/pages/show_product/show_product.wxml b/pages/show_product/show_product.wxml index 33e3597..cda05a6 100644 --- a/pages/show_product/show_product.wxml +++ b/pages/show_product/show_product.wxml @@ -1,11 +1,31 @@ - {{name}} - + {{product.attributes.name}} + - - address + + {{address}} + edit - - address + + add new address + + + +
+ + 数量 + + + + sku:{{product.attributes.sku || '无法获取sku'}} + + + {{product.attributes.price || '无法获取价格'}} + + + + + +
diff --git a/utils/address.js b/utils/address.js index 499e66f..53c2c75 100644 --- a/utils/address.js +++ b/utils/address.js @@ -1,20 +1,42 @@ -const API_URL = 'https://api.bayekeji.com/v2' +const API_URL = 'http://127.0.0.1:3000' function postAddress (data) { return new Promise((resolve, reject) => { wx.request({ - method: POST, + method: 'POST', url: `${API_URL}/addresses/new`, data: Object.assign({}, data), header: { 'Content-Type': 'application/json' }, - success: resolve, - fail: reject + success (res) { + console.log('success') + wx.setStorageSync('addrss', res.data) + wx.navigateBack() + }, + fail (e) { + console.log('error') + console.error(e) + } }) }) } +function getLocation () { + wx.getLocation({ + type: 'gcj02', //返回可以用于 wx.openLocation 的经纬度 + success: function(res) { + var latitude = res.latitude + var longitude = res.longitude + wx.openLocation({ + latitude: latitude, + longitude: longitude, + scale: 28 + }) + } + }) +} + module.exports = { postAddress (data) { return postAddress(data).then(res => res.data) } -} \ No newline at end of file +} diff --git a/utils/product.js b/utils/product.js index e69de29..bb53895 100644 --- a/utils/product.js +++ b/utils/product.js @@ -0,0 +1,34 @@ +const API_URL = 'http://localhost:3000' + +function getProducts (data) { + data = data ? `/${data}` : '' + return new Promise((resolve, reject) => { + wx.request({ + url: `${API_URL}/products${data}`, + header: { 'Content-Type': 'application/json' }, + success: resolve, + fail: reject + }) + }) +} + +function postBuyProduct (data) { + return new Promise((resolve, reject) => { + wx.request({ + url: `${API_URL}/products/buy`, + data: Object.assign({}, data), + header: { 'Content-Type': 'application/json' }, + success: resolve, + fail: reject + }) + }) +} + +module.exports = { + getProducts (data) { + return getProducts(data).then(res => res.data) + }, + getProduct (data) { + return getProducts(data).then(res => res.data) + } +}