123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace app\api\controller;
- use Alicode\Alisms;
- use app\admin\model\Smslog;
- use app\common\controller\Api;
- use app\common\library\Sms as Smslib;
- use app\common\model\User;
- use think\Hook;
- /**
- * 手机短信接口
- */
- class Sms extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- public function add_sms_log($user_id){
- $sms_log_model= new Smslog();
- $sms_info = $sms_log_model->whereTime('create_time','today')->find();
- if($sms_info){
- $sms_log_model->isUpdate(true,['id'=>$sms_info['id'], 'user_id' => $user_id])->save(['number'=>$sms_info['number']+1]);
- }
- else {
- $data = [
- 'user_id'=>$user_id,
- 'number' => 1,
- 'create_time' => time(),
- ];
- $sms_log_model->insert($data);
- }
- }
- public function SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code)
- {
- $sendSmsRequest = [
- "TemplateCode" => $TemplateCode,
- "phoneNumber" => $phoneNumber,
- "TemplateParam" => $TemplateParam
- ];
- $res = Alisms::sendSmsCode($sendSmsRequest);
- \app\api\model\SmsLog::create([
- 'params' => json_encode($sendSmsRequest, JSON_UNESCAPED_UNICODE),
- 'event' => $event,
- 'code' => $code,
- 'user_id' => $this->auth->id ?? 0,
- 'response' => json_encode($res, JSON_UNESCAPED_UNICODE),
- 'created_at' => date('Y-m-d H:i:s')
- ]);
- if($res['Code']=='OK') {
- // 短信发送记录
- $sms_model = new \app\common\model\Sms();
- $insert_data = [
- 'mobile' => $phoneNumber,
- 'event' => $event,
- 'code' => $code,
- 'times' => 1,
- 'createtime' => time(),
- ];
- $sms_model->insert($insert_data);
- }
- return $res['Code'] == 'OK' ? true : false;
- }
- //发送短信验证码
- public function send_register(){
- $mobile = input('mobile');
- $event = input('event');
- $TemplateCode = "SMS_269130458";
- $phoneNumber = $mobile;
- $code = rand('000000','999999');
- $TemplateParam = json_encode(['code' => $code]);
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code);
- if($code){
- $this->success('发送成功');
- }
- else{
- $this->error('发送失败,请重新发送');
- }
- }
- //发送午餐通知
- public function wucna_tongzhi($mobiles,$user_id){
- foreach ($mobiles as $value){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269235572";
- $phoneNumber = $value['phone'];
- $ls = $this->SmsTemplate($TemplateCode, $phoneNumber, '', $event, 0000);
- if ($ls) {
- $this->add_sms_log($user_id);
- }
- }
- }
- //发送座位排序通知
- public function zuowei_tongzhi($mobiles,$user_id){
- foreach ($mobiles as $value){
- $event = 'tongzhi';
- $TemplateCode = "SMS_270945003";
- $phoneNumber = $value['phone'];
- $TemplateParam = $value['number'];
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam, $event, 0000);
- if($code){
- $this->add_sms_log($user_id);
- } else {
- $this->error('发送失败,请重新发送');
- }
- }
- return true;
- }
- //发送超时未核销
- public function order_guoshi($mobiles,$order_no,$price,$user_id){
- $event = 'guoshi';
- $TemplateCode = "SMS_269210582";
- $phoneNumber = $mobiles;
- $code = 0000;
- $TemplateParam = json_encode(['date' => $order_no, 'price' => $price]);;
- $ls = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam, $event, $code);
- if ($ls) {
- $this->add_sms_log($user_id);
- }
- return $ls;
- }
- //开始前半小时
- public function order_sta($mobiles,$date,$user_id){
- $event = 'sta';
- $TemplateCode = "SMS_269075614";
- $phoneNumber = $mobiles;
- $code =0000;
- $TemplateParam = json_encode(['date' => $date]);;
- $ls=$this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code);
- if($ls) {
- $this->add_sms_log($user_id);
- }
- }
- //结束前半小时
- public function order_end($mobiles,$date,$user_id){
- $event = 'sta';
- $TemplateCode = "SMS_269100595";
- $phoneNumber = $mobiles;
- $code =0000;
- $TemplateParam = json_encode(['date' => $date]);;
- $ls=$this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$code);
- if($ls) {
- $this->add_sms_log($user_id);
- }
- return $ls;
- }
- //鱼塘主审核通过通知
- public function yutangzhu_shenhe($mobile, $name)
- {
- $event = 'tongzhi';
- $TemplateCode = "SMS_269180580";
- // $TemplateCode = "SMS_269235572";
- $phoneNumber = $mobile;
- $TemplateParam = json_encode(['name' => $name]);;
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam, $event, $name);
- return $code;
- }
- public function apply_yutangzhu($mobile, $name)
- {
- return $this->SmsTemplate('SMS_269275572', $mobile, json_encode(['name' => $name]), 'tongzhi', $name);
- }
-
- //鱼塘主审核拒绝通知
- public function yutang_jujue($mobile,$name){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269215520";
- $phoneNumber =$mobile;
- $TemplateParam =json_encode(['name' => $name]);;
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,$name);
- return $code;
- }
- //订单退款通知
- public function order_reund($mobile,$order_no,$price){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269180579";
- $phoneNumber =$mobile;
- $TemplateParam =json_encode(['date' => $order_no,'price'=>$price]);;
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'退款');
- return $code;
- }
- //订单退款通知
- public function yuchang_jujue($mobile,$name){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269200606";
- $phoneNumber =$mobile;
- $TemplateParam =json_encode(['name' => $name]);;
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'渔场主驳回');
- return $code;
- }
- //订单退款通知
- public function yuchang_shenhe($mobile,$name){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269200606";
- $phoneNumber =$mobile;
- $TemplateParam =json_encode(['name' => $name]);;
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'渔场主审核');
- return $code;
- }
- //订单退款通知
- public function dainyue_xiaoxi($mobile,$ytang_name,$number,$price,$date){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269605122";
- $phoneNumber =$mobile;
- $TemplateParam =json_encode(['name' => $ytang_name,'number'=>$number,'price'=>$price,'date'=>$date]);
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'订阅消息');
- return $code;
- }
- public function order_huiyu($mobile,$order_no,$nickname){
- $event = 'tongzhi';
- $TemplateCode = "SMS_269065590";
- $phoneNumber =$mobile;
- $TemplateParam =json_encode(['code' => $order_no,'name'=>$nickname]);
- $code = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam,$event,'回鱼信息');
- return $code;
- }
- /**
- * 购买短息通知
- * @param $mobile
- * @param $number
- * @return bool
- */
- public function pay_sms($mobile, $number)
- {
- return $this->SmsTemplate('SMS_269045613', $mobile, json_encode(['number' => $number]), 'pay_sms', 0);
- }
- /**
- * 发送验证码
- *
- * @ApiMethod (POST)
- * @param string $mobile 手机号
- * @param string $event 事件名称
- */
- public function send()
- {
- $mobile = $this->request->post("mobile");
- $event = $this->request->post("event");
- $event = $event ? $event : 'register';
- if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('手机号不正确'));
- }
- $last = Smslib::get($mobile, $event);
- if ($last && time() - $last['createtime'] < 60) {
- $this->error(__('发送频繁'));
- }
- $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
- if ($ipSendTotal >= 5) {
- $this->error(__('发送频繁'));
- }
- if ($event) {
- $userinfo = User::getByMobile($mobile);
- if ($event == 'register' && $userinfo) {
- //已被注册
- $this->error(__('已被注册'));
- } elseif (in_array($event, ['changemobile']) && $userinfo) {
- //被占用
- $this->error(__('已被占用'));
- } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
- //未注册
- $this->error(__('未注册'));
- }
- }
- if (!Hook::get('sms_send')) {
- $this->error(__('请在后台插件管理安装短信验证插件'));
- }
- $ret = Smslib::send($mobile, null, $event);
- if ($ret) {
- $this->success(__('发送成功'));
- } else {
- $this->error(__('发送失败,请检查短信配置是否正确'));
- }
- }
- /**
- * 检测验证码
- *
- * @ApiMethod (POST)
- * @param string $mobile 手机号
- * @param string $event 事件名称
- * @param string $captcha 验证码
- */
- public function check()
- {
- $mobile = $this->request->post("mobile");
- $event = $this->request->post("event");
- $event = $event ? $event : 'register';
- $captcha = $this->request->post("captcha");
- if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('手机号不正确'));
- }
- if ($event) {
- $userinfo = User::getByMobile($mobile);
- if ($event == 'register' && $userinfo) {
- //已被注册
- $this->error(__('已被注册'));
- } elseif (in_array($event, ['changemobile']) && $userinfo) {
- //被占用
- $this->error(__('已被占用'));
- } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
- //未注册
- $this->error(__('未注册'));
- }
- }
- $ret = Smslib::check($mobile, $captcha, $event);
- if ($ret) {
- $this->success(__('成功'));
- } else {
- $this->error(__('验证码不正确'));
- }
- }
- }
|