IndexController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\scancodepay\controllers\client;
  8. use app\models\AccountLog;
  9. use app\models\AggregateQrcode;
  10. use app\models\AggregateSaasQrcode;
  11. use app\models\Option;
  12. use app\models\Store;
  13. use app\models\User;
  14. use app\plugins\scanCodePay\controllers\BaseController;
  15. use app\plugins\scanCodePay\models\client\IndexForm;
  16. use app\utils\GenerateShareQrcode;
  17. use app\utils\QrCode;
  18. use app\utils\ShareQrcode;
  19. class IndexController extends BaseController
  20. {
  21. public function actionGetCoupon()
  22. {
  23. $form = new IndexForm();
  24. $form->price = get_params('price');
  25. $form->store_id = get_store_id();
  26. $form->user_id = get_user_id();
  27. return $form->search();
  28. }
  29. public function actionGetStore()
  30. {
  31. $store_id = get_store_id();
  32. $store_info = [];
  33. if ($store_id > 0) {
  34. // TODO: store
  35. $store_info = [
  36. 'id' => $store_id,
  37. 'name' => Option::get('name', $store_id, 'store')['value'],
  38. 'img' => Option::get('web_log', $store_id, 'web')['value']
  39. ];
  40. }
  41. return [
  42. 'code' => 0,
  43. 'msg' => 'success',
  44. 'data' => [
  45. 'store_info' => $store_info,
  46. ]
  47. ];
  48. }
  49. /**
  50. * 获取收款码
  51. */
  52. public function actionGetQrcode()
  53. {
  54. $price = get_params('price', 0);
  55. $store_id = get_store_id();
  56. // TODO: store_info
  57. $store_info = [
  58. 'id' => $store_id,
  59. 'name' => Option::get('name', $store_id, 'store')['value'],
  60. 'img' => Option::get('web_log', $store_id, 'web')['value']
  61. ];
  62. $store_id = get_store_id();
  63. $filename = md5('scan_' . $store_id);
  64. $ag = AggregateQrcode::findOne(['store_id' => $store_id]);
  65. $store = Store::findOne($store_id);
  66. $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
  67. if (!empty($store) && ((int)$store->business_model !== 1 || (\Yii::$app->prod_is_dandianpu()) && !$self_mini)) {
  68. $asg = AggregateSaasQrcode::findOne(['store_id' => $store_id]);
  69. //先查询聚合表
  70. if(isset($asg->qrcode_url) && file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath,$asg->qrcode_url))){
  71. return [
  72. 'code' => 0,
  73. 'data' => [
  74. 'url' => $asg->qrcode_url,
  75. 'store' => $store_info,
  76. ]
  77. ];
  78. }
  79. if(empty($asg->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath,$asg->qrcode_url))){
  80. if(!empty($asg->param_url)){
  81. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  82. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  83. $text = $asg->param_url;
  84. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  85. $asg->qrcode_url = $pic_url;
  86. $asg->save();
  87. return [
  88. 'code' => 0,
  89. 'data' => [
  90. 'url' => $pic_url,
  91. 'store' => $store_info,
  92. ]
  93. ];
  94. }
  95. }
  96. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  97. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  98. if (file_exists($path)) {
  99. return [
  100. 'code' => 0,
  101. 'data' => [
  102. 'url' => $pic_url,
  103. 'store' => $store_info,
  104. ]
  105. ];
  106. }
  107. $text = 'store_id:'. $store_id;
  108. $app = ShareQrcode::setBussinessWechat();
  109. $response = $app->getUnlimit($text, [
  110. 'width' => 500,
  111. 'page' => "face-pay/face-pay/payOut"
  112. ]);
  113. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  114. $filename = $response->save(\Yii::$app->runtimePath . '/image/', $filename);
  115. } else {
  116. return [
  117. 'code' => 1,
  118. 'data' => [
  119. 'response' => $response,
  120. 'store' => $store_info,
  121. ]
  122. ];
  123. }
  124. return [
  125. 'code' => 0,
  126. 'data' => [
  127. 'url' => $pic_url,
  128. 'store' => $store_info,
  129. ]
  130. ];
  131. } else if (empty($ag->param_url)) {
  132. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  133. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  134. if (file_exists($path)) {
  135. return [
  136. 'code' => 0,
  137. 'data' => [
  138. 'url' => $pic_url,
  139. 'store' => $store_info,
  140. ]
  141. ];
  142. }
  143. //$text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/scan/' . $store_id);
  144. $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/web/face/pay/' . $store_id);
  145. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  146. } else {
  147. //同步后端的聚合码
  148. $filename = md5(date('YmdHis') . $ag->id);
  149. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  150. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  151. $ag = AggregateQrcode::find()->where(['id' => $ag->id])->select('param_url,id,qrcode_url')->one();
  152. if(empty($ag->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath,$ag->qrcode_url))) {
  153. if (!empty($ag->param_url)) {
  154. $text = $ag->param_url;
  155. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  156. }
  157. }
  158. }
  159. // Todo 待添加背景
  160. // $goods_qrcode = new \claviska\SimpleImage($path);
  161. // $bgPath = \Yii::$app->basePath . '/web/v1/statics/images/alipay-bg.png';
  162. // $bg = new \claviska\SimpleImage($bgPath);
  163. // $bg->overlay($goods_qrcode, 'top left', 1, 200, 350);
  164. // $bg->toFile($path, 'image/jpeg', 85);
  165. return [
  166. 'code' => 0,
  167. 'msg' => 'success',
  168. 'data' => [
  169. 'url' => $pic_url,
  170. 'store' => $store_info,
  171. ]
  172. ];
  173. }
  174. /**
  175. * 获取当面付收款记录
  176. */
  177. public function actionGetMoneyRecord()
  178. {
  179. // TODO: 目前需要获取store下的收款记录
  180. $store_id = get_store_id();
  181. $list = AccountLog::find()->where(['store_id' => $store_id,
  182. 'order_type' => AccountLog::TYPE_SCAN_CODE_PAY, 'type' => AccountLog::TYPE_WECHAT])->asArray()->all();
  183. if (empty($list)) {
  184. return [
  185. 'code' => 0,
  186. 'msg' => 'success',
  187. 'data' => []
  188. ];
  189. }
  190. $res_arr = [];
  191. foreach ($list as $key => &$value) {
  192. $value['nickname'] = User::findOne($value['user_id'])->nickname;
  193. $list[$key]['date'] = date('Y-m-d', $value['created_at']);
  194. $list[$key]['time'] = date('H:i:s', $value['created_at']);
  195. $res_arr[$list[$key]['date']][] = $value;
  196. }
  197. $result = [];
  198. foreach ($res_arr as $k => $v) {
  199. $tmp = [];
  200. $tmp['date'] = $k;
  201. foreach ($v as $p) {
  202. $tmp['data'][] = $p;
  203. }
  204. $result[] = $tmp;
  205. }
  206. $date_arr = array_column($result, 'date');
  207. array_multisort($date_arr, SORT_DESC, $result);
  208. return [
  209. 'code' => 0,
  210. 'msg' => 'success',
  211. 'data' => $result
  212. ];
  213. }
  214. }