Browse Source

发票首页接口、发送短信验证码接口

master
wanghongjun 1 year ago
parent
commit
061e641c4b
  1. 1
      .env
  2. 23
      app/controller/api/InvoiceIssuance.php
  3. 41
      app/controller/wechat/Wechat.php
  4. 2
      app/model/InvoiceHead.php
  5. 4
      app/model/InvoiceIssuance.php
  6. 60
      app/model/WechatSms.php
  7. 50
      app/service/invoice/InvoiceIssuanceService.php
  8. 95
      app/service/user/SmsService.php
  9. 2
      app/validate/InvoiceIssuanceValidate.php
  10. 5
      app/validate/WechatUser.php
  11. 3
      route/apiRoute.php
  12. 3
      route/wechatRoute.php

1
.env

@ -27,5 +27,6 @@ SOAP_URL = http://120.234.15.170:4000/FeeService.dll/soap/IFeeService
SOAP_PORT = 4000
[SMS]
API_URL = https://www.sms-cly.cn/v7/msg/submit.json
USERNAME = clygysw
PASSWORD = r9jndi

23
app/controller/api/InvoiceIssuance.php

@ -4,14 +4,33 @@ namespace app\controller\api;
use app\model\InvoiceHead;
use app\model\InvoiceIssuance as InvoiceIssuanceModel;
use app\service\invoice\InvoiceIssuanceService;
use app\util\ReturnCode;
use app\validate\InvoiceHeadValidate;
use app\validate\InvoiceIssuanceValidate;
use think\facade\Db;
use think\Response;
class InvoiceIssuance extends Base
{
/**
* @return Response
*/
public function getList(): Response
{
try {
$params = $this->request->post();
$list = InvoiceIssuanceService::getListPage($params,$this->request->wechat_user_id);
return $this->buildSuccess($list);
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::UPDATE_FAILED, $e->getMessage());
}
}
public function add()
{
@ -71,7 +90,7 @@ class InvoiceIssuance extends Base
return $this->buildSuccess();
} catch (\Exception $e) {
Db::rollback();
return $this->buildFailed(ReturnCode::INVALID, $e->getMessage());
return $this->buildFailed(ReturnCode::UPDATE_FAILED, $e->getMessage());
}
}
@ -88,7 +107,7 @@ class InvoiceIssuance extends Base
return $this->buildSuccess();
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::INVALID, $e->getMessage());
return $this->buildFailed(ReturnCode::DELETE_FAILED, $e->getMessage());
}
}
}

41
app/controller/wechat/Wechat.php

