Esempio n. 1
0
 /**
  * 发送手机验证码
  */
 function code()
 {
     $system = $this->model('system')->fetch('sms');
     $system = $this->model('system')->toArray($system, 'sms');
     $sms = new sms($system['uid'], $system['key'], $system['sign']);
     $telephone = $this->post->telephone;
     if ($telephone === NULL) {
         return $sms->getNum();
     } else {
         if (validate::telephone($telephone)) {
             $smslogModel = $this->model('smslog');
             if ($smslogModel->check($telephone)) {
                 $code = random::number(6);
                 $template = $system['template'];
                 $template = sprintf($template, $code);
                 $result = $sms->send($telephone, $template);
                 if ($result > 0) {
                     $smslogModel->create($telephone, $code);
                     return json_encode(array('code' => 1, 'result' => 'ok', 'body' => $code));
                 }
             }
             return json_encode(array('code' => 2, 'result' => '短信发送失败'));
         } else {
             return json_encode(array('code' => 0, 'result' => '手机号码不合法'));
         }
     }
 }
Esempio n. 2
0
 /**
  * 给用户发送短信
  */
 function message()
 {
     $roleModel = $this->model('role');
     if (login::admin() && $roleModel->checkPower($this->session->role, 'user', roleModel::POWER_ALL)) {
         $content = htmlspecialchars_decode($this->post->content);
         if (mb_strlen($content, 'utf8') > 350) {
             return new json(4, '短信长度不得超过350个字');
         }
         if (mb_strlen($content, 'utf-8') < 10) {
             return new json(5, '短信长度太短了,为了能通过接口还是多写几个字吧');
         }
         $userModel = $this->model('user');
         if (!empty($this->post->data)) {
             $data = json_decode(htmlspecialchars_decode($this->post->data));
             $userModel->where('id in (?)', array(implode(',', $data)));
             $result = $userModel->select('telephone');
             $telephone = array();
             if (isset($result[0]['telephone']) && !empty($result[0]['telephone'])) {
                 $telephone[] = $result[0]['telephone'];
             }
             $sms = new sms();
             $result = $sms->send($telephone, $content);
             if ((int) $result > 0) {
                 $this->model('log')->write($this->session->username, '给' . count($telephone) . '个用户发送了短信:' . $content);
                 return new json(json::OK);
             } else {
                 if ((int) $result < 0) {
                     return new json(6, '短信接口异常' . $result);
                 }
             }
             return new json(json::PARAMETER_ERROR);
         }
         return new json(json::PARAMETER_ERROR);
     }
     return new json(json::NO_POWER);
 }