Commit da130b05 Harvey

新增 graphql 请求

1 个父辈 741f082a
//const baseUrl = 'https://app.ydniu.com/graphql' //const baseUrl = 'https://app.ydniu.com/graphql'
//const baseUrl = '/test_graphql' const baseUrl = '/test_graphql'
const baseUrl = '/graphql' //const baseUrl = '/graphql'
function request(options = {}) { function request(options = {}) {
let header = Object.assign({ let header = Object.assign({
...@@ -33,8 +33,29 @@ function post(options = {}) { ...@@ -33,8 +33,29 @@ function post(options = {}) {
return request(options) return request(options)
} }
async function graphql(options = {}) {
let opts = Object.assign({
type: 'query'
}, options)
var body = []
body.push(`${opts.type}{${opts.name}`)
if (opts.args) body.push(`(${opts.args})`)
if (opts.body) body.push(`{${opts.body}}`)
body.push('}')
const res = await post({
data: {
query: body.join('')
}
})
return res.data.data[opts.name]
}
uni.$u.request = { uni.$u.request = {
request, request,
get, get,
post post,
graphql
} }
<template> <template>
<view> <view>
<view class="top-nav"> <view class="top-nav">
<button class="top-nav-btn" :class="{'top-nav-btn-on':sub_module_id == item.value}" v-for="(item,i) in nav" :data-v="item.value" <button class="top-nav-btn" :class="{'top-nav-btn-on':sub_module_id == item.value}" v-for="(item,i) in nav"
@click="navChange" :key="i">{{item.text}}</button> :data-v="item.value" @click="navChange" :key="i">{{item.text}}</button>
</view> </view>
<view class="content"> <view class="content">
<!-- <view v-for="(item,index) in list" class="msg-info" :key="index"> <!-- <view v-for="(item,index) in list" class="msg-info" :key="index">
...@@ -11,16 +11,16 @@ ...@@ -11,16 +11,16 @@
<view class="tip">{{item.text}}</view> <view class="tip">{{item.text}}</view>
</view> --> </view> -->
<view class='left'> <view class='left'>
<view v-for="(item,index) in leftData" class="msg-info" :key="index"> <div v-for="(item,index) in leftData" class="msg-info" :key="index">
<image :mode="mode" lazy-load="true" :src="item.src" @click="imgClick(item)"></image> <image :mode="mode" lazy-load="true" :src="item.src+'/eshop'" @click="imgClick(item)"></image>
<view class="tip">{{item.text}}</view> <view class="tip">{{item.text}}</view>
</view> </div>
</view> </view>
<view class="right"> <view class="right">
<view v-for="(item,index) in rightData" class="msg-info" :key="index"> <div v-for="(item,index) in rightData" class="msg-info" :key="index">
<image :mode="mode" :src="item.src" @click="imgClick(item)"></image> <image :mode="mode" :src="item.src+'/eshop'" @click="imgClick(item)"></image>
<view class="tip">{{item.text}}</view> <view class="tip">{{item.text}}</view>
</view> </div>
</view> </view>
</view> </view>
</view> </view>
...@@ -68,31 +68,28 @@ ...@@ -68,31 +68,28 @@
}, },
methods: { methods: {
async bindData() { async bindData() {
const res = await uni.$u.request.post({ const res = await uni.$u.request.graphql({
data: { name: 'lottery_shop_get_posters',
query: `query{lottery_shop_get_posters(module_id:4,sub_module_id:${this.sub_module_id})}` args: `module_id:4,sub_module_id:${this.sub_module_id}`
} }).then(v => v.map(a => ({
})
var data = []
var resData = res.data.data['lottery_shop_get_posters'].map(a => ({
text: a.title, text: a.title,
src: a.main_image_url, src: a.main_image_url,
mid: a.module_id, mid: a.module_id,
id: a.id id: a.id
})) })))
if (resData.length < 2) return
if (res.length < 2) return
var leftData = [] var leftData = []
var rightData = [] var rightData = []
for (var i = 1, k = 0; i < resData.length; i += 2, k++) { for (var i = 1, k = 0; i < res.length; i += 2, k++) {
if (k % 2 == 1) { if (k % 2 == 1) {
leftData.push(resData[i]) leftData.push(res[i])
rightData.push(resData[i - 1]) rightData.push(res[i - 1])
} else { } else {
leftData.push(resData[i - 1]) leftData.push(res[i - 1])
rightData.push(resData[i]) rightData.push(res[i])
} }
} }
...@@ -100,10 +97,9 @@ ...@@ -100,10 +97,9 @@
this.rightData = rightData this.rightData = rightData
}, },
navChange(e) { navChange(e) {
console.log(e.target.dataset.v)
var sid = e.target.dataset.v var sid = e.target.dataset.v
if (this.sub_module_id == sid) return if (this.sub_module_id == sid) return
this.sub_module_id=sid this.sub_module_id = sid
this.bindData() this.bindData()
}, },
imgClick(img) { imgClick(img) {
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!