123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace app\admin\controller\sms;
- use app\model\Member;
- use app\model\MemberBenefit;
- use app\model\MemberQuota;
- use support\Redis;
- use support\Request;
- use Hhink\WebmanSms\Sms;
- class SmsController
- {
- const KEY_PREFIX = 'SMS:CODE:';
- /**
- * @Desc 核销验证码
- * @Author Gorden
- * @Date 2024/6/4 8:46
- *
- * @param Request $request
- * @return \support\Response
- */
- public function sendSmsCodeByQuota(Request $request)
- {
- $memberId = $request->post('member_id', []);
- $componentId = $request->post('component_id', []);
- $nbr = $request->post('nbr');
- if (!$memberId || !$componentId) {
- return json_fail("参数异常");
- }
- $quotas = MemberQuota::where('join_quota_member_id', $memberId[0])
- ->where('join_member_rule_added_component_id', $componentId[0])
- ->where('member_quota_status', 'PENDING')
- ->limit($nbr)
- ->get()
- ->toArray();
- if (count($quotas) < intval($nbr)) {
- return json_fail("可核销数量不足");
- }
- try {
- $member = Member::find(current($memberId));
- $mobile = $member->member_mobile;
- $code = random_string(6, 'number');
- $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
- Redis::set($key, $code);
- Redis::expire($key, 600);
- Sms::app()->send($mobile, [
- 'template' => 'SMS_240865706',
- 'data' => [
- 'code' => $code
- ],
- ]);
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail("短信发送失败,请稍后重试");
- }
- return json_success('success');
- }
- public function sendSmsCodeByBenefit(Request $request)
- {
- $memberId = $request->post('member_id', '');
- $benefitId = $request->post('benefit_id', '');
- $nbr = $request->post('nbr');
- if (!$memberId || !$benefitId) {
- return json_fail("参数异常");
- }
- $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
- ->where('join_benefit_member_id', $memberId)
- ->first();
- if (!$benefit) {
- return json_fail("权益不存在");
- }
- if (($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count) < intval($nbr)) {
- return json_fail("可核销数量不足");
- }
- try {
- $member = Member::find($memberId);
- $mobile = $member->member_mobile;
- $code = random_string(6, 'number');
- $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
- Redis::set($key, $code);
- Redis::expire($key, 600);
- Sms::app()->send($mobile, [
- 'template' => 'SMS_240865706',
- 'data' => [
- 'code' => $code
- ],
- ]);
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail("短信发送失败,请稍后重试");
- }
- return json_success('success');
- }
- }
|