IndexController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\scanCodePay\controllers;
  8. use app\models\AggregateQrcode;
  9. use app\models\AggregateSaasQrcode;
  10. use app\models\Option;
  11. use app\models\Store;
  12. use app\plugins\scanCodePay\models\form\ScanCodePaySettingForm;
  13. use app\utils\QrCode;
  14. use app\utils\ShareQrcode;
  15. class IndexController
  16. {
  17. public function actionSetting ()
  18. {
  19. $form = new ScanCodePaySettingForm();
  20. $form->store_id = get_store_id();
  21. $form->mch = 0;
  22. if (\Yii::$app->request->isPost) {
  23. $form->setScenario('save');
  24. $form->attributes= post_params();
  25. $res = $form->save();
  26. return $res;
  27. }
  28. $form->setScenario('search');
  29. $data = $form->search();
  30. return [
  31. 'code' => 0,
  32. 'data' => $data
  33. ];
  34. }
  35. public function actionDownQr()
  36. {
  37. $store_id = get_store_id();
  38. $mch_id = intval(get_mch_id());
  39. $filename = md5('scan_' . $store_id . '_mch_' . $mch_id);
  40. $ag = AggregateQrcode::findOne(['store_id' => $store_id]);
  41. $store = Store::findOne($store_id);
  42. $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
  43. $data = [];
  44. if (!\Yii::$app->isSaas()) {
  45. $data['mini_url'] = '/face-pay/face-pay/payOut';
  46. $data['param_url'] = 'https://' . \Yii::$app->request->hostName . '/web/face/pay/' . $store_id. '?mch_id=' . $mch_id;
  47. }
  48. if (!empty($store) && ((int)$store->business_model !== 1 || (\Yii::$app->prod_is_dandianpu()) && !$self_mini)) {
  49. $asg = AggregateSaasQrcode::findOne(['store_id' => $store_id]);
  50. //先查询聚合表
  51. if(empty($asg->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath,$asg->qrcode_url))){
  52. if(!empty($asg->param_url)){
  53. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  54. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  55. $text = $asg->param_url . '?mch_id=' . $mch_id;
  56. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  57. $asg->qrcode_url = $pic_url;
  58. $asg->save();
  59. return [
  60. 'code' => 0,
  61. 'data' => [
  62. 'qr_url' => $pic_url,
  63. 'business_model' => $store->business_model,
  64. 'is_saas' => (int)\Yii::$app->isSaas(),
  65. 'data' => [
  66. 'param_url' => $pic_url
  67. ]
  68. ]
  69. ];
  70. }
  71. }
  72. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  73. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  74. if (file_exists($path)) {
  75. return [
  76. 'code' => 0,
  77. 'data' => [
  78. 'qr_url' => $pic_url,
  79. 'business_model' => $store->business_model,
  80. 'is_saas' => (int)\Yii::$app->isSaas(),
  81. 'data' => [
  82. 'param_url' => $pic_url
  83. ]
  84. ]
  85. ];
  86. }
  87. $text = 'store_id:'. $store_id . ',mch_id:'. $mch_id;
  88. $app = ShareQrcode::setBussinessWechat();
  89. $response = $app->getUnlimit($text, [
  90. 'width' => 500,
  91. 'page' => "face-pay/face-pay/payOut"
  92. ]);
  93. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  94. $filename = $response->save(\Yii::$app->runtimePath . '/image/', $filename);
  95. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/dy_qrcode/' . $filename);
  96. } else {
  97. return [
  98. 'code' => 1,
  99. 'data' => [
  100. 'response' => $response,
  101. 'business_model' => $store->business_model,
  102. ]
  103. ];
  104. }
  105. return [
  106. 'code' => 0,
  107. 'data' => [
  108. 'qr_url' => $pic_url,
  109. 'business_model' => $store->business_model,
  110. 'is_saas' => (int)\Yii::$app->isSaas(),
  111. 'data' => [
  112. 'param_url' => $pic_url
  113. ]
  114. ]
  115. ];
  116. } else if (empty($ag->param_url)) {
  117. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  118. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  119. if (file_exists($path)) {
  120. return [
  121. 'code' => 0,
  122. 'data' => [
  123. 'qr_url' => $pic_url,
  124. 'business_model' => $store->business_model,
  125. 'is_saas' => (int)\Yii::$app->isSaas(),
  126. 'data' => $data
  127. ]
  128. ];
  129. }
  130. //$text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/scan/' . $store_id);
  131. $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/web/face/pay/' . $store_id);
  132. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  133. } else {
  134. //同步后端的聚合码
  135. $filename = md5(date('YmdHis') . $ag->id);
  136. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  137. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  138. $ag = AggregateQrcode::find()->where(['id' => $ag->id])->select('param_url,id,qrcode_url')->one();
  139. if(empty($ag->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath,$ag->qrcode_url))) {
  140. if (!empty($ag->param_url)) {
  141. $text = $ag->param_url . '?mch_id=' . $mch_id;
  142. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  143. }
  144. }
  145. }
  146. // Todo 待添加背景
  147. // $goods_qrcode = new \claviska\SimpleImage($path);
  148. // $bgPath = \Yii::$app->basePath . '/web/v1/statics/images/alipay-bg.png';
  149. // $bg = new \claviska\SimpleImage($bgPath);
  150. // $bg->overlay($goods_qrcode, 'top left', 1, 200, 350);
  151. // $bg->toFile($path, 'image/jpeg', 85);
  152. return [
  153. 'code' => 0,
  154. 'data' => [
  155. 'qr_url' => $pic_url,
  156. 'business_model' => $store->business_model,
  157. 'data' => [
  158. 'param_url' => $pic_url,
  159. ],
  160. 'is_saas' => (int)\Yii::$app->isSaas(),
  161. ]
  162. ];
  163. }
  164. }