8000 添加小程序端模板和公众号端模板 · lpzhi/laravel_template_with_vue@cab6af5 · GitHub
[go: up one dir, main page]

Skip to content

Commit cab6af5

Browse files
committed
添加小程序端模板和公众号端模板
1 parent b937acd commit cab6af5

24 files changed

+1054
-8
lines changed

api/.env.example

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,26 @@ PUSHER_APP_CLUSTER=mt1
4444

4545
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
4646
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
47+
48+
49+
## 微信服务号配置
50+
WECHAT_OFFICIAL_ACCOUNT_APPID=
51+
WECHAT_OFFICIAL_ACCOUNT_SECRET=
52+
WECHAT_OFFICIAL_ACCOUNT_TOKEN=
53+
WECHAT_OFFICIAL_ACCOUNT_AES_KEY=
54+
55+
56+
JWT_SECRET=5P3U1ZKauzoCErvTTphe92tEdDo1hym82XloZ38kgmtN8mgxINGoEzMr26BNRR1u
57+
58+
59+
## 微信支付配置
60+
WECHAT_PAYMENT_APPID=
61+
WECHAT_PAYMENT_MCH_ID=
62+
WECHAT_PAYMENT_KEY=
63+
WECHAT_PAYMENT_CERT_PATH=D:\wmhello.mynatapp.cc\wx\cert\apiclient_cert.pem
64+
WECHAT_PAYMENT_KEY_PATH=D:\wmhello.mynatapp.cc\wx\cert\apiclient_key.pem
65+
WECHAT_PAYMENT_NOTIFY_URL=
66+
67+
## 微信小程序配置
68+
WECHAT_MINI_PROGRAM_APPID=
69+
WECHAT_MINI_PROGRAM_SECRET=

