GoodsController.php
7.1 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
<?php
namespace api\portal\controller;
use cmf\controller\RestBaseController;
use Think\Db;
/**
* @title 积分商城
* @description 积分商城相关接口
* @header name:key require:1 default: desc:秘钥(区别设置)
* @param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
*/
class GoodsController extends RestBaseController
{
/**
* @title 积分商城
* @description 积分商城首页渲染
* @author panhaowen
* @url /portal/Goods/index
* @method POST
*/
public function index()
{
$data=Db::name('goods')->where('status',1)->field('id,goods_name,before_price,now_price,vip_price,content')->select()->each(function ($item) {
$item['image'] =cmf_get_image_preview_url($item['image']);
return $item;
})->toArray();
$this->success('商城首页信息',$data);
}
/**
* @title 订单详情
* @description 订单详情页面
* @author panhaowen
* @url /portal/Goods/goodsOrder
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @param name:id type:int require:1 other: desc:商品id
*/
public function goodsOrder()
{
$param=$this->request->param();
$data=Db::name('goods')->where('id',$param['id'])->field('id,goods_name,before_price,now_price,vip_price,content')->find();
$status=Db::name('user')->where('id',$this->getUserId())->find()['status'];
if ($status==1){
$data['price']=$data['now_price'];
}else if ($status==2){
$data['price']=$data['vip_price'];
}
$data['image']=mf_get_image_preview_url($data['image']);
$data['address']=Db::name('address')->where(['user_id'=>$this->getUserId(),'status'=>1])->field('id,address')->find();
$this->success('订单详情信息',$data);
}
/**
* @title 立即兑换
* @description 立即兑换接口
* @author panhaowen
* @url /portal/Goods/goodsOrder
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @param name:goods_id type:int require:1 other: desc:商品id
* @param name:address_id type:int require:1 other: desc:收货地址id
* @param name:remark type:text require:0 other: desc:备注
* @param name:count type:int require:1 other: desc:商品数量
* @param name:price type:int require:1 other: desc:商品数量
*/
public function orderPost()
{
$param=$this->request->param();
$user_score=Db::name('user')->where('id',$this->getUserId())->find()['score'];
if ($user_score<$param['price']){
$this->error('用户积分不足',2);
}
$re=Db::name('goods_order')->insert([
'user_id'=>$this->getUserId(),
'goods_id'=>$param['goods_id'],
'real_price'=>$param['price'],
'count'=>$param['count'],
'remark'=>$param['remark'],
'order_sn'=>cmf_get_order_sn(),
'create_time'=>time(),
'address_id'=>$param['address_id']
]);
if ($re) {
Db::name('user')->where('id',$this->getUserId())->setDec('score',$param['score']);
$this->success('兑换成功',1);
}else{
$this->error('兑换失败',3);
}
}
/**
* @title 日打卡提交
* @description 麦夫子日打卡提交接口
* @author panhaowen
* @url /portal/Sign/daySignPost
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @param name:sign type:int require:1 other: desc:日打卡种类
* @return slogan:口号
*/
public function daySignPost(){
$param=$this->request->param();
//根据星期判断music,image
$date=date("w",time()); //这个可以获取数字星期比如123,注意0是星期日
$config=Db::name('sign_image')->where('date',$date)->find();
$music=json_decode($config['music'],true);
$music=$music[rand(0,count($music,0)-1)];
//根据签到种类判断增加积分数
$score=Db::name('config')->where('id',1)->find();
if ($param['status']==1){
$score=$score['morning_sign_score'];
$type='morning';
}elseif ($param['status']==2){
$score=$score['noon_sign_score'];
$type='noon';
}elseif ($param['status']==3){
$score=$score['night_sign_score'];
$type='night';
}elseif ($param['status']==4){
$score=$score['sleep_sign_score'];
$type='sleep';
}
//将签到写入记录
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$yesterday = strtotime(date("Y-m-d",strtotime("-1 day")));
//已有记录
$re=Db::name('user_sign')->where(['user_id'=>$this->getUserId(),'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
if ($re){
//更新打卡
Db::name('user_sign')->where('id',$re['id'])->update(["$type"=>time()]);
//判断是否打满了4次
if (!empty($re['morning'])&&!empty($re['noon'])&&!empty($re['night'])&&!empty($re['sleep'])){
Db::name('user_sign')->where('id',$re['id'])->update(['status'=>1]);
Db::name('user')->where('id',$this->getUserId())->setInc('all_sign_day',1);
//判断是否为连续打卡
$last=Db::name('user_sign')->where(['user_id'=>$this->getUserId(),'status'=>1,'create_time'=>[ 'between time', [$yesterday,$beginToday]]])->find();
$user=Db::name('user')->where('id',$this->getUserId())->find();
if ($user['stay_sign_day']==0 ||$last)
{
Db::name('user')->where('id',$this->getUserId())->setInc('all_sign_day',1);
//判断最大连续打卡天数
if ($user['stay_sign_day']>$user['longest_sign_day']){
Db::name('user')->where('id',$this->getUserId())->update(['longest_sign_day'=>$user['stay_sign_day']]);
}
}
}
}
//无记录
else{
Db::name('user_sign')->insert(['user_id'=>$this->getUserId(),'create_time'=>time(),"$type"=>time()]);
}
//获取打卡积分
Db::name('user')->where('id',$this->getUserId())->setInc('score',$score);
//判断是否需要进行周打卡
if (!Db::name('user_week_sign')->where('user_id',$this->getUserId())->find()){
$is_week=1;
}elseif (Db::name('user_week_sign')->where(['user_id'=>$this->getUserId(),'create_time'=>[ '<', $beginToday-($score['week_sign']*24*60*60)]])->find()){
$is_week=1;
}else{
$is_week=0;
}
//返回
$this->success('日打卡提交结果',array(
'background'=>cmf_get_image_preview_url($config['image']),
'music'=>cmf_get_file_download_url($music['url']),
'music_name'=>$music['name'],
'score'=>$score,
'week'=>$is_week
));
}
}