123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- namespace app\api\controller\members;
- use app\api\controller\Controller;
- use app\api\model\Coupon;
- use app\api\model\Goods as GoodsModel;
- use app\api\model\member\MemberCardOrder;
- use app\api\model\member\MemberCards;
- use app\api\model\member\MemberGoods;
- use app\api\model\member\MemberWelfare;
- use app\api\model\member\GoldRice;
- use app\api\model\member\MemberWelfareDraw;
- use app\api\model\shop\Shops;
- use app\api\model\User;
- use app\api\model\UserCoupon;
- use app\api\service\User as UserService;
- use app\common\library\helper;
- use app\store\model\CouponGood;
- use think\Exception;
- use think\facade\Db;
- class MemberService extends Controller
- {
- // public function tj(){
- // $otj = new MemberTj;
- // $day = $otj->day(1);
- // $month = $otj->month();
- // $week = $otj->week();
- // echo 'success';
- // }
- /**
- * 会员专区
- */
- public function memberZone(){
- $userId = UserService::getCurrentLoginUserId();
- $info = User::detail(['user_id'=>$userId]);
- $data['duration'] = 1;//年卡
- $data['curr_month'] = intval(date('m'));//年卡
- $data['member_expire_time'] = $info['member_expire_time']>0?date('Y-m-d',$info['member_expire_time']):'';
- $data['gold_rice'] = $info['gold_rice'];
- $data['welfare'] = MemberWelfare::currMonthWelfare($userId);
- return $this->renderSuccess($data);
- }
- /**
- * 推荐商品
- * @return array|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function vipGoodsList(){
- $model = new GoodsModel;
- $list = $model->getList([],15,true);
- foreach ($list as &$item){
- $dis = MemberGoods::where(['goods_id'=>$item['goods_id'],'status'=>0])
- ->field('goods_id,discount')->find();
- if ($dis){
- //$t = helper::bcmul($item['goods_price_min'],$dis['discount'],3);
- //$item['member_goods_price'] = helper::bcadd(ceil($t)/100,0,2);
- $item['member_goods_dis_price'] = helper::bcsub($item['goods_price_min'],$item['member_price'],2);
- }
- }
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 优惠券适用商品
- * @return array|\think\response\Json
- * @throws \think\db\exception\DbException
- */
- public function vipCouponGoodsList(){
- $couponId = $this->request->get('coupon_id');
- $model = new GoodsModel;
- $exGoodsId = CouponGood::getExceptGoodsId($couponId,1);
- $params['not_goods_id_arr'] = $exGoodsId;
- $list = $model->getList($params,15,false);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 会员开通记录
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- */
- public function vipOpenRecords(){
- $userId = UserService::getCurrentLoginUserId();
- $list = MemberCardOrder::getList($userId);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 校验会员时效
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- */
- public function checkMemberExpire(){
- $user = UserService::getCurrentLoginUser(true);
- if ($user['member_expire_time'] - time() > 63072000){//大于两年
- $end_date = date('Y-m-d',$user['member_expire_time']).'后方可进行续费操作';
- return $this->renderError('您的会员有效期天数已达上限啦~',['hint'=>$end_date]);
- }
- return $this->renderSuccess();
- }
- /**
- * 会员卡详情
- * @return array|\think\response\Json
- */
- public function memberCardInfo(){
- $user = UserService::getCurrentLoginUser();
- $member_status = 0;
- $expire_at = '';
- if ($user){
- if ($user['member_expire_time'] == 0){
- $member_status = 0;//未开通
- }elseif($user['member_expire_time'] < time()){
- $member_status = 1;//已过期
- }else{
- $member_status = 2;//生效中
- $expire_at = date('Y-m-d',$user['member_expire_time']);
- }
- }
- $card = MemberCards::findCard();
- return $this->renderSuccess(compact('card','member_status','expire_at'));
- }
- /**
- * 金米粒收入/支出列表
- * @param string $dataType in收入 out 支出
- * @return array
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DbException
- * @author: zjwhust
- * @Time: 2022/6/7 11:58
- */
- public function listGoldRice(string $dataType = GoldRice::LIST_TYPE_IN)
- {
- $user = UserService::getCurrentLoginUser();
- $model = new GoldRice;
- $param = [];
- $param['userId'] = $user['user_id'];
- $list = $model->getList($dataType,$param);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 领取福利
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- */
- public function drawMemberWelfare(){
- $user = UserService::getCurrentLoginUser(true);
- if ($user['member_expire_time'] < time()){
- return $this->renderError('您的会员已过期');
- }
- $id = $this->request->get('id');
- $m = new MemberWelfareDraw();
- $flag = $m->drawAction($id,$user);
- if ($flag){
- return $this->renderSuccess();
- }
- return $this->renderError($m->getError());
- }
- /**
- * 福利领取记录
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DbException
- */
- public function myWelfareRecords(){
- $user = UserService::getCurrentLoginUser(true);
- if ($user['member_expire_time'] < time()){
- return $this->renderError('您的会员已过期');
- }
- $m = new MemberWelfareDraw();
- $list = $m->welfareRecords($user->user_id);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 我领取的福利详情
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- */
- public function myWelfareDetail(){
- $user = UserService::getCurrentLoginUser(true);
- $id = $this->request->get('id');
- $m = new MemberWelfareDraw();
- $list = $m->myWelfareDetail($id);
- if($user['user_id']!=$list['user_id']){
- return $this->renderError('不是你的福利');
- }
- //todo 需根据第三方接口联调更新
- if($list['target_type']==2){
- $list['hx_qrcode_url'] = url('/api/order/getHxWelQrcode',['id'=>$list['id']], true, true)->build();
- }
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 用户优惠券详情
- * @param int $user_coupon_id
- * @return array|\think\response\Json
- */
- public function VipCouponDetail(int $user_coupon_id){
- $list = UserCoupon::showDetail($user_coupon_id);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * v1.4.1开通/续费会员后的会员开通礼优惠券列表弹窗
- * @author: zjwhust
- * @Time: 2022/6/24 14:44
- */
- public function vipRechargeCouponList(){
- $list = (new Coupon)->getMemList();
- return $this->renderSuccess(compact('list'));
- }
- /**
- * v1.4.1是否有待领取的会员开通礼
- * @author: zjwhust
- * @Time: 2022/6/24 15:31
- */
- public function isReceive(){
- $user = UserService::getCurrentLoginUser(true);
- $count = MemberCardOrder::where('user_id',$user['user_id'])
- ->where('pay_status',1)
- ->where('order_status',30)
- ->where('is_del',0)
- ->where('is_receive',0)
- ->count();
- return $this->renderSuccess(compact('count'));
- }
- /**
- * v1.4.1领取会员开通礼
- * @author: zjwhust
- * @Time: 2022/6/24 15:34
- */
- public function receive(){
- $user = UserService::getCurrentLoginUser(true);
- if($user['member_expire_time']<time()){
- return $this->renderError('不是会员');
- }
- $memberOrder = MemberCardOrder::where('user_id',$user['user_id'])
- ->where('pay_status',1)
- ->where('order_status',30)
- ->where('is_del',0)
- ->where('is_receive',0)
- ->select();
- if(!$memberOrder){
- return $this->renderError('您已领取,请在优惠券卡包查看');
- }
- Db::startTrans();
- try{
- $list = (new Coupon)->getMemList();
- foreach ($list as $coupon){
- for ($i=0;$i<$coupon['limit_receive_cnt'];$i++){
- $model = new UserCoupon();
- $model->receiveLogin($coupon,$user);
- }
- }
- //会员订单修改为已处理
- MemberCardOrder::where('id',$memberOrder[0]['id'])->update(['is_receive'=>1]);
- Db::commit();
- return $this->renderSuccess('领取成功');
- }catch (Exception $e) {
- Db::rollback();
- return $this->renderError($e->getMessage());
- }
- }
- /**
- * 异业门店核销详情接口
- * @author: zjwhust
- * @Time: 2022/7/21 18:08
- */
- public function hxWelfareDetail($id){
- // $id = $this->request->get('id');
- $user = UserService::getCurrentLoginUser(true);
- $shop = Shops::find($user['shop_id']);
- if(!$shop || $shop['is_virtual']!=3){
- return $this->renderError('不是异业门店,不能核销');
- }
- $m = new MemberWelfareDraw();
- $list = $m->myWelfareDetail($id);
- if($list['deploy_type']!=2){
- return $this->renderError('不是自制券码');
- }
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 核销异业门店核销
- * @author: zjwhust
- * @Time: 2022/7/21 18:14
- */
- public function hxWelfare($id){
- $user = UserService::getCurrentLoginUser(true);
- $shop = Shops::find($user['shop_id']);
- if(!$shop || $shop['is_virtual']!=3){
- return $this->renderError('不是异业门店,不能核销');
- }
- $m = new MemberWelfareDraw();
- $list = $m->myWelfareDetail($id);
- if($list['deploy_type']!=2){
- return $this->renderError('不是自制券码');
- }
- if($list['use_end_time']<date('Y-m-d')){
- return $this->renderError('已过使用有效期');
- }
- $list->check_status = 1;
- $list->check_time = date('Y-m-d H:i:s');
- if($list->save()){
- return $this->renderSuccess('核销成功');
- }
- return $this->renderError('核销失败');
- }
- }
|