api/app/Http/Controllers/Admin/LoginController.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
use GuzzleHttp\Client;
1414
use Illuminate\Http\Request;
1515
use Faker\Factory;
16+
//use SGH\PdfBox\PdfBox;
17+
use Pdfbox\Driver\Pdfbox;
18+
use Pdfbox\Processor\PdfFile;
19+
use Psr\Log\NullLogger;
20+
21+
use PhpParser\Parser;
1622
/**
1723
* @group 管理员登陆管理
1824
* 管理员登陆、退出、刷新和获取个人信息
@@ -24,7 +30,43 @@ class LoginController extends Controller
2430
use Tool;
2531
public function __construct()
2632
{
27-
$this->middleware('auth:api', ['except' => ['login', 'refresh', 'loginByPhone', 'captcha']]);
33+
$this->middleware('auth:api', ['except' => ['login', 'refresh', 'loginByPhone', 'captcha', 'test']]);
34+
}
35+
36+
public function test()
37+
{
38+
$jarFile = public_path('/pdf/pdfbox-app-2.0.24.jar');
39+
$javaPath = 'C:/Program Files/Java/jdk-17.0.1/bin/java.exe';
40+
$pdfFile = public_path('1.pdf');
41+
42+
$parser = new \Smalot\PdfParser\Parser();
43+
$pdf = $parser->parseFile($pdfFile);
44+
$text = $pdf->getDetails();; //将所有内容读取到一个字符串中
45+
foreach ($text as $property => $value) {
46+
if (is_array($value)) {
47+
$value = implode(', ', $value);
48+
}
49+
echo $property . ' => ' . $value . "\n";
50+
echo '<br>';
51+
}
52+
53+
54+
55+
// $converter = new PdfBox;
56+
// $converter->setPathToPdfBox($jarFile);
57+
// $text = $converter->textFromPdfFile($pdfFile);
58+
// echo $text;
59+
60+
// 可以实现
61+
62+
$file = new PdfFile(
63+
new Pdfbox(
64+
$javaPath,
65+
$jarFile,
66+
new NullLogger()
67+
));
68+
echo $file->toText($pdfFile);
69+
2870
}
2971

3072

api/app/Http/Controllers/MP/LoginController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ public function getInfo(Request $request)
8484
return $result;
8585
}
8686

87-
public function getPhone(Request $request)
87+
public function getRun(Request $request)
8888
{
89+
8990
$sessionKey = $request->input('session_key');
9091
$iv = $request->input('iv', '');
9192
$encryptedData = $request->input('encryptedData');
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Wx;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Foundation\Bus\DispatchesJobs;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Routing\Controller as BaseController;
9+
use Illuminate\Foundation\Validation\ValidatesRequests;
10+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
11+
use Illuminate\Support\Facades\Validator;
12+
use Rap2hpoutre\FastExcel\Facades\FastExcel;
13+
14+
class Controller extends BaseController
15+
{
16+
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
17+
use Tool;
18+
use Validate;
19+
20+
/**
21+
*
22+
* 1.接收文件,打开数据
23+
* 2. 处理打开的数据,循环转换
24+
* 3. 导入到数据库
25+
* @return \Illuminate\Http\JsonResponse
26+
*/
27+
28+
public function import()
29+
{
30+
31+
$action = request()->input("action", "import");
32+
switch ($action) {
33+
case 'download':
34+
$result = [];
35+
$result [] = [
36+
"昵称" => "xpyzwm",
37+
"登录名" => "xpyzwm",
38+
"电话号码" => "13577728948",
39+
"角色" => 'user'
40+
];
41+
$list = collect($result);
42+
// 直接下载
43+
return (FastExcel::data($list))->download('template.xlsx');
44+
break;
45+
default:
46+
$data = FastExcel::import(request()->file('file'));
47+
$arrData = $data->toArray();
48+
$arr = $this->importHandle($arrData);
49+
$this->model::insert($arr['successData']);
50+
$tips = '当前操作导入数据成功' . $arr['successCount'] . '';
51+
if ($arr['isError']) {
52+
// 有失败的数据,无法插入,要显示出来,让前端能下载
53+
$file = time() . '.xlsx';
54+
$fileName = public_path('xls') . '\\' . $file;
55+
$file = 'xls\\' . $file;
56+
$data = collect($arr['errorData']);
57+
(new FastExcel($data))->export($fileName);
58+
$tips .= ',失败' . $arr['errorCount'] . '';
59+
return response()->json([
60+
'info' => $tips,
61+
'fileName' => $file,
62+
'status' => 'error',
63+
'status_code' => 422
64+
], 422);
65+
} else {
66+
return $this->successWithInfo($tips, 201);
67+
}
68+
}
69+
}
70+
71+
/**
72+
* 1. 要对每一条记录进行校验
73+
* 2. 根据校验的结果,计算出可以导入的条数,以及错误的内容
74+
* @param $arrData
75+
* @return array
76+
*/
77+
protected function importHandle($arrData){
78+
$error = []; // 错误的具体信息
79+
$isError = false; // 是否存在信息错误
80+
$successCount = 0; // 统计数据导入成功的条数
81+
$errorCount = 0; // 出错的条数
82+
$arr = []; // 正确的内容存储之后,返回数据
83+
foreach ($arrData as $key => $item) {
84+
$data = $this->handleItem($item, 'import');
85+
$data['created_at'] = Carbon::now();
86+
// 可以根据需要,进一步处理数据
87+
$this->validatorData($item,$data,$error, $isError ,$successCount, $errorCount,$arr);
88+
}
89+
return [
90+
'successData' => $arr,
91+
'errorData' => $error,
92+
'isError' => $isError,
93+
'errorCount' => $errorCount,
94+
'successCount' => $successCount,
95+
];
96+
}
97+
98+
protected function validatorData($item, $data, &$error, &$isError ,&$successCount, &$errorCount,&$arr){
99+
if (method_exists($this, 'message')){
100+
$validator = Validator::make($data,$this->storeRule(),$this->message());
101+
} else {
102+
$validator = Validator::make($data,$this->storeRule());
103+
}
104+
if ($validator->fails()){
105+
// 获取相关的错误信息,并且把错误信息单独存放
106+
$errors = $validator->errors($validator);
107+
$tips = '';
108+
foreach ($errors->all() as $message){
109+
$tips .= $message.',';
110+
}
111+
$tips = substr($tips,0,strlen($tips)-1);
112+
// 状态信息
113+
$item['错误原因'] = $tips;
114+
$error[] = $item;
115+
$isError = true;
116+
$errorCount ++;
117+
} else {
118+
// 没有出错的,我们先存在正确的数组
119+
$arr[] = $data;
120+
$successCount ++;
121+
}
122+
}
123+
124+
protected function storeRule(){
125+
return [];
126+
}
127+
128+
protected function UpdateRule($id){
129+
return [];
130+
}
131+
132+
133+
// protected function message(){
134+
// return [];
135+
// }
136+
137+
138+
139+
140+
}

