ShareController.php 15.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/31
 * Time: 1:33
 */

namespace api\wxapp\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
/**
 * @title 钱包/分享日志模块
 * @description 钱包/分享日志模块
 */
class ShareController extends RestBaseController
{
    /**
     * @title 获取名片分享页信息
     * @description 分享/钱包
     * @url /wxapp/share/getShareInfo
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     *
     * @return rebate:分享返利金额
     * @return avatar:头像
     * @return money:我的佣金
     */
    public function getShareInfo() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        //获取用户信息
        $info = Db::name('user')
            ->alias('u')
            ->join('card c','u.id = c.user_id')
            ->where(['c.id' => $data['id']])
            ->field('u.id,u.avatar,u.money,u.num')
            ->find();
        if ($info === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $avg = $info['num'] == 0 ? 0 : $info['money']/$info['num'];
        $result['avatar'] = $info['avatar'];
        //获取该用户对应名片下的当前佣金
        if ($this->userId == $info['id']) {//业务员分享自己的名片不获利
            $result['rebate'] = 0;
        } else {
            $result['rebate'] = round($avg,2);
        }
        $money = Db::name('money_log')->where(['card_id'=>$data['id'],'user_id'=>$this->userId])->value('money');
        $result['money'] = $money ? : 0;
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取红包发放记录
     * @description 分享/钱包
     * @url /wxapp/share/getGiveOutLog
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     * @param name:page type:string require:0 other: desc:页码
     * @param name:per_page type:string require:0 other: desc:每页数据量
     *
     * @return total:总数据量
     * @return per_page:每页数据量
     * @return current_page:当前页
     * @return last_page:尾页
     * @return data:对象信息@!
     * @data avatar:头像 user_nickname:姓名 money:金额 pay_time:发放时间
     */
    public function getGiveOutLog() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        $per_page = !empty($data['per_page']) ? : config('per_page');
        $result = Db::name('share_log')
            ->alias('s')
            ->join('user u','s.user_id=u.id','left')
            ->where(['s.card_id' => $data['id'],'state' => 1])
            ->field('u.avatar,u.user_nickname,s.money,s.create_time pay_time')
            ->order('s.pay_time desc')
            ->paginate($per_page);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取领取记录
     * @description 分享/钱包 仅展示与登录人相关记录
     * @url /wxapp/share/getPullLog
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     * @param name:page type:string require:0 other: desc:页码
     * @param name:per_page type:string require:0 other: desc:每页数据量
     *
     * @return total:总数据量
     * @return per_page:每页数据量
     * @return current_page:当前页
     * @return last_page:尾页
     * @return data:对象信息@!
     * @data money:金额 pay_time:领取时间 avatar:用户头像
     */
    public function getPullLog() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        $per_page = !empty($data['per_page']) ? : config('per_page');
        $result = Db::name('share_log')
            ->alias('s')
            ->join('user u','s.user_id=u.id','left')
            ->where(['s.card_id' => $data['id'],'s.state' => 1,'s.user_id' => $this->userId])
            ->field('s.money,s.create_time pay_time,u.avatar')
            ->order('s.create_time desc')
            ->paginate($per_page);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取提现记录
     * @description 分享/钱包 仅展示与登录人相关记录
     * @url /wxapp/share/getWithDrawLog
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     * @param name:page type:string require:0 other: desc:页码
     * @param name:per_page type:string require:0 other: desc:每页数据量
     *
     * @return total:总数据量
     * @return per_page:每页数据量
     * @return current_page:当前页
     * @return last_page:尾页
     * @return data:对象信息@!
     * @data money:金额 pay_time:提现时间
     */
    public function getWithDrawLog() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        $per_page = !empty($data['per_page']) ? : config('per_page');
        $result = Db::name('withdraw_log')
             ->where(['card_id' => $data['id'],'state' => 1,'user_id' => $this->userId])
             ->field('money,pay_time')
             ->order('pay_time desc')
             ->paginate($per_page);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取充值记录
     * @description 分享/钱包 仅展示与登录人相关记录
     * @url /wxapp/share/getRechargeLog
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:page type:string require:0 other: desc:页码
     * @param name:per_page type:string require:0 other: desc:每页数据量
     *
     * @return total:总数据量
     * @return per_page:每页数据量
     * @return current_page:当前页
     * @return last_page:尾页
     * @return data:对象信息@!
     * @data money:金额 pay_time:提现时间
     */
    public function getRechargeLog() {
        $per_page = !empty($data['per_page']) ? : config('per_page');
        $result = Db::name('recharge_log')
            ->where(['state' => 1,'user_id' => $this->userId])
            ->field('money,pay_time')
            ->order('pay_time desc')
            ->paginate($per_page);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * 首先创建二维码ticket
     * @param string $sceneid 场景值ID
     * @param string $type 值为'temp'的时候生成临时二维码
     * @param string $expire_seconds 二维码过期时间
     * @return string 二维码ticket
     */
    public function _getTicket($sceneid,$type='temp',$expire_seconds=604800){
        if($type=='temp'){
            $data = '{"expire_seconds": %s, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
            $data = sprintf($data,$expire_seconds,$sceneid);
        }else{
            $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
            $data = sprintf($data,$sceneid);
        }   $curl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->_getAccessToken();
        $content = $this->_request($curl,true,'POST',$data);
        $cont = json_decode($content);
        return $cont->ticket;
    }
    //然后凭借ticket到指定URL换取二维码
    public function _getQRCode($sceneid,$type='temp',$expire_seconds=604800){
        $ticket = $this->_getTicket($sceneid,$type,$expire_seconds);
        $curl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
        $content = $this->_request($curl);
        return $content;
    }

    /**
     * @title 获取我的钱包
     * @description 分享/钱包 仅展示与登录人相关记录
     * @url /wxapp/share/getMyWallet
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:page type:string require:0 other: desc:页码
     * @param name:per_page type:string require:0 other: desc:每页数据量
     *
     * @return total:总数据量
     * @return per_page:每页数据量
     * @return current_page:当前页
     * @return last_page:尾页
     * @return data:对象信息@!
     * @data card_id:名片id name:名片名称 money:总佣金
     */
    public function getMyWallet() {
        $per_page = !empty($data['per_page']) ? : config('per_page');
        $result = Db::name('money_log')
            ->alias('m')
            ->join('card c','m.card_id = c.id')
            ->where(['m.user_id' => $this->userId])
            ->field('m.card_id,m.money,c.name')
            ->order('m.create_time desc')
            ->paginate($per_page);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 分享获利接口
     * @description 分享/钱包
     * @url /wxapp/share/shareLink
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:card_id type:string require:0 other: desc:分享名片id
     * @param name:user_id type:string require:0 other: desc:分享人id
     *
     */
    public function shareLink() {
        $validate = new Validate([
            'card_id'           => 'require',
            'user_id'           => 'require',
        ]);
        $validate->message([
            'card_id.require'           => '缺少参数card_id!',
            'user_id.require'           => '缺少参数user_id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        //获取用户信息
        $info = Db::name('user')
            ->alias('u')
            ->join('card c','u.id = c.user_id')
            ->field('u.id,u.money,u.num')
            ->where(['c.id' => $data['card_id']])
            ->find();
        //检查一下当前登录人是否获利过
        $check = Db::name('card_log')->where(['card_id'=>$data['card_id'],'user_id'=>$this->userId])->find();
        if ($check) {
            $this->error(['code'=>'40010','msg'=>'已关注过该名片']);
        }
        if ($info['num'] == 0 || $info['money'] == 0) {//奖励金不足就不给奖励金了 正常分享
            $arr2 = [
                'card_id'=>$data['card_id'],
                'user_id'=>$this->userId,
                'share_id'=>$data['user_id']
            ];
            Db::name('card_log')->insert($arr2);
            $this->error(['code'=>'40009','msg'=>'奖励金不足']);
        }
        //业务员自己 不能关注自己的名片
        if ($info['id'] == $this->userId) {
            $this->error(['code'=>'40010','msg'=>'已关注过该名片']);
        }
        $money = round($info['money']/$info['num'],1);
        Db::startTrans();
        if ($info['id'] == $data['user_id']) {
            $res1 = true;
            $res2 = true;
        } else {
            //业务员 减
            $array = [
                'money' => ['exp','money - '.$money],
                'num' => ['exp','num - 1']
            ];
            $res1 = Db::name('user')->where(['id'=>$info['id']])->update($array);
            //分享人 加
            $arr = [
                'card_id'=>$data['card_id'],
                'user_id'=>$data['user_id'],
                'money'=>$money
            ];
            $res2 = Db::name('share_log')->insert($arr);
            //处理佣金记录
            $check_log = Db::name('money_log')->where(['card_id' => $data['card_id'],'user_id' => $data['user_id']])->find();
            if ($check_log) {//有该用户的此名片佣金记录 做更新
                $array2 = [
                    'money' => ['exp','money +'.$money]
                ];
                $condition = [
                    'card_id' => $data['card_id'],
                    'user_id' => $data['user_id']
                ];
                Db::name('money_log')->where($condition)->update($array2);
            } else {
                $update_arr = [
                    'card_id' => $data['card_id'],
                    'user_id' => $data['user_id'],
                    'money' => $money,
                ];
                Db::name('money_log')->insert($update_arr);
            }
        }
        //分享记录
        $arr2 = [
            'card_id'=>$data['card_id'],
            'user_id'=>$this->userId,
            'share_id'=>$data['user_id']
        ];
        $res3 = Db::name('card_log')->insert($arr2);
        if ($res1 === false || $res2 === false || $res3 === false) {
            Db::rollback();
            $this->error(['code'=>'40000','msg'=>'操作失败']);
        }
        Db::commit();
        $this->success('操作成功');
    }
}