wechat_user_type
liuxiaoyan 8 years ago
parent f2960ba057
commit fffde2fda1
  1. 8
      pages/address/address.js
  2. 13
      pages/address/address.wxml
  3. 33
      pages/index/index.js
  4. 9
      pages/index/index.wxml
  5. 40
      pages/show_product/show_product.js
  6. 32
      pages/show_product/show_product.wxml
  7. 32
      utils/address.js
  8. 34
      utils/product.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
}
})

@ -1,4 +1,15 @@
<form bindsubmit="formSubmit" bindreset="formReset">
<view>
</view>
<view>
</view>
<view>
</view>
<view class="section">
<view class="section_title">详细地址</view>
@ -9,4 +20,4 @@
<button formType="submit">Submit</button>
<button formType="reset">Reset</button>
</view>
</form>
</form>

@ -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 = []
// }
// }
}
})
}
})

@ -2,9 +2,12 @@
<scroll-view scroll-y="true" height="100%">
<block wx:for="{{items}}" wx:for-item="item">
<navigator url="../show_product/show_product?id={{item.id}}">
<view bindtap="bindViewTap">
<image class="" src="{{item.url}}" background-size="cover"></image>
<text> {{item.name}} </text>
<view>
<image class="" src="{{item.attributes['image-url']}}" background-size="cover"></image>
</view>
<view>
<text> {{item.attributes.name}} </text>
<text> {{item.attributes.price}} </text>
</view>
</navigator>
</block>

@ -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事件')
}
})

@ -1,11 +1,31 @@
<view>
<text>{{name}}</text>
<image src="/images/product_show_{{id}}.png" mode="aspectFill"/>
<text>{{product.attributes.name}}</text>
<image src="{{product.attributes['image-url']}}" mode="aspectFill"/>
<view wx:if="{{ address.length > 0 }}">
<text bindtap="bindTapAddress">address</text>
<view wx:if="{{ address > 0 }}" bindtap="bindTapAddress">
<text>{{address}}</text>
<text>edit</text>
</view>
<view wx:else>
<text bindtap="bindTapAddress">address</text>
<view wx:else bindtap="bindTapAddress">
<text>add new address</text>
</view>
<view>
<form bindsubmit="formSubmit" bindreset="formReset">
<view class="section">
<text class="">数量</text>
<input name="quantity" value="1" />
</view>
<view class="section">
<text type="hidden">sku:{{product.attributes.sku || '无法获取sku'}}</text>
</view>
<view class="section">
<text>{{product.attributes.price || '无法获取价格'}}</text>
</view>
<view class="btn-area">
<button formType="submit">buy it!</button>
<button formType="reset">Reset</button>
</view>
</form>
</view>
</view>

@ -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)
}
}
}

@ -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)
}
}
Loading…
Cancel
Save