| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- <?php
- namespace addons\MembersCard\api\modules\v1\controllers;
- use Yii;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use addons\MembersCard\common\enums\MembersCardEnum;
- use addons\MembersCard\common\models\MembersCardGive;
- use addons\MembersCard\common\models\MembersCard;
- use addons\MembersCard\common\models\MembersCardUsedLog;
- use addons\MembersCard\common\models\CommissionLog;
- use addons\Store\common\models\Store;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\goods\Goods;
- use common\models\user\User;
- use addons\Store\common\models\StoreCashierOrder;
- use common\enums\RedisKeyEnum;
- use common\helpers\LockHelper;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\MembersCard\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- return 'Hello world';
- }
- public function actionList(){
- try{
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['user_id' => $this->user['id']];
- if(isset($params['type']) && $params['type'] == 2){
- $where[] = [
- 'or',
- ['<','expired_at',time()],
- ['is_recycle'=>1],
- ];
- }elseif($params['type']==1){
- $members_card_give = MembersCardGive::find()
- ->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED])
- ->andWhere(['>','parent_card_id',0])
- ->asArray()
- ->all();
- $parent_card_ids = array_column($members_card_give,'parent_card_id');
- $where[] = ['in','id',$parent_card_ids];
- $where[] = [
- 'and',
- ['>=','expired_at',time()],
- ['is_recycle'=>0],
- ];
- }else{
- $where[] = [
- 'and',
- ['>=','expired_at',time()],
- ['is_recycle'=>0],
- ];
- }
- $params['where'] = $where;
- $params['select'] = 'id,card_id,discount,child_num,parent_card_id,expired_at,created_at,card_no,is_recycle,recycle_at';
- $data = MembersCardGive::listPage($params, false);
- if(!empty($data['list'])){
- $config = Yii::$app->services->setting->addons->get($this->mall_id, MembersCardEnum::ADDONS_NAME, 'basic');
- foreach($data['list'] as &$list){
- $list['expired_at'] = date("Y-m-d",$list['expired_at']);
- $list['created_at'] = date("Y-m-d",$list['created_at']);
- $list['name'] = '';
- $list['background_url'] = '';
- //是否显示赠送亲友
- $list['is_give'] = 0;
- //是否显示子卡列表
- $list['is_list'] = 0;
- //是否显示使用记录
- $list['is_used'] = 1;
- //是否显示去升级
- $list['is_up'] = 0;
- $list['is_up_desc'] = '';
- $list['is_up_link'] = '';
- $list['is_show'] = 0;
- $list['user'] = [];
- $members_card = MembersCard::findOne(['id'=>$list['card_id']]);
- if($members_card){
- $list['name'] = $members_card['name'];
- $list['background_url'] = $members_card['background_url'];
- $levels = json_decode($members_card['member_limit'],true);
- if(!empty($levels) && !in_array($this->user['level'],$levels) && $list['parent_card_id']){
- $list['is_up'] = 1;
- $list['is_up_desc'] = $config ? $config['description'] : '';
- $list['is_up_link'] = $members_card['child_upgrade_links'];
- }
- }
- //子卡已使用数量
- $members_card_give_num = MembersCardGive::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'parent_card_id'=>$list['id']])->count();
- $list['child_used_num'] = $members_card_give_num ?? 0;
- if(!$list['parent_card_id']){
- if($list['child_used_num'] < $list['child_num']){
- $list['is_give'] = 1;
- }
- }
- $list['child_num'] = $list['child_num'] - $list['child_used_num'];
- $list['child_num'] = $list['child_num'] > 0 ? $list['child_num'] : 0;
- //已赠送列表不显示送亲友按钮
- if($members_card_give_num && isset($params['type']) && $params['type'] == 1){
- $list['is_list'] = 1;
- $list['is_give'] = 0;
- }
- if($list['parent_card_id']){
- $list['is_show'] = 1;
- $parent = MembersCardGive::findOne(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'id'=>$list['parent_card_id']]);
- if($parent){
- $parent_user = User::findOne(['id'=>$parent['user_id']]);
- }
- $list['give_user'] = [
- 'user_id' => $parent_user['id'],
- 'avatar_url' => $parent_user['avatar_url'],
- 'nickname' => $parent_user['nickname'],
- ];
- }
-
- }
- }
-
- return $this->success('操作成功', $data);
- }else{
- throw new \Exception("请求方法错误!");
- }
- }catch(\Exception $e){
- return $this->error();
- }
- }
- //子卡列表
- public function actionChildList(){
- try{
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- if(!isset($params['id'])){
- throw new \Exception("参数错误!");
- }
- $where[] = ['=','parent_card_id',$params['id']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- // $where[] = ['user_id' => $this->user['id']];
- $params['where'] = $where;
- $params['select'] = 'id,card_id,user_id,discount,expired_at,created_at,is_recycle,card_no';
- $data = MembersCardGive::listPage($params, false);
- if(!empty($data['list'])){
- foreach($data['list'] as &$list){
- $list['expired_at'] = date("Y-m-d",$list['expired_at']);
- $list['created_at'] = date("Y-m-d",$list['created_at']);
- $list['name'] = '';
- $list['background_url'] = '';
- $members_card = MembersCard::findOne(['id'=>$list['card_id']]);
- if($members_card){
- $list['name'] = $members_card['name'];
- $list['background_url'] = $members_card['background_url'];
- }
- $user = User::findOne(['id'=>$list['user_id']]);
- $list['user'] = [
- 'user_id' => $user['id'],
- 'avatar_url' => $user['avatar_url'],
- 'nickname' => $user['nickname'],
- ];
-
- }
- }
-
- return $this->success('操作成功', $data);
- }else{
- throw new \Exception("请求方法错误!");
- }
- }catch(\Exception $e){
- return $this->error();
- }
- }
- public function actionUsedLog(){
- try{
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- if(!isset($params['id'])){
- throw new \Exception("参数错误!");
- }
- $where[] = ['=','card_give_id',$params['id']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- // $where[] = ['user_id' => $this->user['id']];
-
- if($params['created_at']){
- $start_time = strtotime(date('Y-m-01 00:00:00', strtotime($params['created_at'])));
- $end_time = strtotime(date('Y-m-t 23:59:59', strtotime($params['created_at'])));
- $where[] = ['>', 'created_at', $start_time];
- $where[] = ['<=', 'created_at', $end_time];
- }
- $params['where'] = $where;
- $params['select'] = 'id,discount_money,pay_money,order_id,order_detail_id,store_id,is_cashier';
- $data = MembersCardUsedLog::listPage($params, false);
- $newList = [];
- if(!empty($data['list'])){
- foreach($data['list'] as &$list){
- if(!isset($newList[$list['order_id']])){
- $list['store_name'] = '平台';
- if($list['store_id']){
- $store = Store::getOne([['id' => $list['store_id']], ['mall_id' => $this->mall_id]]);
- if(!empty($store)){
- $list['store_name'] = $store['store_name'];
- }
- }
- if($list['is_cashier']){
- $order = StoreCashierOrder::findOne(['id'=>$list['order_id']]);
- $list['goods'][] = [
- 'name' => '门店扫码',
- 'price' => $order['money'],
- 'goods_attr_name' => '',
- 'num' => 1,
- 'discount_money' => $list['discount_money'],
- 'pic_url' => '',
- ];
- }else{
- $order = Order::findOne(['id'=>$list['order_id']]);
- $order_detail = OrderDetail::findOne(['id'=>$list['order_detail_id']]);
- if($order_detail){
- $list['goods'][] = [
- 'name' => $order_detail['goods_name'],
- 'price' => $order_detail['price'],
- 'goods_attr_name' => $order_detail['goods_attr_name'],
- 'num' => $order_detail['num'],
- 'discount_money' => $list['discount_money'],
- 'pic_url' => $order_detail['pic_url'],
- ];
- }
- }
- if($order){
- $list['pay_time'] = date("Y-m-d H:i:s",$order['pay_time']);
- $list['pay_money'] = $order['pay_money'];
- }
-
- $newList[$list['order_id']] = $list;
- unset($newList[$list['order_id']]['discount_money'],$newList[$list['order_id']]['order_id'],$newList[$list['order_id']]['order_detail_id']);
- }else{
- if(!$list['is_cashier']){
- $order_detail = OrderDetail::findOne(['id'=>$list['order_detail_id']]);
- if($order_detail){
- $newList[$list['order_id']]['goods'][] = [
- 'name' => $order_detail['goods_name'],
- 'price' => $order_detail['price'],
- 'goods_attr_name' => $order_detail['goods_attr_name'],
- 'num' => $order_detail['num'],
- 'discount_money' => $list['discount_money'],
- 'pic_url' => $order_detail['pic_url'],
- ];
- }
- }
- }
- }
- // $data['list'] = $newList;
- }
- $arr = [];
- foreach($newList as $key => $item){
- $arr[] = $item;
- }
- $data['list'] = $arr;
- return $this->success('操作成功', $data);
- }else{
- throw new \Exception("请求方法错误!");
- }
- }catch(\Exception $e){
- return $this->error();
- }
- }
- /**
- * @name:
- * @msg: 会员卡信息信息
- * @param {integer} $card_give_id
- * @return {*}
- */
- public function actionGiveCardInfo()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = $this->user_id;
- $card_give_id = \Yii::$app->request->get('card_give_id');
- if (empty($card_give_id)) {
- return $this->error('参数错误');
- }
- $card_give = MembersCardGive::find()->where(['id' => $card_give_id, 'mall_id' => $this->mall_id])->asArray()->one();
- if (empty($card_give)) return $this->error('母卡不存在');
- $members_card = MembersCard::find()->where(['id' => $card_give['card_id'], 'mall_id' => $this->mall_id])->asArray()->one();
- if (empty($members_card)) return $this->error('数据不存在');
- $members_card_give_num = MembersCardGive::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'parent_card_id'=>$card_give_id])->count();
- if($members_card_give_num && $members_card_give_num >= $card_give['child_num']){
- return $this->error('来晚啦,亲友卡已被领完~');
- }
- $give_user = User::findOne(['id'=>$card_give['user_id']]);
- if(!$give_user){
- return $this->error('分享用户不存在');
- }
- $detail = [
- 'card_give_id' => $card_give_id,
- 'discount' => $card_give['child_discount'],
- 'name' => $members_card['name'],
- 'background_url' => $members_card['background_url'],
- 'desc' => '领取亲友卡,可享受'.$card_give['child_discount'].'的优惠折扣',
- 'user_id' => $give_user['id'],
- 'avatar_url' => $give_user['avatar_url'],
- 'nickname' => $give_user['nickname'],
- 'created_at' => date("Y-m-d",time()),
- 'expired_at' => date("Y-m-d",strtotime("+".$card_give['child_expired_days']." day")),
- ];
- return $this->success('操作成功', ['card_info' => $detail]);
- }
- //领取亲友卡
- public function actionReceive(){
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['card_give_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_USER_WALLET , $params['card_give_id']);
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 100, 100)) {
- \Yii::error(__CLASS__ . ' batchHandle error : 抢不到锁,task_id:' . $task_id);
- return $this->error('网络异常,请稍后再试~');
-
- }
- $trans = \Yii::$app->db->beginTransaction();
- try {
- $card_give = MembersCardGive::find()->where(['id' => $params['card_give_id'], 'mall_id' => $this->mall_id])->asArray()->one();
- if (empty($card_give)) throw new \Exception('数据异常');
- if($this->user['id'] == $card_give['user_id']){
- throw new \Exception('不能领取自己赠送的亲友卡');
- }
- $card_give_log = MembersCardGive::find()->where(['parent_card_id' =>$params['card_give_id'], 'mall_id' => $this->mall_id,'user_id'=>$this->user['id'],'status'=>StatusEnum::ENABLED])->one();
- if($card_give_log){
- throw new \Exception('您已领取过亲友卡!');
- }
- $members_card_give_num = MembersCardGive::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'parent_card_id'=>$params['card_give_id']])->count();
- if($members_card_give_num && $members_card_give_num + 1 >= $card_give['child_num']){
- throw new \Exception('来晚啦,亲友卡已被领完~');
- }
- if(!$members_card_give_num){
- $card_no = 1;
- }else{
- $card_no = $members_card_give_num + 1;
- }
- $expired_at = strtotime("+".$card_give['child_expired_days']." day");
- if($expired_at > $card_give['expired_at']){
- $expired_at = $card_give['expired_at'];
- }
- $data = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $this->user['id'],
- 'discount' => $card_give['child_discount'],
- 'parent_card_id' => $card_give['id'],
- 'card_id' => $card_give['card_id'],
- 'receive_at' => time(),
- 'expired_at' => $expired_at,
- 'card_no' => $card_no,
- ];
- $model = MembersCardGive::setData($data);
- if(!$model){
- throw new \Exception('领取失败');
- }
- $members_card = MembersCard::find()->where(['id' => $card_give['card_id'], 'mall_id' => $this->mall_id])->asArray()->one();
- $detail = [
- 'id' => $model['id'],
- 'discount' => $model['discount'],
- 'name' => $members_card['name'],
- 'background_url' => $members_card['background_url'],
- 'created_at' => date("Y-m-d",$model['created_at']),
- 'expired_at' => date("Y-m-d",$model['expired_at']),
- ];
- $trans->commit();
- LockHelper::uLock($lock_key, $identification);
- return $this->success('领取成功', ['card_info' => $detail]);
- } catch (\Exception $e) {
- $trans->rollBack();
- LockHelper::uLock($lock_key, $identification);
- \Yii::error($e->getMessage());
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 回收亲友卡
- * @param {integer} $card_give_id
- * @return {*}
- */
- public function actionRecycle()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $user_id = $this->user['id'];
- $card_give_id = \Yii::$app->request->post('id');
- if (empty($card_give_id)) {
- return $this->error('参数错误');
- }
- $card_give = MembersCardGive::find()->where(['id' => $card_give_id, 'mall_id' => $this->mall_id])->one();
- if (empty($card_give)) return $this->error('亲友卡不存在');
- $parent_card = MembersCardGive::find()->where(['id' => $card_give['parent_card_id'], 'mall_id' => $this->mall_id])->asArray()->one();
- if($parent_card['user_id'] != $user_id){
- return $this->error('您无权回收该亲友卡');
- }
- $card_give->is_recycle = 1;
- $card_give->recycle_at = time();
- $card_give->status = StatusEnum::DISABLED;
- if(!$card_give->save()){
- return $this->error('回收失败');
- }
- return $this->success('回收成功');
- }
- /**
- * @name:
- * @msg: 批量回收亲友卡
- * @param {integer} $card_give_id
- * @return {*}
- */
- public function actionRecycleAll()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $user_id = $this->user['id'];
- $card_give_id = \Yii::$app->request->post('id');
- if (empty($card_give_id)) {
- return $this->error('参数错误');
- }
- $card_give = MembersCardGive::find()->where(['id' => $card_give_id, 'mall_id' => $this->mall_id])->one();
- if (empty($card_give)) return $this->error('母卡不存在');
- if($card_give['user_id'] != $user_id){
- return $this->error('您无权回收该亲友卡子卡');
- }
- $t = \Yii::$app->db->beginTransaction();
- try{
- $card_list = MembersCardGive::find()->where(['parent_card_id' => $card_give_id, 'mall_id' => $this->mall_id,'is_recycle'=>0,'status'=>StatusEnum::ENABLED])->all();
- if($card_list){
- foreach($card_list as $list){
- $list->is_recycle = 1;
- $list->recycle_at = time();
- $list->status = StatusEnum::DISABLED;
- if(!$list->save()){
- throw new \Exception('保存失败');
- }
- }
- }
- $t->commit();
- return $this->success('回收成功');
- }catch(\Exception $e){
- $t->rollBack();
- $this->error('回收失败');
- }
-
- }
- //佣金列表
- public function actionIncomeList(){
- try{
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- if(isset($params['type']) && $params['type'] == 1){
- $where[] = ['=','is_frozen',1];
- }else{
- $where[] = ['=','is_frozen',0];
- }
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['reward_user_id' => $this->user['id']];
- $params['where'] = $where;
- $params['select'] = 'id,discount_money,user_id,card_give_id,reward_money,goods_name,order_id,store_id,is_cashier,order_detail_id';
- $data = CommissionLog::listPage($params, false);
- if(!empty($data['list'])){
- foreach($data['list'] as &$list){
- $list['nickname'] = $list['avatar_url'] = '';
- $user = User::findOne(['id'=>$list['user_id']]);
- if($user){
- $list['nickname'] = $user['nickname'];
- $list['avatar_url'] = $user['avatar_url'];
- }
- $list['store_name'] = '平台';
- if($list['store_id']){
- $store = Store::findOne(['id'=>$list['store_id']]);
- if($store){
- $list['store_name'] = $store['store_name'];
- }
- }
- if($list['is_cashier']){
- $order = StoreCashierOrder::findOne(['id'=>$list['order_id']]);
- $list['money'] = $order['money'];
- }else{
- $order = Order::findOne(['id'=>$list['order_id']]);
- $order_detail = OrderDetail::findOne(['id'=>$list['order_detail_id']]);
- $list['money'] = $order_detail['price'];
- }
- if($order){
- $list['pay_time'] = date("Y-m-d H:i:s",$order['pay_time']);
- $list['pay_money'] = $order['pay_money'];
- $list['order_no'] = $order['order_no'];
- }
- $card_give = MembersCardGive::findOne(['id'=>$list['card_give_id']]);
- $members_card = MembersCard::findOne(['id'=>$card_give['card_id']]);
- $list['card_name'] = $members_card['name'] ?? '';
-
- unset($list['card_give_id'],$list['order_id'],$list['store_id'],$list['is_cashier'],$list['order_detail_id']);
- }
- }
- $data['user'] = [
- 'user_id' => $this->user['id'],
- 'nickname' => $this->user['nickname'],
- 'avatar_url' => $this->user['avatar_url'],
- ];
- $data['imcome']['total_income'] = CommissionLog::find()->where(['mall_id'=>$this->mall_id,'reward_user_id'=>$this->user['id'],'is_frozen'=>0])->sum('reward_money') ?? 0;
- $data['imcome']['wait_income'] = CommissionLog::find()->where(['mall_id'=>$this->mall_id,'reward_user_id'=>$this->user['id'],'is_frozen'=>1])->sum('reward_money') ?? 0;
-
- return $this->success('操作成功', $data);
- }else{
- throw new \Exception("请求方法错误!");
- }
- }catch(\Exception $e){
- return $this->error();
- }
- }
- }
|