QrCode.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * MpaySign.php UTF-8
  4. * 米大师支付签名
  5. *
  6. * @date : 2018/8/16 11:26
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpQr;
  13. use Exception;
  14. use FileSystem\FileSystem;
  15. use huolib\constant\CacheConst;
  16. use think\Cache;
  17. use think\Log;
  18. class QrCode {
  19. private $url = "https://api.weixin.qq.com";
  20. const FILE_PATH = 'upload/qrcode/';
  21. /**
  22. * https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html
  23. * 获取小程序码
  24. * 接口A: 适用于需要的码数量较少的业务场景 接口地址:
  25. * https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
  26. * 通过该接口生成的小程序码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。
  27. *
  28. * @param string $appid 小游戏AppId
  29. * @param string $app_secret 小游戏AppSecret
  30. * @param String $path 不能为空,最大长度 128 字节
  31. * @param int $width 二维码的宽度
  32. * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  33. * @param string $line_color auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"},十进制表示
  34. * @param bool $is_hyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码
  35. *
  36. * @return int|mixed
  37. */
  38. public function getWxACode(
  39. $appid, $app_secret,
  40. $path, $width = 430, $auto_color = false, $line_color = '{"r":"0","g":"0","b":"0"}', $is_hyaline = false
  41. ) {
  42. $_uri = '/wxa/getwxacode';
  43. $_param['path'] = $path;
  44. $_param['width'] = $width;
  45. $_param['auto_color'] = $auto_color;
  46. $_param['line_color'] = $line_color;
  47. $_param['is_hyaline'] = $is_hyaline;
  48. return $this->getImage($appid, $app_secret, $_uri, $_param);
  49. }
  50. /**
  51. * 获取小程序码
  52. * 接口B:适用于需要的码数量极多的业务场景
  53. * https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
  54. * 注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中scene 字段的值,再做处理逻辑。
  55. * 使用如下代码可以获取到二维码中的 scene字段的值。
  56. * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
  57. *
  58. * @param string $appid 小游戏AppId
  59. * @param string $app_secret 小游戏AppSecret
  60. * @param String $scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用
  61. * urlencode 处理,请使用其他编码方式)
  62. * @param String $page 必须是已经发布的小程序存在的页面(否则报错),例如 "pages/index/index"
  63. * ,根路径前不要填加'/',不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
  64. * @param int $width 二维码的宽度
  65. * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  66. * @param string $line_color auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"},十进制表示
  67. * @param bool $is_hyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码
  68. *
  69. * @return int|mixed
  70. */
  71. public function getWxACodeUnLimit(
  72. $appid, $app_secret, $page = '', $scene = '',
  73. $width = 430, $auto_color = false, $line_color = '{"r":"0","g":"0","b":"0"}', $is_hyaline = false
  74. ) {
  75. $_uri = '/wxa/getwxacodeunlimit';
  76. $_param['scene'] = $scene;
  77. $_param['page'] = $page;
  78. // $_param['page'] = '';
  79. // $_param['width'] = $width;
  80. // $_param['auto_color'] = $auto_color;
  81. // $_param['line_color'] = $line_color;
  82. // $_param['is_hyaline'] = $is_hyaline;
  83. $_rs = $this->getImage($appid, $app_secret, $_uri, $_param);
  84. return $_rs;
  85. }
  86. /**
  87. * 获取小程序码
  88. * 接口C:适用于需要的码数量较少的业务场景
  89. *
  90. * https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
  91. * 通过该接口生成的小程序二维码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。
  92. * tip:通过该接口,仅能生成已发布的小程序的二维码。
  93. * tip:可以在开发者工具预览时生成开发版的带参二维码。
  94. * tip:接口A加上接口C,总共生成的码数量限制为100,000,请谨慎调用。
  95. * tip: POST 参数需要转成 json 字符串,不支持 form 表单提交。
  96. * tip: auto_color line_color 参数仅对小程序码生效。
  97. *
  98. * @param string $appid 小游戏AppId
  99. * @param string $app_secret 小游戏AppSecret
  100. * @param String $path 不能为空,最大长度 128 字节
  101. * @param int $width 二维码的宽度
  102. *
  103. * @return int|mixed
  104. */
  105. public function createWAQrCode($appid, $app_secret, $path, $width = 430) {
  106. $_uri = '/cgi-bin/wxaapp/createwxaqrcode';
  107. $_param['path'] = $path;
  108. $_param['width'] = $width;
  109. return $this->getImage($appid, $app_secret, $_uri, $_param);
  110. }
  111. /**
  112. * 获取图片信息
  113. *
  114. * @param string $appid 小游戏AppId
  115. * @param string $app_secret 小游戏AppSecret
  116. * @param string $uri 请求URI
  117. * @param array $param 参数
  118. *
  119. * @return int|mixed
  120. */
  121. public function getImage($appid, $app_secret, $uri, $param) {
  122. $_file_name = md5(json_encode(array($appid, $param))).'.png';
  123. $_target_file = CMF_ROOT.'public/'.self::FILE_PATH.$_file_name;
  124. if (file_exists($_target_file)) {
  125. return STATICSITE.DS.self::FILE_PATH.$_file_name;
  126. }
  127. /* Modified by luowei BEGIN 2019/8/9 ISSUES: 使用组件上传,不用ossfs */
  128. /*try {
  129. $_filesystem = FileSystem::create();
  130. } catch (Exception $_exception) {
  131. return [];
  132. }
  133. $_path = DS.self::FILE_PATH.$_file_name;
  134. if ($_filesystem->has($_path)) {
  135. return STATICSITE.DS.self::FILE_PATH.$_file_name;
  136. }*/
  137. /* END 2019/8/9 ISSUES: */
  138. $_access_token = self::getAccessToken($appid, $app_secret);
  139. $_url = $this->url.$uri.'?access_token='.$_access_token;
  140. $_query_param_json = json_encode($param);
  141. Log::write(
  142. "==========Request Info==========\n url:$_url,$appid,query_string:$_query_param_json", Log::WECHAT, true
  143. );
  144. $header = ['Content-Type: application/json; charset=utf-8'];
  145. $_ret = self::curl($_url, $_query_param_json, 'POST', $header);
  146. if ($this->isJson($_ret)) {
  147. Log::write('The resp is '.$_ret, Log::ERROR);
  148. return json_decode($_ret, true);
  149. }
  150. $_rs = self::save($_ret, $_file_name);
  151. /* Modified by luowei BEGIN 2019/8/9 ISSUES: 使用组件上传,不用ossfs */
  152. //$_rs = $_filesystem->write($_path, $_ret);
  153. /* END 2019/8/9 ISSUES: */
  154. if (true == $_rs) {
  155. return STATICSITE.DS.self::FILE_PATH.$_file_name;
  156. }
  157. return [];
  158. }
  159. /**
  160. * 判断是否json
  161. *
  162. * @param $string
  163. *
  164. * @return bool
  165. */
  166. public function isJson($string) {
  167. json_decode($string);
  168. return (json_last_error() == JSON_ERROR_NONE);
  169. }
  170. /**
  171. * @param $content
  172. * @param $filename
  173. *
  174. * @return bool
  175. */
  176. public static function save($content, $filename) {
  177. $_target_dir = CMF_ROOT.'public/'.self::FILE_PATH;
  178. if (!file_exists($_target_dir)) {
  179. mkdir($_target_dir, 0777, true);
  180. }
  181. try {
  182. $_handle = fopen($_target_dir.$filename, "wb");
  183. fwrite($_handle, $content);
  184. fclose($_handle);
  185. return true;
  186. } catch (Exception $e) {
  187. $_msg = 'Exception reçue : '.$e->getMessage().'\n';
  188. Log::write("func=".__FUNCTION__."&class=".__CLASS__."&code=".$e->getCode()."&msg=".$_msg, Log::ERROR);
  189. return false;
  190. }
  191. }
  192. /**
  193. * 获取 access_token
  194. * https://developers.weixin.qq.com/miniprogram/dev/api/token.html
  195. *
  196. * @param string $appid 第三方用户唯一凭证
  197. * @param string $app_secret 第三方用户唯一凭证密钥,即appsecret
  198. *
  199. * @return string|bool
  200. */
  201. public static function getAccessToken($appid, $app_secret) {
  202. $_cache_key = CacheConst::CACHE_ACCESS_TOKEN_PREFIX.$appid;
  203. $_data = Cache::get($_cache_key);
  204. if (!empty($_data)) {
  205. $_data = json_decode($_data, true);
  206. $_now_time = time();
  207. if (isset($_data['expires_time']) && $_now_time < $_data['expires_time']) {
  208. return $_data['access_token'];
  209. }
  210. }
  211. $_url = 'https://api.weixin.qq.com/cgi-bin/token';
  212. $_param['grant_type'] = 'client_credential';
  213. $_param['appid'] = $appid;
  214. $_param['secret'] = $app_secret;
  215. $_rdata = self::curl($_url, http_build_query($_param));
  216. $_rdata = json_decode($_rdata, true);
  217. if (!isset($_rdata['access_token'])) {
  218. Log::write(
  219. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&appid=$appid&app_secret=$app_secret".
  220. json_encode($_rdata), Log::ERROR
  221. );
  222. return false;
  223. }
  224. $_data['access_token'] = $_rdata['access_token'];
  225. $_data['expires_in'] = $_rdata['expires_in'];
  226. $_data['expires_time'] = time() + $_rdata['expires_in'];
  227. $_rs = Cache::set($_cache_key, json_encode($_data));
  228. if (false == $_rs) {
  229. return false;
  230. }
  231. return $_data['access_token'];
  232. }
  233. /**
  234. * CURL请求
  235. *
  236. * @param string $url 请求的URL
  237. * @param string $params 请求的参数
  238. * @param string $is_get 是否是get请求
  239. * @param array $header 头
  240. *
  241. * @return int|mixed
  242. */
  243. public static function curl($url, $params, $is_get = "GET", $header = []) {
  244. $_header = $header;
  245. $ch = curl_init();
  246. if ("GET" == $is_get) {
  247. $url = $url.'?'.$params;
  248. curl_setopt($ch, CURLOPT_HEADER, 0); // 过滤HTTP头
  249. } else {
  250. curl_setopt($ch, CURLOPT_POST, 1);
  251. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  252. }
  253. curl_setopt($ch, CURLOPT_URL, $url);
  254. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  255. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  256. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  257. /* https 请求 */
  258. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  259. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  260. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  261. }
  262. if (!empty($_header)) {
  263. array_push($_header, 'Content-Length: '.strlen($params));
  264. curl_setopt($ch, CURLOPT_HTTPHEADER, $_header);
  265. }
  266. $_rs = curl_exec($ch);
  267. if (false === $_rs) {
  268. $_rs = curl_errno($ch);
  269. }
  270. curl_close($ch);
  271. return $_rs;
  272. }
  273. /**
  274. * 获取原始拼接的链接
  275. *
  276. * @param array $params
  277. *
  278. * @return bool|string
  279. */
  280. public static function createRawLinkString($params = []) {
  281. $_arg = "";
  282. foreach ($params as $_k => $_v) {
  283. $_arg .= $_k."=".$_v."&";
  284. }
  285. // 去掉最后一个&字符
  286. $_arg = substr($_arg, 0, strlen($_arg) - 1);
  287. return $_arg;
  288. }
  289. public function getPosterImg($qrcode) {
  290. $_bg = '/bg.png';
  291. $_qrcode = str_replace(STATICSITE, '', $qrcode);
  292. $_qrcode .= '?x-oss-process=image/resize,P_80';
  293. $_img_str = $this->urlsafe_b64encode($_qrcode);
  294. return STATICSITE.$_bg.'?x-oss-process=image/watermark,image_'.$_img_str.',x_54,y_254';
  295. }
  296. public function urlsafe_b64encode($string) {
  297. $data = base64_encode($string);
  298. $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
  299. return $data;
  300. }
  301. }