SmsController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_469030007',
  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_469030007',
  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. public function sendSmsCodeByWriteOff(Request $request)
  93. {
  94. $memberId = $request->post('member_id', '');
  95. try {
  96. $member = Member::find($memberId);
  97. $mobile = $member->member_mobile;
  98. $code = random_string(6, 'number');
  99. $key = self::KEY_PREFIX . 'WRITE_OFF:' . $mobile;
  100. Redis::set($key, $code);
  101. Redis::expire($key, 600);
  102. Sms::app()->send($mobile, [
  103. 'template' => 'SMS_469030007',
  104. 'data' => [
  105. 'code' => $code
  106. ],
  107. ]);
  108. } catch (\Exception $e) {
  109. dump($e->getMessage());
  110. return json_fail("短信发送失败,请稍后重试");
  111. }
  112. return json_success('success');
  113. }
  114. public function sendSmsByOrderPay(Request $request)
  115. {
  116. $memberId = $request->post('member_id', '');
  117. try {
  118. $member = Member::find($memberId);
  119. $mobile = $member->member_mobile;
  120. $code = random_string(6, 'number');
  121. $key = self::KEY_PREFIX . 'ORDER_PAY:' . $mobile;
  122. Redis::set($key, $code);
  123. Redis::expire($key, 600);
  124. Sms::app()->send($mobile, [
  125. 'template' => 'SMS_469030007',
  126. 'data' => [
  127. 'code' => $code
  128. ],
  129. ]);
  130. } catch (\Exception $e) {
  131. dump($e->getMessage());
  132. return json_fail("短信发送失败,请稍后重试");
  133. }
  134. return json_success('success');
  135. }
  136. /**
  137. * 餐饮核销验证码
  138. */
  139. public function dishesWriteOffCode(Request $request)
  140. {
  141. $memberId = $request->post('member_id', '');
  142. try {
  143. $member = Member::find($memberId);
  144. $mobile = $member->member_mobile;
  145. $code = random_string(6, 'number');
  146. $key = self::KEY_PREFIX . 'DISHES:' . $mobile;
  147. Redis::set($key, $code);
  148. Redis::expire($key, 600);
  149. Sms::app()->send($mobile, [
  150. 'template' => 'SMS_469030007',
  151. 'data' => [
  152. 'code' => $code
  153. ],
  154. ]);
  155. } catch (\Exception $e) {
  156. dump($e->getMessage());
  157. return json_fail("短信发送失败,请稍后重试");
  158. }
  159. return json_success('success');
  160. }
  161. }