Member.php 17.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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
<?php

namespace app\api\controller;

use app\api\controller\Pay;
use app\api\model\OrderDetail;
use app\api\model\OrderInfo;
use app\api\model\Order;
use app\api\model\OrderMoney;
use app\api\model\UserAddress;
use app\api\model\UserMoneyLog;
use app\api\model\UserScoreLog;
use app\api\model\UserTicket;
use app\common\controller\Api;
use think\Config;
use think\Db;
use app\api\model\User;
use think\Validate;

/**
 * 会员接口
 */
class Member extends Api
{
    protected $userModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->userModel = new User();
    }

    /**
     * @ApiTitle    (用户信息)
     * @ApiSummary  (用户信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/getUserInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data": {
    "id": 1, 用户id
    "nickname": "admin1", 昵称
    "avatar": "http://q7s0a1rb4.bkt.clouddn.com/assets/img/qrcode.png", 头像
    "type": "2", 用户类型:1=普通用户,2=会员,3=员工,4=代理
    "type_text": "会员"
    "money": "1"    余额
    "score": "1"     积分
    }
    })
     */
    public function getUserInfo()
    {
        $userId = $this->getUserId();

        $userInfo = $this->userModel->where('id', $userId)->field('id,nickname,avatar,type,money,score')->find();

        $this->success('SUCCESS', $userInfo);
    }

    /**
     * @ApiTitle    (我的优惠券)
     * @ApiSummary  (我的优惠券)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/getUserTicket)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=status, type=string, required=true, description="状态:1=未使用,2=已使用,3=已过期")
     * @ApiParams  (name=price, type=string, required=false, description="价格 确认订单页传,用于查询满减优惠价")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587558975",
    "data": {
    "total": 1,
    "list": [
    {
    "user_ticket_id": 1, 用户优惠券id
    "pasttime": "2017-07-10 18:28",  截至时间
    "price": "5.00",   优惠价格
    "fill_price": "100",   满减使用价格 有值为满减优惠券,返回空值为抵扣券
    "ch_name": "邀请用户",  优惠券中文名称
    "en_name": null   优惠券英文名称
    }
    ]
    }
    })
     */
    public function getUserTicket()
    {
        $model = new UserTicket();
        $userId = $this->getUserId();
        $status = $this->request->param('status');
        $page = $this->request->param('page');
        $price = $this->request->param('price',0);
        $limit = Config::get('paginate.index_rows');

        if (empty($status)) $this->error('缺少参数 status!');
        $where['user_id'] = $userId;
        $where['status'] = $status;
        if (!empty($price)) $where['ut.fill_price'] = null;
        else $price = 0;

        $data = $model->selectPageData($where, $page, $limit,$this->lang,$price);

        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (我的绑定)
     * @ApiSummary  (我的绑定)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/getUserLower)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data": {
    "user_id": 1, 用户id
    "nickname": "admin1", 昵称
    "avatar": "http://q7s0a1rb4.bkt.clouddn.com/assets/img/qrcode.png", 头像
    }
    })
     */
    public function getUserLower()
    {
        $userId = $this->getUserId();
        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');

        $where['pid'] = $userId;

        $data = $this->userModel->selectPageData($where, $page, $limit);

        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (余额明细)
     * @ApiSummary  (余额明细)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/getMoneyLog)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=date, type=string, required=true, description="日期 例2020-2-1")
     * @ApiParams  (name=type, type=string, required=false, description="类型:1=支出,2=收入")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    "total": 3,
    "list": [
    {
    "money": "145.50",
    "after": "9769.25",
    "type": "3", 类型:1=提现,2=充值,3=购物消费,4=佣金收入,5=工资发放,6=退款
    "createtime": "2020-05-11 16:07",
    "type_text": "购物消费"
    }
    },
    }
    })
     */
    public function getMoneyLog()
    {
        $model = new UserMoneyLog();
        $userId = $this->getUserId();
        $page = $this->request->param('page');
        $date = $this->request->param('date');
        $type = $this->request->param('type');

        $limit = Config::get('paginate.index_rows');

        $betweenTime = ['1970-1-1', date('Y-m-d')];
        if ($date) {
            $dateArr = explode('-', $date);
            $lastDay = days_in_month($dateArr[1], $dateArr[0]);
            $lastDate = $dateArr[0] . '-' . $dateArr[1] . '-' . $lastDay;
            $betweenTime = [$date, $lastDate];
        }

        if (!empty($type)){
            if ($type == 1) $where['type'] = ['in','1,3'];
            else $where['type'] = ['in','2,4,5,6'];
        }

        $where['user_id'] = $userId;

        $data = $model->selectPageData($where, $page, $limit, $betweenTime);

        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (积分明细)
     * @ApiSummary  (积分明细)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/getScoreLog)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=date, type=string, required=true, description="日期 例2020-2-1")
     * @ApiParams  (name=type, type=string, required=false, description="类型:1=支出,2=收入")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587560895",
    "data": {
    "total": 1,
    "list": [
    {
    "score": "10.00", 变更积分
    "after": "10.00",  变更后积分
    "type": "type", //类型:1=购物返积分,2=积分商城兑换
    "createtime": "2020-04-20 16:13"
    }
    ]
    }
    })
     */
    public function getScoreLog()
    {
        $model = new UserScoreLog();
        $userId = $this->getUserId();
        $page = $this->request->param('page');
        $date = $this->request->param('date');
        $type = $this->request->param('type');
        $limit = Config::get('paginate.index_rows');
        $betweenTime = ['1970-1-1', date('Y-m-d')];
        if ($date) {
            $dateArr = explode('-', $date);
            $lastDay = days_in_month($dateArr[1], $dateArr[0]);
            $lastDate = $dateArr[0] . '-' . $dateArr[1] . '-' . $lastDay;
            $betweenTime = [$date, $lastDate];
        }
        if (!empty($type)){
            if ($type == 1) $where['type'] = 2;
            else $where['type'] = 1;
        }
        $where['user_id'] = $userId;
        $data = $model->selectPageData($where, $page, $limit, $betweenTime);

        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/getOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=type, type=string, required=true, description="1=我的订单2=我的团单")
     * @ApiParams  (name=page, type=string, required=true, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587560895",
    "data": {
    "total": 1,
    "list": [
    {
    "score": "10.00", 变更积分
    "after": "10.00",  变更后积分
    "type": "type",
    "createtime": "2020-04-20 16:13"
    }
    ]
    }
    })
     */
    public function getOrder()
    {
        $orderInfoModel = new OrderInfo();
        $orderDetailModel = new OrderDetail();
        $orderModel = new Order();
        $userId = $this->getUserId();
        $type = $this->request->param('type');
        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');
        if (empty($type)) $this->error('缺少参数 type!');
        $where = ['user_id' => $userId];
        if ($type == 1) $where['type'] = ['neq', 3];
        else $where['type'] = ['eq', 3];
        $list = $orderModel->selectPageData($where, $page, $limit);
        $data = [];
        foreach ($list['list'] as $k => $order) {
            if ($order['status'] == -1) {
                $order['status_text'] = '已取消';
                $order['goods_image'] = cdnurl_arr($orderInfoModel->where('order_id', $order['order_id'])->column('goods_image'));
                $data[] = $order;
            } elseif ($order['status'] == 1) {
                $order['status_text'] = '待付款';
                if ($order['type'] == 3 && $order['team_group_status'] == 3){
                    $order['status_text'] = '拼团失败';
                    $order['status'] = 7;
                }
                $order['goods_image'] = cdnurl_arr($orderInfoModel->where('order_id', $order['order_id'])->column('goods_image'));
                $data[] = $order;
            } elseif ($order['status'] == 2) {
                $detailInfo = $orderDetailModel->where('order_id', $order['order_id'])->field('id,status')->select();
                foreach ($detailInfo as $key => $detail) {
                    $tmp['order_id'] = $order['order_id'];
                    $tmp['order_detail_id'] = $detail['id'];
                    $tmp['team_group_id'] = $order['team_group_id'];
                    $tmp['order_num'] = $order['order_num'];
                    $tmp['createtime'] = $order['createtime'];
                    $tmp['pay_total'] = 0;
                    $tmp['status'] = $detail['status'];
                    $tmp['status_text'] = $detail['status_text'];
                    $tmp['goods_image'] = [];
                    if ($order['type'] == 3 && $order['team_group_status'] == 1){
                        $tmp['status_text'] = '待成团';
                        $tmp['status'] = 6;
                    }
                    $orderInfo = $orderInfoModel->where(['order_detail_id' => $detail['id']])->field('goods_image,goods_total')->select();
                    foreach ($orderInfo as $kk => $info) {
                        $tmp['pay_total'] += $orderInfo[$kk]['goods_total'];
                        $tmp['goods_image'][] = $orderInfo[$kk]['goods_image'];
                    }
                    $data[] = $tmp;
                }
            } elseif ($order['status'] == 3) {
                $order['status_text'] = '已退款';
                $order['status'] = 8;
                $order['goods_image'] = cdnurl_arr($orderInfoModel->where('order_id', $order['order_id'])->column('goods_image'));
                $data[] = $order;
            }
        }
        $this->success('SUCCESS', $data);
    }

    /**
     * @ApiTitle    (余额充值)
     * @ApiSummary  (余额充值)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/recharge)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=money, type=string, required=true, description="充值金额")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587560895",
    "data": {
    }
    })
     */
    public function recharge()
    {
        $orderMoneyModel = new OrderMoney();
        $userId = $this->getUserId();
        $money = $this->request->param('money');
        if (empty($money)) $this->error('缺少参数 money!');
        $pay = new Pay();
        $order_num = get_order_num() . '1';
        $openid = Db::name('third')->where('user_id', $userId)->value('openid');
        $res = $pay->wx_pay($order_num, '充值', $money, 'api/pay/recharge_notify', $openid);
        $resArr = json_decode($res);
        if ($resArr) {
            $orderMoneyData = [
                'pay_type' => '1',
                'order_num' => $order_num,
                'user_id' => $userId,
                'total' => $money,
                'status' => '1',
                'type' => '1',  //类型:1=充值,2=购买会员,3=续费会员
            ];
            $orderMoneyModel->save($orderMoneyData);
            $this->success('success', $resArr);
        } else {
            $this->error($res);
        }
    }

    /**
     * @ApiTitle    (余额提现)
     * @ApiSummary  (余额提现)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/withdrawal)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=money, type=string, required=true, description="提现金额")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587560895",
    "data": {
    }
    })
     */
    public function withdrawal()
    {
        $userId = $this->getUserId();
        $money = $this->request->param('money');
        if (empty($money)) $this->error('缺少参数 money!');
        $userInfo = $this->userModel->where('id', $userId)->find();
        if ($userInfo['type'] == 3 || $userInfo['type'] == 4) {
            $this->error('员工及代理不可以提现!');
        }
        if ($userInfo['money'] < $money) $this->error('余额不足!');
        $openid = Db::name('third')->where('user_id', $userId)->value('openid');
        $pay = new Pay();
        $withdraw = $pay->merchantPay($openid, $userInfo['nickname'], $money, '商户提现');

        if ($withdraw['return_code'] == 'SUCCESS') {
            if ($withdraw['result_code'] == 'SUCCESS') {
                // 提现成功
                $before = $userInfo['money'];
                $after = $before - $money;
                $logData = [
                    'user_id' => $userId,
                    'money' => $money,
                    'before' => $before,
                    'after' => $after,
                    'type' => 1,
                ];
                $res = $this->userModel->withdrawal($logData);
                if ($res) $this->success('SUCCESS');
                else $this->error('ERROR');
            } else {
                $this->error($withdraw['err_code_des']);
            }
        } else {
            $this->error($withdraw['return_msg']);
        }
    }

    /**
     * @ApiTitle    (购买会员)
     * @ApiSummary  (购买会员)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/buyVip)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587560895",
    "data": {
    }
    })
     */
    public function buyVip()
    {
        $orderMoneyModel = new OrderMoney();
        $userId = $this->getUserId();
        $pay = new Pay();
        $order_num = get_order_num() . '4';
        $openid = Db::name('third')->where('user_id', $userId)->value('openid');
        $money = config('site.vip_price');
        $res = $pay->wx_pay($order_num, '购买会员', $money, 'api/pay/buy_vip_notify', $openid);
        $resArr = json_decode($res);
        if ($resArr) {
            $orderMoneyData = [
                'pay_type' => '1',
                'order_num' => $order_num,
                'user_id' => $userId,
                'total' => $money,
                'status' => '1',
                'type' => '2',  //类型:1=充值,2=购买会员,3=续费会员
            ];
            $orderMoneyModel->save($orderMoneyData);
            $this->success('success', $resArr);
        } else {
            $this->error($res);
        }
    }

    /**
     * @ApiTitle    (续费会员)
     * @ApiSummary  (续费会员)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/member/renewVip)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587560895",
    "data": {
    }
    })
     */
    public function renewVip()
    {
        $orderMoneyModel = new OrderMoney();
        $userId = $this->getUserId();
        $pay = new Pay();
        $order_num = get_order_num() . '5';
        $openid = Db::name('third')->where('user_id', $userId)->value('openid');
        $money = config('site.vip_price');
        $res = $pay->wx_pay($order_num, '续费会员', $money, 'api/pay/renew_vip_notify', $openid);
        $resArr = json_decode($res);
        if ($resArr) {
            $orderMoneyData = [
                'pay_type' => '1',
                'order_num' => $order_num,
                'user_id' => $userId,
                'total' => $money,
                'status' => '1',
                'type' => '3',  //类型:1=充值,2=购买会员,3=续费会员
            ];
            $orderMoneyModel->save($orderMoneyData);
            $this->success('success', $resArr);
        } else {
            $this->error($res);
        }
    }
}