Sms.php 6.9 KB

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