diff --git a/app/common/model/store/order/StoreOrderOther.php b/app/common/model/store/order/StoreOrderOther.php index 084c1cfe..b113e5a0 100755 --- a/app/common/model/store/order/StoreOrderOther.php +++ b/app/common/model/store/order/StoreOrderOther.php @@ -52,7 +52,7 @@ class StoreOrderOther extends BaseModel public function refundOrder() { - return $this->hasMany(StoreRefundOrder::class,'order_id','order_id'); + return $this->hasMany(StoreRefundOrderOther::class,'order_id','order_id'); } public function orderStatus() diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index 06267ba7..a4070cba 100755 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -122,7 +122,7 @@ class Auth extends BaseController $destination = public_path('uploads').'img/' . $filename; // 目标路径 $pict_url= $url.'/uploads/img/'.$filename; file_put_contents($destination, file_get_contents($res->pict_url)); - + if($resp && isset($resp->small_images) && isset($resp->small_images->string)){ foreach($resp->small_images->string as $k=>$v){ $filename = basename($v); // 获取文件名 @@ -197,13 +197,13 @@ class Auth extends BaseController function convertUrlQuery($query) { $queryParts = explode('&', $query); - + $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } - + return $params; } public function dotest() @@ -914,6 +914,7 @@ class Auth extends BaseController if (!$users) return app('json')->fail('授权失败'); $authInfo = $users[0]; + /** @var UserRepository $userRepository */ $userRepository = app()->make(UserRepository::class); $user = $users[1] ?? $userRepository->wechatUserIdBytUser($authInfo['wechat_user_id']); $code = (int)($auth['auth']['spread_code']['id'] ?? $auth['auth']['spread_code'] ?? ''); @@ -931,6 +932,9 @@ class Auth extends BaseController if ($auth['auth']['spread'] ?? 0) { $userRepository->bindSpread($user, (int)($auth['auth']['spread'])); } + if (!empty($user['account'])) { + $user = $userRepository->accountByUser($user['account']); + } $tokenInfo = $userRepository->createToken($user); $userRepository->loginAfter($user); return app('json')->status(200, $userRepository->returnToken($user, $tokenInfo)); diff --git a/app/controller/api/user/User.php b/app/controller/api/user/User.php index 903daa1c..9e6d8c28 100755 --- a/app/controller/api/user/User.php +++ b/app/controller/api/user/User.php @@ -15,8 +15,16 @@ 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; @@ -26,6 +34,7 @@ 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; @@ -36,6 +45,10 @@ 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\facade\Db; class User extends BaseController { @@ -594,4 +607,97 @@ 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) + { + $user = $this->request->userInfo(); + $data = $this->request->params(['phone', 'sms_code'] ); + if ($user['account'] == $data['phone']) { + return app('json')->fail('绑定的账号不能相同'); + } + if (!empty($user['account'])) { + return app('json')->fail('已绑定手机号'); + } + $sms_code = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['sms_code'], 'login'); + if (!$sms_code && !env('APP_DEBUG')) { + return app('json')->fail('验证码不正确'); + } + $targetUser = $repository->accountByUser($data['phone']); + if (empty($targetUser)) { + $user->account = $data['phone']; + $user->phone = $data['phone']; + $user->save(); + $tokenInfo = $repository->createToken($user); + return app('json')->success($repository->returnToken($user, $tokenInfo)); + } else { + 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) { + $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(); + } + $user->account = $data['phone']; + $user->phone = $data['phone']; + $user->save(); + } + Db::commit(); + } catch (\Throwable $e) { + Db::rollback(); + } + $tokenInfo = $repository->createToken($targetUser); + return app('json')->success($repository->returnToken($targetUser, $tokenInfo)); + } + } + } diff --git a/route/api.php b/route/api.php index 5eb990c1..b6648122 100755 --- a/route/api.php +++ b/route/api.php @@ -173,6 +173,8 @@ 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');