Sms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\api\controller;
  3. use Alicode\Alisms;
  4. use app\admin\model\Smslog;
  5. use app\common\controller\Api;
  6. use app\common\library\Sms as Smslib;
  7. use app\common\model\User;
  8. use think\Hook;
  9. /**
  10. * 手机短信接口
  11. */
  12. class Sms extends Api
  13. {
  14. protected $noNeedLogin = '*';
  15. protected $noNeedRight = '*';
  16. public function add_sms_log($user_id){
  17. $sms_log_model= new Smslog();
  18. $sms_info = $sms_log_model->whereTime('create_time','today')->find();
  19. if($sms_info){
  20. $sms_log_model->isUpdate(true,['id'=>$sms_info['id'], 'user_id' => $user_id])->save(['number'=>$sms_info['number']+1]);
  21. }
  22. else {
  23. $data = [
  24. 'user_id'=>$user_id,
  25. 'number' => 1,
  26. 'create_time' => time(),
  27. ];
  28. $sms_log_model->insert($data);
  29. }
  30. }
  31. public function SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code)
  32. {
  33. $sendSmsRequest = [
  34. "TemplateCode" => $TemplateCode,
  35. "phoneNumber" => $phoneNumber,
  36. "TemplateParam" => $TemplateParam
  37. ];
  38. $res = Alisms::sendSmsCode($sendSmsRequest);
  39. \app\api\model\SmsLog::create([
  40. 'params' => json_encode($sendSmsRequest, JSON_UNESCAPED_UNICODE),
  41. 'event' => $event,
  42. 'code' => $code,
  43. 'user_id' => $this->auth->id ?? 0,
  44. 'response' => json_encode($res, JSON_UNESCAPED_UNICODE),
  45. 'created_at' => date('Y-m-d H:i:s')
  46. ]);
  47. if($res['Code']=='OK') {
  48. // 短信发送记录
  49. $sms_model = new \app\common\model\Sms();
  50. $insert_data = [
  51. 'mobile' => $phoneNumber,
  52. 'event' => $event,
  53. 'code' => $code,
  54. 'times' => 1,
  55. 'createtime' => time(),
  56. ];
  57. $sms_model->insert($insert_data);
  58. }
  59. return $res['Code'] == 'OK' ? true : false;
  60. }
  61. //发送短信验证码
  62. public function send_register(){
  63. $mobile = input('mobile');
  64. $event = input('event');
  65. $TemplateCode = "SMS_269130458";
  66. $phoneNumber = $mobile;
  67. $code = rand('000000','999999');
  68. $TemplateParam = json_encode(['code' => $code]);
  69. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code);
  70. if($code){
  71. $this->success('发送成功');
  72. }
  73. else{
  74. $this->error('发送失败,请重新发送');
  75. }
  76. }
  77. //发送午餐通知
  78. public function wucna_tongzhi($mobiles,$user_id){
  79. foreach ($mobiles as $value){
  80. $event = 'tongzhi';
  81. $TemplateCode = "SMS_269235572";
  82. $phoneNumber = $value['phone'];
  83. $ls = $this->SmsTemplate($TemplateCode, $phoneNumber, '', $event, 0000);
  84. if ($ls) {
  85. $this->add_sms_log($user_id);
  86. }
  87. }
  88. }
  89. //发送座位排序通知
  90. public function zuowei_tongzhi($mobiles,$user_id){
  91. foreach ($mobiles as $value){
  92. $event = 'tongzhi';
  93. $TemplateCode = "SMS_270945003";
  94. $phoneNumber = $value['phone'];
  95. $TemplateParam = $value['number'];
  96. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam, $event, 0000);
  97. if($code){
  98. $this->add_sms_log($user_id);
  99. } else {
  100. $this->error('发送失败,请重新发送');
  101. }
  102. }
  103. return true;
  104. }
  105. //发送超时未核销
  106. public function order_guoshi($mobiles,$order_no,$price,$user_id){
  107. $event = 'guoshi';
  108. $TemplateCode = "SMS_269210582";
  109. $phoneNumber = $mobiles;
  110. $code = 0000;
  111. $TemplateParam = json_encode(['date' => $order_no, 'price' => $price]);;
  112. $ls = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam, $event, $code);
  113. if ($ls) {
  114. $this->add_sms_log($user_id);
  115. }
  116. return $ls;
  117. }
  118. //开始前半小时
  119. public function order_sta($mobiles,$date,$user_id){
  120. $event = 'sta';
  121. $TemplateCode = "SMS_269075614";
  122. $phoneNumber = $mobiles;
  123. $code =0000;
  124. $TemplateParam = json_encode(['date' => $date]);;
  125. $ls=$this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code);
  126. if($ls) {
  127. $this->add_sms_log($user_id);
  128. }
  129. }
  130. //结束前半小时
  131. public function order_end($mobiles,$date,$user_id){
  132. $event = 'sta';
  133. $TemplateCode = "SMS_269100595";
  134. $phoneNumber = $mobiles;
  135. $code =0000;
  136. $TemplateParam = json_encode(['date' => $date]);;
  137. $ls=$this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code);
  138. if($ls) {
  139. $this->add_sms_log($user_id);
  140. }
  141. return $ls;
  142. }
  143. //鱼塘主审核通过通知
  144. public function yutangzhu_shenhe($mobile, $name)
  145. {
  146. $event = 'tongzhi';
  147. $TemplateCode = "SMS_269180580";
  148. // $TemplateCode = "SMS_269235572";
  149. $phoneNumber = $mobile;
  150. $TemplateParam = json_encode(['name' => $name]);;
  151. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam, $event, $name);
  152. return $code;
  153. }
  154. public function apply_yutangzhu($mobile, $name)
  155. {
  156. return $this->SmsTemplate('SMS_269275572', $mobile, json_encode(['name' => $name]), 'tongzhi', $name);
  157. }
  158. //鱼塘主审核拒绝通知
  159. public function yutang_jujue($mobile,$name){
  160. $event = 'tongzhi';
  161. $TemplateCode = "SMS_269215520";
  162. $phoneNumber =$mobile;
  163. $TemplateParam =json_encode(['name' => $name]);;
  164. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$name);
  165. return $code;
  166. }
  167. //订单退款通知
  168. public function order_reund($mobile,$order_no,$price){
  169. $event = 'tongzhi';
  170. $TemplateCode = "SMS_269180579";
  171. $phoneNumber =$mobile;
  172. $TemplateParam =json_encode(['date' => $order_no,'price'=>$price]);;
  173. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'退款');
  174. return $code;
  175. }
  176. //订单退款通知
  177. public function yuchang_jujue($mobile,$name){
  178. $event = 'tongzhi';
  179. $TemplateCode = "SMS_269200606";
  180. $phoneNumber =$mobile;
  181. $TemplateParam =json_encode(['name' => $name]);;
  182. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'渔场主驳回');
  183. return $code;
  184. }
  185. //订单退款通知
  186. public function yuchang_shenhe($mobile,$name){
  187. $event = 'tongzhi';
  188. $TemplateCode = "SMS_269200606";
  189. $phoneNumber =$mobile;
  190. $TemplateParam =json_encode(['name' => $name]);;
  191. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'渔场主审核');
  192. return $code;
  193. }
  194. //订单退款通知
  195. public function dainyue_xiaoxi($mobile,$ytang_name,$number,$price,$date){
  196. $event = 'tongzhi';
  197. $TemplateCode = "SMS_269605122";
  198. $phoneNumber =$mobile;
  199. $TemplateParam =json_encode(['name' => $ytang_name,'number'=>$number,'price'=>$price,'date'=>$date]);
  200. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'订阅消息');
  201. return $code;
  202. }
  203. public function order_huiyu($mobile,$order_no,$nickname){
  204. $event = 'tongzhi';
  205. $TemplateCode = "SMS_269065590";
  206. $phoneNumber =$mobile;
  207. $TemplateParam =json_encode(['code' => $order_no,'name'=>$nickname]);
  208. $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'回鱼信息');
  209. return $code;
  210. }
  211. /**
  212. * 购买短息通知
  213. * @param $mobile
  214. * @param $number
  215. * @return bool
  216. */
  217. public function pay_sms($mobile, $number)
  218. {
  219. return $this->SmsTemplate('SMS_269045613', $mobile, json_encode(['number' => $number]), 'pay_sms', 0);
  220. }
  221. /**
  222. * 发送验证码
  223. *
  224. * @ApiMethod (POST)
  225. * @param string $mobile 手机号
  226. * @param string $event 事件名称
  227. */
  228. public function send()
  229. {
  230. $mobile = $this->request->post("mobile");
  231. $event = $this->request->post("event");
  232. $event = $event ? $event : 'register';
  233. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  234. $this->error(__('手机号不正确'));
  235. }
  236. $last = Smslib::get($mobile, $event);
  237. if ($last && time() - $last['createtime'] < 60) {
  238. $this->error(__('发送频繁'));
  239. }
  240. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  241. if ($ipSendTotal >= 5) {
  242. $this->error(__('发送频繁'));
  243. }
  244. if ($event) {
  245. $userinfo = User::getByMobile($mobile);
  246. if ($event == 'register' && $userinfo) {
  247. //已被注册
  248. $this->error(__('已被注册'));
  249. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  250. //被占用
  251. $this->error(__('已被占用'));
  252. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  253. //未注册
  254. $this->error(__('未注册'));
  255. }
  256. }
  257. if (!Hook::get('sms_send')) {
  258. $this->error(__('请在后台插件管理安装短信验证插件'));
  259. }
  260. $ret = Smslib::send($mobile, null, $event);
  261. if ($ret) {
  262. $this->success(__('发送成功'));
  263. } else {
  264. $this->error(__('发送失败,请检查短信配置是否正确'));
  265. }
  266. }
  267. /**
  268. * 检测验证码
  269. *
  270. * @ApiMethod (POST)
  271. * @param string $mobile 手机号
  272. * @param string $event 事件名称
  273. * @param string $captcha 验证码
  274. */
  275. public function check()
  276. {
  277. $mobile = $this->request->post("mobile");
  278. $event = $this->request->post("event");
  279. $event = $event ? $event : 'register';
  280. $captcha = $this->request->post("captcha");
  281. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  282. $this->error(__('手机号不正确'));
  283. }
  284. if ($event) {
  285. $userinfo = User::getByMobile($mobile);
  286. if ($event == 'register' && $userinfo) {
  287. //已被注册
  288. $this->error(__('已被注册'));
  289. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  290. //被占用
  291. $this->error(__('已被占用'));
  292. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  293. //未注册
  294. $this->error(__('未注册'));
  295. }
  296. }
  297. $ret = Smslib::check($mobile, $captcha, $event);
  298. if ($ret) {
  299. $this->success(__('成功'));
  300. } else {
  301. $this->error(__('验证码不正确'));
  302. }
  303. }
  304. }