SmsController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller\sms;
  3. use app\model\Member;
  4. use app\model\MemberBenefit;
  5. use app\model\MemberQuota;
  6. use support\Redis;
  7. use support\Request;
  8. use Hhink\WebmanSms\Sms;
  9. class SmsController
  10. {
  11. const KEY_PREFIX = 'SMS:CODE:';
  12. /**
  13. * @Desc 核销验证码
  14. * @Author Gorden
  15. * @Date 2024/6/4 8:46
  16. *
  17. * @param Request $request
  18. * @return \support\Response
  19. */
  20. public function sendSmsCodeByQuota(Request $request)
  21. {
  22. $memberId = $request->post('member_id', []);
  23. $componentId = $request->post('component_id', []);
  24. $nbr = $request->post('nbr');
  25. if (!$memberId || !$componentId) {
  26. return json_fail("参数异常");
  27. }
  28. $quotas = MemberQuota::where('join_quota_member_id', $memberId[0])
  29. ->where('join_member_rule_added_component_id', $componentId[0])
  30. ->where('member_quota_status', 'PENDING')
  31. ->limit($nbr)
  32. ->get()
  33. ->toArray();
  34. if (count($quotas) < intval($nbr)) {
  35. return json_fail("可核销数量不足");
  36. }
  37. try {
  38. $member = Member::find(current($memberId));
  39. $mobile = $member->member_mobile;
  40. $code = random_string(6, 'number');
  41. $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
  42. Redis::set($key, $code);
  43. Redis::expire($key, 600);
  44. Sms::app()->send($mobile, [
  45. 'template' => 'SMS_240865706',
  46. 'data' => [
  47. 'code' => $code
  48. ],
  49. ]);
  50. } catch (\Exception $e) {
  51. dump($e->getMessage());
  52. return json_fail("短信发送失败,请稍后重试");
  53. }
  54. return json_success('success');
  55. }
  56. public function sendSmsCodeByBenefit(Request $request)
  57. {
  58. $memberId = $request->post('member_id', '');
  59. $benefitId = $request->post('benefit_id', '');
  60. $nbr = $request->post('nbr');
  61. if (!$memberId || !$benefitId) {
  62. return json_fail("参数异常");
  63. }
  64. $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
  65. ->where('join_benefit_member_id', $memberId)
  66. ->first();
  67. if (!$benefit) {
  68. return json_fail("权益不存在");
  69. }
  70. if (($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count) < intval($nbr)) {
  71. return json_fail("可核销数量不足");
  72. }
  73. try {
  74. $member = Member::find($memberId);
  75. $mobile = $member->member_mobile;
  76. $code = random_string(6, 'number');
  77. $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
  78. Redis::set($key, $code);
  79. Redis::expire($key, 600);
  80. Sms::app()->send($mobile, [
  81. 'template' => 'SMS_240865706',
  82. 'data' => [
  83. 'code' => $code
  84. ],
  85. ]);
  86. } catch (\Exception $e) {
  87. dump($e->getMessage());
  88. return json_fail("短信发送失败,请稍后重试");
  89. }
  90. return json_success('success');
  91. }
  92. }