dggjimai/application/api/controller/UserController.php

610 lines
22 KiB
PHP
Executable File

<?php
namespace app\api\controller;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\MallImage;
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 app\common\model\Notice;
use app\common\model\GoodsCollect;
use think\Request;
use tools\Crypt;
class UserController extends Controller
{
protected $authExcept = [
'center_config'
];
//个人中心
public function center(Request $request,GoodsCollect $gcmodel){
$param = $request->param();
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$user_id){
return unauthorized('请先授权');
}
$user = User::where('id',$user_id)->field('avatar,nickname,user_money')->find();
//消息数量
$user->notice_num = Notice::unRead($user_id) ? 1 : 1;
$goods_list=$gcmodel
->alias('gc')
->field("mi.thumb_image as img, g.name,g.price,g.id as goods_id,gb.name as brand_name")
->join('goods g', 'g.id = gc.goods_id')
->join('mall_image mi', 'mi.union_id= gc.goods_id and is_cover=1 and mi.type=1', 'LEFT')
->join('goods_brand gb', 'gb.id = g.brand_id')
->where(array('user_id'=>$user_id))
->limit(3)
->order('g.sort_number asc')
->select();
foreach ($goods_list as $k1 => $v1) {
$goods_list[$k1]['img'] =$this->img_url.$v1['img'];
$goods_list[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']);
}
$user->collect =$goods_list;
return success($user);
}
//个人中心参数
public function center_config(Request $request){
$param = $request->param();
$user=array();
//联系我们
$user['contactus'] =ShopConfig::get('policy', 'contactus');
//社群与交流
$user['community'] =ShopConfig::get('policy', 'community');
//隐私政策
$user['privacy'] =ShopConfig::get('policy', 'privacy');
//条款与条件
$user['clause'] =ShopConfig::get('policy', 'clause');
//分享图片
$user['share_img'] =$this->img_url.ShopConfig::get('website', 'share_img');
//小程序太阳码
$user['wxqrcode'] =$this->img_url.ShopConfig::get('website', 'wxqrcode');
return success($user);
}
//我的消息
public function notice(Request $request,Notice $nmodel)
{
$param = $request->param();
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$user_id){
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','=',$user_id);
$where[]=array('send_type','=',1);
$where[]=array('receive_type','in','2,3');
$notice_count = $nmodel
->where($where)
->count();
$totalPages = ceil($notice_count / $size);
$notice_list = $nmodel
->field("title,content")
->where($where)
->limit($page, $size)
->order('create_time DESC')
->select();
if (!$notice_list) {
return success(array('list'=>[]));
}
$where1[]=array('user_id','=',$user_id);
Notice::setRead($where1);
$page=array(
'count'=>$notice_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$notice_list,'page'=>$page));
}
//我的订单
public function order(Request $request,Order $model)
{
$param = $request->param();
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$user_id){
return unauthorized('请先授权');
}
$where[] = ['o.user_id', '=', $user_id];
//订单类型
if ($param['order_status'] != '') {
$where[] = ['o.order_status', 'in', $param['order_status']];
}
$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')
->join('order_goods g', 'g.order_id = o.id')
->with(['order_goods'])
->where($where)
->order('o.id desc')
->group('o.id')
->count();
$totalPages = ceil($order_count / $size);
$order_list = $model
->alias('o')
->field('o.order_sn,o.order_status,o.id')
->join('order_goods g', 'g.order_id = o.id')
->with(['order_goods'])
->where($where)
->order('o.id desc')
->group('o.id')
->limit($page, $size)
->select();
if (!$order_list) {
return success(array('list'=>[]));
}
foreach ($order_list as $k1 => $v1) {
$order_list[$k1]['order_id'] = Crypt::encrypt($v1['id']);
$order_list[$k1]['order_status_text'] = $v1['order_status_text'];
unset($v1['id']);
foreach ($v1['order_goods'] as &$order_goods){
$order_goods['goods_img']=$this->img_url.$order_goods['goods_img'];
$order_goods['goods_id'] = Crypt::encrypt($order_goods['goods_id']);
unset($order_goods['id'],$order_goods['order_id']);
}
}
$page=array(
'count'=>$order_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$order_list,'page'=>$page));
}
//我的订单详情
public function orderdetail(Request $request,Order $model)
{
$param = $request->param();
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$user_id){
return unauthorized('请先授权');
}
$order_id = $param['order_id']?Crypt::decrypt($param['order_id']):'';
if(!$order_id){
return client_error('该订单不存在');
}
$where[] = ['o.user_id', '=', $user_id];
$where[] = ['o.id', '=', $order_id];
$order_info = $model
->alias('o')
->field('o.order_sn,o.order_status,o.id,o.pay_time,total_amount,shipping_time,invoice_name,invoice_no,transaction_id,user_remark,confirm_take_time,cancel_time,o.create_time,province,city,district,address,mobile,consignee')
->join('order_goods g', 'g.order_id = o.id')
->with(['order_goods'])
->append(['delivery_address'])
->where($where)
->find();
if (!$order_info) {
return client_error('该订单不存在');
}
$order_info['order_id'] = Crypt::encrypt($order_info['id']);
$order_info['order_status_text'] = $order_info['order_status_text'];
foreach ($order_info['order_goods'] as &$order_goods){
$order_goods['goods_img']=$this->img_url.$order_goods['goods_img'];
$order_goods['goods_id'] = Crypt::encrypt($order_goods['goods_id']);
unset($order_goods['id'],$order_goods['order_id']);
}
unset($order_info['id']);
return success($order_info);
}
/**
* Notes: 取消订单
* @param $order_id
* @param $user_id
*/
public static function ordercancel(Request $request,Order $model)
{
$param = $request->param();
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$user_id){
return unauthorized('请先授权');
}
$order_id = $param['order_id']?Crypt::decrypt($param['order_id']):'';
if(!$order_id){
return client_error('该订单不存在');
}
$order = Order::get([
'user_id' => $user_id,
'id' => $order_id
]);
if (!$order || $order['order_status']!='0') {
return client_error('很抱歉!订单无法取消');
}
$model->startTrans();
try {
//取消订单
$update = [
'cancel_time' => time(),
'order_status' => 4,
];
$model->where(['id' => $order_id])->update($update);
$model->commit();
} catch (Exception $e) {
$model->rollback();
return client_error($e->getMessage());
}
return success('','取消成功');
}
/**
* Notes: 确认收货
* @param $order_id
* @param $user_id
*/
public static function orderconfirm(Request $request,Order $model)
{
$param = $request->param();
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
if(!$user_id){
return unauthorized('请先授权');
}
$order_id = $param['order_id']?Crypt::decrypt($param['order_id']):'';
if(!$order_id){
return client_error('该订单不存在');
}
$order = Order::get([
'user_id' => $user_id,
'id' => $order_id
]);
if (!$order || $order['order_status']!='2') {
return client_error('很抱歉!订单未发货');
}
$model->startTrans();
try {
//确认订单
$update = [
'confirm_take_time' => time(),
'order_status' => 3,
];
$model->where(['id' => $order_id])->update($update);
$model->commit();
} catch (Exception $e) {
$model->rollback();
return client_error($e->getMessage());
}
return success('','确认成功');
}
//收益统计
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 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('请先授权');
}
//提现积分
$money = $param['money']??'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('user_money')->where(array('id'=>$uid))->find();
if($umodel_info['user_money']<$money){
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($money<$min_withdraw){
return client_error('最少提现金额'.$min_withdraw.',无法提现');
}
if($money>$max_withdraw){
return client_error('最多提现金额'.$max_withdraw.',无法提现');
}
if($poundage_type==2){
$poundage=$money*($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' => $money,
'left_money' => $money - $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->user_money = ['dec', $money];
$user->save();
//增加佣金变动记录
$almodel->AccountRecord(
$uid,
$money,
2,
120,
'',
$withdraw_id,
$data['sn']
);
$money_sum=$umodel_info['user_money']-$money;
$money_sum=sprintf("%.2f",$money_sum);
// 提交事务
$waymodel->commit();
}catch(\Exception $e){
// 回滚事务
$waymodel->rollback();
return client_error($e->getMessage());
}
return success($money_sum,'提交提现申请成功,等待审核打款');
}
}