278 lines
11 KiB
PHP
278 lines
11 KiB
PHP
<?php
|
||
|
||
namespace app\controller\api\user;
|
||
|
||
|
||
use app\common\model\user\ExchangeIntegralRecord;
|
||
use app\common\model\user\ExchangePickupRecord;
|
||
use app\common\model\user\ExchangeQuota;
|
||
use app\common\model\user\ExchangeQuotaRecord;
|
||
use app\common\model\user\User;
|
||
use app\common\repositories\user\ExchangePickupPointRepository;
|
||
use app\common\repositories\user\ExchangePickupRecordRepository;
|
||
use app\common\repositories\user\ExchangeQuotaRepository;
|
||
use app\common\repositories\user\UserRepository;
|
||
use crmeb\basic\BaseController;
|
||
use crmeb\services\QrcodeService;
|
||
use think\App;
|
||
use think\facade\Db;
|
||
|
||
class Exchange extends BaseController{
|
||
protected $user;
|
||
|
||
public function __construct(App $app){
|
||
parent::__construct($app);
|
||
}
|
||
/**
|
||
* Common: 获取用户持有信息
|
||
* Author: wu-hui
|
||
* Time: 2024/01/14 16:53
|
||
* @return mixed
|
||
*/
|
||
public function getUserHold(){
|
||
$params = $this->request->params(['consume_uid']);
|
||
$uid = $this->request->uid();
|
||
// 判断:是否存在指定消费者,不存在则使用当前登录用户
|
||
$uid = (int)$params['consume_uid'] > 0 ? (int)$params['consume_uid'] : $uid;
|
||
// 获取额度
|
||
$info = app()->make(ExchangeQuotaRepository::class)
|
||
->getSearch([])
|
||
->field([
|
||
'uid',
|
||
// 可用额度=剩余额度-冻结额度
|
||
'(surplus_quota - freeze_quota) as available'
|
||
])
|
||
->where('uid',$uid)
|
||
->findOrEmpty();
|
||
$user = app()->make(UserRepository::class)->getSearch([])->where('uid',$uid)->findOrEmpty();
|
||
$info->available = (float)$info->available;
|
||
$info->available_integral = (float)$user->exchange_integral;
|
||
$info->diff_rate = 30;// 差价 应补金额,默认为30%
|
||
|
||
return app('json')->success($info);
|
||
}
|
||
/**
|
||
* Common: 获取提货点列表
|
||
* Author: wu-hui
|
||
* Time: 2024/01/14 16:06
|
||
* @return mixed
|
||
*/
|
||
public function getPointList(){
|
||
$search = $this->request->params(['uid', 'address', 'default_point_id','lng','lat']);
|
||
$search['is_show'] = 1;
|
||
$make = app()->make(ExchangePickupPointRepository::class);
|
||
[$page, $limit] = $this->getPage();
|
||
$data = $make->getList($search, $page, $limit);
|
||
$list = $data['list'];
|
||
|
||
return app('json')->success($list);
|
||
}
|
||
/**
|
||
* Common: 兑换处理
|
||
* Author: wu-hui
|
||
* Time: 2024/01/14 18:02
|
||
* @return mixed
|
||
*/
|
||
public function exchangeHandle(){
|
||
// 参数获取
|
||
$uid = $this->request->uid();
|
||
$data = $this->request->params(['total_money','use_integral','diff_money','diff_money_pay','point_id','staff_uid','consume_uid']);
|
||
if ((float)$data['total_money'] <= 0) return app('json')->fail('价值必须大于0!');
|
||
if ((float)$data['point_id'] <= 0) return app('json')->fail('请选择提货点!');
|
||
if ((float)$data['staff_uid'] <= 0) return app('json')->fail('请选择操作员!');
|
||
// 判断:是否存在指定消费者,不存在则使用当前登录用户
|
||
$uid = (int)$data['consume_uid'] > 0 ? (int)$data['consume_uid'] : $uid;
|
||
if ($data['staff_uid'] == $uid) return app('json')->fail('操作员和消费用户不能是同一人!');
|
||
// 添加兑换记录
|
||
try{
|
||
Db::transaction(function () use ($data, $uid) {
|
||
// 添加兑换记录
|
||
ExchangePickupRecord::insert([
|
||
'uid' => $uid,
|
||
'point_id' => $data['point_id'],
|
||
'staff_uid' => $data['staff_uid'],
|
||
'total_money' => $data['total_money'],
|
||
'use_integral' => $data['use_integral'],
|
||
'diff_money' => $data['diff_money'],
|
||
'diff_money_pay' => $data['diff_money_pay'],
|
||
]);
|
||
// 积分&额度变更数量
|
||
$changeNum = (float)sprintf("%.2f",$data['total_money'] - $data['diff_money_pay']);
|
||
// 变更额度
|
||
$userHoldInfo = ExchangeQuota::where('uid',$uid)->findOrEmpty();
|
||
$changeFront = (float)$userHoldInfo->surplus_quota;
|
||
$userHoldInfo->use_quota += (float)$changeNum;
|
||
$userHoldInfo->surplus_quota -= (float)$changeNum;
|
||
$userHoldInfo->save();
|
||
ExchangeQuotaRecord::insert([
|
||
'uid' => $uid,
|
||
'product_id' => 0,
|
||
'order_id' => 0,
|
||
'order_product_id' => 0,
|
||
'change_type' => 0,
|
||
'change_quantity' => (float)$changeNum,
|
||
'change_front' => $changeFront,
|
||
'change_after' => (float)$userHoldInfo->surplus_quota,
|
||
'remark' => "兑换消费",
|
||
'source' => 2,
|
||
]);
|
||
// 变更积分
|
||
$userInfo = User::where('uid',$uid)->findOrEmpty();
|
||
$integralChangeFront = (float)$userInfo->exchange_integral;
|
||
$userInfo->exchange_integral -= (float)$changeNum;
|
||
$userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0;
|
||
$userInfo->save();
|
||
ExchangeIntegralRecord::insert([
|
||
'uid' => $uid,
|
||
'product_id' => 0,
|
||
'order_id' => 0,
|
||
'order_product_id' => 0,
|
||
'change_type' => 0,
|
||
'change_quantity' => (float)$changeNum,
|
||
'change_front' => $integralChangeFront,
|
||
'change_after' => (float)$userInfo->exchange_integral,
|
||
'remark' => "兑换消费",
|
||
]);
|
||
});
|
||
|
||
return app('json')->success('success');
|
||
}catch(\Exception $e){
|
||
|
||
return app('json')->fail($e->getMessage());
|
||
}
|
||
}
|
||
/**
|
||
* Common: 取货记录
|
||
* Author: wu-hui
|
||
* Time: 2024/01/14 18:44
|
||
* @return mixed
|
||
*/
|
||
public function exchangeRecord(){
|
||
$params = $this->request->params(['staff_uid','address']);
|
||
$params['uid'] = $this->request->uid();
|
||
[$page, $limit] = $this->getPage();
|
||
|
||
$data = app()->make(ExchangePickupRecordRepository::class)->getList((array)$params,(int)$page,(int)$limit);
|
||
|
||
return app('json')->success($data);
|
||
}
|
||
/**
|
||
* Common: 获取消费者列表
|
||
* Author: wu-hui
|
||
* Time: 2024/01/16 11:16
|
||
* @return mixed
|
||
*/
|
||
public function getConsumeList(){
|
||
$search = $this->request->params(['search_text', 'default_consume_id']);
|
||
$list = app()->make(UserRepository::class)
|
||
->getSearch([])
|
||
->field(['uid','real_name','nickname','avatar','phone'])
|
||
->where(function($query) use ($search){
|
||
// 用户ID/用户昵称/真实姓名/联系电话
|
||
$query->where('uid',$search['search_text'])
|
||
->whereOr('nickname','like',"%{$search['search_text']}%")
|
||
->whereOr('real_name','like',"%{$search['search_text']}%")
|
||
->whereOr('phone','like',"%{$search['search_text']}%");
|
||
})
|
||
->when(isset($search['default_consume_id']) && $search['default_consume_id'] > 0,function($query) use ($search){
|
||
$query->where('uid',$search['default_consume_id']);
|
||
})
|
||
->page(1, 30)
|
||
->select()
|
||
->toArray();
|
||
|
||
return app('json')->success($list);
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 获取当前用户身为管理员时 参与管理的站点
|
||
* Author: wu-hui
|
||
* Time: 2024/01/15 11:22
|
||
* @return mixed
|
||
*/
|
||
public function siteList(){
|
||
$search = $this->request->params(['uid','address']);
|
||
$search['uid'] = $this->request->uid();
|
||
[$page, $limit] = $this->getPage();
|
||
$data = app()->make(ExchangePickupPointRepository::class)->getList($search, $page, $limit);
|
||
|
||
return app('json')->success($data);
|
||
}
|
||
/**
|
||
* Common: 获取当前用户身为管理员时 操作的兑换记录
|
||
* Author: wu-hui
|
||
* Time: 2024/01/15 11:20
|
||
* @return mixed
|
||
*/
|
||
public function siteExchangeRecord(){
|
||
$params = $this->request->params(['staff_uid','address','uid']);
|
||
$params['staff_uid'] = $this->request->uid();
|
||
[$page, $limit] = $this->getPage();
|
||
|
||
$data = app()->make(ExchangePickupRecordRepository::class)->getList((array)$params,(int)$page,(int)$limit);
|
||
|
||
return app('json')->success($data);
|
||
}
|
||
/**
|
||
* Common: 获取兑换二维码显示(操作员二维码)
|
||
* Author: wu-hui
|
||
* Time: 2024/01/15 14:37
|
||
* @return mixed
|
||
*/
|
||
public function siteQrCode(){
|
||
// 参数获取
|
||
$uid = $this->request->uid();
|
||
$params = $this->request->params(['point_id']);
|
||
if((int)$params['point_id'] > 0){
|
||
try{
|
||
$valueData = 'staff_uid=' . $uid . '&point_id=' . $params['point_id'];
|
||
$name = md5($valueData) . '.jpg';
|
||
// pages/users/online_payment/exchange/index
|
||
$qrcode = app()->make(QrcodeService::class)->getRoutineQrcodePath($name, 'pages/users/online_payment/exchange/index', $valueData);
|
||
if (!$qrcode) throw new \Exception('二维码生成失败');
|
||
|
||
return app('json')->success([
|
||
'qr_code' => $qrcode
|
||
]);
|
||
}catch(\Exception $e){
|
||
return app('json')->fail($e->getMessage());
|
||
}catch(\Throwable $e){
|
||
return app('json')->fail($e->getMessage());
|
||
}
|
||
}
|
||
|
||
return app('json')->fail('小程序码生成失败!');
|
||
}
|
||
/**
|
||
* Common: 获取兑换二维码(用户二维码)
|
||
* Author: wu-hui
|
||
* Time: 2024/01/16 10:17
|
||
* @return mixed
|
||
*/
|
||
public function userQrCode(){
|
||
// 参数获取
|
||
$uid = $this->request->uid();
|
||
if((int)$uid > 0){
|
||
try{
|
||
$valueData = 'consume_uid=' . $uid;
|
||
$name = md5('consume_qrcode_'.$valueData) . '.jpg';
|
||
$qrcode = app()->make(QrcodeService::class)->getRoutineQrcodePath($name, 'pages/users/online_payment/exchange/index', $valueData);
|
||
if (!$qrcode) throw new \Exception('二维码生成失败');
|
||
|
||
return app('json')->success([
|
||
'qr_code' => $qrcode
|
||
]);
|
||
}catch(\Exception $e){
|
||
return app('json')->fail($e->getMessage());
|
||
}catch(\Throwable $e){
|
||
return app('json')->fail($e->getMessage());
|
||
}
|
||
}
|
||
|
||
return app('json')->fail('小程序码生成失败!');
|
||
}
|
||
|
||
|
||
}
|