diff --git a/app/friendship/controller/FriendController.php b/app/friendship/controller/FriendController.php index 3433aeb..e5d6101 100644 --- a/app/friendship/controller/FriendController.php +++ b/app/friendship/controller/FriendController.php @@ -45,7 +45,7 @@ class FriendController extends HomeBaseController return $this->fetch(':friend_list', [ 'keyword' => $keyword, 'list' => $list, - 'balance' => Db::name('user')->where(['id'=>session('user.id')])->value('balance') + 'balance' => Db::name('user')->where(['id' => session('user.id')])->value('balance') ]); } else { $this->error('查无此人'); @@ -60,73 +60,83 @@ class FriendController extends HomeBaseController Db::startTrans(); $uid = session('user.id'); $total = $request->param('total'); - //获取赠送者角色、父级和当前积分 - $info = Db::name('user')->field('role,parent_id,balance')->where(['id' => $uid])->find(); - //判断此人积分是否足够赠送,且是否能被500整除 - if ($info['balance'] < $total && $total >= 0) { - echo json_encode(['msg' => '您的积分不足', 'status' => false]); + //获取赠送者角色、父级、当前积分和赠送密码 + $info = Db::name('user')->field('role,parent_id,balance,integral_pwd')->where(['id' => $uid])->find(); + $integral_pwd = $request->param('integral_pwd'); + //判断密码是否正确 + if (md5($integral_pwd) != $info['integral_pwd']) { + echo json_encode(['msg' => '密码错误', 'status' => false]); exit(); } else { - if ($total % 500 != 0) { - echo json_encode(['msg' => '赠送积分必须是500积分的整数倍', 'status' => false]); + //判断此人积分是否足够赠送,且是否能被500整除 + if ($info['balance'] < $total && $total >= 0) { + echo json_encode(['msg' => '您的积分不足', 'status' => false]); exit(); } else { - $parent_id = ($info['role'] == 2) ? $uid : $info['parent_id']; - $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 ($total % 500 != 0) { + echo json_encode(['msg' => '赠送积分必须是500积分的整数倍', 'status' => false]); + exit(); + } else { + $parent_id = ($info['role'] == 2) ? $uid : $info['parent_id']; + $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) { - //赠送时 - 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 = [ - 'uid' => $friend_id, - 'create_time' => time(), - 'balance' => $total, - 'type' => 3 - ]; - if (Db::name('zj_integral_log')->insert($log)) { - Db::commit(); - $tag = true; + $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 = [ + 'uid' => $friend_id, + 'create_time' => time(), + 'balance' => $total, + 'type' => 3 + ]; + if (Db::name('zj_integral_log')->insert($log)) { + Db::commit(); + $tag = true; + } else { + Db::rollback(); + $tag = false; + } } else { Db::rollback(); $tag = false; @@ -135,36 +145,33 @@ class FriendController extends HomeBaseController Db::rollback(); $tag = false; } - } else { - Db::rollback(); - $tag = false; + } //只加好友 + else { + Db::commit(); + $tag = true; } - } //只加好友 - else { - Db::commit(); - $tag = true; + } else { + Db::rollback(); + $tag = false; } } else { Db::rollback(); $tag = false; } - } else { - Db::rollback(); - $tag = false; - } - if ($tag) { - if ($total == 0) { - $msg = '添加好友成功'; + if ($tag) { + if ($total == 0) { + $msg = '添加好友成功'; + } else { + $msg = '赠送积分成功'; + } } else { - $msg = '赠送积分成功'; + $msg = '未知错误'; } - } else { - $msg = '未知错误'; - } - echo json_encode(['msg' => $msg, 'status' => $tag]); - exit(); + echo json_encode(['msg' => $msg, 'status' => $tag]); + exit(); + } } } } diff --git a/public/themes/simpleboot3/friendship/friend_list.html b/public/themes/simpleboot3/friendship/friend_list.html index f8885ff..c0c4229 100644 --- a/public/themes/simpleboot3/friendship/friend_list.html +++ b/public/themes/simpleboot3/friendship/friend_list.html @@ -410,12 +410,9 @@ dataType:"JSON", success: function (data) { - if(data.status === true) { - alert(data.msg); - window.location.reload(); - }else { - alert(data.msg); - } + alert(data.msg); + //清空数据 + } }) }