Airdrop.php 3.9 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. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\common\model\UserAirdrop;
  30. }
  31. /**
  32. * 查看
  33. *
  34. * @return string|Json
  35. * @throws \think\Exception
  36. * @throws DbException
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if (false === $this->request->isAjax()) {
  43. return $this->view->fetch();
  44. }
  45. //如果发送的来源是 Selectpage,则转发到 Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  50. $list = $this->model->with('product,rwaProduct')
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. $result = ['total' => $list->total(), 'rows' => $list->items()];
  55. return json($result);
  56. }
  57. /**
  58. * 添加
  59. * @return string
  60. * @throws \think\Exception
  61. */
  62. public function add()
  63. {
  64. if (false === $this->request->isPost()) {
  65. return $this->view->fetch();
  66. }
  67. $params = $this->request->post('row/a');
  68. if (empty($params)) {
  69. $this->error(__('Parameter %s can not be empty', ''));
  70. }
  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()->validate($validate);
  83. }
  84. $params['address'] = (substr($params['address'], 0, 2) == '0x')? $params['address']: (new UserModel)->getUserByUid($params['address']);
  85. if($params['type_id'] == 0){
  86. $user = (new UserModel)->getByAddress($params['address']);
  87. if(empty($user)) throw new Exception('用户不存在');
  88. WelfareLoginc::setUserProductOrder($params['num'], 0, 0, 0, $params['product_id'], $user->id, ProductOrder::Airdrop);
  89. $params['status'] = 1;
  90. }
  91. if($params['type_id'] == 1){
  92. $totalNum = WelfareLoginc::getUserRwaProductNum($params['rwa_product_id'], $params['rwa_num'], $params['rwa_mod'], $params['num']);
  93. if($totalNum == 0) throw new Exception(__('暂无符合条件的用户'));
  94. $params['total_num'] = $totalNum;
  95. }
  96. if(!empty($params['area_id'][0])){
  97. $params['area_id'] = rtrim(implode(',', $params['area_id']), ',');
  98. }else{
  99. unset($params['area_id']);
  100. }
  101. $result = $this->model->allowField(true)->save($params);
  102. Db::commit();
  103. } catch (ValidateException|PDOException|Exception $e) {
  104. Db::rollback();
  105. $this->error($e->getMessage());
  106. }
  107. if ($result === false) {
  108. $this->error(__('No rows were inserted'));
  109. }
  110. $this->success();
  111. }
  112. }