LottoSmsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use frontend\models\LottoSms;
  5. use common\helpers\Utils;
  6. use backend\helpers\DateTimeHelper;
  7. use backend\helpers\RandomHelper;
  8. class LottoSmsController extends BaseController
  9. {
  10. /**
  11. * 抽奖短信记录
  12. */
  13. public function actionList()
  14. {
  15. $post = Yii::$app->request->post();
  16. $order = isset($post['order']) ? strtolower($post['order']) : '';
  17. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  18. $search = isset($post['search']) ? $post['search'] : '';
  19. $start = isset($post['start']) ? (int) $post['start'] : 0;
  20. $length = isset($post['length']) ? (int) $post['length'] : 20;
  21. $draw = isset($post['draw']) ? $post['draw'] : 1;
  22. // 搜索
  23. $where = ['and'];
  24. if ($search) {
  25. $where[] = ['like', 'mobile', $search];
  26. }
  27. // 排序
  28. $allowOrderColumn = ['id', 'mobile', 'in_time', 'code', 'code_time', 'lottery'];
  29. if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
  30. if ($orderBy == 'asc') {
  31. $orderCondition = [$order => SORT_ASC];
  32. } else {
  33. $orderCondition = [$order => SORT_DESC];
  34. }
  35. } else {
  36. $orderCondition = ['id' => SORT_DESC];
  37. }
  38. $query = LottoSms::find();
  39. $query->where($where);
  40. $count = $query->count();
  41. $list = [];
  42. if ($count) {
  43. $list = $query->orderBy($orderCondition)->offset($start)->limit($length)->asArray()->all();
  44. }
  45. $data['data'] = $list;
  46. $data['draw'] = $draw;
  47. $data['recordsFiltered'] = $count;
  48. $data['recordsTotal'] = $count;
  49. return $this->outJson(1, $data);
  50. }
  51. /**
  52. * 删除抽奖短信记录
  53. */
  54. public function actionDelete()
  55. {
  56. $id = (int) Yii::$app->request->post('id');
  57. $model = LottoSms::find()->where(['id' => $id])->limit(1)->one();
  58. if (!$model) {
  59. return $this->outJson(0, [], '参数错误');
  60. }
  61. $result = $model->delete();
  62. if ($result) {
  63. return $this->outJson(1);
  64. } else {
  65. return $this->outJson(0, [], '删除失败');
  66. }
  67. }
  68. /**
  69. * 发送短信
  70. */
  71. public function actionSendSmsCode()
  72. {
  73. $mobile = Yii::$app->request->post('mobile');
  74. if (!Utils::checkPhoneNumber($mobile)) {
  75. return $this->outJson(0, [], '手机号码格式错误');
  76. }
  77. /** @var LottoSms $model */
  78. $model = LottoSms::find()->where(['mobile' => $mobile])->limit(1)->one();
  79. if ($model && $model->code) {
  80. return $this->outJson(0, [], '您已抽奖,请勿重复提交');
  81. }
  82. $code = RandomHelper::getRandomNo(4);
  83. $sendResult = LottoSms::sendSmsCode($mobile, $code);
  84. if ($sendResult['code'] != 1) {
  85. return $this->outJson(0, [], '发送短信失败: ' . $sendResult['message']);
  86. }
  87. if (!$model) {
  88. $model = new LottoSms();
  89. $model->mobile = $mobile;
  90. $model->in_time = DateTimeHelper::microtime_float();
  91. $model->code = $code;
  92. $model->code_time = DateTimeHelper::microtime_float();
  93. $model->save();
  94. } else {
  95. $model->code = $code;
  96. $model->code_time = DateTimeHelper::microtime_float();
  97. $model->save();
  98. }
  99. return $this->outJson(1, [], '短信发送成功');
  100. }
  101. /**
  102. * 验证短信验证码是否正确
  103. */
  104. public function actionVerifySmsCode()
  105. {
  106. $mobile = Yii::$app->request->post('mobile');
  107. $code = Yii::$app->request->post('code');
  108. /** @var LottoSms $model */
  109. $model = LottoSms::find()->where(['mobile' => $mobile])->limit(1)->one();
  110. if (!$model || !$model->code) {
  111. return $this->outJson(0, [], '请先获取短信验证码');
  112. }
  113. if ($code != $model->code) {
  114. return $this->outJson(0, [], '短信验证码错误');
  115. }
  116. if ($model->lottery) {
  117. return $this->outJson(0, [], '您已抽奖,请勿重复提交');
  118. }
  119. return $this->outJson(1, [], '验证通过');
  120. }
  121. /**
  122. * 保存中间记录
  123. */
  124. public function actionSaveLottery()
  125. {
  126. $mobile = Yii::$app->request->post('mobile');
  127. $lottery = Yii::$app->request->post('lottery');
  128. /** @var LottoSms $model */
  129. $model = LottoSms::find()->where(['mobile' => $mobile])->limit(1)->one();
  130. if (!$model) {
  131. return $this->outJson(0, [], '请先获取验证码抽奖');
  132. }
  133. $model->lottery = $lottery;
  134. $model->save();
  135. return $this->outJson(1, [], '请求成功');
  136. }
  137. }