SoundcoinLog.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 用户声币流水记录
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class SoundcoinLog extends Backend
  13. {
  14. protected $searchFields = 'user.u_id,user.nickname';
  15. /**
  16. * SoundcoinLog模型对象
  17. * @var \app\admin\model\user\SoundcoinLog
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\user\SoundcoinLog;
  24. $this->view->assign("typeList", $this->model->getTypeList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 列表
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['user'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['u_id','nickname']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $params = $this->request->post("row/a");
  70. if ($params) {
  71. $params = $this->preExcludeFields($params);
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. $result = false;
  76. Db::startTrans();
  77. try {
  78. //是否采用模型验证
  79. if ($this->modelValidate) {
  80. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  81. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  82. $this->model->validateFailException(true)->validate($validate);
  83. }
  84. // 获取用户的变更前余额
  85. $userSoundCoin = \app\admin\model\User::where('id', $params['user_id'])->value('sound_coin');
  86. $params['obj_id'] = $this->auth->id;
  87. $params['before'] = $userSoundCoin;
  88. $adjustmentValue = intval($params['value']);
  89. switch ($params['mode']) {
  90. case '+':
  91. $params['balance'] = $userSoundCoin + $adjustmentValue;
  92. break;
  93. case '-':
  94. if ($adjustmentValue > $userSoundCoin) {
  95. $this->error('调整声币余额大于现有声币余额');
  96. }
  97. $params['balance'] = $userSoundCoin - $adjustmentValue;
  98. break;
  99. default:
  100. $this->error('请输入正确的变更方式');
  101. }
  102. $res1 = $this->model->allowField(true)->save($params);
  103. // 调整用户声币余额
  104. $res2 = \app\admin\model\User::where(["id" => $params['user_id']])->update([
  105. 'sound_coin' => Db::raw("sound_coin{$params['mode']}{$adjustmentValue}"),
  106. ]);
  107. if ($res1 && $res2) {
  108. Db::commit();
  109. $this->success();
  110. } else {
  111. $this->error(__('Operation failed'));
  112. }
  113. Db::commit();
  114. } catch (ValidateException $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. } catch (PDOException $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. } catch (Exception $e) {
  121. Db::rollback();
  122. $this->error($e->getMessage());
  123. }
  124. }
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. return $this->view->fetch();
  128. }
  129. }