ShareController.php 15.3 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
<?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')
            ->alias('w')
            ->join('user u','w.user_id=u.id','left')
             ->where(['w.card_id' => $data['id'],'w.state' => 1,'w.user_id' => $this->userId])
             ->field('w.money,w.pay_time,u.avatar')
             ->order('w.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:总佣金 avatar:头像
     */
    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')
            ->join('user u','c.user_id = u.id')
            ->where(['m.user_id' => $this->userId])
            ->field('m.card_id,m.money,c.name,u.avatar')
            ->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'],2);
        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('操作成功');
    }
}