Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
liu 2024-03-09 11:50:42 +08:00
commit 5e33bfa9a4
5 changed files with 131 additions and 127 deletions

View File

@ -319,7 +319,7 @@ class Merchant extends BaseModel
public function promoter()
{
return User::where('uid', $this->uid)->field('spread_uid,nickname')->find();
return User::where('uid', $this->uid)->field('spread_uid as uid,nickname')->find();
}
}

View File

@ -802,15 +802,16 @@ class StoreOrderRepository extends BaseRepository
if (!empty($promoterCommission)) {
$userBillRepository->incBill($promoterCommission['user_id'], 'brokerage', 'order_one', [
'link_id' => $order['order_id'],
'status' => 0,
'status' => 1,
'title' => '获得推广佣金',
'number' => $promoterCommission['number'],
'mark' => $order->merchant['mer_name'] . '成功销售' . floatval($order['pay_price']) . '元,奖励推广佣金' . floatval($promoterCommission['number']),
'balance' => 0
]);
$balance = bcadd($user->now_money, $promoterCommission['number'], 2);
$user->save(['now_money' => $balance]);
$promoter = User::where('uid', $promoterCommission['user_id'])->find();
$balance = bcadd($promoter->now_money, $promoterCommission['number'], 2);
$promoter->save(['now_money' => $balance]);
}
}

View File

