UserDeleteLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\OrderEnum;
  22. use app\common\enum\WithdrawEnum;
  23. use app\common\logic\BaseLogic;
  24. use app\common\model\AfterSale;
  25. use app\common\model\ChatRelation;
  26. use app\common\model\Distribution;
  27. use app\common\model\FootprintRecord;
  28. use app\common\model\GoodsComment;
  29. use app\common\model\LuckyDrawRecord;
  30. use app\common\model\Order;
  31. use app\common\model\SelffetchVerifier;
  32. use app\common\model\User;
  33. use app\common\model\UserAuth;
  34. use app\common\model\UserSession;
  35. use app\common\model\WithdrawApply;
  36. use app\common\service\ConfigService;
  37. use think\facade\Db;
  38. /**
  39. * @notes 用户注销
  40. * author lbzy
  41. * @datetime 2023-08-21 16:24:13
  42. * @class UserDeleteLogic
  43. * @package app\shopapi\logic
  44. */
  45. class UserDeleteLogic extends BaseLogic
  46. {
  47. /**
  48. * @notes 检测是否可注销
  49. * @param $user_id
  50. * @return int[]
  51. * @author lbzy
  52. * @datetime 2023-08-21 16:37:38
  53. */
  54. static function checkCanDelete($user_id) : array
  55. {
  56. $order_status = [
  57. OrderEnum::STATUS_WAIT_PAY,
  58. OrderEnum::STATUS_WAIT_DELIVERY,
  59. OrderEnum::STATUS_WAIT_RECEIVE,
  60. ];
  61. $after_status = [
  62. AfterSaleEnum::STATUS_ING,
  63. ];
  64. $withdraw_status = [
  65. WithdrawEnum::STATUS_ING,
  66. WithdrawEnum::STATUS_WAIT,
  67. ];
  68. $status = User::where('id', $user_id)->value('disable') == 0 ? 1 : 0;
  69. $order = Order::where('user_id', $user_id)->where('order_status', 'IN', $order_status)->value('id') ? 0 : 1;
  70. $after_sale = AfterSale::where('user_id', $user_id)->where('status', 'IN', $after_status)->value('id') ? 0 : 1;
  71. $withdraw = WithdrawApply::where('user_id', $user_id)->where('status', 'in', $withdraw_status)->value('id') ? 0 : 1;
  72. $result = [
  73. 'data' => [
  74. // 是否冻结
  75. 'status' => [
  76. 'pass' => $status,
  77. 'msg' => $status ? '通过' : '账号冻结中,无法申请注销',
  78. ],
  79. // 是否有未完成订单
  80. 'order' => [
  81. 'pass' => $order,
  82. 'msg' => $order ? '通过' : '存在未完成订单,无法申请注销',
  83. ],
  84. // 是否有售后处理中
  85. 'after_sale' => [
  86. 'pass' => $after_sale,
  87. 'msg' => $after_sale ? '通过' : '存在售后订单,无法申请注销',
  88. ],
  89. // 提现申请
  90. 'withdraw' => [
  91. 'pass' => $withdraw,
  92. 'msg' => $withdraw ? '通过' : '存在佣金待提现申请,无法申请注销',
  93. ],
  94. ],
  95. 'pass' => 1,
  96. 'msg' => '通过',
  97. ];
  98. foreach ($result['data'] as $info) {
  99. if ($info['pass'] == 0) {
  100. $result['pass'] = 0;
  101. $result['msg'] = $info['msg'];
  102. break;
  103. }
  104. }
  105. return $result;
  106. }
  107. /**
  108. * @notes 确定注销
  109. * @param $user_id
  110. * @return bool|string
  111. * @author lbzy
  112. * @datetime 2023-08-21 16:47:13
  113. */
  114. static function sureDelete($user_id) : bool|string
  115. {
  116. $check = static::checkCanDelete($user_id);
  117. if ($check['pass'] == 0) {
  118. return $check['msg'];
  119. }
  120. try {
  121. Db::startTrans();
  122. // 用户数据
  123. User::update([
  124. 'user_delete' => 1,
  125. 'disable' => 1,
  126. 'account' => '',
  127. 'password' => '',
  128. 'pay_password' => '',
  129. 'mobile' => '',
  130. 'first_leader' => 0,
  131. 'second_leader' => 0,
  132. 'third_leader' => 0,
  133. 'ancestor_relation' => '',
  134. 'inviter_id' => 0,
  135. 'code' => '',
  136. ], [ [ 'id', '=', $user_id ] ]);
  137. // 用户openid unionid
  138. $userAuths = UserAuth::where('user_id', $user_id)->select()->toArray();
  139. foreach ($userAuths as $userAuth) {
  140. UserAuth::update([
  141. 'openid' => "{$user_id}-{$userAuth['openid']}-" . time(),
  142. 'unionid' => "{$user_id}-{$userAuth['unionid']}-" . time(),
  143. ], [ [ 'id', '=', $userAuth['id'] ] ]);
  144. }
  145. // 用户token
  146. UserSession::destroy(function ($query) use ($user_id) {
  147. $query->where('user_id', $user_id);
  148. });
  149. // 用户客服
  150. ChatRelation::destroy(function ($query) use ($user_id) {
  151. $query->where('user_id', $user_id);
  152. });
  153. // 用户分销关系清除
  154. User::update([ 'first_leader' => 0, 'second_leader' => 0, 'third_leader' => 0 ], [ [ 'first_leader', '=', $user_id ] ]);
  155. User::update([ 'second_leader' => 0, 'third_leader' => 0 ], [ [ 'second_leader', '=', $user_id ] ]);
  156. User::update([ 'third_leader' => 0 ], [ [ 'third_leader', '=', $user_id ] ]);
  157. User::update([ 'inviter_id' => 0 ], [ [ 'inviter_id', '=', $user_id ] ]);
  158. // 分销冻结
  159. Distribution::update([
  160. 'is_distribution' => 0,
  161. 'is_freeze' => 1,
  162. ], [ [ 'user_id', '=', $user_id ] ]);
  163. // 核销员
  164. SelffetchVerifier::destroy(function ($query) use ($user_id) {
  165. $query->where('user_id', $user_id);
  166. });
  167. // 足迹气泡
  168. FootprintRecord::destroy(function ($query) use ($user_id) {
  169. $query->where('user_id', $user_id);
  170. });
  171. // 幸运转盘活动
  172. LuckyDrawRecord::destroy(function ($query) use ($user_id) {
  173. $query->where('user_id', $user_id);
  174. });
  175. // 商品评论
  176. GoodsComment::destroy(function ($query) use ($user_id) {
  177. $query->where('user_id', $user_id);
  178. });
  179. Db::commit();
  180. return true;
  181. } catch(\Throwable $e) {
  182. static::setError($e->getMessage());
  183. Db::rollback();
  184. return $e->getMessage();
  185. }
  186. }
  187. }