TaskSystem/app/api/controller/IndexController.php
2023-09-13 19:16:10 +08:00

590 lines
26 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\api\logic\IndexLogic;
use app\common\logic\RedisLogic;
use app\common\model\Company;
use app\common\model\company\CompanyProperty;
use app\common\model\contract\Contract;
use app\common\model\contract\ShopContract;
use app\common\model\contract\VehicleContract;
use app\common\model\ShopMerchant;
use app\common\model\vehicle\VehicleRent;
use think\cache\driver\Redis;
use think\facade\Db;
use think\facade\Log;
use think\model\Pivot;
use think\response\Json;
/**
* index
* Class IndexController
* @package app\api\controller
*/
class IndexController extends BaseApiController
{
public array $notNeedLogin = ['index', 'config', 'policy', 'decorate', 'notifyUrl', 'notifyProperty', 'notifyAuthentication', 'notifyVehicleContractUpdate','townCarRent','systemCarRent', 'selfCarRent', 'cancelRent'];
/**
* @notes 首页数据
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/9/21 19:15
*/
public function index()
{
$result = IndexLogic::getIndexData();
return $this->data($result);
}
/**
* @notes 全局配置
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/9/21 19:41
*/
public function config()
{
$parmas = $this->request->param();
$group_id = $this->userInfo ? $this->userInfo['group_id'] : 1;
$result = IndexLogic::getConfigData($group_id, $parmas);
return $this->data($result);
}
/**
* @notes 政策协议
* @return Json
* @author 段誉
* @date 2022/9/20 20:00
*/
public function policy()
{
$type = $this->request->get('type/s', '');
$result = IndexLogic::getPolicyByType($type);
return $this->data($result);
}
/**
* @notes 装修信息
* @return Json
* @author 段誉
* @date 2022/9/21 18:37
*/
public function decorate()
{
$id = $this->request->get('id/d');
$result = IndexLogic::getDecorate($id);
return $this->data($result);
}
/**签约回调 */
public function notifyUrl()
{
$a = Request()->get();
if ($a) {
$find = Db::name('contract')->where('id', $a['id'])->find();
if ($find) {
if ($find['signing_timer'] == 0) {
Db::name('contract')->where('id', $a['id'])->update(['signing_timer' => 1]);
return true;
} else if ($find['signing_timer'] == 1) {
Db::name('contract')->where('id', $a['id'])->update(['status' => 1, 'signing_timer' => 2]);
if ($find['type'] == 1) {
Db::name('company')->where('id', $find['party_a'])->update(['status' => 1, 'is_contract' => 1]);
Db::name('company')->where('id', $find['party_b'])->update(['status' => 1, 'is_contract' => 1]);
} else {
Db::name('company')->where('id', $find['party_a'])->update(['status' => 1, 'is_contract' => 1]);
Db::name('user')->where('id', $find['party_b'])->update(['is_contract' => 1]);
}
}
}
}
return json(['success' => true, 'msg' => '成功']);
}
/**商户合同签约回调 */
public function shopContractNotifyUrl()
{
$a = Request()->get();
Log::info(['商户合同签约回调', $a]);
// 获取合同详情
if ($a) {
$find = Db::name('contract')->where('id', $a['id'])->find();
if ($find) {
// 合同一方已签约
if ($find['signing_timer'] == 0) {
Db::name('shop_contract')->where('id', $a['id'])->update(['signing_timer' => 1]);
return true;
} else if ($find['signing_timer'] == 1) {
// 合同另一方已签约,修改合同状态,修改公司状态
Db::name('shop_contract')->where('id', $a['id'])->update(['status' => 1, 'signing_timer' => 2]);
Db::name('shop_merchant')->where('id', $find['party_a'])->update(['status' => 1, 'is_contract' => 1]);
Db::name('shop_merchant')->where('id', $find['party_b'])->update(['status' => 1, 'is_contract' => 1]);
// todo 通知商城,商户签约完成
}
}
}
return json(['success' => true, 'msg' => '成功']);
}
//镇街车辆租赁回调
public function townCarRent() {
$id = Request()->get('id');
if(empty($id)){
return json(['success' => false, 'msg' => '缺少参数']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '获取数据失败']);
}
if($contract['status'] != 2){
return json(['success' => false, 'msg' => '合同状态错误']);
}
if ($contract['signing_timer'] == 0) {
//更新本地合同状态
$updateLocalRes = VehicleContract::where('id',$contract['id'])->update(['signing_timer'=>1]);
//更新远程
$updateSverRes =curl_post(env('project.logistic_domain').'/api/contractUpdate',[],[
'id' => $contract['contract_logistic_id'],
'signing_timer' => 1,
]);
if(!$updateLocalRes || $updateSverRes['code']==0){
return json(['success' => false, 'msg' => '更新失败']);
}
return json(['success' => true, 'msg' => '成功']);
}else if($contract['signing_timer'] == 1){
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
//更新本地合同状态
$updateLocalRes = VehicleContract::where('id',$contract['id'])->update(['signing_timer'=>2,'status' => 3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
//将车辆加入到本地租赁列表
$cars = json_decode($contract['cars_info'], true);
$data = [];
foreach ($cars as $k => $v) {
$hasCar = VehicleRent::where('car_id',$v['id'])->findOrEmpty();
if($hasCar->isEmpty()){
$data[$k]['car_id'] = $v['id'];
$data[$k]['car_license'] = $v['license'];
$data[$k]['type'] = 0;
$data[$k]['status'] = 0;
$data[$k]['company_id'] = $contract['company_b_id'];
$data[$k]['rent_time'] = 0;
$data[$k]['rent_company_id'] = 0;
$data[$k]['contract_id'] = $contract['id'];
$data[$k]['create_time'] = strtotime($contract['create_time']);
}
}
$vehicleRent = new VehicleRent();
$vehicleRent->saveAll($data);
//更新远程
$updateSverRes = curl_post(env('project.logistic_domain').'/api/contractUpdate',[],[
'id' => $contract['contract_logistic_id'],
'signing_timer' => 2,
'status' => 3,
'contract_url'=>$signContractFile,
'contract_evidence'=>$contractEvidence
]);
if(!$updateLocalRes || $updateSverRes['code']==0){
return json(['success' => false, 'msg' => '更新失败']);
}else{
return json(['success' => true, 'msg' => '更新成功']);
}
}
return json(['success' => true, 'msg' => '更新成功@']);
}
//系统车辆租赁回来
public function systemCarRent()
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败1.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败1.2']);
}
if ($contract['type'] != 0) {
return json(['success' => false, 'msg' => '失败1.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败1.4']);
}
//判断签约方
if ($contract['signing_timer'] == 0) {
//更新合同的签约次数
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败1.5']);
}
return json(['success' => true, 'msg' => '成功']);
} else if ($contract['signing_timer'] == 1) {
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
//更改合同状态
VehicleContract::where('id', $id)->update(['signing_timer' => 2, 'status' => 3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
//添加车辆到租赁列表
$vehicle = json_decode($contract['cars_info'], true);
VehicleRent::where('car_id', $vehicle['id'])->update([
'status' => 2,
'rent_time' => time(),
'rent_company_id' => $contract['company_b_id'],
]);
$party_b = Company::where('id', $contract['company_b_id'])->find();
//通知物流系统跟新
curl_post(env('project.logistic_domain').'/api/updateVehicleRent', [], [
'car_id' => $vehicle['id'],
'use_user_id' => $party_b['user_id'],
'use_user_name' => $party_b['master_name'],
'use_user_phone' => $party_b['master_phone']
]);
CompanyProperty::create([
'company_id' => $contract['company_b_id'],
'object_id' => $vehicle['id'],
'type' => 1,
'create_time' => time(),
'update_time' => time(),
]);
return json(['success' => true, 'msg' => '成功']);
} else {
return json(['success' => true, 'msg' => '成功']);
}
}
//自有车辆租赁回答
public function selfCarRent()
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败2.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败2.2']);
}
if ($contract['type'] != 1) {
return json(['success' => false, 'msg' => '失败2.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败2.4']);
}
//判断签约方
if ($contract['signing_timer'] == 0) {
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败2.5']);
}
return json(['success' => true, 'msg' => '成功']);
} else if ($contract['signing_timer'] == 1) {
//添加车辆到物流系统
$vehicle = json_decode($contract['cars_info'], true);
$curl_res = curl_post(env('project.logistic_domain').'/api/addSelfCar', [], [
'license' => $vehicle['license'],
'pic' => $vehicle['pic'],
'company_id' => $contract['company_a_id'],
'company_name' => $contract['company_a_name'],
'company_user' => $contract['company_a_user'],
'company_phone' => $contract['company_a_phone'],
]);
if (!$curl_res || $curl_res['code'] == 0) {
return json(['success' => false, 'msg' => '失败2.6']);
}
$cars_info = json_encode(['id' => $curl_res['data']['car_id'], 'license' => $vehicle['license']]);
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
VehicleContract::where('id', $id)->update(['cars_info'=>$cars_info,'update_time'=>time(),'signing_timer'=>2,'status'=>3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
VehicleRent::create([
'car_id' => $curl_res['data']['car_id'],
'contract_id' => $contract['id'],
'company_id' => $contract['company_a_id'],
'car_license' => $vehicle['license'],
'status' => 2,
'rent_time' => time(),
'rent_company_id' => $contract['company_b_id'],
'create_time' => time(),
'type' => 1
]);
$car_id = $curl_res['data']['car_id'];
$party_b = Company::where('id', $contract['company_b_id'])->find();
//通知物流系统跟新
curl_post(env('project.logistic_domain').'/api/updateVehicleRent', [], [
'car_id' => $car_id,
'use_user_id' => $party_b['user_id'],
'use_user_name' => $party_b['master_name'],
'use_user_phone' => $party_b['master_phone']
]);
CompanyProperty::create([
'company_id' => $contract['company_b_id'],
'object_id' => $car_id,
'type' => 1,
'create_time' => time(),
'update_time' => time(),
]);
return json(['success' => true, 'msg' => '成功']);
} else {
return json(['success' => true, 'msg' => '成功']);
}
}
//解除合同回调
public function cancelRent()
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败3.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败3.2']);
}
if ($contract['type'] != 2) {
return json(['success' => false, 'msg' => '失败3.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败3.4']);
}
if ($contract['signing_timer'] == 0) {
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败3.5']);
}
return json(['success' => true, 'msg' => '成功']);
} else if ($contract['signing_timer'] == 1) {
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
//更改合同状态
VehicleContract::where('id', $id)->update(['signing_timer'=>2,'status'=>3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
//更改租赁列表车辆状态
$vehicle = json_decode($contract['cars_info'], true);
//获取租赁车辆信息
$vehicleRentInfo = VehicleRent::where('car_id', $vehicle['id'])->find();
//更新原始合同类型
VehicleContract::where('id', $vehicleRentInfo['contract_id'])->update(['status' => 6]);
VehicleRent::where('car_id', $vehicle['id'])->update([
'status' => 3,
]);
//通知物流系统跟新
curl_post(env('project.logistic_domain').'/api/cancelRent', [], [
'car_id' => $vehicle['id'],
'status' => 1
]);
return json(['success' => true, 'msg' => '成功']);
} else {
return json(['success' => true, 'msg' => '成功']);
}
}
//车辆租赁签约合同状态更改
public function notifyVehicleContractUpdate()
{
if (!$this->request->isPost()) {
return $this->fail('请求方式错误');
}
$param = $this->request->post();
if (empty($param['contract_logistic_id'])) {
return $this->fail('缺少必要参数');
}
$model = VehicleContract::where('contract_logistic_id', $param['contract_logistic_id'])->find();
if (empty($model)) {
return $this->fail('数据不存在');
}
$cars = json_decode($model['cars_info'], true);
$result = $model->where('id', $model['id'])->save($param);
if ($result && isset($param['status']) && $param['status'] == 3) {
$data = [];
foreach ($cars as $k => $v) {
$hasCar = VehicleRent::where('car_id',$v['id'])->findOrEmpty();
if($hasCar->isEmpty()) {
$data[$k]['car_id'] = $v['id'];
$data[$k]['car_license'] = $v['license'];
$data[$k]['type'] = 0;
$data[$k]['status'] = 0;
$data[$k]['company_id'] = $model['company_b_id'];
$data[$k]['rent_time'] = 0;
$data[$k]['rent_company_id'] = 0;
$data[$k]['contract_id'] = $model['id'];
$data[$k]['create_time'] = strtotime($model['create_time']);
}
}
$vehicleRent = new VehicleRent();
$vehicleRent->saveAll($data);
}
//返回
return $result ? $this->success('更新成功') : $this->fail('更新失败');
}
/**
* 资产回调
*/
public function notifyProperty()
{
$parmas = Request()->post();
if ($parmas) {
if (!isset($parmas['company_id']) || $parmas['company_id'] < 0) {
return json(['success' => false, 'msg' => '公司id不能为空']);
}
if (!isset($parmas['object_id'])) {
return json(['success' => false, 'msg' => '对象id不能为空']);
}
$data = [];
$object_id = explode(',', $parmas['object_id']);
foreach ($object_id as $k => $v) {
if ($v > 0) {
$data[$k]['company_id'] = $parmas['company_id'];
$data[$k]['object_id'] = $v;
$data[$k]['type'] = 1;
}
}
if (count($data) <= 0) {
return json(['success' => false, 'msg' => '失败']);
}
$res = CompanyProperty::insertAll($data);
if ($res) {
return json(['success' => true, 'msg' => '成功']);
} else {
return json(['success' => false, 'msg' => '失败']);
}
}
return json(['success' => false, 'msg' => '失败,没有参数']);
}
/**
* 认证回调
*/
public function notifyAuthentication()
{
$parmas = Request()->param();
Log::info(['认证回调:',$parmas]);
try {
if ($parmas) {
// 商城系统商户入驻
if (isset($parmas['type']) && $parmas['type'] == 'shop_merchant') {
$this->shopMerchantCall($parmas);
return json(['success' => true, 'msg' => '成功']);
} else {
$data=json_decode($parmas['data'],true);
// 成功回调标记
$redis = RedisLogic::getInstance();
$cache = $redis->get('authentication_company_id_'.$parmas['id']);
if (!empty($cache)) {
$cacheData = json_decode($cache, true);
$cacheData['is_callback'] = 1;
$redis->set('authentication_company_id_'.$parmas['id'], json_encode($cacheData));
}
if($data['status']==1){
Company::where('id', $parmas['id'])->update(['is_authentication' => 1]);
}
if($data['status']==2){
// 记录认证失败原因
Db::name('company_authentication_fail_log')->insert(['company_id'=>$parmas['id'],'fail_reason'=>$data['msg'],'create_time'=>time()]);
Company::where('id', $parmas['id'])->update([ 'is_contract'=>0]);
}
return json(['success' => true, 'msg' => '成功']);
}
}
} catch (\Exception $e) {
Log::error('认证回调错误:'.$e->getMessage());
}
return json(['success' => false, 'msg' => '失败,没有参数']);
}
private function shopMerchantCall($parmas)
{
$shopMerchant = ShopMerchant::find(['id', $parmas['id']]);
$data=json_decode($parmas['data'],true);
if($data['status']==1){
$shopMerchant->save(['is_authentication' => 1]);
// 生成合同
$createContractData = [
'id' => $parmas['id'],
'party_a' => 1,
'party_a_name' => '泸州市海之农科技有限公司',
'party_b' => $parmas['id'],
'party_b_name' => $shopMerchant->company_name,
'contract_type' => 22,
];
$model = new ShopContract();
$model->contract_no = time();
$model->create_time = time();
$model->check_status = 1;
$model->update_time = time();
$model->setAttrs($createContractData);
$res = $model->save($data);
}
if($data['status']==2){
// 记录认证失败原因
Db::name('company_authentication_fail_log')->insert(['company_id'=>$parmas['id'], 'log_type'=>3, 'fail_reason'=>$data['msg'], 'create_time'=>time()]);
}
}
}