new-admin-api/app/controller/api/user/Exchange.php

217 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 crmeb\basic\BaseController;
use crmeb\services\QrcodeService;
use crmeb\services\wechat\MiniProgram;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;
class Exchange extends BaseController{
protected $user;
public function __construct(App $app){
parent::__construct($app);
$this->user = $this->request->userInfo();
}
/**
* Common: 获取用户持有信息
* Author: wu-hui
* Time: 2024/01/14 16:53
* @return mixed
*/
public function getUserHold(){
$uid = $this->request->uid();
// 获取额度
$info = app()->make(ExchangeQuotaRepository::class)
->getSearch([])
->field([
'uid',
// 可用额度=剩余额度-冻结额度
'(surplus_quota - freeze_quota) as available'
])
->where('uid',$uid)
->findOrEmpty();
$info->available_integral = $this->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']);
$search['is_show'] = 1;
$data = app()->make(ExchangePickupPointRepository::class)->getList($search, 1, 30);
$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']);
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('请选择操作员!');
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/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('小程序码生成失败!');
}
}