dggjimai/application/api/controller/UserController.php

468 lines
17 KiB
PHP
Executable File

<?php
namespace app\api\controller;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\Goods;
use app\common\validate\WithdrawAccountValidate;
use app\common\model\ShopConfig;
use app\common\model\AccountLog;
use app\common\model\WithdrawApply;
use app\common\model\WithdrawAccount;
use think\Request;
use tools\Crypt;
class UserController extends Controller
{
//我的设备
public function equipment(Request $request,Order $model)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$size=$param['size']>0?$param['size']:12;
$page_l=$param['page']>1?$param['page']:1;
$page=$size * ($page_l - 1);
$order_count = $model
->alias('o')
->where(array('o.user_id'=>$uid))
->join('mall_image mi', 'mi.union_id= o.goods_id and is_cover=1 and mi.type=1', 'LEFT')
->count();
$totalPages = ceil($order_count / $size);
$order_list = $model
->alias('o')
->field("mi.thumb_image as img, o.goods_name,o.attr,o.order_price,number,o.id as order_id,o.create_time,o.end_time")
->where(array('o.user_id'=>$uid))
->join('mall_image mi', 'mi.union_id= o.goods_id and is_cover=1 and mi.type=1', 'LEFT')
->limit($page, $size)
->order('o.create_time DESC')
->select();
if (!$order_list) {
return client_error('暂无数据');
}
foreach ($order_list as $k1 => $v1) {
$order_list[$k1]['img'] =$this->img_url.$v1['img'];
$order_list[$k1]['order_id'] = Crypt::encrypt($v1['order_id']);
}
$page=array(
'count'=>$order_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$order_list,'page'=>$page));
}
//收益统计
public function profit_census(Request $request,AccountLog $almodel,WithdrawApply $wamodel,User $umodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$type = $param['type']??'1';
//积分规则
$integral_rule =ShopConfig::get('policy', 'integral_rule');
//个人收益
if($type==1){
$where[]=array('source_type','in','100,110,120');
$where[]=array('user_id','eq',$uid);
//今日收益
$amount_today = $almodel
->where($where)
->whereTime('create_time', 'today')
->sum('change_amount');
//已提现金额
$amount_ytx = $wamodel->where(array('user_id'=>$uid,'status'=>2))->sum('money');
//可提现金额
$amount_ktx = $umodel->where(array('id'=>$uid))->value('integral');
$data=array(
'amount_today'=>$amount_today,
'amount_ytx'=>$amount_ytx,
'amount_ktx'=>$amount_ktx,
'integral_rule'=>$integral_rule,
);
return success($data);
}
if($type==2){
$where[]=array('user_id','eq',$uid);
//今日收益
$amount_today = $almodel
->where($where)
->where(array('source_type'=>100))
->whereTime('create_time', 'today')
->sum('change_amount');
//累计收益
$amount_sum = $almodel
->where($where)
->where(array('source_type'=>100))
->sum('change_amount');
//推广人数
$user_fans =$umodel->where([['first_leader|second_leader', '=', $uid]])->count();
$data=array(
'amount_today'=>$amount_today,
'amount_sum'=>$amount_sum,
'user_fans'=>$user_fans,
'integral_rule'=>$integral_rule,
);
return success($data);
}
}
//提现记录
public function withdraw(Request $request,WithdrawApply $wamodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$size=$param['size']>0?$param['size']:12;
$page_l=$param['page']>1?$param['page']:1;
$page=$size * ($page_l - 1);
$withdraw_count = $wamodel
->where(array('user_id'=>$uid))
->count();
$totalPages = ceil($withdraw_count / $size);
$withdraw_list = $wamodel
->field("money,create_time,poundage,status")
->where(array('user_id'=>$uid))
->limit($page, $size)
->order('create_time DESC')
->select();
if (!$withdraw_list) {
return client_error('暂无数据');
}
foreach ($withdraw_list as $key => $value) {
$withdraw_list[$key]['status_text'] =$wamodel->STATUS_TYPE[$value['status']];
}
$page=array(
'count'=>$withdraw_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$withdraw_list,'page'=>$page));
}
//收益记录
public function account_log(Request $request,AccountLog $almodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$size=$param['size']>0?$param['size']:12;
$page_l=$param['page']>1?$param['page']:1;
$page=$size * ($page_l - 1);
$where[]=array('user_id','=',$uid);
$type = $param['type']??'';
if($type){
$where[]=array('source_type','=',$type);
}
$accountlog_count = $almodel
->alias('al')
->where($where)
->join('user u', 'u.id= al.user_id')
->count();
$totalPages = ceil($accountlog_count / $size);
$accountlog_list = $almodel
->alias('al')
->field("change_amount,al.create_time,source_type,avatar,nickname")
->where($where)
->join('user u', 'u.id= al.user_id')
->limit($page, $size)
->order('create_time DESC')
->select();
if (!$accountlog_list) {
return client_error('暂无数据');
}
foreach ($accountlog_list as $key => $value) {
$accountlog_list[$key]['avatar'] =$this->img_url.$value['avatar'];
$accountlog_list[$key]['source_type_text'] =$almodel->SOURCE_TYPE[$value['source_type']];
}
$amount_sum = $almodel
->where($where)
->sum('change_amount');
$page=array(
'count'=>$accountlog_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$accountlog_list,'page'=>$page,'amount_sum'=>$amount_sum));
}
//邀请明细
public function invitation_user(Request $request,User $umodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$size=$param['size']>0?$param['size']:12;
$page_l=$param['page']>1?$param['page']:1;
$page=$size * ($page_l - 1);
$user_count = $umodel
->where([['first_leader|second_leader', '=', $uid]])
->count();
$totalPages = ceil($user_count / $size);
$user_list = $umodel
->field("create_time,avatar,nickname")
->where([['first_leader|second_leader', '=', $uid]])
->limit($page, $size)
->order('create_time DESC')
->select();
if (!$user_list) {
return client_error('暂无数据');
}
foreach ($user_list as $key => $value) {
$user_list[$key]['avatar'] =$this->img_url.$value['avatar'];
}
$page=array(
'count'=>$user_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
//推广规则
$extension_rule =ShopConfig::get('policy', 'extension_rule');
return success(array('list'=>$user_list,'page'=>$page,'extension_rule'=>$extension_rule));
}
//用户中心
public function userhome(Request $request,User $umodel,Order $omodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$user_info=$umodel->field('avatar,nickname,user_level_id as level,integral,mobile')->where(array('id'=>$uid))->find();
if($user_info['level']==2){
$time=time();
$order_count=$omodel->where('end_time>='.$time.' and user_id='.$uid)->count();
if($order_count==0){
$user_info['level']=1;
}
}
$user_info['avatar'] =$this->img_url.$user_info['avatar'];
//联系电话
$tel =ShopConfig::get('website', 'tel');
$data=array(
'user_info'=>$user_info,
'tel'=>$tel
);
return success($data);
}
//提现账户列表
public function account_list(Request $request,WithdrawAccount $wamodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$account_list=$wamodel->field('real_name,real_tel,account,subbank,type,id')->where(array('user_id'=>$uid))->select();
foreach ($account_list as $key => $value) {
$account_list[$key]['id'] = Crypt::encrypt($value['id']);
}
return success($account_list);
}
//添加提现账户
public function account_add(Request $request,WithdrawAccount $wamodel, WithdrawAccountValidate $validate)
{
$param = $request->param();
$param['user_id'] = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$param['user_id']){
return unauthorized('请先授权');
}
$validate_result = $validate->scene('add')->check($param);
if (!$validate_result) {
return client_error($validate->getError());
}
// 启动事务
$wamodel->startTrans();
try{
$wamodel::create($param);
// 提交事务
$wamodel->commit();
}catch(\Exception $e){
// 回滚事务
$wamodel->rollback();
return client_error($e->getMessage());
}
return success('','添加成功');
}
//删除提现账户
public function account_dal(Request $request,WithdrawAccount $wamodel)
{
$param = $request->param();
$param['user_id'] = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$param['user_id']){
return unauthorized('请先授权');
}
$param['id'] = $param['id']?Crypt::decrypt($param['id']):'';
if (!$param['id']) {
return client_error('账户不存在');
}
$result = $wamodel->whereIn('id', $param['id'])->delete();
return $result ? success('','删除成功') : client_error('删除失败');
}
//提现操作
public function wthdrawal(Request $request,WithdrawAccount $wamodel,WithdrawApply $waymodel,AccountLog $almodel,User $umodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
//提现积分
$integral = $param['integral']??'0';
//提现账户
$account_id = $param['account_id']?Crypt::decrypt($param['account_id']):'';
if(!$account_id){
return client_error('请选择提现账户');
}
$account_info=$wamodel->where(array('id'=>$account_id))->find();
if(!$account_info){
return client_error('请选择提现账户');
}
$umodel_info=$umodel->field('integral')->where(array('id'=>$uid))->find();
if($umodel_info['integral']<$integral){
return client_error('积分不足,无法提现');
}
$min_withdraw =ShopConfig::get('withdraw', 'min_withdraw');//最低提现
$max_withdraw =ShopConfig::get('withdraw', 'max_withdraw');//最高提现积分
$poundage_type =ShopConfig::get('withdraw', 'poundage_type');//手续费类型 1固定 2比例
$poundage =ShopConfig::get('withdraw', 'poundage');//手续费
if($integral<$min_withdraw){
return client_error('最少提现积分数'.$min_withdraw.',无法提现');
}
if($integral>$max_withdraw){
return client_error('最多提现积分数'.$max_withdraw.',无法提现');
}
if($poundage_type==2){
$poundage=$integral*($poundage/100);
}
// 启动事务
$waymodel->startTrans();
try{
$data = [
'sn' => createSn('withdraw_apply', 'sn'),
'user_id' => $uid,
'type' => $account_info['type'],
'account' => $account_info['account'] ?? '',
'real_name' => $account_info['real_name'] ?? '',
'real_tel' => $account_info['real_tel'] ?? '',
'money' => $integral,
'left_money' => $integral - $poundage,
'money_qr_code' => '',
'subbank' => $account_info['subbank'] ?? '',
'poundage' => $poundage,
'status' => 1, // 待提现
'create_time' => time(),
];
$withdraw_id = $waymodel->insertGetId($data);
//提交申请后,扣减用户的积分
$user = User::get($uid);
$user->integral = ['dec', $integral];
$user->save();
//增加佣金变动记录
$almodel->AccountRecord(
$uid,
$integral,
2,
140,
'',
$withdraw_id,
$data['sn']
);
// 提交事务
$waymodel->commit();
}catch(\Exception $e){
// 回滚事务
$waymodel->rollback();
return client_error($e->getMessage());
}
return success('','提交提现申请成功,等待审核打款');
}
//领取体验设备
public function experience(Request $request,Order $omodel,User $umodel)
{
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$uid){
return unauthorized('请先授权');
}
$goods_id=ShopConfig::get('goods', 'goods_id');
if(!$goods_id){
return client_error('体验设备信息不存在');
}
$is_open=ShopConfig::get('goods', 'is_open');
if($is_open==0){
return client_error('体验设备已关闭,无法领取');
}
$ty_count=$omodel->where(array('order_type'=>2,'goods_id'=>$goods_id))->count();
if($ty_count>0){
return client_error('您已领取该体验设备,无法重复领取');
}
$is_first=ShopConfig::get('goods', 'is_first');
if($is_first==1){
$user_count=$umodel->where(array('user_level_id'=>1))->count();
if($user_count==0){
return client_error('您不是新用户,无法领取');
}
}
$day=ShopConfig::get('goods', 'day');
$field_data = array();
$goods_info=Goods::where(array('id'=>$goods_id))->find()->toArray();
$field_data[] = [
'order_no'=>get_order_sn(),
'goods_id' => $goods_id,
'number' => 1,
'end_time' => time()+$day*60*60*24,
'user_id' => $uid,
'goods_name' => $goods_info['name'],
'attr' => $goods_info['attr'],
'order_price' =>'0.00',
'create_time'=>time(),
'order_type'=>2,
];
if(count($field_data)==0){
return client_error('体验设备信息不存在');
}
// 启动事务
$omodel->startTrans();
try{
$omodel->insertAll($field_data);
// 提交事务
$omodel->commit();
}catch(\Exception $e){
// 回滚事务
$omodel->rollback();
return client_error($e->getMessage());
}
return success('','领取成功');
}
}