Qrcode.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\system;
  11. use app\model\BaseModel;
  12. use app\model\web\Config as ConfigModel;
  13. use extend\QRcode as QRcodeExtend;
  14. /**
  15. * 二维码生成类
  16. */
  17. class Qrcode extends BaseModel
  18. {
  19. public function createQrcode(array $param)
  20. {
  21. try {
  22. $checkpath_result = $this->checkPath($param[ 'qrcode_path' ]);
  23. if ($checkpath_result[ "code" ] != 0) return $checkpath_result;
  24. $urlParam = '';
  25. if (!empty($param[ 'data' ])) {
  26. foreach ($param[ 'data' ] as $key => $value) {
  27. if ($urlParam == '') $urlParam .= '?' . $key . '=' . $value;
  28. else $urlParam .= '&' . $key . '=' . $value;
  29. }
  30. }
  31. $domain = getH5Domain();
  32. if ($param[ 'app_type' ] == 'pc') {
  33. $config_model = new ConfigModel();
  34. $domain = $config_model->getPcDomainName()[ 'data' ][ 'value' ]['domain_name_pc'];
  35. }
  36. $url = $domain . $param[ 'page' ] . $urlParam;
  37. $filename = $param[ 'qrcode_path' ] . '/' . $param[ 'qrcode_name' ] . '_' . $param[ 'app_type' ] . '.png';
  38. if ($param[ 'type' ] == 'create') {
  39. delFile($filename);
  40. }
  41. QRcodeExtend::png($url, $filename, 'L', $param[ 'qrcode_size' ] ?? 16, 1);
  42. return $this->success([ 'type' => 'h5', 'path' => $filename, 'url' => $url ]);
  43. } catch (\Exception $e) {
  44. return $this->error('', $e->getMessage());
  45. }
  46. }
  47. /**
  48. * 校验目录是否可写
  49. * @param unknown $path
  50. * @return multitype:number unknown |multitype:unknown
  51. */
  52. private function checkPath($path)
  53. {
  54. if (is_dir($path) || mkdir($path, intval('0755', 8), true)) {
  55. return $this->success();
  56. }
  57. return $this->error('', "directory {$path} creation failed");
  58. }
  59. }