FriendController.php 10.2 KB
<?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);
        }
    }

}