917 lines
38 KiB
PHP
917 lines
38 KiB
PHP
<?php
|
|
namespace app\controllers\channelApi;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yii;
|
|
use app\models\common\Config;
|
|
use app\models\common\Order;
|
|
use app\models\common\Member;
|
|
use app\models\common\Window;
|
|
use app\models\common\Store;
|
|
class MemberController extends CommonController{
|
|
//添加收货地址
|
|
public function actionSaveUserAddress() {
|
|
$request = Yii::$app->request;
|
|
$result=axios_request();
|
|
if($request->isGet){
|
|
if(!$result['id']){
|
|
return $this->result(2, '缺少参数');
|
|
}
|
|
$addressInfo=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_address}}')
|
|
->where('id=:id AND deleteAt=0',[':id'=>$result['id']])->one();
|
|
return $this->result(1, '成功',$addressInfo);
|
|
}
|
|
$appType = $this->wqData['appType'];
|
|
$data['userId'] = $this->wqData['userId'];
|
|
$data['userName'] = $result['userName'];
|
|
$data['userTel'] = $result['userTel'];
|
|
$data['address'] = $result['address'];
|
|
$data['details'] = $result['details'];
|
|
$data['label'] = $result['label'];
|
|
$data['sex'] = $result['sex'];
|
|
$data['lat'] = $result['lat'];
|
|
$data['lng'] = $result['lng'];
|
|
$set = Config::getSystemSet('system', $this->wqData['uniacid']);
|
|
if ($appType == 7) {
|
|
$location = "https://apis.map.qq.com/ws/geocoder/v1/?address=" . $result['address'] . $result['details'] . "&key=" . $set['txKey'];
|
|
$coordinate =httpRequest($location);
|
|
$coordinate = json_decode($coordinate, true);
|
|
if ($coordinate['result']['reliability'] < 7) {
|
|
$this->result(2, '地址准确度不高,请重新填写');
|
|
}
|
|
$data['lat'] = $coordinate['result']['location']['lat'];
|
|
$data['lng'] = $coordinate['result']['location']['lng'];
|
|
$data['area'] = $coordinate['result']['address_components']['city'];
|
|
} else {
|
|
$zb = $result['lat'] . "," . $result['lng'];
|
|
$location = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $zb . "&key=" . $set['txKey'];
|
|
$coordinate = httpRequest($location);
|
|
$coordinate = json_decode($coordinate, true);
|
|
$data['area'] = $coordinate['result']['address_component']['city'];
|
|
}
|
|
if (!$data['area']) {
|
|
return $this->result(2, '地址解析异常,获取城市失败');
|
|
}
|
|
if ($result['id']) {
|
|
$data['changeAt'] = time();
|
|
$res = Yii::$app->db->createCommand()->update('{{%ybwm_user_address}}', $data, 'id=:id', ['id' =>$result['id']])->execute();
|
|
} else {
|
|
$data['createdAt'] = time();
|
|
$data['uniacid'] = $this->wqData['uniacid'];
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_user_address}}', $data)->execute();
|
|
}
|
|
if ($res) {
|
|
return $this->result(1, '成功');
|
|
} else {
|
|
return $this->result(2, '失败');
|
|
}
|
|
}
|
|
|
|
//我的收货地址
|
|
public function actionMyAddress() {
|
|
$result=axios_request();
|
|
$res=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_address}}')
|
|
->where('userId=:userId AND deleteAt=0',[':userId'=>$this->wqData['userId']])->all();
|
|
$store=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')
|
|
->where('id=:id AND deleteAt=0',[':id'=>$result['storeId']])->one();
|
|
if (!$res) {
|
|
return $this->result(1, '暂无地址');
|
|
}
|
|
if ($store) {
|
|
for ($i = 0; $i < count($res); $i++) {
|
|
$countFreight = Order::getDeliveryMoney($res[$i]['lat'], $res[$i]['lng'],$result['storeId']);
|
|
if ($countFreight ===false) {
|
|
$res[$i]['isClick'] = 2;
|
|
} else {
|
|
$res[$i]['isClick'] = 1;
|
|
}
|
|
}
|
|
return $this->result(1, '成功', $res);
|
|
}
|
|
|
|
if ($res) {
|
|
return $this->result(1, '成功', $res);
|
|
} else {
|
|
return $this->result(1, '暂无地址');
|
|
}
|
|
}
|
|
//删除收货地址
|
|
public function actionDelUserAddress() {
|
|
$request = Yii::$app->request;
|
|
$result=axios_request();
|
|
if($request->isGet){
|
|
return $this->result(2, '请求异常');
|
|
}
|
|
if(!$result['id']){
|
|
return $this->result(2, '缺少参数');
|
|
}
|
|
$res = Yii::$app->db->createCommand()->update('{{%ybwm_user_address}}', ['deleteAt'=>time()], 'id=:id', ['id' =>$result['id']])->execute();
|
|
if ($res) {
|
|
return $this->result(1, '成功');
|
|
} else {
|
|
return $this->result(2, '失败');
|
|
}
|
|
}
|
|
//选择收货地址
|
|
public function actionReceivingAddress() {
|
|
$result=axios_request();
|
|
if(!$result['storeId']){
|
|
return $this->result(2, '参数错误');
|
|
}
|
|
$userAddList=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_address}}')
|
|
->where('userId=:userId AND deleteAt=0',[':userId'=>$this->wqData['userId']])->one();
|
|
if (!$userAddList) {
|
|
return $this->result(2, '没有添加收货地址');
|
|
}
|
|
$moneyArr=Order::getCarMoney($this->wqData['userId'],$result['storeId']);
|
|
$takeOutSet=Config::getStoreSet('takeOutSet',$result['storeId']);
|
|
$takeOutSet['boxType']=$takeOutSet['boxType']?:1;
|
|
if($takeOutSet['boxType']==1){
|
|
$boxMoney = $takeOutSet['boxMoney']; //订单餐盒费
|
|
}else{
|
|
$boxMoney = $moneyArr['boxMoney']; //订单餐盒费
|
|
}
|
|
$orderMoney=bcadd($moneyArr['goodsMoney'],$boxMoney,2);
|
|
|
|
if ($result['addressId']) {
|
|
$userAdd=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_address}}')
|
|
->where('id=:id',[':id'=>$result['addressId']])->one();
|
|
|
|
$res = Order::getDeliveryMoney($userAdd['lat'], $userAdd['lng'],$result['storeId'],$orderMoney);
|
|
if ($res) {
|
|
$userAdd['deliveryMoney'] = (string)$res['money']?:0;
|
|
$userAdd['startMoney'] = $res['startMoney']?:0;
|
|
$userAdd['oldMoney'] = $res['oldMoney']?:0;
|
|
$userAdd['discount'] = $res['discount']?:0;
|
|
return $this->result(1, '成功', $userAdd);
|
|
} else {
|
|
return $this->result(2, '不在配送范围内');
|
|
}
|
|
} else {
|
|
$userAdd=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_address}}')
|
|
->where('userId=:userId AND deleteAt=0',[':userId'=>$this->wqData['userId']])->orderBy(["SQRT(POWER(" . $result['lat'] . " - lat, 2) + POWER(" . $result['lng'] . " - lng,2))"=>SORT_ASC])->one();
|
|
$res = Order::getDeliveryMoney($userAdd['lat'], $userAdd['lng'],$result['storeId'],$orderMoney);
|
|
if ($res) {
|
|
$userAdd['deliveryMoney'] =(string)$res['money'];
|
|
$userAdd['startMoney'] = $res['startMoney']?:0;
|
|
$userAdd['oldMoney'] = $res['oldMoney']?:0;
|
|
$userAdd['discount'] = $res['discount']?:0;
|
|
return $this->result(1, '成功', $userAdd);
|
|
} else {
|
|
return $this->result(2, '不在配送范围内');
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//获取帮助中心
|
|
public function actionGetHelpsList(){
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$data= (new \yii\db\Query())
|
|
->select('id,title')
|
|
->from('{{%ybwm_helps}}')
|
|
->where(['deleteAt'=>0,'uniacid'=>$uniacid,'display'=>1,'otherType'=>1])
|
|
->all();
|
|
return $this->result(1,'成功',$data);
|
|
}
|
|
//帮助中心详情
|
|
public function actionGetHelpsDetail(){
|
|
$result=axios_request();
|
|
$data= (new \yii\db\Query())
|
|
->from('{{%ybwm_helps}}')
|
|
->where(['id'=>$result['id']])
|
|
->one();
|
|
return $this->result(1,'成功',$data);
|
|
}
|
|
|
|
//查看储值规则
|
|
public function actionRechargeRule(){
|
|
$res = (new \yii\db\Query())
|
|
->from('{{%ybwm_user_wallet}}')
|
|
->where(['display'=>1,'deleteAt'=>0,'uniacid'=>$this->wqData['uniacid']])
|
|
->all();
|
|
$user=(new \yii\db\Query())
|
|
->select('balance')
|
|
->from('{{%ybwm_member}}')
|
|
->where(['id'=>$this->wqData['userId']])
|
|
->one();
|
|
$order=(new \yii\db\Query())
|
|
->select('sum(giveMoney) as money')
|
|
->from('{{%ybwm_user_balance_order}}')
|
|
->where(['userId'=>$this->wqData['userId'],'state'=>2])
|
|
->one();
|
|
foreach ($res as $key => $value) {
|
|
$coupon=(new \yii\db\Query())
|
|
->select('name')
|
|
->from('{{%ybwm_coupon}}')
|
|
->where(['in','id',json_decode($value['couponId'],true)])
|
|
->all();
|
|
$res[$key]['couponArr']=$coupon;
|
|
}
|
|
$data['list']=$res;
|
|
$data['money']=$user['balance']?:0;
|
|
$data['giveMoney']=$order['money']?:0;
|
|
return $this->result(1,'成功',$data);
|
|
}
|
|
//充值下订单
|
|
public function actionRechargeOrder(){
|
|
$request = Yii::$app->request;
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$storeId=$result['acid'];
|
|
if($request->isGet){
|
|
return $this->result(2,'请求异常');
|
|
}
|
|
$data['userId'] = $userId;
|
|
$data['money'] = $result['money'];
|
|
$data['createdAt'] = time();
|
|
$res=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_wallet}}')
|
|
->where('money<=:money AND deleteAt=0 AND uniacid=:uniacid AND display=1',[':money'=>$result['money'],':uniacid'=>$uniacid])
|
|
->orderBy('money desc')
|
|
->all();
|
|
$data['giveMoney'] =0;
|
|
$data['giveScore'] =0;
|
|
$data['giveGrow'] =0;
|
|
$data['couponId'] ='';
|
|
if ($res) {
|
|
if($res[0]['moneyOpen']==1){
|
|
$data['giveMoney'] = $res[0]['giveMoney']; //赠送金额
|
|
}
|
|
if($res[0]['integralOpen']==1){
|
|
$data['giveScore'] = $res[0]['integral']; //赠送积分
|
|
}
|
|
if($res[0]['couponOpen']==1){
|
|
$data['couponId'] = $res[0]['couponId'];//赠送优惠券
|
|
$data['num']=count(json_decode($res[0]['couponId'],true))?:0;
|
|
}
|
|
if($res[0]['growOpen']==1){
|
|
$data['giveGrow'] = $res[0]['giveGrow'];//赠送成长值
|
|
}
|
|
$data['giveId'] = $res[0]['id'];
|
|
}
|
|
$data['outTradeNo'] = '3409' . date("YmdHis") . rand(111111, 999999);
|
|
$data['state'] = 1;
|
|
$data['storeId'] = $storeId;
|
|
$data['uniacid'] = $uniacid;
|
|
$order = Yii::$app->db->createCommand()->insert('{{%ybwm_user_balance_order}}', $data)->execute();
|
|
$orderId = Yii::$app->db->getLastInsertID();
|
|
if ($order) {
|
|
return $this->result(1, '成功', $orderId);
|
|
} else {
|
|
return $this->result(2, '失败');
|
|
}
|
|
|
|
}
|
|
//余额明细
|
|
public function actionBalanceDetails(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$type=$result['type']?:1;
|
|
$page=$result['page']?:1;
|
|
$num=($page-1)*10;
|
|
$limit=10;
|
|
$list=(new \yii\db\Query())
|
|
->select('id,type,from_unixtime(createdAt) createdAt,money,note')
|
|
->from('{{%ybwm_user_balance}}')
|
|
->where(['userId'=>$userId,'type'=>$type])
|
|
->offset($num)
|
|
->limit($limit)
|
|
->orderBy('id desc')
|
|
->all();
|
|
$data['list']=$list?:[];
|
|
$order=(new \yii\db\Query())
|
|
->select('sum(giveMoney) as giveMoney,sum(money) as money')
|
|
->from('{{%ybwm_user_balance_order}}')
|
|
->where(['userId'=>$userId,'state'=>2])
|
|
->one();
|
|
$detailed=(new \yii\db\Query())
|
|
->select('sum(money) as money')
|
|
->from('{{%ybwm_user_balance}}')
|
|
->where(['userId'=>$userId,'type'=>2])
|
|
->one();
|
|
$data['money']=$order['money'];
|
|
$data['giveMoney']=$order['giveMoney'];
|
|
$data['expenditure']=$detailed['money'];
|
|
return $this->result(1, '成功', $data);
|
|
}
|
|
|
|
//积分明细
|
|
public function actionIntegralDetails(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$page=$result['page']?:1;
|
|
$num=($page-1)*10;
|
|
$limit=10;
|
|
$list=(new \yii\db\Query())
|
|
->select('id,type,from_unixtime(createdAt) createdAt,integral,note')
|
|
->from('{{%ybwm_user_integral}}')
|
|
->where(['userId'=>$userId])
|
|
->offset($num)
|
|
->limit($limit)
|
|
->orderBy('id desc')
|
|
->all();
|
|
return $this->result(1, '成功', $list);
|
|
}
|
|
//成长值明细
|
|
public function actionGrowthDetails(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$page=$result['page']?:1;
|
|
$num=($page-1)*10;
|
|
$limit=10;
|
|
$list=(new \yii\db\Query())
|
|
->select('id,type,from_unixtime(createdAt) createdAt,growth,note')
|
|
->from('{{%ybwm_user_growth}}')
|
|
->where(['userId'=>$userId])
|
|
->offset($num)
|
|
->limit($limit)
|
|
->orderBy('id desc')
|
|
->all();
|
|
return $this->result(1, '成功', $list);
|
|
}
|
|
//新客专享
|
|
public function actionNew(){
|
|
$now=time();
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$member=(new \yii\db\Query())
|
|
->select('portrait')
|
|
->from('{{%ybwm_member}}')
|
|
->where('id=:id',[':id'=>$userId])
|
|
->one();
|
|
$new=(new \yii\db\Query())
|
|
->from('{{%ybwm_new}}')
|
|
->where('uniacid=:uniacid AND deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime',[':startTime'=>$now,':endTime'=>$now,':uniacid'=>$uniacid])
|
|
->one();
|
|
$record=(new \yii\db\Query())
|
|
->from('{{%ybwm_new_record}}')
|
|
->where('userId=:userId',['userId'=>$userId])
|
|
->one();
|
|
if(!$new || $record || $member['portrait']){
|
|
return $this->result(1, '成功', []);
|
|
}
|
|
$data['icon']=$new['icon'];
|
|
$data['balance']=0;
|
|
$data['integral']=0;
|
|
$data['growth']=0;
|
|
$data['coupon']=[];
|
|
$data['couponArr']=[];
|
|
if($new['isBalance']==1){
|
|
$data['balance']=$new['balance'];
|
|
}
|
|
if($new['isIntegral']==1){
|
|
$data['integral']=$new['integral'];
|
|
}
|
|
if($new['isGrowth']==1){
|
|
$data['growth']=$new['growth'];
|
|
}
|
|
$coupon=json_decode($new['coupon'],true);
|
|
if($new['isCoupon']==1){
|
|
|
|
$list=(new \yii\db\Query())
|
|
->select('id,name,timeType,useEndTime,isNextDay,day,isFull,fullMoney,type,money,discount')
|
|
->from('{{%ybwm_coupon}}')
|
|
->where(['in','id',$coupon])
|
|
->all();
|
|
for($i=0;$i<count($list);$i++){
|
|
if($list[$i]['timeType']==1){
|
|
//日期范围
|
|
$list[$i]['useExplain']="有效期至".date("Y-m-d H:i",$list[$i]['useEndTime']);
|
|
}else{
|
|
if($list[$i]['isNextDay']==1){
|
|
$list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效(次日起)";
|
|
}else{
|
|
$list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效";
|
|
}
|
|
}
|
|
$list[$i]['explain']='';
|
|
if($list[$i]['isFull']==1){
|
|
$list[$i]['explain'] .='无门槛,';
|
|
}else{
|
|
$list[$i]['explain'] .='满'.$list[$i]['fullMoney'].'可用,';
|
|
}
|
|
}
|
|
$data['couponArr']=$list;
|
|
$data['coupon']=$coupon;
|
|
}
|
|
return $this->result(1, '成功', $data);
|
|
|
|
}
|
|
|
|
//领取新客专享
|
|
public function actionReceiveNew(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$new=(new \yii\db\Query())
|
|
->from('{{%ybwm_new}}')
|
|
->where('id=:id',[':id'=>$result['id']])
|
|
->one();
|
|
$record=(new \yii\db\Query())
|
|
->from('{{%ybwm_new_record}}')
|
|
->where('userId=:userId',['userId'=>$userId])
|
|
->one();
|
|
if(!$new || $record){
|
|
return $this->result(2, '活动不存在或已经领取过了');
|
|
}
|
|
$transaction = Yii::$app->db->beginTransaction(); //开始事务
|
|
if($new['isBalance']==1){
|
|
$data['balance']=$new['balance'];
|
|
$saveBalance=Member::saveBalance($userId, 2, $new['balance'], 1, '新客专享',$uniacid);
|
|
if(!$saveBalance){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败','赠送余额失败');
|
|
}
|
|
}
|
|
if($new['isGrowth']==1){
|
|
$data['growth']=$new['growth'];
|
|
$saveBalance=Member::saveGrowth($userId, 2, $new['growth'], 1, '新客专享',$uniacid);
|
|
if(!$saveBalance){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败','赠送余额失败');
|
|
}
|
|
}
|
|
if($new['isIntegral']==1){
|
|
$data['integral']=$new['integral'];
|
|
$saveIntegral=Member::saveIntegral($userId, 2, $new['integral'], 1, '新客专享',$uniacid);
|
|
if(!$saveIntegral){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败','赠送积分失败');
|
|
}
|
|
}
|
|
if($new['isCoupon']==1){
|
|
$coupon=json_decode($new['coupon'],true);
|
|
//发放优惠券
|
|
$receiveCoupon=Member::receiveCoupon($coupon, $userId,3);
|
|
if(!$receiveCoupon){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败','发放优惠券失败');
|
|
}
|
|
|
|
}
|
|
$newRecord['userId']=$userId;
|
|
$newRecord['newId']=$new['id'];
|
|
$newRecord['balance']=$data['balance'];
|
|
$newRecord['integral']=$data['integral'];
|
|
$newRecord['growth']=$data['growth'];
|
|
$newRecord['coupon']=json_encode($data['coupon']);
|
|
$newRecord['uniacid']=$new['uniacid'];
|
|
$newRecord['createdAt']=time();
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_new_record}}', $newRecord)->execute();
|
|
if(!$res){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败');
|
|
}
|
|
$transaction->commit();
|
|
return $this->result(1, '成功', []);
|
|
}
|
|
|
|
//收藏有礼
|
|
public function actionCollection(){
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$now=time();
|
|
$result=axios_request();
|
|
$list=(new \yii\db\Query())
|
|
->from('{{%ybwm_collection_courtesy}}')
|
|
->where(['userId'=>$userId])
|
|
->one()?:[];
|
|
$request = Yii::$app->request;
|
|
if($request->isGet){
|
|
return $this->result(1, '成功', $list);
|
|
}
|
|
if($list['state']==1){
|
|
return $this->result(2, '您的申请正在审核中');
|
|
}
|
|
$data['media']=$result['media'];
|
|
if($list['id']){
|
|
$data['state']=1;
|
|
$data['changeAt']=$now;
|
|
$res=Yii::$app->db->createCommand()->update('{{%ybwm_collection_courtesy}}', $data, 'id=:id', ['id' =>$list['id']])->execute();
|
|
}else{
|
|
$data['userId']=$userId;
|
|
$data['uniacid']=$uniacid;
|
|
$data['createdAt']=$now;
|
|
$res = Yii::$app->db->createCommand()->insert('{{%ybwm_collection_courtesy}}', $data)->execute();
|
|
}
|
|
if($res){
|
|
return $this->result(1,'成功');
|
|
}else{
|
|
return $this->result(2,'失败');
|
|
}
|
|
|
|
}
|
|
//发券宝
|
|
public function actionIssueCoupons(){
|
|
$now=time();
|
|
$storeId=Store::getMainStore($this->wqData['uniacid'])['id'];
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$monthArr = getMonthRange();
|
|
$weekArr = getWeekRange();
|
|
$member = (new \yii\db\Query())
|
|
->from('{{%ybwm_member}}')
|
|
->where(['id'=>$userId])
|
|
->one();
|
|
$user = (new \yii\db\Query())
|
|
->from('{{%ybwm_member_bind}}')
|
|
->where(['userId'=>$userId,'storeId'=>$storeId])
|
|
->one();
|
|
$newOrder = (new \yii\db\Query())
|
|
->from('{{%ybwm_bill}}')
|
|
->where(['userId'=>$userId])
|
|
->andWhere(['in','origin',[1]])
|
|
->one();//新用户
|
|
$issueCoupons = (new \yii\db\Query())
|
|
->from('{{%ybwm_issue_coupons}}')
|
|
->where('uniacid=:uniacid AND deleteAt=0 AND display=1 AND startTime<:time AND endTime>:time',[':uniacid'=>$uniacid,':time'=>$now])
|
|
->one();
|
|
$resData['activity'] = [];
|
|
$resData['coupon'] = [];
|
|
if(!$issueCoupons){
|
|
return $this->result(1, '成功', $resData);
|
|
}
|
|
if($issueCoupons['people']==2 AND $newOrder){
|
|
return $this->result(1,'成功',[]);
|
|
}
|
|
if($issueCoupons['people']==3 AND !$member['level']){
|
|
return $this->result(1,'成功',[]);
|
|
}
|
|
if($issueCoupons['people']==5 AND !in_array($user['vipLabel'],json_decode($issueCoupons['userLabel'],true))){
|
|
return $this->result(1,'成功',[]);
|
|
}
|
|
if($issueCoupons['people']==6 AND !in_array($userId,json_decode($issueCoupons['userArr'],true))){
|
|
return $this->result(1,'成功',[]);
|
|
}
|
|
$receive=[];
|
|
if ($issueCoupons['numType'] == 1) {
|
|
$receive =(new \yii\db\Query())
|
|
->from('{{%ybwm_grant_coupon_receive}}')
|
|
->where(['grantId'=>$issueCoupons['id'],'userId'=>$userId,'activityType'=>1])
|
|
->one();
|
|
}
|
|
if ($issueCoupons['numType'] == 2) {
|
|
//每月一次
|
|
$receive = (new \yii\db\Query())
|
|
->from('{{%ybwm_grant_coupon_receive}}')
|
|
->where('grantId=:grantId AND activityType=1 AND userId=:userId AND createdAt>:startTime AND createdAt<=:endTime',[':startTime'=>$monthArr['start'],'endTime'=>$monthArr['end'],'grantId'=>$issueCoupons['id'],'userId'=>$userId])
|
|
->one();
|
|
}
|
|
if ($issueCoupons['numType'] == 3) {
|
|
//每周一次
|
|
$receive = (new \yii\db\Query())
|
|
->from('{{%ybwm_grant_coupon_receive}}')
|
|
->where('grantId=:grantId AND userId=:userId AND activityType=1 AND createdAt>:startTime AND createdAt<=:endTime',[':startTime'=>$weekArr['start'],'endTime'=>$weekArr['end'],'grantId'=>$issueCoupons['id'],'userId'=>$userId])
|
|
->one();
|
|
}
|
|
if (!$receive) {
|
|
// $data=array('activityType'=>1,'userId' => $userId, 'grantId' => $issueCoupons['id'], 'createdAt' => time(), 'uniacid' => $uniacid);
|
|
// Yii::$app->db->createCommand()->insert('{{%ybwm_grant_coupon_receive}}', $data)->execute();
|
|
// Member::receiveCoupon(json_decode($issueCoupons['coupon'],true), $userId, 6);
|
|
$list=(new \yii\db\Query())
|
|
->select('id,name,timeType,useEndTime,isNextDay,day,isFull,fullMoney,type,money,discount')
|
|
->from('{{%ybwm_coupon}}')
|
|
->where(['in','id',json_decode($issueCoupons['coupon'],true)])
|
|
->all();
|
|
for($i=0;$i<count($list);$i++){
|
|
if($list[$i]['timeType']==1){
|
|
//日期范围
|
|
$list[$i]['useExplain']="有效期至".date("Y-m-d H:i",$list[$i]['useEndTime']);
|
|
}else{
|
|
if($list[$i]['isNextDay']==1){
|
|
$list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效(次日起)";
|
|
}else{
|
|
$list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效";
|
|
}
|
|
}
|
|
$list[$i]['explain']='';
|
|
if($list[$i]['isFull']==1){
|
|
$list[$i]['explain'] .='无门槛,';
|
|
}else{
|
|
$list[$i]['explain'] .='满'.$list[$i]['fullMoney'].'可用,';
|
|
}
|
|
}
|
|
$resData['activity'] = $issueCoupons;
|
|
$resData['coupon'] = $list;
|
|
return $this->result(1, '成功', $resData);
|
|
}
|
|
return $this->result(1, '成功', $resData);
|
|
}
|
|
//收藏
|
|
public function actionSaveCollection(){
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$result=axios_request();
|
|
$collectionId=$result['collectionId'];
|
|
$type=$result['type'];
|
|
$collection=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_collection}}')
|
|
->where('userId=:userId AND collectionId=:collectionId AND type=:type',[':userId'=>$userId,':collectionId'=>$collectionId,':type'=>$type])
|
|
->one();
|
|
if($collection){
|
|
$data['state']=1;
|
|
$data['changeAt']=time();
|
|
if($collection['state']==1){
|
|
$data['state']=2;
|
|
}
|
|
Yii::$app->db->createCommand()->update('{{%ybwm_user_collection}}', $data,['id'=>$collection['id']])->execute();
|
|
}else{
|
|
$data['userId']=$userId;
|
|
$data['collectionId']=$collectionId;
|
|
$data['type']=$type;
|
|
$data['createdAt']=time();
|
|
$data['uniacid']=$uniacid;
|
|
Yii::$app->db->createCommand()->insert('{{%ybwm_user_collection}}', $data)->execute();
|
|
}
|
|
return $this->result(1, '成功');
|
|
}
|
|
|
|
//首页弹窗合集
|
|
public function actionWindow(){
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$data['issueCoupons']=Window::getIssueCoupons($uniacid,$userId);
|
|
return $this->result(1, '成功',$data);
|
|
}
|
|
public function actionReceiveIssueCoupons(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$id=$result['id'];
|
|
|
|
$monthArr = getMonthRange();
|
|
$weekArr = getWeekRange();
|
|
$issueCoupons = (new \yii\db\Query())
|
|
->from('{{%ybwm_issue_coupons}}')
|
|
->where('id=:id',[':id'=>$id])
|
|
->one();
|
|
if ($issueCoupons['numType'] == 1) {
|
|
$receive =(new \yii\db\Query())
|
|
->from('{{%ybwm_grant_coupon_receive}}')
|
|
->where(['grantId'=>$issueCoupons['id'],'userId'=>$userId,'activityType'=>1])
|
|
->one();
|
|
}
|
|
if ($issueCoupons['numType'] == 2) {
|
|
//每月一次
|
|
$receive = (new \yii\db\Query())
|
|
->from('{{%ybwm_grant_coupon_receive}}')
|
|
->where('grantId=:grantId AND activityType=1 AND userId=:userId AND createdAt>:startTime AND createdAt<=:endTime',[':startTime'=>$monthArr['start'],'endTime'=>$monthArr['end'],'grantId'=>$issueCoupons['id'],'userId'=>$userId])
|
|
->one();
|
|
}
|
|
if ($issueCoupons['numType'] == 3) {
|
|
//每周一次
|
|
$receive = (new \yii\db\Query())
|
|
->from('{{%ybwm_grant_coupon_receive}}')
|
|
->where('grantId=:grantId AND userId=:userId AND activityType=1 AND createdAt>:startTime AND createdAt<=:endTime',[':startTime'=>$weekArr['start'],'endTime'=>$weekArr['end'],'grantId'=>$issueCoupons['id'],'userId'=>$userId])
|
|
->one();
|
|
}
|
|
if($receive){
|
|
return $this->result(2, '不可重复领取');
|
|
}
|
|
$data=array('activityType'=>1,'userId' => $userId, 'grantId' => $id, 'createdAt' => time(), 'uniacid' => $uniacid);
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_grant_coupon_receive}}', $data)->execute();
|
|
if($res){
|
|
Member::receiveCoupon(json_decode($issueCoupons['coupon'],true), $userId, 6);
|
|
return $this->result(1, '成功');
|
|
}else{
|
|
return $this->result(2, '失败');
|
|
}
|
|
|
|
}
|
|
|
|
//兑换码
|
|
public function actionExchangeCode(){
|
|
$request = Yii::$app->request;
|
|
$userId=$this->wqData['userId'];
|
|
$result=axios_request();
|
|
$code=$result['code'];
|
|
if(!$code){
|
|
return $this->result(2, '兑换码不能为空');
|
|
}
|
|
$now=time();
|
|
$info = (new \yii\db\Query())
|
|
->select('a.code,b.id,b.coupon,b.name,b.uniacid,b.isCoupon')
|
|
->from('{{%ybwm_exchange_code_info}} as a')
|
|
->join('LEFT JOIN', '{{%ybwm_exchange_code}} as b', 'b.id = a.codeId')
|
|
->where('a.code=:code AND a.state=2 AND b.stock>0 AND b.deleteAt=0 AND b.display=1 AND b.startTime<:startTime AND b.endTime>:endTime',[':startTime'=>$now,':endTime'=>$now,':code'=>$code])
|
|
->one();
|
|
if(!$info){
|
|
return $this->result(2, '兑换码不存在或活动已结束');
|
|
}
|
|
$info['coupon']=$info['coupon']?json_decode($info['coupon'],true):[];
|
|
if($request->isGet){
|
|
$coupon=(new \yii\db\Query())
|
|
->select('name')
|
|
->from('{{%ybwm_coupon}}')
|
|
->where(['in','id',$info['coupon']])
|
|
->all();
|
|
$info['couponArr']=$coupon;
|
|
return $this->result(1, '成功',$info);
|
|
}
|
|
$record=(new \yii\db\Query())
|
|
->from('{{%ybwm_exchange_code_record}}')
|
|
->where('userId=:userId AND codeId=:codeId',[':userId'=>$userId,':codeId'=>$info['id']])
|
|
->count();
|
|
if($record>=$info['maxNum'] AND $info['limitType']==2){
|
|
return $this->result(2, '兑换次数已达上限');
|
|
}
|
|
$data['userId']=$userId;
|
|
$data['codeId']=$info['id'];
|
|
$data['createdAt']=$now;
|
|
$data['uniacid']=$info['uniacid'];
|
|
$transaction = Yii::$app->db->beginTransaction(); //开始事务
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_exchange_code_record}}', $data)->execute();
|
|
if($res){
|
|
if($info['isCoupon']==1 AND $info['coupon']){
|
|
$receiveCoupon=Member::receiveCoupon($info['coupon'],$userId,10);
|
|
if(!$receiveCoupon){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败','发放优惠券失败');
|
|
}
|
|
}
|
|
$changeCode=Yii::$app->db->createCommand()->update('{{%ybwm_exchange_code_info}}', ['userId'=>$userId,'state'=>1,'changeAt'=>$now],['code'=>$code])->execute();
|
|
if(!$changeCode){
|
|
$transaction->rollBack();
|
|
return $this->result(2, '失败','核销失败');
|
|
}
|
|
Yii::$app->db->createCommand()->update('{{%ybwm_exchange_code}}', ['stock'=>new \yii\db\Expression('stock - 1'),'salesNum'=>new \yii\db\Expression('salesNum + 1')], 'id=:id', ['id' =>$info['id']])->execute();
|
|
$transaction->commit();
|
|
return $this->result(1, '成功');
|
|
}else{
|
|
return $this->result(2, '失败');
|
|
}
|
|
}
|
|
//奖励金提现
|
|
public function actionBonusWithdrawal(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$money=$result['money'];
|
|
$item=$result['item']?:1;
|
|
$type=$result['type']?:1;
|
|
if($item==1){
|
|
//老带新
|
|
$userBonus=(new \yii\db\Query())
|
|
->select('bonus')
|
|
->from('{{%ybwm_member}}')
|
|
->where('id=:id',[':id'=>$userId])
|
|
->one()['bonus']?:0;
|
|
$withdrawal = Config::getSystemSet('oldWithNewWithdrawalSet', $uniacid);
|
|
}else{
|
|
//分销商
|
|
$userBonus=(new \yii\db\Query())
|
|
->select('commission')
|
|
->from('{{%ybwm_member}}')
|
|
->where('id=:id',[':id'=>$userId])
|
|
->one()['commission']?:0;
|
|
$withdrawal = Config::getSystemSet('distributionWithdrawalSet', $uniacid);
|
|
}
|
|
$downMoney=$withdrawal['downMoney']?:0;
|
|
if($money<$downMoney){
|
|
return $this->result(2,'最低提现'.$downMoney.'元');
|
|
}
|
|
$record=(new \yii\db\Query())
|
|
->from('{{%ybwm_bonus_withdrawal}}')
|
|
->where('userId=:userId AND item=:item AND state!=3',[':item'=>$item,':userId'=>$userId])
|
|
->orderBy('id desc')
|
|
->one();
|
|
if (time() < $record['createdAt'] + $withdrawal['day'] * 24 * 3600) {
|
|
return $this->result(2, '提现周期未到,不可提现');
|
|
}
|
|
if($userBonus<$money){
|
|
return $this->result(2, '账户余额不足');
|
|
}
|
|
if($type==2){
|
|
//支付宝
|
|
$account['aliAccount']=$result['aliAccount'];
|
|
$account['aliUserName']=$result['aliUserName'];
|
|
}
|
|
if($type==3){
|
|
//银行卡
|
|
$account['bankName']=$result['bankName'];
|
|
$account['bankUserName']=$result['bankUserName'];
|
|
$account['bankAccount']=$result['bankAccount'];
|
|
}
|
|
$data['money']=$money;
|
|
$charge=0;
|
|
if($withdrawal['rate']>0){
|
|
$charge = bcmul($money, $withdrawal['rate'] / 100, 2);
|
|
if ($charge < $withdrawal['rateDownMoney']) {
|
|
$charge = $withdrawal['rateDownMoney'];
|
|
}
|
|
if ($charge > $withdrawal['rateUpMoney']) {
|
|
$charge = $withdrawal['rateUpMoney'];
|
|
}
|
|
}
|
|
$data['account']=json_encode($account);
|
|
$data['charge']=$charge;//手续费
|
|
$data['type']=$type;
|
|
$data['actualMoney'] = bcsub($money, $charge, 2);
|
|
$data['state'] = 1;
|
|
$data['item'] = $item;
|
|
$data['userId'] = $userId;
|
|
$data['uniacid'] = $uniacid;
|
|
$data['createdAt'] = time();
|
|
$data['outTradeNo'] = date("YmdHis") . rand(111111, 999999);
|
|
$transaction = Yii::$app->db->beginTransaction(); //开始事务
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_bonus_withdrawal}}', $data)->execute();
|
|
if(!$res){
|
|
$transaction->rollBack();
|
|
return $this->result(2,'失败');
|
|
}
|
|
if($item==1) {
|
|
$userRes=Member::saveBonus($userId,2,$money,2,'奖励金提现',$uniacid);
|
|
}else{
|
|
$userRes=Member::saveBonus($userId,2,$money,2,'分销佣金提现',$uniacid,2);
|
|
}
|
|
if(!$userRes){
|
|
$transaction->rollBack();
|
|
return $this->result(2,'失败');
|
|
}
|
|
$transaction->commit();
|
|
return $this->result(1,'成功');
|
|
}
|
|
//提现列表
|
|
public function actionWithdrawalList(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$item=$result['item'];
|
|
$page=$result['page']?:1;
|
|
$num=($page-1)*10;
|
|
$limit=10;
|
|
$table=(new \yii\db\Query())
|
|
->from('{{%ybwm_bonus_withdrawal}}')
|
|
->where(['userId'=>$userId,'item'=>$item]);
|
|
$list=$table->offset($num)
|
|
->limit($limit)->orderBy('id desc')->all();
|
|
for($i=0;$i<count($list);$i++){
|
|
$list[$i]['account']=json_decode($list[$i]['account'],true)?:[];
|
|
$list[$i]['createdAt']=date("Y-m-d H:i:s",$list[$i]['createdAt']);
|
|
}
|
|
return $this->result(1, '成功',$list);
|
|
}
|
|
|
|
//集点活动
|
|
public function actionCollectList(){
|
|
$result=axios_request();
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$storeId=$result['storeId'];
|
|
$now=time();
|
|
$collectGrant=(new \yii\db\Query())
|
|
->from('{{%ybwm_order_collect_coupon}}')
|
|
->where('uniacid=:uniacid AND deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime',[':startTime'=>$now,':endTime'=>$now,':uniacid'=>$uniacid])
|
|
->andWhere([
|
|
'or',
|
|
['and',['storeType'=>1,'storeId'=>$storeId]],
|
|
['storeType'=>2],
|
|
['and',['storeType'=>3],['like','storeArr',$storeId]]
|
|
])->one();
|
|
$startTime=$collectGrant['startTime'];
|
|
$endTime=$collectGrant['endTime'];
|
|
$collectGrantId=$collectGrant['id'];
|
|
$orderNum=$collectGrant['orderNum'];
|
|
$bool=0;$data=[];
|
|
$orderData=(new \yii\db\Query())
|
|
->from('{{%ybwm_takeout_order}}')
|
|
->where('uniacid=:uniacid AND storeId=:storeId AND userId=:userId AND state=5 AND collectCouponId=:collectGrantId AND createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime,':uniacid'=>$uniacid,':storeId'=>$storeId,':collectGrantId'=>$collectGrantId,':userId'=>$userId])
|
|
->orderBy('id asc')->limit($orderNum)
|
|
->all();
|
|
for($i=0;$i<$orderNum;$i++){
|
|
$res = isset($orderData[$i]);
|
|
$data[]=array(
|
|
'state'=>$res
|
|
);
|
|
}
|
|
//查出用户集点活动的卡券
|
|
$collectList=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('couponId=:couponId AND userId=:userId AND receiveType=17',[':couponId'=>$collectGrantId,':userId'=>$userId])
|
|
->one();
|
|
|
|
$state=$collectList?true:false;
|
|
|
|
$newData=array(
|
|
'name'=>$collectGrant['name'],
|
|
'orderNum'=>$collectGrant['orderNum'],
|
|
'useNum'=>count($orderData)?:0,
|
|
'state'=>$state,
|
|
'money'=>$collectGrant['money'],
|
|
'saleNum'=>bcsub($collectGrant['orderNum'],count($orderData)),
|
|
'data'=>$data,
|
|
'list'=>$orderData,
|
|
'collectGrant'=>$collectGrant
|
|
);
|
|
//Member::saveColletCoupon(0,$uniacid,$storeId,$userId);
|
|
return $this->result(1, '成功',$newData);
|
|
}
|
|
}
|