Index.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/10/18
* Time: 11:03
*/
namespace app\home\controller;
use app\common\controller\WechatBase;
use app\home\model\Goods;
use app\home\model\Slide;
use think\Db;
class Index extends WechatBase
{
public function index(){
$domain_name = $this->request->domain();//域名
//幻灯片
$slideModel = new Slide();
$slide = $slideModel->selectData([]);
$slide = collection($slide)->toArray();
foreach($slide as $key => $s){
$s[$key]['image'] = $domain_name.$s['image'];
}
//广告信息
$user_id = get_current_user_id();
$goods_ids = [];
if(!empty($user_id)){
//获取用户答对了哪些广告(拿过广告的积分就不要展示在列表里了)
$user_exp_log = Db::name('user_exp_log')->where(['user_id'=>$user_id,'type'=>4])->select();
foreach($user_exp_log as $key => $value){
$goods_ids[] = $value['goods_id'];
}
}
$goodsModel = new Goods();
$data = $goodsModel->selectPageData(['id'=>['not in',$goods_ids]],1,5);
foreach($data as $key => $vo){
if(!empty($vo['video'])){
$data[$key]['video'] = $domain_name.$vo['video'];
}
$voice = explode(',',$vo['voice']);
if(!empty($voice)){
foreach($voice as $key2 => $vo2){
$voice[$key2] = $domain_name.$vo2;
}
$data[$key]['voice'] = $voice;
}
$data[$key]['start_time'] = date('Y-m-d',$vo['start_time']);
$data[$key]['end_time'] = date('Y-m-d',$vo['end_time']);
$images = explode(',',$vo['images']);
if(!empty($images)){
foreach($images as $key3 => $vo3){
$images[$key3] = $domain_name.$vo3;
}
$data[$key]['images'] = $images;
}
}
$this->assign('slide',$slide);
$this->assign('data',$data);
$this->assign('title','广告商城');
return $this->fetch();
}
/**
* 加载更多
*/
public function more(){
$domain_name = $this->request->domain();//域名
$user_id = $this->request->param('user_id',0,'intval');
$page = $this->request->param('page',1,'intval');
$pageNum = $this->request->param('pageNum',5,'intval');
$goods_ids = [];
if(!empty($user_id)){
//获取用户答对了哪些广告(拿过广告的积分就不要展示在列表里了)
$user_exp_log = Db::name('user_exp_log')->where(['user_id'=>$user_id,'type'=>4])->select();
foreach($user_exp_log as $key => $value){
$goods_ids[] = $value['goods_id'];
}
}
$goodsModel = new Goods();
$data = $goodsModel->selectPageData(['id'=>['not in',$goods_ids]],$page,$pageNum);
foreach($data as $key => $vo){
if(!empty($vo['video'])){
$data[$key]['video'] = $domain_name.$vo['video'];
}
$voice = explode(',',$vo['voice']);
if(!empty($voice)){
foreach($voice as $key2 => $vo2){
$voice[$key2] = $domain_name.$vo2;
}
$data[$key]['voice'] = $voice;
}
$data[$key]['start_time'] = date('Y-m-d',$vo['start_time']);
$data[$key]['end_time'] = date('Y-m-d',$vo['end_time']);
$images = explode(',',$vo['images']);
if(!empty($images)){
foreach($images as $key3 => $vo3){
$images[$key3] = $domain_name.$vo3;
}
$data[$key]['images'] = $images;
}
}
$this->success('SUCCESS','',$data);
}
/**
* 收藏操作
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function collect(){
$type = $this->request->param('type',0,'intval');
$goods_id = $this->request->param('goods_id',0,'intval');
$user_id = $this->request->param('user_id',0,'intval');
if(empty($type) || empty($goods_id) || empty($user_id)){
$this->error('404');
}
if($type == 1){
//收藏
if(empty($collect)){
$arr['user_id'] = $user_id;
$arr['goods_id'] = $goods_id;
$arr['createtime'] = time();
Db::name('collect')->insert($arr);
}
}else if($type == 2){
//取消收藏
Db::name('collect')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->delete();
}
$this->success('SUCCESS');
}
/**
* 判断距离是否满足条件
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function distance_where(){
$param = $this->request->param();
$validate = new \think\Validate([
'user_id' => 'require',
'longitude' => 'require',
'latitude' => 'require',
'goods_id' => 'require',
]);
$validate->message([
'user_id' => 'user_id参数错误',
'longitude.require' => 'longitude参数错误!',
'latitude.require' => 'latitude参数错误!',
'goods_id.require' => 'goods_id参数错误!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$goods = Db::name('goods')->where(['id'=>$param['goods_id']])->find();
$user = Db::name('user')->where(['id'=>$param['user_id']])->find();
$user['age'] = getAge($user['birthday']);//年龄
$distance = distance($goods,$param);//根据两地经纬度获取距离
$is_distance = 1;
//判断距离条件是否满足
if($goods['distance'] != 0){
if($distance > $goods['distance']){
$is_distance = 0;
$this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
}
}
//判断性别
if($goods['sex'] != 0){
if($user['gender'] == $goods['sex']){
$is_distance = 0;
$this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
}
}
//判断年龄是否满足
if($goods['min_age'] != 0 || $goods['max_age'] != 0){
if($goods['min_age'] <= $user['age'] && $user['age'] <= $goods['max_age']){
$is_distance = 0;
$this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
}
}
$this->success('SUCCESS','',['is_where_satisfy'=>$is_distance]);
}
}