123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\controller\card;
- use app\api\model\card\UserRiceCard as UserRiceCardModel;
- use app\api\model\card\RiceCard as RiceCardModel;
- use app\common\exception\BaseException;
- use app\api\service\User as UserService;
- use app\common\model\Order as OrderModel;
- use app\common\service\Order as OrderService;
- use think\facade\Db;
- use app\common\library\helper;
- use app\api\controller\Controller;
- use app\common\model\User as UserModel;
- use app\api\model\card\UserRiceCardGoods as UserRiceCardGoodsModel;
- use think\cache\driver\Redis;
- use app\common\model\PushLog;
- use app\common\model\card\RealRiceCardList;
- use app\api\model\card\RiceCardGoods as RiceCardGoodsModel;
- use app\api\model\UserAddress as UserAddressModel;
- use app\api\model\card\UserRiceCardDelivery as UserRiceCardDeliveryModel;
- use app\common\model\card\UserRiceDeliveryExpress as UserRiceDeliveryExpressModel;
- use app\common\service\delivery\Express;
- /**
- * 我的米卡
- * Class service
- * @package app\api\controller\user\order
- */
- class UserRiceCard extends Controller
- {
- //配送
- public function delivery(){
- $userinfo = UserService::getCurrentLoginUser(true);
- $param = $this->request->param();
- $address_id = intval($param['address_id'])??0;
- $user_rice_card_id = intval($param['user_rice_card_id'])??0;
- if(empty($address_id)||empty($user_rice_card_id)){
- return $this->renderError("参数不对");
- }
- $user_id = $userinfo->user_id;
- $myricecard = UserRiceCardModel::where('user_id',$userinfo->user_id)->where('id',$user_rice_card_id)->find();
- if(empty($myricecard)){
- return $this->renderError("米卡没找到");
- }
- if($myricecard->type!=3||$myricecard->activation_state!=1||$myricecard->effect_state!=1||$myricecard->frozen_state!=0||$myricecard->is_delete!=0){
- return $this->renderError("米卡可能失效了");
- }
- if(time()>strtotime($myricecard->expire_time)){
- return $this->renderError("兑换已失效");
- }
- $delivery = UserRiceCardDeliveryModel::where("user_rice_card_id",$user_rice_card_id)->find();
- if($delivery){
- return $this->renderError("米卡已兑换,请勿重复操作");
- }
- $address = UserAddressModel::where('address_id',$address_id)->where("user_id",$user_id)->find();
- if(empty($address)){
- return $this->renderError("地址信息异常");
- }
- $city_id = $address['city_id'];
- $e = new Express($city_id,[]);
- $flag = $e->isDeliveryLimit();
- if(!$flag){
- return $this->renderError("当前地区不可配送,请联系客服");
- }
- $data['user_rice_card_id'] = $myricecard->id;
- $data['user_id'] = $user_id;
- $data['name'] = $address['name']??'';
- $data['phone'] = $address['phone']??'';
- $data['detail'] = $address['detail']??'';
- $data['province_id'] = $address['province_id']??0;
- $data['city_id'] = $address['city_id']??0;
- $data['region_id'] = $address['region_id']??0;
- $data['create_time'] = time();
- $data['update_time'] = time();
- $data['card_id'] = $myricecard->card_id;
- $userRiceCardModelDelivery = new UserRiceCardDeliveryModel;
- $userRiceCardModelDelivery->save($data);
- $myricecard->effect_state =2;
- $myricecard->save();
- return $this->renderSuccess('success');
- }
- //我的米卡
- public function list($type =1){
- $model = new UserRiceCardModel;
- $userinfo = UserService::getCurrentLoginUser(true);
- $type = intval($type);
- $list = $model->getList($userinfo->user_id,$type);
- return $this->renderSuccess(compact('list'));
- }
- //米卡详情
- public function detail($user_rice_card_id=0){
- $userinfo = UserService::getCurrentLoginUser(true);
- $user_rice_card_id = intval($user_rice_card_id);
- $detail = UserRiceCardModel::detail($user_rice_card_id,['consume','delivery']);
- if($detail->user_id!=$userinfo->user_id){
- return $this->renderError("米卡不存在");
- }
- $goods = UserRiceCardGoodsModel::field('goods_name,goods_price,goods_num')->where('user_rice_card_id',$user_rice_card_id)->where('is_except',0)->select();
- $card = RiceCardModel::where('id',$detail['card_id'])->find();
- $detail['card_price'] = $card['card_price']??'';
- $detail['goods'] = $goods;
- if(isset($detail['delivery'])){
- $user_rice_delivery_id = $detail['delivery']['id'];
- $express = UserRiceDeliveryExpressModel::field('express_id,express_company,express_no')->where('user_rice_delivery_id',$user_rice_delivery_id)->select();
- $detail['delivery']['express'] = $express;
- }
- return $this->renderSuccess(compact('detail'));
- }
- //卡转赠, 整卡转赠
- public function shareto($user_rice_card_id){
- $userinfo = UserService::getCurrentLoginUser(true);
-
- $rice_card = UserRiceCardModel::where('id',$user_rice_card_id)->where("user_id",$userinfo->user_id)->find();
- if($rice_card->effect_state==2){
- return $this->renderError('该卡已失效');
- }
- if($rice_card->effect_state==0){
- return $this->renderError('该卡转赠中');
- }
- if($rice_card->frozen_state==1){
- return $this->renderError("该卡已冻结");
- }
- if($rice_card->activation_state==1){
- return $this->renderError("该卡已激活不能转赠");
- }
-
- $now = Date("Y-m-d H:i:s",time());
- $value = $rice_card->id."|".$userinfo->user_id.'|'.time();
- $sign = md5($value);
-
- $data['coupon_no'] = $rice_card->coupon_no;
- $data['coupon_code'] = $rice_card->coupon_code;
- $data['card_id'] = $rice_card->card_id;
- $data['card_name'] = $rice_card->card_name;
- $data['type'] = $rice_card->type;
- $data['describe'] = $rice_card->describe;
- $data['content'] = $rice_card->content;
- $data['image_id'] = $rice_card->image_id;
- if($data['type']==2){
- $data['expire_time'] = '2047-07-01 23:59:59';
- }else{
- $data['expire_time'] = $rice_card->expire_time;
- }
-
- $data['face_value'] = $rice_card->face_value;
- $data['balance'] = $rice_card->balance;
- $data['dk_cat_ids'] = $rice_card->dk_cat_ids;
- $data['activation_state'] = $rice_card->activation_state;
- $data['parent_id'] = $rice_card->id;
- $data['order_id'] = $rice_card->order_id;
- $data['frozen_state'] = $rice_card->frozen_state;
- $data['created_at'] = $now;
- $data['effect_state'] = 1;
- $data['sign'] = $sign;
- $model = new UserRiceCardModel;
- $model->save($data);// 写一张新的米卡记录,未领取
- $rice_card->effect_state = 0;//原卡变更为转赠中
- $rice_card->save();
-
- return $this->renderSuccess(compact('sign'));
- }
- //卡转赠, 短信方式
- public function smsshareto(){
- $userinfo = UserService::getCurrentLoginUser(true);
- $param = $this->request->param();
- $user_rice_card_id = $param['user_rice_card_id']??0;
- $mobile = $param['mobile']??'';
- if(strlen($mobile)!=11){
- return $this->renderError('手机号不正确');
- }
- $rice_card = UserRiceCardModel::where('id',$user_rice_card_id)->where("user_id",$userinfo->user_id)->find();
- if($rice_card->effect_state==2){
- return $this->renderError('该卡已失效');
- }
- if($rice_card->effect_state==0){
- return $this->renderError('该卡转赠中');
- }
- if($rice_card->frozen_state==1){
- return $this->renderError("该卡已冻结");
- }
- if($rice_card->activation_state==1){
- return $this->renderError("该卡已激活不能转赠");
- }
- if(empty($rice_card->coupon_code)){
- $codeArray = (new UserRiceCardModel())->createCode();
- $coupon_code = $codeArray[0]??rand(10000000,19999999);
- UserRiceCardModel::where("id",$rice_card->id)->update(['coupon_code'=>$coupon_code]);
- $rice_card->coupon_code = $coupon_code;
- }
-
- $now = Date("Y-m-d H:i:s",time());
- // $value = $rice_card->id."|".$userinfo->user_id.'|'.time();
- // $sign = md5($value);
-
- $data['coupon_no'] = $rice_card->coupon_no;
- $data['coupon_code'] = $rice_card->coupon_code;
- $data['card_id'] = $rice_card->card_id;
- $data['card_name'] = $rice_card->card_name;
- $data['type'] = $rice_card->type;
- $data['describe'] = $rice_card->describe;
- $data['content'] = $rice_card->content;
- $data['image_id'] = $rice_card->image_id;
- if($data['type']==2){
- $data['expire_time'] = '2047-07-01 23:59:59';
- }else{
- $data['expire_time'] = $rice_card->expire_time;
- }
- $data['face_value'] = $rice_card->face_value;
- $data['balance'] = $rice_card->balance;
- $data['dk_cat_ids'] = $rice_card->dk_cat_ids;
- $data['activation_state'] = $rice_card->activation_state;
- $data['parent_id'] = $rice_card->id;
- $data['order_id'] = $rice_card->order_id;
- $data['frozen_state'] = $rice_card->frozen_state;
- $data['created_at'] = $now;
- $data['effect_state'] = 1;
- // $data['sign'] = $sign;
- $model = new UserRiceCardModel;
- $model->save($data);// 写一张新的米卡记录,未领取
- $rice_card->effect_state = 0;//原卡变更为转赠中
- $rice_card->save();
- $sms_content = '您的好友给您赠送米卡,激活码'.$rice_card->coupon_code.',打开微信小程序“公明腊肠优选-我的-我的米卡”激活有疑问咨询0755-26910451';
- PushLog::addSmsMon($sms_content,$mobile,'');
- return $this->renderSuccess('短信发送成功');
- }
- //领取转赠卡的信息
- public function receiveinfo($sign){
- // $userinfo = UserService::getCurrentLoginUser(true);
- $rice_card = UserRiceCardModel::where('sign',$sign)->find();
- if(!$rice_card||$rice_card->is_delete==1){
- return $this->renderError('礼物已被撤回');
- }
- if($rice_card->effect_state==2){
- return $this->renderError('礼物已被撤回');
- }
- if($rice_card->frozen_state==1){
- return $this->renderError("该卡已冻结");
- }
- $parent_id = $rice_card->parent_id;
- $parent = UserRiceCardModel::where('id',$parent_id)->find();
-
- $user = UserModel::detail($parent->user_id);
-
- $nick_name = $user->nick_name??'';
-
- $info['id'] = $rice_card->id;
- $info['rice_card_user_id'] = $parent->user_id;
- $info['rice_card_nick_name'] = $nick_name;
- $info['type_text'] = $rice_card->type_text;
- $info['card_name'] = $rice_card->card_name;
- $info['image_text'] = $rice_card->image_text;
- $info['face_value'] = $rice_card->face_value;
- $info['sign'] =$rice_card->sign;
- return $this->renderSuccess(compact('info'));
- }
- //领取卡
- public function receive($sign){
- $param = $this->request->param();
- $sign = $param['sign']??'';
- if(empty($sign)){
- return $this->renderError('参数有误');
- }
- $userinfo = UserService::getCurrentLoginUser(true);
- $rice_card = UserRiceCardModel::where('sign',$sign)->find();
- $now = Date("Y-m-d H:i:s",time());
- if(!$rice_card||$rice_card->is_delete==1){
- return $this->renderError('礼物已被撤回');
- }
- if($rice_card->effect_state==2){
- return $this->renderError('礼物已被撤回');
- }
- if($rice_card->frozen_state==1){
- return $this->renderError("该卡已冻结");
- }
- if($rice_card->user_id>0){
- return $this->renderError("该卡已被领取");
- }
- $rice_card->user_id = $userinfo->user_id;
- $rice_card->user_mobile = $userinfo->mobile;
- $rice_card->save();
- //原米卡改为失效
- UserRiceCardModel::where('id',$rice_card->parent_id)->update(['effect_state'=>2]);
- //原米卡的限制商品所属用户更换ID
- UserRiceCardGoodsModel::where("user_rice_card_id",$rice_card->parent_id)->update(['user_rice_card_id'=>$rice_card->id]);
- return $this->renderSuccess('领取成功');
- }
- //撤回
- public function revoke($user_rice_card_id){
- $param = $this->request->param();
- $userinfo = UserService::getCurrentLoginUser(true);
- $rice_card = UserRiceCardModel::where('id',$user_rice_card_id)->find();
- $now = Date("Y-m-d H:i:s",time());
- if(!$rice_card||$rice_card->is_delete==1){
- return $this->renderError('找不到信息');
- }
- if($rice_card->effect_state==2){
- return $this->renderError('该卡已被领取');
- }
- if($rice_card->effect_state==1){
- return $this->renderError("该卡已撤回");
- }
-
- $childrenRice = UserRiceCardModel::where('parent_id',$rice_card->id)->where('is_delete',0)->where('effect_state',1)->find();
-
- if($childrenRice){
- if($childrenRice->user_id>0){
- return $this->renderError("该卡已被领取,不能撤回");
- }
- }
- $childrenRice->effect_state =2;//失效,删除
- $childrenRice->is_delete = 1;
- $childrenRice->save();
- $rice_card->effect_state = 1;
- $rice_card->save();
- //原米卡改为失效
- //原米卡的限制商品所属用户更换ID
- return $this->renderSuccess('撤回成功');
- }
- //激活
- public function activation(){
- $userinfo = UserService::getCurrentLoginUser(true);
- $param = $this->request->param();
- $user_rice_card_id = $param['user_rice_card_id']??'';
- $user_id = $userinfo->user_id;
- $key = 'activation1:user_id:'.$user_id;
- $rds = new Redis(config('cache.stores.redis'));
- $num = $rds->get($key);
- if($num==20){
- return $this->renderError('你激活的次数太多了,请稍后再试');
- }
- if($num==null){
- $num = 1;
- }else{
- $num++;
- }
- $rds->set($key,$num,300);
- $rice_card = UserRiceCardModel::where('id',$user_rice_card_id)->where('user_id',$user_id)->find();
- if(empty($rice_card)){
- return $this->renderError('激活有误');
- }
- if($rice_card->effect_state!=1){
- return $this->renderError('该卡已失效');
- }
- if($rice_card->activation_state==1){
- return $this->renderError('米卡已激活');
- }
- $now = Date("Y-m-d H:i:s",time());
- $rice_card->activation_state = 1;
- $rice_card->activation_time = $now;
- $rice_card->save();
- return $this->renderSuccess('激活成功');
- }
- //激活码激活
- public function activationcode(){
- $userinfo = UserService::getCurrentLoginUser(true);
- $param = $this->request->param();
- $coupon_code = $param['coupon_code']??'';
- $key = 'activation:user_id:'.$userinfo->user_id;
- $rds = new Redis(config('cache.stores.redis'));
- $num = $rds->get($key);
- if($num==20){
- return $this->renderError('你验证的次数太多了,请稍后再试');
- }
- if($num==null){
- $num = 1;
- }else{
- $num++;
- }
- $rds->set($key,$num,600);
-
- // $preg= '/[A-Za-z]*/';
- // $dot = false;
- // $dot = ctype_digit($coupon_code);
- $dot = false;
- if(preg_match('/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]+$/', $coupon_code)){
- $dot = true;
- }
- //米卡转赠的 没有字母
- if($coupon_code<30000000&&!$dot){
- $rice_card = UserRiceCardModel::where('coupon_code',$coupon_code)->where("effect_state",1)->find();
- if(empty($rice_card)){
- return $this->renderError('无效的激活码或兑换码');
- }
- if($rice_card->activation_state==1||$rice_card->user_id>0){
- return $this->renderError('激活码或兑换码已使用');
- }
- $now = Date("Y-m-d H:i:s",time());
-
- $rice_card->user_id = $userinfo->user_id;
- $rice_card->activation_state = 1;
- $rice_card->user_mobile = $userinfo->mobile;
- $rice_card->activation_time = $now;
- $rice_card->save();
- if($rice_card->parent_id>0){
- $parent_rice = UserRiceCardModel::where("id",$rice_card->parent_id)->find();
- if($parent_rice->effect_state==0){
- $parent_rice->effect_state = 2;
- $parent_rice->save();
- }
- }
- return $this->renderSuccess('激活成功');
- }
-
- //实体兑换卡 或者有字母
- if($coupon_code>50000000||$dot){
- Db::startTrans();
- $rrc = RealRiceCardList::where('redeem_code', '=',$coupon_code)->where('is_delete','=',0)->where('frozen_status','=',0)->lock("for update")->find();
- if(empty($rrc)){
- return $this->renderError("无效的激活码或兑换码");
- }
- if($rrc->been_active==1){
- return $this->renderError('激活码或兑换码已使用!');
- }
- $rice_card = RiceCardModel::where('id',$rrc->rice_card_id)->find();
- if(empty($rice_card)){
- return $this->renderError('找不到相关米卡');
- }
-
- $create_date =substr($rrc->create_time,0,10).' 00:00:00';
- if($rice_card->expire_day==0){
- $expire_time = '2047-07-01 23:59:59';
- }
- else if($rice_card->expire_day==1){
- $expire_time = Date("Y-m-d",strtotime($create_date)).' 23:59:59';
- }else{
- $d = $rice_card->expire_day -1;
- $expire_time = date('Y-m-d',strtotime('+'.$d.' day',strtotime($create_date))). ' 23:59:59' ;
- }
- $now = Date("Y-m-d H:i:s",time());
- if($now>$expire_time){
- return $this->renderError("激活码或兑换码已失效");
- }
-
- $data['coupon_no'] = $rrc->card_number;
- $data['coupon_code'] = $rrc->redeem_code;
- $data['card_id'] = $rrc->rice_card_id;
- $data['card_name'] = $rice_card->name;
- $data['type'] = $rice_card->type;
- $data['describe'] = $rice_card->describe??'';
- $data['content'] = $rice_card->content??'';
- $data['image_id'] = $rice_card->image_id;
-
-
- $data['expire_time'] = $expire_time;
- // //实体现金卡
- if($rice_card->type==4){
- $data['face_value'] = $rice_card->card_price;
- $data['balance'] = $rice_card->card_price;
- }else{
- $data['face_value'] = 0.01;
- $data['balance'] = 0.01;
- }
-
- $data['dk_cat_ids'] = $rice_card->dk_cat_ids;
- $data['user_id'] = $userinfo->user_id;
- $data['activation_state'] = 1;
- $data['user_mobile'] = $userinfo->mobile;
- $data['activation_time'] = $now;
- $data['effect_state'] = 1;
-
- $user_rice_card = new UserRiceCardModel;
- $user_rice_card->save($data);
- $user_rice_card_id = $user_rice_card->id;
- $rrc->been_active = 1;//激活状态改变
- $rrc->active_time = time();
- $rrc->save();
- $goodList = RiceCardGoodsModel::where('card_id',$rrc->rice_card_id)->select();
- foreach($goodList as $row){
- $gdata['user_rice_card_id'] = $user_rice_card_id;
- $gdata['goods_id'] = $row->goods_id;
- $gdata['image_id'] = $row->image_id;
- $gdata['goods_sku_id'] = $row->goods_sku_id;
- $gdata['goods_sku_no'] = $row->goods_sku_no;
- $gdata['goods_name'] = $row->goods_name;
- $gdata['goods_props'] = isset($row->goods_props)?json_encode($row->goods_props):'';
- $gdata['goods_price'] = $row->goods_price;
- $gdata['goods_num'] = $row->goods_num;
- $gdata['total_price'] = $row->total_price;
- $gdata['is_except'] = $row->is_except;
- $gdata['create_time'] = $now;
- $gdata['update_time'] = $now;
- $ugood = new UserRiceCardGoodsModel;
- $ugood->save($gdata);
- }
- Db::commit();
- return $this->renderSuccess('兑换成功');
- }
- }
- //米卡未被领取撤回 test 给测试用的
- public function index(){
- $t = 3;
- // $t = 120;
- //整卡转赠
- $list = UserRiceCardModel::field("id")->where('effect_state',0)->select();
- // return $list;
- $idarr = [];
- foreach($list as $row){
- $idarr[] = $row['id'];
- }
- foreach($idarr as $id){
- $coupon = UserRiceCardModel::where('parent_id',$id)->find();
- if($coupon){
- $add_time = strtotime($coupon['create_time']) + $t;//加一天的时间
- $now = time();
- //过期了没领
- if($now>$add_time){
- $coupon->effect_state = 2;//已失效
- $coupon->save();//该卡改为已失效
- $pcouponModel = UserRiceCardModel::where('id',$coupon->parent_id)->where('effect_state',0)->find();
- $pcouponModel->effect_state = 1;//原卡改为生效中,
- //可用数量改为原来的值
- $pcouponModel->save();
- }
- }
- }
- echo 'success';
- echo 't:'.$t;
- }
- }
|