Airdrop.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * 1. 从平台向指定账号空投一定数量的指定产品
  33. * 2. 向所有持有超过设定数量RWA茶的用户空投一定"数量"的指定产品(同一用户是否重复空头) a.先统计(RWA茶的用户)
  34. * 3. 茶付宝上指定区域消费指定金额空投 D 茶权需要开发回调接口
  35. */
  36. /**
  37. * 查看
  38. *
  39. * @return string|Json
  40. * @throws \think\Exception
  41. * @throws DbException
  42. */
  43. public function index()
  44. {
  45. //设置过滤方法
  46. $this->request->filter(['strip_tags', 'trim']);
  47. if (false === $this->request->isAjax()) {
  48. return $this->view->fetch();
  49. }
  50. //如果发送的来源是 Selectpage,则转发到 Selectpage
  51. if ($this->request->request('keyField')) {
  52. return $this->selectpage();
  53. }
  54. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  55. $list = $this->model
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. $result = ['total' => $list->total(), 'rows' => $list->items()];
  60. return json($result);
  61. }
  62. /**
  63. * 添加
  64. * @return string
  65. * @throws \think\Exception
  66. */
  67. public function add()
  68. {
  69. if (false === $this->request->isPost()) {
  70. return $this->view->fetch();
  71. }
  72. $params = $this->request->post('row/a');
  73. if (empty($params)) {
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. $params = $this->preExcludeFields($params);
  77. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  78. $params[$this->dataLimitField] = $this->auth->id;
  79. }
  80. $result = false;
  81. Db::startTrans();
  82. try {
  83. //是否采用模型验证
  84. if ($this->modelValidate) {
  85. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  86. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  87. $this->model->validateFailException()->validate($validate);
  88. }
  89. if($params['type_id'] == 0){
  90. $user = (new UserModel)->getByAddress($params['address']);
  91. if(empty($user)) throw new Exception('用户不存在');
  92. WelfareLoginc::setUserWelfareLos($user->id, $params['product_id'], $params['num'], time(), 'zh', ProductOrder::Airdrop);
  93. //添加Rwa数
  94. UserModel::updateForRwaNum($user->id, $user->parent_id, $params['num'], '+');
  95. $params['status'] = 1;
  96. }
  97. if($params['type_id'] == 1){
  98. $totalNum = WelfareLoginc::getUserRwaNum($params['rwa_num'], $params['rwa_mod'], $params['num']);
  99. $result = ProductPopular::getPopularByTime($params['product_id'], 'zh', strtotime($params['start_time']));
  100. if(!$result || $totalNum > $result->stock) throw new Exception(__('库存不足'));
  101. $params['total_num'] = $totalNum;
  102. }
  103. if(!empty($params['area_id'][0])){
  104. $params['area_id'] = rtrim(implode(',', $params['area_id']), ',');
  105. }else{
  106. unset($params['area_id']);
  107. }
  108. $result = $this->model->allowField(true)->save($params);
  109. Db::commit();
  110. } catch (ValidateException|PDOException|Exception $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. }
  114. if ($result === false) {
  115. $this->error(__('No rows were inserted'));
  116. }
  117. $this->success();
  118. }
  119. }