Qcode.php
5.6 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Created by PhpStorm.
* User: 86132
* Date: 2020/6/20
* Time: 8:46
*/
namespace app\api\controller;
use EasyWeChat\Foundation\Application;
use app\common\controller\Api;
class Qcode extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/**
* 我的接口
*/
public function index()
{
$this->success('', ['welcome' => $this->auth->nickname]);
}
/**
* @ApiTitle (商家接口-商家收款码)
* @ApiSummary (商家收款码)
* @ApiMethod (POST)
* @ApiRoute (/api/Qcode/seller_code)
* @ApiParams (name="id", type="int", required=true, description="店铺ID")
* @ApiParams (name="url", type="string", required=true, description="买单页URL")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data":{
"url":"携带[买单页URL+店铺ID]的收款码/地址"
}
})
*/
public function seller_code()
{
$domain = $this->request->domain();
$param = $this->request->param();
$id=input('id');
$is_invite_code = './images2/'.'seller_'.$id.'.png';
if (file_exists($is_invite_code) == true) {
$return = [
'url' => $domain . './images2/'.'seller_'.$id.'.png'
];
$this->success('成功', $return);
}
$options = [
'app_id' => 'wx9fff7b42aede19e4', // AppID
'secret' => '97f23d67dc029b2b2244637e7b47bb8b', // AppSecret
];
$app = new Application($options);
// 获取 access token 实例
$accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
$token = $accessToken->getToken(); // token 字符串
//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
//width是二维码宽度
$qcode = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$token";
$param = json_encode(array("path" => $param['url'] . "?id=" . $id, "width" => 150));
//POST参数
$result = $this->httpRequest($qcode, $param, "POST");
//生成二维码
file_put_contents('./images2/'.'seller_'.$id.'.png', $result);
$return = [
'url' => $domain . './images2/'.'seller_'.$id.'.png'
];
$this->success('成功', $return);
}
/**
* @ApiTitle (商家接口-邀请码)
* @ApiSummary (商家收款码)
* @ApiMethod (POST)
* @ApiRoute (/api/Qcode/invite_code)
* @ApiParams (name="id", type="int", required=true, description="openid")
* @ApiParams (name="url", type="string", required=true, description="url")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data":{
"url":"邀请海报"
}
})
*/
public function invite_code()
{
$domain = $this->request->domain();
$param = $this->request->param();
$id=input('openid');
$is_invite_code = './images/'.'user_'.$id.'.png';
if (file_exists($is_invite_code) == true) {
$return = [
'url' => $domain . '/images/'.'user_'.$id.'.png'
];
$this->success('成功', $return);
}
$options = [
'app_id' => 'wx9fff7b42aede19e4', // AppID
'secret' => '97f23d67dc029b2b2244637e7b47bb8b', // AppSecret
];
$app = new Application($options);
// 获取 access token 实例
$accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
$token = $accessToken->getToken(); // token 字符串
//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
//width是二维码宽度
$qcode = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$token";
$param2 = json_encode(array("path" => $param['url'] . "?openid=" . $id, "width" => 150));
// $param2 = json_encode(array("path" => $param['url'] . "?openid=" , "width" => 150));
//POST参数
$result = $this->httpRequest($qcode, $param2, "POST");
//生成二维码
file_put_contents('./images/'.'user_'.$id.'.png', $result);
$return = [
'url' => $domain . '/images/'.'user_'.$id.'.png'
];
$this->success('成功', $return);
}
protected function httpRequest($url, $data = '', $method = 'GET')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}