PrizeController.php
8.4 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\portal\service\LikeService;
use cmf\controller\HomeBaseController;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
class PrizeController extends WeChatBaseController
{
protected $options;
public function _initialize()
{
parent::_initialize();
$this->checkWeChatUserLogin();
$this->options = [
'app_id' => config('wechat_config.app_id'),
'secret' => config('wechat_config.secret'),
'payment' => config('wechat_config.payment'),
];
}
//兑奖首页
public function prize(){
$re=$this->checkUser();
$user=Db::name('users')->where('id',$re['id'])->find();
$prize=Db::name('prize')->where('status',1)->select();
$category=Db::name('prize_category')->select();
$this->assign('user',$user);
$this->assign('list',$prize);
$this->assign('category',$category);
return $this->fetch();
}
//兑奖首页ajax
public function prizeAjax(){
$this->checkUser();
$category=$this->request->param('category');
$where['status']=1;
if (!empty($category)){
$where['category_id']=$category;
}
$data=Db::name('prize')->where($where)->select()->toArray();
foreach ($data as $k => $v) {
$data[$k]['url']=url('portal/prize/prizeDetail',array('id'=>$data[$k]['id']));
$data[$k]['image'] = cmf_get_image_preview_url($data[$k]['thumbnail']);
};
return $data;
}
//兑奖详情页
public function prizeDetail(){
$re=$this->checkUser();
$id=$this->request->param('id');
$data=Db::name('prize')
->where('id',$id)
->find();
$data['more']=json_decode($data['more'],true);
$data['content']=cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
$like=Db::name('like')->where(['cid'=>$id,'users_id'=>$re['id'],'status'=>3])->find();
if ($like){
$like=1;
}else{
$like=0;
}
$order=Db::name('prize_order')->where(['prize_id'=>$id,'users_id'=>$re['id'],'status'=>0])->find();
if ($order){
$order=1;
}else{
$order=0;
}
$comment=Db::name('comment')
->alias('c')
->join('users u','c.users_id=u.id')
->join('brand b','c.cid=b.id')
->where(['b.id'=>$id,'c.status'=>3])
->field('c.*,u.*')
->select();
$this->assign('comment',$comment);
$this->assign('order',$order);
$this->assign('like',$like);
$this->assign('list',$data);
return $this->fetch('prize_detail');
}
//点赞ajax接口
public function like(){
$re=$this->checkUser();
$param=$this->request->param();
$like=new LikeService();
return $like->like($param['id'],3,$re['id']);
}
public function comment(){
$this->checkUser();
$id=$this->request->param('id');
$this->assign('id',$id);
return $this->fetch();
}
public function commentPost(){
$re=$this->checkUser();
$param=$this->request->param();
$param['create_time']=time();
$param['status']=3;
$param['users_id']=$re['id'];
$result=Db::name('comment')->where(['users_id'=>$param['users_id'],'status'=>3,'cid'=>$param['cid']])->find();
if ($result){
return 2;
}
$re1=Db::name('comment')->insert($param);
$re2=Db::name('prize')->where('id',$param['cid'])->setInc('comment',1);
if ($re1 && $re2) {
$url = url('portal/prize/prizeDetail', array('id' => $param['cid']));
return $url;
}else{
return 0;
}
}
//兑换奖品
public function getPrize(){
$re=$this->checkUser();
$id=$this->request->param('id');
if(Db::name('users')->where('id',$re['id'])->find()['score']<Db::name('prize')->where('id',$id)->find()['score']){
return 0;
}
$data['prize_id']=$id;
$data['users_id']=$re['id'];
$data['create_time']=time();
Db::name('prize_order')->insert($data);
return 1;
}
public function getScore(){
$this->checkUser();
$app = new Application($this->options);
$js = $app->js;
$jss = $js->config(['chooseImage', 'uploadImage', 'previewImage'], $debug = true, $beta = false, $json = true);
$this->assign('js',$jss);
return $this->fetch('getScore');
}
//上传小票提交
public function uploadPost(){
$re=$this->checkUser();
$param = $this->request->param();
$app = new Application($this->options);
// 获取 access token 实例
$access_token = $app->access_token;
$token = $access_token->getToken();
$save_name=$this->getmedia($token, $param['media'], date('Ymd'));
Db::name('users_score')->insert(['users_id'=>$re['id'],'create_time'=>time(),'thumbnail'=>$save_name]);
return 1;
}
private function getmedia($access_token, $media_id, $foldername)
{
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $media_id;
if (!file_exists("./upload/" . $foldername)) {
mkdir("./upload/" . $foldername, 0777, true);
}
$file_name = date('YmdHis') . rand(1000, 9999) . '.jpg';
$targetName = './upload/' . $foldername . '/' . $file_name;
$saveName = './upload/' . $foldername . '/' . $file_name;
$ch = curl_init($url); // 初始化
$fp = fopen($targetName, 'wb'); // 打开写入
curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $saveName;
}
public function uploadSuccess(){
return $this->fetch('successupload');
}
//兑奖记录
public function prizeRecord(){
$status=$this->request->param('status');
$where['p.status']=!empty($status)?$status:0;
$data=Db::name('prize_order')
->alias('po')
->join('prize p'.'po.prize_id=p.id')
->where($where)
->select();
$this->assign('list',$data);
return $this->fetch();
}
//个人首页
public function userIndex(){
$last = Db::name('users_score_log')->where(['users_id' => session('wechat_user')['users_id'], 'action' => '签到'])->max('create_time');
if (date('Ymd', time()) > date('Ymd', $last)) {
$is_qiandao=0;
}else{
$is_qiandao=1;
}
$data=Db::name('users')->where('id',session('wechat_user')['users_id'])->find();
$this->assign('list',$data);
$this->assign('qiandao',$is_qiandao);
return $this->fetch();
}
public function scoreRecord(){
$score=Db::name('users')->where('id',session('wechat_user')['users_id'])->find()['score'];
$data=Db::name('users_score_log')->where('users_id',session('wechat_user')['users_id'])->select();
$this->assign('score',$score);
$this->assign('list',$data);
return $this->fetch();
}
//每日签到功能
public function qiandao(){
$last = Db::name('users_score_log')->where(['users_id' => session('wechat_user')['users_id'], 'action' => '签到'])->max('create_time');
if (date('Ymd', time()) > date('Ymd', $last)) {
$data['score'] = 10;
$data['users_id'] = session('users_id');
$data['create_time'] = time();
$data['action'] = '签到';
Db::name('users_score_log')->insert($data);
Db::name('users')->where('id', session('wechat_user')['users_id'])->setInc('score', 10);
return 1;
} else {
return 0;
}
}
}