User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\api\service\UserService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Auth;
  6. use redis\RedLock;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * 会员管理
  11. *
  12. * @icon fa fa-user
  13. */
  14. class User extends Backend
  15. {
  16. protected $relationSearch = true;
  17. protected $searchFields = 'id,username,nickname';
  18. protected $noNeedRight = ["*"];
  19. /**
  20. * @var \app\admin\model\User
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->assignconfig(["admin" => $this->auth->getUserInfo()]);
  27. $this->model = model('User');
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $list = $this->model
  43. ->with(['group','store', 'parent'])
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->paginate($limit);
  47. foreach ($list as $k => $v) {
  48. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  49. $v->hidden(['password', 'salt']);
  50. }
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 添加
  58. */
  59. public function add()
  60. {
  61. if ($this->request->isPost()) {
  62. $this->token();
  63. }
  64. return parent::add();
  65. }
  66. /**
  67. * 编辑
  68. */
  69. public function edit($ids = null)
  70. {
  71. if ($this->request->isPost()) {
  72. $this->token();
  73. }
  74. $row = $this->model->get($ids);
  75. $this->modelValidate = true;
  76. if (!$row) {
  77. $this->error(__('No Results were found'));
  78. }
  79. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  80. return parent::edit($ids);
  81. }
  82. /**
  83. * 删除
  84. */
  85. public function del($ids = "")
  86. {
  87. if (!$this->request->isPost()) {
  88. $this->error(__("Invalid parameters"));
  89. }
  90. $ids = $ids ? $ids : $this->request->post("ids");
  91. $row = $this->model->get($ids);
  92. $this->modelValidate = true;
  93. if (!$row) {
  94. $this->error(__('No Results were found'));
  95. }
  96. Auth::instance()->delete($row['id']);
  97. $this->success();
  98. }
  99. public function wallet($id = null)
  100. {
  101. if (!$id)
  102. $this->error("ID 为空!");
  103. $w = $this->model->get($id);
  104. if (!$w)
  105. $this->error("用户不存在!");
  106. $wallet = (new \app\api\model\user\Wallet())->getUserWallet($id);
  107. $bill = (new \app\api\model\user\Bill())->where("user_id", $id)->limit(0, 100)->order("createtime", "desc")->select();
  108. $this->assign("wallet", $wallet);
  109. $this->assign("rows", $bill);
  110. return $this->view->fetch();
  111. }
  112. /**
  113. * 充值
  114. */
  115. public function recharge($ids = null)
  116. {
  117. $admin = $this->auth->getUserInfo();
  118. if (!$admin || $admin["id"] !== 1)
  119. $this->error("您没有权限充值余额!");
  120. $user = $this->model->get($ids);
  121. if (!$user)
  122. $this->error("不存在!");
  123. if ($this->request->post()) {
  124. $params = $this->request->param();
  125. if ($admin["recharge_password"] !== md5(md5($params["recharge_password"]) . $admin["salt"]))
  126. $this->error("密码错误,充值失败!");
  127. $change = 0;
  128. if (isset($params["change"])) {
  129. $change = $params["change"];
  130. if ($change < -100000 || $change > 100000)
  131. $this->error("充值的金额必须在 -100000 ~ 100000 之前!");
  132. }
  133. if ($change == 0)
  134. $this->error("无意义的操作!");
  135. $uWalletModel = new \app\api\model\user\Wallet();
  136. $redLock = RedLock::of();
  137. $lock = $redLock->lock(\app\api\model\user\Wallet::UWKey($user["id"]));
  138. if (!is_array($lock))
  139. $this->error("资源竞争中, 请稍后再试!");
  140. $uWallet = $uWalletModel->getUserWallet($user["id"]);
  141. $total_money = $uWallet["money"] + $uWallet["give_money"];
  142. if ($total_money + $change < 0) {
  143. $this->error("用户余额剩余:{$total_money}");
  144. }
  145. Db::startTrans();
  146. try {
  147. if ($change > 0) { // +
  148. $uWalletModel->where("id", $uWallet["id"])->setInc("give_money", $change);
  149. (new \app\admin\model\user\Bill())->save([
  150. "user_id" => $user["id"],
  151. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  152. "change_type" => \E_USER_BILL_CHANGE_TYPE::AdminRecharge[0],
  153. "change" => $change,
  154. "before" => $uWallet->money + $uWallet->give_money,
  155. "after" => ($uWallet->money + $uWallet->give_money) + $change,
  156. "reason" => \E_USER_BILL_CHANGE_TYPE::AdminRecharge[1],
  157. "money" => 0,
  158. "give_money" => $change,
  159. "createtime" => time()
  160. ]);
  161. } else { // -
  162. $abs_change = abs($change);
  163. $log = [
  164. "user_id" => $user["id"],
  165. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  166. "change_type" => \E_USER_BILL_CHANGE_TYPE::AdminReduce[0],
  167. "change" => $change,
  168. "before" => $uWallet->money + $uWallet->give_money,
  169. "after" => ($uWallet->money + $uWallet->give_money) + $change,
  170. "reason" => \E_USER_BILL_CHANGE_TYPE::AdminReduce[1],
  171. "money" => 0,
  172. "give_money" => 0,
  173. "createtime" => time()
  174. ];
  175. if ($uWallet["money"] > $abs_change) {
  176. $log["money"] = $change;
  177. $uWalletModel->where("id", $uWallet["id"])->setDec("money", $abs_change);
  178. } else {
  179. $log["money"] = -$uWallet["money"];
  180. $log["give_money"] = $change + $uWallet["money"];
  181. $uWalletModel->where("id", $uWallet["id"])->setDec("money", $uWallet["money"]);
  182. $uWalletModel->where("id", $uWallet["id"])->setDec("give_money", $abs_change - $uWallet["money"]);
  183. }
  184. (new \app\admin\model\user\Bill())->save($log);
  185. }
  186. Db::commit();
  187. } catch (Exception $e) {
  188. Db::rollback();
  189. $this->error($e->getMessage());
  190. } finally {
  191. $redLock->unlock($lock);
  192. }
  193. $this->success();
  194. }
  195. $this->view->assign([
  196. "user" => $user
  197. ]);
  198. return $this->view->fetch();
  199. }
  200. public function deduct($id)
  201. {
  202. }
  203. }