FriendController.php
10.2 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/11/7
* Time: 11:05
*/
namespace app\friendship\controller;
use think\Db;
use cmf\controller\HomeBaseController;
class FriendController extends HomeBaseController
{
//我的好友赠送记录(对外赠送)
public function friendGiveLog()
{
$data = Db::name('friendship')->alias('f')
->field('f.friend_uid as friend_id,f.total,u.user_nickname,u.avatar')
->join('user u', 'u.id=f.friend_uid')
->where(['f.uid' => session('user.id')])
->select()->toArray();
return $this->fetch(':friend_give_log', [
'list' => $data,
'balance' => Db::name('user')->where(['id' => session('user.id')])->value('balance')
]);
}
//好友列表(只允许搜索到非代理员)
public function friendList()
{
$request = request();
$where['role'] = ['eq', 1];
$where['user_type'] = ['eq', 2];
$where['id'] = ['neq', session('user.id')];
$keyword = $request->param('keyword');
if (!empty($keyword)) {
$where['mobile'] = ['like', "%$keyword%"];
}
$list = Db::name('user')
->field('id as friend_id,avatar,user_nickname')
->where($where)
->select()->toArray();
if (!empty($list)) {
return $this->fetch(':friend_list', [
'keyword' => $keyword,
'list' => $list,
'balance' => Db::name('user')->where(['id' => session('user.id')])->value('balance')
]);
} else {
$this->error('查无此人');
}
}
//赠送积分
public function donateIntegral()
{
$request = request();
if ($request->isAjax()) {
Db::startTrans();
$uid = session('user.id');
$total = $request->param('total');
//获取赠送者角色、父级、当前积分和赠送密码
$info = Db::name('user')->field('role,parent_id,balance,integral_pwd')->where(['id' => $uid])->find();
$integral_pwd = $request->param('integral_pwd');
//判断密码是否正确(加好友时不用验证密码)
if ($total > 0) {
if (md5($integral_pwd) != $info['integral_pwd']) {
echo json_encode(['msg' => '密码错误', 'status' => false]);
exit();
}
}
//判断此人积分是否足够赠送,且是否能被500整除
if ($info['balance'] < $total && $total >= 0) {
echo json_encode(['msg' => '您的积分不足', 'status' => false]);
exit();
} else {
if ($total % 500 != 0) {
echo json_encode(['msg' => '赠送积分必须是500积分的整数倍', 'status' => false]);
exit();
} else {
$parent_id = ($info['role'] == 2) ? $uid : $info['parent_id'];
//如果只是单纯加好友,则不绑定
if($total == 0) {
$parent_id = null;
}
$tag = true;
$friend_id = $request->param('friend_id');
$friend_parent_id = Db::name('user')->where(['id' => $friend_id])->value('parent_id');
//如果此人有父级id或本身就是代理员,检测被分享人是否有父级id,若没有则绑定关系
if (!empty($parent_id)) {
if (empty($friend_parent_id)) {
$bind = [
'id' => $friend_id,
'parent_id' => $parent_id
];
if (Db::name('user')->update($bind)) {
$tag = true;
} else {
$tag = false;
}
}
}
//判断两人是否首次赠送
if ($tag) {
$ship = [
'uid' => $uid,
'friend_uid' => $friend_id,
];
//非首次赠送,积分叠加
if (Db::name('friendship')->where($ship)->count()) {
if (Db::name('friendship')->where($ship)->setInc('total', $total)) {
$tag = true;
} else {
$tag = false;
}
} //首次赠送,插入好友表
else {
$ship['total'] = $total;
if (Db::name('friendship')->insert($ship)) {
$tag = true;
} else {
$tag = false;
}
}
//好友表建立后赠送者减积分,被赠送者加积分
if ($tag) {
//赠送时
if ($total > 0) {
if (Db::name('user')->where(['id' => $uid])->setDec('balance', $total)) {
if (Db::name('user')->where(['id' => $friend_id])->setInc('balance', $total)) {
//记录日志log
$log = [
0 => [
'uid' => $friend_id,
'create_time' => time(),
'balance' => $total,
'type' => 3
],
1 => [
'uid' => session('user.id'),
'create_time' => time(),
'balance' => $total,
'type' => 4
]
];
if (Db::name('zj_integral_log')->insertAll($log)) {
Db::commit();
$tag = true;
} else {
Db::rollback();
$tag = false;
}
} else {
Db::rollback();
$tag = false;
}
} else {
Db::rollback();
$tag = false;
}
} //只加好友
else {
Db::commit();
$tag = true;
}
} else {
Db::rollback();
$tag = false;
}
} else {
Db::rollback();
$tag = false;
}
if ($tag) {
if ($total == 0) {
$msg = '添加好友成功';
} else {
$msg = '赠送积分成功';
}
} else {
$msg = '未知错误';
}
echo json_encode(['msg' => $msg, 'status' => $tag, 'data' => Db::name('user')->where(['id' => session('user.id')])->value('balance')]);
exit();
}
}
}
}
//密码管理验证码页面
public function passwordManager()
{
return $this->fetch(':password_manager');
}
//输入密码
public function enterPassword()
{
return $this->fetch(':enter_password');
}
//确认密码
public function surePassword()
{
return $this->fetch(':sure_password');
}
public function pwd()
{
$request = request();
if ($request->isAjax()) {
$integral_pwd = $request->param('integral_pwd');
$step = $request->param('step');
if ($step == 'enter') {
session('user.pwd', $integral_pwd);
if (session('user.pwd')) {
$this->success('', url('friendship/Friend/surePassword'), true);
}
} elseif ($step == 'sure') {
if ($integral_pwd == session('user.pwd')) {
$data = [
'id' => session('user.id'),
'integral_pwd' => md5($integral_pwd)
];
if (Db::name('user')->update($data)) {
session('user.pwd', null);
$this->success('修改成功', url('user/center/myBalance'), true);
} else {
session('user.pwd', null);
$this->success('您未作出任何修改', url('user/center/myBalance'), true);
}
} else {
$this->success('密码不一致', '', false);
}
} else {
$this->success('未知错误', '', false);
}
}
}
public function checkPwd()
{
$pwd = Db::name('user')->where(['id' => session('user.id')])->value('integral_pwd');
if ($pwd == 'e10adc3949ba59abbe56e057f20f883e') {
$this->error('您还没修改过密码,系统默认密码为123456,如需修改请前往个人中心-积分密码管理进行修改', '', false);
} else {
$this->success('', '', true);
}
}
}