Mini.php
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* Trait类 微信公众号
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Factories\Wechat
* @package App\Factories\Wechat
* @author Richer <yangzi1028@163.com>
* @date 2021年5月6日10:44:56
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.xxxxxx.com License
* @link http://www.xxxxxx.com
*/
namespace App\Factories\Wechat;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Storage;
use Yansongda\LaravelPay\Facades\Pay;
use Yansongda\Pay\Log;
/**
* Trait Mini
*
* @category App\Factories\Wechat
* @package App\Factories\Wechat
* @author Richer <yangzi1028@163.com>
* @date 2021年5月6日10:44:56
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.xxxxxx.com License
* @link http://www.xxxxxx.com
*/
trait Mini
{
/**
* @param null $config
* @return $this
*/
public function setMiniConfigure($config = null)
{
if ($config) {
$this->mini = Factory::miniProgram($config);
}
return $this;
}
/**
* 通过code获取用户session信息
*
* @param $code
* @return mixed
*/
public function loginMini($code)
{
return $this->mini->auth->session($code);
}
/**
* 比如获取电话等功能,信息是加密的,需要解密。
*
* @param $session
* @param $iv
* @param $encryptedData
* @return mixed
*/
public function getEncryptedData($session, $iv, $encryptedData)
{
try {
return $decryptedData = $this->mini->encryptor->decryptData($session, $iv, $encryptedData);
} catch (\Exception $e) {
$this->errMsg = $e->getMessage();
Log::info('错误信息:' . $this->errMsg);
return false;
}
}
/**
* 账户转账
*/
public function transfer($partner_trade_no, $openid, $amount = 0, $desc = '帐户提现')
{
$order = [
'partner_trade_no' => $partner_trade_no, //商户订单号
'openid' => $openid, //收款人的openid
'check_name' => 'NO_CHECK', //NO_CHECK:不校验真实姓名\FORCE_CHECK:强校验真实姓名
// 're_user_name'=>'张三', //check_name为 FORCE_CHECK 校验实名的时候必须提交
'amount' => $amount, //企业付款金额,单位为分
'desc' => $desc, //付款说明
'type' => 'miniapp', // 如果您需要通过 APP/小程序 的商户账号appid进行转账,请传入参数:['type' => 'app']/['type' => 'miniapp']
'spbill_create_ip' => request()->ip(), // 如果您在队列中使用,请自行传参 spbill_create_ip。
];
$result = Pay::wechat()->transfer($order);
}
/**
* 生成小程序码,并且携带用参数
*
* @param int $user_id 参数值
* @param string $page 页面路径
* @return string
*/
public function generateMiniProgramCode($user_id, $page = 'pages/index/index')
{
// 定义参数
$scene = 'inviter_id='.$user_id;
$result = $this->mini->app_code->getUnlimit($scene, [
'page' => $page,
]);
$filename = $user_id . '.png'; // 文件名
Storage::disk('mini_program_code')->put($filename, $result); // 存储到本地
return $url = Storage::disk('mini_program_code')->url($filename); // 获取 URL;
// return response($result, 200, [
// 'Content-Type' => 'image/png',
// ]);
}
}