1.先功能需求的一个了解
2.wxml 部分 运用到 vantui
<view wx:for="{{weightList}}" wx:for-index="index" wx:for-item="item" wx:key="weightList">
<van-cell title="{{ index }}月" value="总变化{{ item.cha_num }}kg" border="{{ false }}" />
<view wx:for="{{ item.list }}" wx:for-index="index" wx:for-item="iteme" wx:key="item.list ">
<van-cell-group>
<van-cell title="{{ iteme.create_time}}" value="{{ iteme.weight }}kg" border="{{ false }}" />
</van-cell-group>
</view>
</view>
3.js部分 发起请求
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
var that = this
wx.request({
url: 'http://tp51.com/index.php/testapi/Courses/weightList',
success:(res)=>{
console.log(res)
that.setData({
weightList : res.data.data
})
}
})
},
3.后端 方法
/**
* 每个月 显示的数据
* @return \think\response\Json
*/
public function weightList()
{
$u_id = 1;
//总共12个月
$month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
//查找每个月 对应记录的数据
foreach ($month as $k => $v) {
$list[$v]['list'] = Weight::where(['u_id' => $u_id, 'm_num' => $v])
->order('create_time', 'asc')
->field('m_num,create_time,weight')
->select()->toArray();
//循环删除 为空的月份
foreach ($list as $z => $j) {
if ($j['list'] == null) {
unset($list[$z]);
}else{
//找出最大体重 和最小体重
$difference = Weight::where(['u_id' => $u_id, 'm_num' => $v])
->group('m_num')
->field('max(weight) as max_weight,min(weight) as min_weight')
->select()->toArray();
// 将差集 循环添加到 数组中
foreach ($difference as $e) {
$list[$v]['cha_num'] = $e['max_weight'] - $e['min_weight'];
}
}
}
}
return json(['code' => 200, 'data' => $list]);
}