@ -4,7 +4,9 @@ namespace app\controller\wechat;
use app\model\WechatPucode;
use app\model\WechatSms;
use app\model\WechatUser;
use app\service\user\SmsService;
use app\service\webService\FeeService;
use app\validate\WechatUser as WechatUserValidate;
use app\service\wechat\WechatService;
@ -38,6 +40,37 @@ class Wechat extends Base
return $this->buildFailed(400,'微信认证失败!');
}
public function sendSms(Request $request)
{
$param = $request->param();
$wechat_user_id = $request->wechat_user_id;
try {
validate(WechatUserValidate::class)->scene('smsCode')->check($param);
$create_time = WechatSms::getValidUser($wechat_user_id);
if ($create_time) {
$time = time() - $create_time;
throw new \Exception('请在' . $time . '秒后再次发送');
}
$code = WechatSms::getCode($wechat_user_id);
$addResult = WechatSms::addSmsCode($request->wechat_user_id,$code);
if (!$addResult) {
throw new \Exception('发送失败');
}
$SmsService = new SmsService();
$SmsService->sendSms($param['mobile'],$SmsService->smsCodeTemplate($code));
return $this->buildSuccess();
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::INVALID,$e->getMessage());
}
}
/**
* 用户添加手机号用户编号
* @param Request $request
@ -50,10 +83,13 @@ class Wechat extends Base
$param = $request->param();
// 验证数据
$defaultSnsCode = '439764';
$wechat_user_id = $request->wechat_user_id;
//$defaultSnsCode = '439764';
try {
validate(WechatUserValidate::class)->scene('savePuCode')->check($param);
if ($param['snsCode'] != $defaultSnsCode) {
$res = WechatSms::getValidCode($param['snsCode'],$wechat_user_id);
if (!$res) {
throw new \Exception('短信验证码错误');
}
} catch (ValidateException $e) {
@ -76,7 +112,6 @@ class Wechat extends Base
throw new \Exception('手机号或用户编号错误');
}
$wechat_user_id = $request->wechat_user_id;
$userData = ['phone' => $mobile];
$WechatUser = WechatUser::update($userData,['id' => $wechat_user_id]);

2
app/model/InvoiceHead.php

@ -7,6 +7,8 @@ use think\Model;
class InvoiceHead extends Model
{
public static $typeArr = ['单位', '个人'];
public static function createHead($wechat_user_id, $data)
{

4
app/model/InvoiceIssuance.php

@ -7,4 +7,8 @@ use think\Model;
class InvoiceIssuance extends Model
{
public static $projectArr = ['', '垃圾费', '污水费', '游泳'];
public static $statusArr = ['作废', '申请中', '已开票'];
}

60
app/model/WechatSms.php

@ -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);
}
}

50
app/service/invoice/InvoiceIssuanceService.php

@ -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();
}
}

95
app/service/user/SmsService.php

@ -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);
}
}

2
app/validate/InvoiceIssuanceValidate.php

@ -10,7 +10,7 @@ class InvoiceIssuanceValidate extends Validate
* 验证规则.
*/
protected $rule = [
'project_id|开票项目' => 'require|number',
'project_id|开票项目' => 'require|number|in:1,2,3',
'merge|合并开票' => 'require|number',
'pucode_id|用户编号' => 'require|number',
'expire_time|到期时间' => 'require',

5
app/validate/WechatUser.php

@ -12,7 +12,7 @@ class WechatUser extends Validate
protected $rule = [
'mobile|手机号' => 'require|mobile',
'pucode|用户编号' => 'require|number',
'snsCode|短信验证码' => 'require',
'snsCode|短信验证码' => 'require|length:6',
];
/**
@ -31,7 +31,8 @@ class WechatUser extends Validate
* 验证场景.
*/
protected $scene = [
'savePuCode' => ['mobile', 'pucode', 'snsCode']
'savePuCode' => ['mobile', 'pucode', 'snsCode'],
'smsCode' => ['mobile']
];
}

3
route/apiRoute.php

@ -6,9 +6,10 @@
use think\facade\Route;
Route::group('api', function() {
// 微信验证路由地址
// 发票管理地址
Route::rule('InvoiceIssuance/add', 'api.InvoiceIssuance/add', 'post')->middleware(\app\middleware\WechatAuth::class);
Route::rule('InvoiceIssuance/delete', 'api.InvoiceIssuance/delete', 'post')->middleware(\app\middleware\WechatAuth::class);
Route::rule('InvoiceIssuance/getList', 'api.InvoiceIssuance/getList', 'post')->middleware(\app\middleware\WechatAuth::class);
// 测试FeeService 接口
Route::rule('Test/index', 'api.Test/index', 'get');
//MISS路由定义

3
route/wechatRoute.php

@ -4,6 +4,7 @@
*/
use think\facade\Route;
use app\middleware\WechatAuth;
Route::group('wechat', function() {
// 微信验证路由地址
@ -12,5 +13,7 @@ Route::group('wechat', function() {
Route::rule('Login/getWxCode', 'wechat.Login/getWxCode', 'get');
Route::rule('Login/wx', 'wechat.Login/wx', 'get');
Route::rule('Wechat/index', 'wechat.Wechat/index', 'get');
Route::rule('Wechat/savePuCode', 'wechat.Wechat/savePuCode', 'post')->middleware(WechatAuth::class);
Route::rule('Wechat/sendSms', 'wechat.Wechat/sendSms', 'post')->middleware(WechatAuth::class);
});

Loading…
Cancel
Save