api/app/Http/Controllers/Wx/Tool.php

Lines changed: 87 additions & 0 deletions
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
4+
namespace App\Http\Controllers\Wx;
5+
6+
7+
use Illuminate\Support\Facades\Storage;
8+
9+
trait Tool
10+
{
11+
protected function success()
12+
{
13+
return response()->json([
14+
'status' => 'success',
15+
'status_code' => 200
16+
], 200);
17+
}
18+
19+
protected function successWithInfo($msg = '操作成功', $code = 200)
20+
{
21+
return response()->json([
22+
'info' => $msg,
23+
'status' => 'success',
24+
'status_code' => $code
25+
], $code);
26+
}
27+
28+
protected function successWithData($data = [], $code = 200)
29+
{
30+
return response()->json([
31+
'data' => $data,
32+
'status' => 'success',
33+
'status_code' => $code
34+
], $code);
35+
}
36+
37+
protected function error()
38+
{
39+
return response()->json([
40+
'status' => 'error',
41+
'status_code' => 404
42+
], 404);
43+
}
44+
45+
protected function errorWithInfo($msg = '操作失败', $code = 404)
46+
{
47+
return response()->json([
48+
'info' => $msg,
49+
'status' => 'error',
50+
'status_code' => $code
51+
], $code);
52+
}
53+
54+
protected function errorWithData($data = [], $code = 404)
55+
{
56+
return response()->json([
57+
'data' => $data,
58+
'status' => 'error',
59+
'status_code' => $code
60+
], $code);
61+
}
62+
63+
protected function receiveFile()
64+
{
65+
$file = request()->file('file');
66+
if ($file->isValid()) {
67+
$domain = getDomain();
68+
$fileName = $file->store('', 'public');
69+
$file = Storage::url($fileName);
70+
$path = $domain.$file;
71+
return $path;
72+
} else {
73+
return '';
74+
}
75+
}
76+
77+
protected function deleteAvatar($url) {
78+
$domain = getDomain();
79+
$path = $domain.'/storage/';
80+
$file = str_replace($path, '', $url);
81+
$address = storage_path('app/public');
82+
$fileName = $address.'/'.$file;
83+
if (file_exists($fileName)) {
84+
unlink($fileName);
85+
}
86+
}
87+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
4+
namespace App\Http\Controllers\Wx;
5+
use Illuminate\Support\Facades\Validator;
6+
7+
trait Validate
8+
{
9+
10+
/**
11+
* 新增数据校验,如果校验通过,则显示为true,否则返回错误信息
12+
* @param $data
13+
* @return bool|string
14+
*/
15+
public function validateStore($data)
16+
{
17+
18+
$validator = Validator::make($data, $this->storeRule(), $this->message());
19+
if ($validator->fails()) {
20+
$errors = $validator->errors()->toArray();
21+
$msg = $this->tranMessage($errors);
22+
return $msg;
23+
}else {
24+
return true;
25+
}
26+
27+
}
28+
29+
public function validateUpdate($data, $id)
30+
{
31+
32+
$validator = Validator::make($data, $this->updateRule($id), $this->message());
33+
if ($validator->fails()) {
34+
$errors = $validator->errors()->toArray();
35+
$msg = $this->tranMessage($errors);
36+
return $msg;
37+
}else {
38+
return true;
39+
}
40+
41+
}
42+
43+
/**
44+
* 错误信息转换
45+
* @param $errors
46+
* @return string
47+
*/
48+
protected function tranMessage($errors)
49+
{
50+
$tips = '';
51+
foreach ($errors as $k => $v) {
52+
foreach ($v as $v1) {
53+
$tips .= $v1.',';
54+
}
55+
}
56+
$end = strrpos($tips,',');
57+
$tips = substr($tips, 0, $end);
58+
return $tips;
59+
}
60+
}

0 commit comments

Comments
 (0)
0