Sms.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms as Smslib;
  5. use app\common\model\User;
  6. use app\common\service\SmsService;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Hook;
  10. use think\Log;
  11. /**
  12. * 手机短信接口
  13. */
  14. class Sms extends Api
  15. {
  16. protected $noNeedLogin = '*';
  17. protected $noNeedRight = '*';
  18. /**
  19. * 发送验证码
  20. *
  21. * @ApiMethod (POST)
  22. * @param string $mobile 手机号
  23. * @param string $event 事件名称
  24. */
  25. public function send()
  26. {
  27. $mobile = $this->request->post("mobile");
  28. $event = $this->request->post("event");
  29. $event = $event ? $event : 'register';
  30. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  31. $this->error(__('手机号不正确'));
  32. }
  33. $last = Smslib::get($mobile, $event);
  34. if ($last && time() - $last['createtime'] < 60) {
  35. $this->error(__('发送频繁'));
  36. }
  37. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  38. if ($ipSendTotal >= 5) {
  39. $this->error(__('发送频繁'));
  40. }
  41. if ($event) {
  42. $userinfo = User::getByMobile($mobile);
  43. if ($event == 'register' && $userinfo) {
  44. //已被注册
  45. $this->error(__('已被注册'));
  46. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  47. //被占用
  48. $this->error(__('已被占用'));
  49. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  50. //未注册
  51. $this->error(__('未注册'));
  52. }
  53. }
  54. if (!Hook::get('sms_send')) {
  55. $this->error(__('请在后台插件管理安装短信验证插件'));
  56. }
  57. $ret = Smslib::send($mobile, null, $event);
  58. if ($ret) {
  59. $this->success(__('发送成功'));
  60. } else {
  61. $this->error(__('发送失败,请检查短信配置是否正确'));
  62. }
  63. }
  64. /**
  65. * 检测验证码
  66. *
  67. * @ApiMethod (POST)
  68. * @param string $mobile 手机号
  69. * @param string $event 事件名称
  70. * @param string $captcha 验证码
  71. */
  72. public function check()
  73. {
  74. $mobile = $this->request->post("mobile");
  75. $event = $this->request->post("event");
  76. $event = $event ? $event : 'register';
  77. $captcha = $this->request->post("captcha");
  78. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  79. $this->error(__('手机号不正确'));
  80. }
  81. if ($event) {
  82. $userinfo = User::getByMobile($mobile);
  83. if ($event == 'register' && $userinfo) {
  84. //已被注册
  85. $this->error(__('已被注册'));
  86. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  87. //被占用
  88. $this->error(__('已被占用'));
  89. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  90. //未注册
  91. $this->error(__('未注册'));
  92. }
  93. }
  94. $ret = Smslib::check($mobile, $captcha, $event);
  95. if ($ret) {
  96. $this->success(__('成功'));
  97. } else {
  98. $this->error(__('验证码不正确'));
  99. }
  100. }
  101. /**
  102. * 保养提醒短信发送
  103. * @return void
  104. */
  105. public function remindSend()
  106. {
  107. try {
  108. $startTime = strtotime('00:00:00'); //今天开始时间戳
  109. $endTime = strtotime('+ 1day',$startTime);
  110. $where['remind_time'] = ['between',[$startTime,$endTime]];
  111. $where['status'] = 1;
  112. $where['is_remind'] = 0;
  113. $remindCount = Db::name('user_car_remind')->where($where)->count();
  114. if (!empty($remindCount)) {
  115. $remindIds = [];
  116. $limit = 10;
  117. $total = intval(ceil($remindCount / $limit));
  118. $service = new SmsService();
  119. $templateArr = config('ali_sms_template');
  120. $template = isset($templateArr['service_expire']) ? $templateArr['service_expire'] : '';
  121. for ($i=1; $i <= $total;$i++) {
  122. $page = $i;
  123. $offset = ($page - 1) * $limit;
  124. $remindData = model('UserCarRemind')->with(['order'=>function($oQuery){
  125. $oQuery->field('id,user_mobile,user_name');
  126. }])->where($where)->limit($offset,$limit)->select();
  127. $remindData = collection($remindData)->toArray();
  128. if (!empty($remindData)) {
  129. //尊敬的${name}先生/女士,您的爱车${chepai}要到保养周期了,预计保养时间为:${time},不要错过爱车的黄金保养时间哦!
  130. foreach ($remindData as $key => $value) {
  131. $order = isset($value['order']) ? $value['order'] : [];
  132. $mobile = isset($order['user_mobile']) ? $order['user_mobile'] : '';//手机号
  133. $name = isset($order['user_name']) ? $order['user_name'] : ''; //联系人
  134. $params = [
  135. 'template' => $template,//短息模版
  136. 'mobile' => $mobile, //手机号
  137. 'data_params' => [
  138. 'name' => $name, //联系人
  139. 'chepai' => $value['car_number'], //车牌号
  140. 'time' => date('Y-m-d',$value['upkeep_time']), //保养时间
  141. ],//短信参数
  142. ];
  143. $smsRes = $service->send($params);
  144. if (!$smsRes['status']) {
  145. Log::error('短信发送失败:params:'.json_encode($params));
  146. } else {
  147. $remindIds[] = $value['id'];
  148. }
  149. }
  150. }
  151. }
  152. if (!empty($remindIds)) {
  153. $updateWhere['id'] = ['in',$remindIds];
  154. $remindRes = model('UserCarRemind')->where($updateWhere)->update(['is_remind'=>1]);
  155. }
  156. }
  157. $this->success();
  158. } catch (Exception $e) {
  159. $this->error($e->getMessage());
  160. }
  161. }
  162. }