@ -14,6 +14,18 @@
namespace app\controller\api;
use app\common\model\store\consumption\StoreConsumptionDetail;
use app\common\model\store\consumption\StoreConsumptionUser;
use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreOrderOther;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\order\StoreOrderStatus;
use app\common\model\store\order\StoreRefundOrder;
use app\common\model\system\merchant\FinancialRecord;
use app\common\model\user\UserAddress;
use app\common\model\user\UserBill;
use app\common\model\user\UserRecharge;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\order\StoreRefundOrderRepository;
use app\common\repositories\system\notice\SystemNoticeConfigRepository;
@ -41,6 +53,7 @@ use Symfony\Component\HttpFoundation\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\Query;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
@ -910,7 +923,11 @@ class Auth extends BaseController
public function authLogin()
{
$auth = $this->request->param('auth');
$users = $this->authInfo($auth, systemConfig('is_phone_login') !== '1');
$createUser = true;
if ($auth['type'] == 'app_wechat' || systemConfig('is_phone_login') == '1') {
$createUser = false;
}
$users = $this->authInfo($auth, $createUser);
if (!$users)
return app('json')->fail('授权失败');
$authInfo = $users[0];
@ -1656,4 +1673,110 @@ class Auth extends BaseController
$data = $repository->getResult($type);
return app('json')->success($data);
}
/**
* 合并账号
* @param UserRepository $repository
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function merge(UserRepository $repository)
{
$data = $this->request->params(['phone', 'sms_code', 'spread', 'pwd', 'auth_token', ['user_type', 'h5']]);
$sms_code = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['sms_code'], 'login');
if (!$sms_code && !env('APP_DEBUG')) {
return app('json')->fail('验证码不正确');
}
$auth = Cache::get('u_try' . $data['auth_token']);
$auth && Cache::delete('u_try' . $data['auth_token']);
if (empty($auth)) {
return app('json')->fail('授权已过期,请重新登录');
}
$user = $repository->wechatUserIdBytUser($auth['id']);
if (!empty($user['account'])) {
return app('json')->fail('已绑定手机号');
}
$targetUser = $repository->accountByUser($data['phone']);
if (empty($targetUser)) {
if (!empty($user)) {
$user->account = $data['phone'];
$user->phone = $data['phone'];
$user->save();
$tokenInfo = $repository->createToken($user);
return app('json')->success($repository->returnToken($user, $tokenInfo));
} else {
$targetUser = $repository->registr($data['phone'], $data['pwd'], $data['user_type']);
$repository->bindSpread($targetUser, intval($data['spread']));
}
}
$repository->syncBaseAuth($auth, $targetUser);
if (!empty($user)) {
Db::startTrans();
try {
StoreGroupOrder::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreOrder::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreOrderProduct::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
UserBill::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
FinancialRecord::where('user_id', $user['uid'])->update(['user_id' => $targetUser['uid']]);
UserAddress::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreConsumptionUser::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreConsumptionDetail::where('user_id', $user['uid'])->update(['user_id' => $targetUser['uid']]);
StoreRefundOrder::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
UserRecharge::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreOrderStatus::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
if ($user->now_money > 0) {
$user->now_money = 0;
$targetUser->now_money = bcadd($targetUser->now_money, $user->now_money, 2);
$targetUser->save();
}
$merchant = Merchant::where('uid', $targetUser['uid'])->find();
if (!empty($merchant)) {
$orders = StoreOrder::with(['refundOrder' => function(Query $query) {
$query->where('status', '<>', -1)->field('order_id,refund_price');
}])->where('uid', $user['uid'])
->whereIn('status', [0, 1, 2, 3, 9, 10])
->field('order_id,uid,total_price')
->select()->toArray();
$otherOrders = StoreOrderOther::with(['refundOrder' => function(Query $query) {
$query->where('status', '<>', -1)->field('order_id,refund_price');
}])->where('uid', $user['uid'])
->whereIn('status', [0, 1, 2, 3, 9, 10])
->field('order_id,uid,total_price')
->select()->toArray();
$purchaseAmount = 0;
foreach ($orders as $order) {
$purchaseAmount = bcadd($purchaseAmount, $order['total_price'], 2);
foreach ($order['refundOrder']as $refundOrder) {
$purchaseAmount = bcsub($purchaseAmount, $refundOrder['refund_price'], 2);
}
}
unset($refundOrder, $order);
foreach ($otherOrders as $otherOrder) {
$purchaseAmount = bcadd($purchaseAmount, $otherOrder['total_price'], 2);
foreach ($otherOrder['refundOrder']as $refundOrder) {
$purchaseAmount = bcsub($purchaseAmount, $refundOrder['refund_price'], 2);
}
}
unset($refundOrder, $otherOrder);
if ($purchaseAmount > 0) {
$merchant->purchase_amount = bcadd($merchant->purchase_amount, $purchaseAmount, 2);
$merchant->save();
}
}
if (!empty($user)) {
$user->wechat_user_id = 0;
$user->save();
}
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
throw new ValidateException('绑定出错');
}
}
$tokenInfo = $repository->createToken($targetUser);
return app('json')->success($repository->returnToken($targetUser, $tokenInfo));
}
}

View File

@ -10,21 +10,11 @@
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\user;
use app\common\dao\store\order\StoreOrderDao;
use app\common\model\store\consumption\StoreConsumptionDetail;
use app\common\model\store\consumption\StoreConsumptionUser;
use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreOrderOther;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\order\StoreOrderStatus;
use app\common\model\store\order\StoreRefundOrder;
use app\common\model\system\merchant\Merchant;
use app\common\model\user\UserBill;
use app\common\repositories\store\IntegralRepository;
use app\common\repositories\store\order\PresellOrderRepository;
use app\common\repositories\store\service\StoreServiceRepository;
@ -34,7 +24,6 @@ use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserBrokerageRepository;
use app\common\repositories\user\UserRepository;
use app\common\repositories\user\UserVisitRepository;
use app\common\model\system\merchant\FinancialRecord;
use app\validate\api\UserBaseInfoValidate;
use crmeb\basic\BaseController;
use crmeb\services\MiniProgramService;
@ -45,12 +34,6 @@ use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use app\controller\api\Common;
use app\common\model\user\User as UserModel;
use app\common\model\user\UserAddress;
use app\common\model\user\UserRecharge;
use think\db\Query;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
class User extends BaseController
{
@ -609,107 +592,4 @@ class User extends BaseController
return app('json')->success(['count' => count($userIds), 'list' => $result]);
}
/**
* 合并账号
* @param UserRepository $repository
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function merge(UserRepository $repository)
{
$data = $this->request->params(['phone', 'sms_code', 'spread', 'pwd', 'auth_token', ['user_type', 'h5']]);
$sms_code = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['sms_code'], 'login');
if (!$sms_code && !env('APP_DEBUG')) {
return app('json')->fail('验证码不正确');
}
$auth = Cache::get('u_try' . $data['auth_token']);
$auth && Cache::delete('u_try' . $data['auth_token']);
if (empty($auth)) {
return app('json')->fail('授权已过期,请重新登录');
}
$user = $repository->wechatUserIdBytUser($auth['id']);
if (!empty($user['account'])) {
return app('json')->fail('已绑定手机号');
}
$targetUser = $repository->accountByUser($data['phone']);
if (empty($targetUser) && !empty($user)) {
if (!empty($user)) {
$user->account = $data['phone'];
$user->phone = $data['phone'];
$user->save();
$tokenInfo = $repository->createToken($user);
return app('json')->success($repository->returnToken($user, $tokenInfo));
} else {
$targetUser = $repository->registr($data['phone'], $data['pwd'], $data['user_type']);
$repository->bindSpread($targetUser, intval($data['spread']));
}
}
$repository->syncBaseAuth($auth, $targetUser);
Db::startTrans();
try {
StoreGroupOrder::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreOrder::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreOrderProduct::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
UserBill::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
FinancialRecord::where('user_id', $user['uid'])->update(['user_id' => $targetUser['uid']]);
UserAddress::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreConsumptionUser::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreConsumptionDetail::where('user_id', $user['uid'])->update(['user_id' => $targetUser['uid']]);
StoreRefundOrder::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
UserRecharge::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
StoreOrderStatus::where('uid', $user['uid'])->update(['uid' => $targetUser['uid']]);
if ($user->now_money > 0) {
$user->now_money = 0;
$targetUser->now_money = bcadd($targetUser->now_money, $user->now_money, 2);
$targetUser->save();
}
$merchant = Merchant::where('uid', $targetUser['uid'])->find();
if (!empty($merchant)) {
$orders = StoreOrder::with(['refundOrder' => function(Query $query) {
$query->where('status', '<>', -1)->field('order_id,refund_price');
}])->where('uid', $user['uid'])
->whereIn('status', [0, 1, 2, 3, 9, 10])
->field('order_id,uid,total_price')
->select()->toArray();
$otherOrders = StoreOrderOther::with(['refundOrder' => function(Query $query) {
$query->where('status', '<>', -1)->field('order_id,refund_price');
}])->where('uid', $user['uid'])
->whereIn('status', [0, 1, 2, 3, 9, 10])
->field('order_id,uid,total_price')
->select()->toArray();
$purchaseAmount = 0;
foreach ($orders as $order) {
$purchaseAmount = bcadd($purchaseAmount, $order['total_price'], 2);
foreach ($order['refundOrder']as $refundOrder) {
$purchaseAmount = bcsub($purchaseAmount, $refundOrder['refund_price'], 2);
}
}
unset($refundOrder, $order);
foreach ($otherOrders as $otherOrder) {
$purchaseAmount = bcadd($purchaseAmount, $otherOrder['total_price'], 2);
foreach ($otherOrder['refundOrder']as $refundOrder) {
$purchaseAmount = bcsub($purchaseAmount, $refundOrder['refund_price'], 2);
}
}
unset($refundOrder, $otherOrder);
if ($purchaseAmount > 0) {
$merchant->purchase_amount = bcadd($merchant->purchase_amount, $purchaseAmount, 2);
$merchant->save();
}
}
if (!empty($user)) {
$user->wechat_user_id = 0;
$user->save();
}
Db::commit();
$tokenInfo = $repository->createToken($targetUser);
return app('json')->success($repository->returnToken($targetUser, $tokenInfo));
} catch (\Throwable $e) {
Db::rollback();
throw new ValidateException('绑定出错');
}
}
}

View File

@ -173,8 +173,6 @@ Route::group('api/', function () {
//切换账号
Route::get('account', 'User/account');
Route::post('switch', 'User/switchUser');
//合并账号
Route::post('merge', 'User/merge');
//修改信息
Route::post('change/phone', 'User/changePhone');
Route::post('change/info', 'User/updateBaseInfo');
@ -741,6 +739,8 @@ Route::group('api/', function () {
Route::post('auth/apple', 'api.Auth/appleAuth');
//修改密码
Route::post('user/change_pwd', 'api.Auth/changePassword');
//合并账号
Route::post('user/merge', 'api.auth/merge');
//验证码
Route::post('auth/verify', 'api.Auth/verify');
Route::post('auth/verifyCode', 'api.Auth/verifyCode');