Sms.php 6.9 KB

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