12 changed files with 280 additions and 9 deletions
@ -0,0 +1,60 @@ |
|||
<?php |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
|
|||
class WechatSms extends Model |
|||
{ |
|||
|
|||
/** |
|||
* @return int |
|||
*/ |
|||
public static function getCode($wechat_user_id) |
|||
{ |
|||
$code = rand(000000,999999); |
|||
|
|||
$result = self::getValidCode($code,$wechat_user_id); |
|||
if ($result) { |
|||
return self::getCode($wechat_user_id); |
|||
} |
|||
|
|||
return $code; |
|||
} |
|||
|
|||
public static function getValidCode($code,$wechat_user_id) |
|||
{ |
|||
$where = [ |
|||
['code', '=', $code], |
|||
['wechat_user_id', '=', $wechat_user_id], |
|||
['create_time', '>=', time() - (10*60)], |
|||
]; |
|||
|
|||
return (new self())->where($where)->value('wechat_user_id'); |
|||
} |
|||
|
|||
|
|||
public static function getValidUser($wechat_user_id) |
|||
{ |
|||
$where = [ |
|||
['wechat_user_id', '=', $wechat_user_id], |
|||
['create_time', '>=', time() - 60], |
|||
]; |
|||
|
|||
return (new self())->where($where)->value('create_time'); |
|||
} |
|||
|
|||
public static function addSmsCode($wechat_user_id,$code): bool |
|||
{ |
|||
|
|||
$data = [ |
|||
'wechat_user_id' => $wechat_user_id, |
|||
'code' => $code, |
|||
'create_time' => time() |
|||
]; |
|||
|
|||
return (new self())->save($data); |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
<?php |
|||
|
|||
namespace app\service\invoice; |
|||
|
|||
use app\model\InvoiceHead; |
|||
use app\model\InvoiceIssuance; |
|||
|
|||
class InvoiceIssuanceService |
|||
{ |
|||
|
|||
/** |
|||
* |
|||
* @param $param |
|||
* @param $wechat_user_id |
|||
* @return array |
|||
* @throws \think\db\exception\DataNotFoundException |
|||
* @throws \think\db\exception\DbException |
|||
* @throws \think\db\exception\ModelNotFoundException |
|||
*/ |
|||
public static function getListPage($param, $wechat_user_id): array |
|||
{ |
|||
|
|||
$limit = $params['limit'] ?? 10; |
|||
|
|||
$where = [ |
|||
['wechat_user_id', '=', $wechat_user_id], |
|||
['delete_time', '=', 0] |
|||
]; |
|||
|
|||
$model = (new InvoiceIssuance()); |
|||
$data = $model->where($where) |
|||
->field('project_id,invoice_head_id,amount,create_time,status') |
|||
->order('create_time desc') |
|||
->paginate($limit, false) |
|||
->each(function ($item, $key) { |
|||
$InvoiceHead = (new InvoiceHead)->find($item->invoice_head_id); |
|||
$item->serial_number = date("YmdHis") . rand(0000, 9999); |
|||
$item->head_type = ''; |
|||
$item->head_title = ''; |
|||
if ($InvoiceHead) { |
|||
$item->head_type = InvoiceHead::$typeArr[$InvoiceHead['type']]; |
|||
$item->head_title = $InvoiceHead['title']; |
|||
} |
|||
$item->project_id = InvoiceIssuance::$projectArr[$item->project_id]; |
|||
$item->status = InvoiceIssuance::$statusArr[$item->status]; |
|||
}); |
|||
return $data->toArray(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,95 @@ |
|||
<?php |
|||
|
|||
namespace app\service\user; |
|||
|
|||
use app\service\BaseService; |
|||
use fast\FuncException; |
|||
|
|||
class SmsService extends BaseService |
|||
{ |
|||
protected $smsUsername; |
|||
protected $smsPassword; |
|||
protected $sendUrl; |
|||
protected $content_prefix = '【大工业区水司】'; |
|||
|
|||
|
|||
public function __construct() |
|||
{ |
|||
parent::__construct(); |
|||
$this->smsUsername = env('sms.username'); |
|||
$this->smsPassword = env('sms.password'); |
|||
$this->sendUrl = env('sms.api_url'); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $code |
|||
* @return string |
|||
*/ |
|||
public function smsCodeTemplate($code):string |
|||
{ |
|||
return '您的短信验证码为:'.$code.',用于手机号绑定,10分钟内有效。若非本人操作,请勿泄露,谨防被骗。'; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $mobile |
|||
* @param $message |
|||
* @throws FuncException |
|||
*/ |
|||
public function sendSms($mobile,$message) |
|||
{ |
|||
$content = $this->content_prefix . $message; |
|||
|
|||
$param = [ |
|||
'userName' => $this->smsUsername, |
|||
'sign' => md5($this->smsUsername . $this->smsPassword . $mobile . $content), |
|||
'mobile' => $mobile, |
|||
'content' => $content, |
|||
]; |
|||
|
|||
$json = json_encode($param); |
|||
|
|||
$result = $this->curl($json); |
|||
if ($result['resultCode'] != 1) { |
|||
throw new FuncException('发送失败'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $json |
|||
* @return bool|string |
|||
* @throws FuncException |
|||
*/ |
|||
public function curl($json) |
|||
{ |
|||
// 初始化cURL会话 |
|||
$ch = curl_init(); |
|||
|
|||
// 设置cURL选项 |
|||
curl_setopt($ch, CURLOPT_URL, $this->sendUrl); // 目标URL |
|||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是输出 |
|||
curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求 |
|||
|
|||
// 设置POST字段 |
|||
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); |
|||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // 设置HTTP头 |
|||
|
|||
// 执行cURL会话 |
|||
$response = curl_exec($ch); |
|||
|
|||
// 检查是否有错误发生 |
|||
if (curl_errno($ch)) { |
|||
throw new FuncException('发送失败'); |
|||
} |
|||
|
|||
// 关闭cURL会话 |
|||
curl_close($ch); |
|||
|
|||
return json_decode($response,true); |
|||
} |
|||
|
|||
|
|||
|
|||
} |
|||
Loading…
Reference in new issue