UserServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. //declare (strict_types=1);
  12. namespace app\services\out;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserDao;
  15. use app\services\user\UserBillServices;
  16. use app\services\user\UserMoneyServices;
  17. use crmeb\exceptions\AdminException;
  18. /**
  19. *
  20. * Class UserServices
  21. * @package app\services\user
  22. * @mixin UserDao
  23. */
  24. class UserServices extends BaseServices
  25. {
  26. /**
  27. * UserServices constructor.
  28. * @param UserDao $dao
  29. */
  30. public function __construct(UserDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取用户信息
  36. * @param $id
  37. * @param $field
  38. */
  39. public function getUserInfo(int $uid, $field = '*')
  40. {
  41. if (is_string($field)) $field = explode(',', $field);
  42. $info = $this->dao->getOutOne($uid, $field);
  43. $info = $info ? $info->toArray() : [];
  44. return $info;
  45. }
  46. /**
  47. * 修改提交处理
  48. * @param $id
  49. * @return mixed
  50. */
  51. public function updateInfo(int $id, array $data)
  52. {
  53. $user = $this->getUserInfo($id);
  54. if (!$user) {
  55. throw new AdminException('数据不存在!');
  56. }
  57. $res1 = false;
  58. $res2 = false;
  59. $edit = array();
  60. if (isset($data['is_other']) && $data['is_other']) {
  61. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  62. /** @var UserMoneyServices $userMoneyServices */
  63. $userMoneyServices = app()->make(UserMoneyServices::class);
  64. if ($data['money_status'] == 1) {//增加
  65. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  66. $res1 = $userMoneyServices->income('system_add', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  67. } else if ($data['money_status'] == 2) {//减少
  68. if ($user['now_money'] > $data['money']) {
  69. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  70. } else {
  71. $edit['now_money'] = 0;
  72. $data['money'] = $user['now_money'];
  73. }
  74. $res1 = $userMoneyServices->income('system_sub', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  75. }
  76. } else {
  77. $res1 = true;
  78. }
  79. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  80. $integral_data = ['link_id' => $data['adminId'] ?? 0, 'number' => $data['integration'], 'balance' => $user['integral']];
  81. /** @var UserBillServices $userBill */
  82. $userBill = app()->make(UserBillServices::class);
  83. if ($data['integration_status'] == 1) {//增加
  84. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  85. $integral_data['title'] = '系统增加积分';
  86. $integral_data['mark'] = '系统增加了' . floatval($data['integration']) . '积分';
  87. $type = 'system_add';
  88. $pm = 1;
  89. } else if ($data['integration_status'] == 2) {//减少
  90. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  91. $integral_data['title'] = '系统减少积分';
  92. $integral_data['mark'] = '系统扣除了' . floatval($data['integration']) . '积分';
  93. $type = 'system_sub';
  94. $pm = 0;
  95. }
  96. $res2 = $userBill->incomeIntegral($user['uid'], $type, $integral_data, $pm);
  97. } else {
  98. $res2 = true;
  99. }
  100. } else {
  101. $res2 = $res1 = true;
  102. }
  103. //修改基本信息
  104. if (!isset($data['is_other']) || !$data['is_other']) {
  105. $edit['real_name'] = $data['real_name'];
  106. $edit['card_id'] = $data['card_id'];
  107. $edit['birthday'] = strtotime($data['birthday']);
  108. $edit['phone'] = $data['phone'];
  109. $edit['addres'] = $data['addres'];
  110. }
  111. if ($edit) $res3 = $this->dao->update($id, $edit);
  112. else $res3 = true;
  113. if ($res1 && $res2 && $res3)
  114. return true;
  115. else throw new AdminException('修改失败');
  116. }
  117. }