和蕙健康小程序后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.8 KiB

9 months ago
<?php
namespace addons\shopro\controller\user;
use addons\shopro\controller\Common;
8 months ago
use addons\shopro\service\Wallet as WalletService;
8 months ago
use app\admin\model\shopro\user\User as UserModel;
9 months ago
use app\admin\model\shopro\user\WalletLog as UserWalletLogModel;
8 months ago
use app\common\library\Sms;
8 months ago
use think\Db;
9 months ago
class WalletLog extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$type = $this->request->param('type', 'money');
$tab = $this->request->param('tab', 'all');
$list_rows = $this->request->param('list_rows', 10);
$date = $this->request->param('date/a');
$user = auth_user();
$where['user_id'] = $user->id;
switch ($tab) {
case 'income':
$where['amount'] = ['>', 0];
break;
case 'expense':
$where['amount'] = ['<', 0];
break;
}
$income = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '>', 0)->whereTime('createtime', 'between', $date)->sum('amount');
$expense = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '<', 0)->whereTime('createtime', 'between', $date)->sum('amount');
$logs = UserWalletLogModel::where($where)->{$type}()->whereTime('createtime', 'between', $date)->order('createtime', 'desc')->paginate($list_rows);
// dd($logs->toArray());
$data = $logs->toArray();
foreach ($data['data'] as &$row) {
$row['oper_type'] = UserModel::get($row['oper_id'])['nickname'] ?? '';
}
$this->success('获取成功', ['list' => $data, 'income' => $income, 'expense' => $expense]);
9 months ago
}
8 months ago
/**
* 用户充值积分
*/
public function recharge()
{
$params = $this->request->only(['type', 'amount']);
if (!in_array($params['type'], ['money', 'score'])) {
$this->error('参数错误');
}
$user = auth_user();
$user_id = $user->id;
$result = Db::transaction(function () use ($params, $user_id) {
8 months ago
return WalletService::change($user_id, $params['type'], $params['amount'], 'user_recharge', [], '积分充值');
8 months ago
});
if ($result) {
$this->success('充值成功');
}
$this->error('充值失败');
}
/**
* 用户转让积分
*/
public function exchange() {
$params = $this->request->only(['type', 'exchange_amount', 'exchange_phone', 'code']);
if (!in_array($params['type'], ['money', 'score'])) {
$this->error('参数错误');
}
$user = auth_user();
$user_id = $user->id;
if ($user->mobile == $params['exchange_phone']) {
$this->error('转让人不能是自己');
8 months ago
}
8 months ago
$exchange_user = UserModel::getByMobile($params['exchange_phone']);
if ($exchange_user) {
if ($exchange_user->status != 'normal') {
$this->error(__('转让账户已经被锁定'));
8 months ago
}
}else {
$this->error('未查到转让用户');
8 months ago
}
if (!Sms::check($user->mobile, $params['code'], 'score')) {
$this->error('验证码不正确');
}
8 months ago
$exchange_user_id = $exchange_user->id;
$result = Db::transaction(function () use ($params, $exchange_user_id, $user_id) {
WalletService::change($user_id, $params['type'], -$params['exchange_amount'], 'user_exchange', [], '积分转让');
return WalletService::change($exchange_user_id, $params['type'], $params['exchange_amount'], 'user_exchange', [], '积分转让');
8 months ago
});
if ($result) {
$this->success('转让成功');
}
$this->error('转让失败');
}
9 months ago
}