Airdrop.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use app\common\model\ProductPopular;
  7. use app\common\model\UserModel;
  8. use app\api\logic\WelfareLoginc;
  9. use app\common\model\ProductOrder;
  10. use think\exception\DbException;
  11. use think\exception\PDOException;
  12. use think\exception\ValidateException;
  13. use think\response\Json;
  14. /**
  15. * 空投管理管理
  16. *
  17. * @icon fa fa-circle-o
  18. */
  19. class Airdrop extends Backend
  20. {
  21. /**
  22. * Airdrop模型对象
  23. * @var \app\admin\model\user\Airdrop
  24. */
  25. protected $model = null;
  26. protected $relationSearch = true;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\common\model\UserAirdrop;
  31. }
  32. /**
  33. * 查看
  34. *
  35. * @return string|Json
  36. * @throws \think\Exception
  37. * @throws DbException
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if (false === $this->request->isAjax()) {
  44. return $this->view->fetch();
  45. }
  46. //如果发送的来源是 Selectpage,则转发到 Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  51. $list = $this->model->with('product,rwaProduct')
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. $result = ['total' => $list->total(), 'rows' => $list->items()];
  56. return json($result);
  57. }
  58. /**
  59. * 添加
  60. * @return string
  61. * @throws \think\Exception
  62. */
  63. public function add()
  64. {
  65. if (false === $this->request->isPost()) {
  66. return $this->view->fetch();
  67. }
  68. $params = $this->request->post('row/a');
  69. if (empty($params)) {
  70. $this->error(__('Parameter %s can not be empty', ''));
  71. }
  72. $params = $this->preExcludeFields($params);
  73. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  74. $params[$this->dataLimitField] = $this->auth->id;
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException()->validate($validate);
  84. }
  85. if($params['type_id'] == 0){
  86. $params['address'] = (substr($params['address'], 0, 2) == '0x')? $params['address']: (new UserModel)->getUserByUid($params['address']);
  87. $user = (new UserModel)->getByAddress($params['address']);
  88. if(empty($user)) throw new Exception('用户不存在');
  89. WelfareLoginc::setUserProductOrder($params['num'], 0, 0, 0, $params['product_id'], $user->id, ProductOrder::Airdrop);
  90. $params['status'] = 1;
  91. }
  92. if($params['type_id'] == 1){
  93. $totalNum = WelfareLoginc::getUserRwaProductNum($params['rwa_product_id'], $params['rwa_num'], $params['rwa_mod'], $params['num']);
  94. if($totalNum == 0) throw new Exception(__('暂无符合条件的用户'));
  95. $params['total_num'] = $totalNum;
  96. }
  97. if(!empty($params['area_id'][0])){
  98. $params['area_id'] = rtrim(implode(',', $params['area_id']), ',');
  99. }else{
  100. unset($params['area_id']);
  101. }
  102. $result = $this->model->allowField(true)->save($params);
  103. Db::commit();
  104. } catch (ValidateException|PDOException|Exception $e) {
  105. Db::rollback();
  106. $this->error($e->getMessage());
  107. }
  108. if ($result === false) {
  109. $this->error(__('No rows were inserted'));
  110. }
  111. $this->success();
  112. }
  113. }