RechargeController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\recharge\classes\RechargeClass;
  5. use biz\recharge\classes\RechargeHbClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\sj\classes\SjClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizHd\custom\classes\HdClass;
  10. use bizHd\member\classes\MemberWealClass;
  11. use bizHd\order\classes\OrderClass;
  12. use bizHd\wx\classes\WxOpenClass;
  13. use bizMall\wx\classes\WxMiniClass;
  14. use common\components\dict;
  15. use common\components\imgUtil;
  16. use common\components\miniUtil;
  17. use common\components\orderSn;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use Yii;
  21. class RechargeController extends BaseController
  22. {
  23. //获取商城端充值页和订单页小程序短链接
  24. public function actionGetMallRechargeShortLink()
  25. {
  26. $get = Yii::$app->request->get();
  27. if (getenv('YII_ENV') == 'dev') {
  28. $ip = Yii::$app->request->getUserIP();
  29. if ($ip == '127.0.0.1') {
  30. util::success(['rechargeAccountLink' => 'www.google.com', 'orderLink' => 'www.google.com']);
  31. }
  32. }
  33. $orderId = $get['orderId'] ?? 0;
  34. $account = $this->shopId;
  35. $page = 'pages/member/recharge?account=' . $account;
  36. $ret = WxMiniClass::getShortLink($page);
  37. $link = $ret['link'] ?? '';
  38. $orderLink = '';
  39. if (!empty($orderId)) {
  40. $order = OrderClass::getById($orderId, true);
  41. if ($order->mainId != $this->mainId) {
  42. util::fail('访问错误');
  43. }
  44. $orderPage = 'pages/order/detail?account=' . $account . '&id=' . $orderId;
  45. $res = WxMiniClass::getShortLink($orderPage);
  46. $orderLink = $res['link'] ?? '';
  47. }
  48. util::success(['rechargeAccountLink' => $link, 'orderLink' => $orderLink]);
  49. }
  50. //获取扫码充值的小程序码
  51. public function actionGetRechargeCode()
  52. {
  53. $shopId = $this->shopId;
  54. $recharge = MemberWealClass::getAllByCondition(['shopId' => $shopId], 'recharge asc', '*', null); //这里取前面三个放在海报里,对应表名是 xhMemberRecharge 不是 MemberWeal
  55. $merchant = WxOpenClass::getMallWxInfo();
  56. $page = 'pages/member/recharge';
  57. $ptStyle = dict::getDict('ptStyle', 'mall');
  58. $scene = 'a=' . $this->shopAdminId . '&s=' . $this->shopId;
  59. $imgUrl = miniUtil::generateUnlimitedRechargeMiniCode($merchant, $page, $scene, $ptStyle);
  60. $prefix = imgUtil::getPrefix();
  61. $titleBase64 = stringUtil::ossBase64('扫码充值');
  62. $memberMiniCodeBase64 = stringUtil::ossBase64($imgUrl);
  63. $memberUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  64. $memberUrl .= '/watermark,image_' . $memberMiniCodeBase64 . ',g_north,x_0,y_40';
  65. $memberUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  66. $posterUrl = $prefix . 'poster/recharge_poster.jpg';
  67. // 组合充值海报:将前3条充值活动写入海报对应位置
  68. // 海报尺寸:955x1439;以下坐标为在设计稿上测得的三行“进店充值/获得”数值区大致位置
  69. // 位置从上到下三行,左列为“进店充值”,中列为“获得”
  70. $posterProcess = '?x-oss-process=image';
  71. // 将小程序码放到右下角白色区域,大小与白色区域一致(可按需微调)
  72. $qrWidth = 230; // 白色区域宽度,像素
  73. $qrRightPadding = 144; // 距离右边的留白
  74. $qrBottomPadding = 72; // 距离底部的留白
  75. // 先用 $imgUrl 生成按宽度缩放后的小程序码图片,再拼接到海报
  76. $miniCodeResizeUrl = $imgUrl . (strpos($imgUrl, '?') === false ? '?x-oss-process=image' : '&x-oss-process=image');
  77. $miniCodeResizeUrl .= '/resize,w_' . $qrWidth;
  78. $miniCodeResizeBase64 = stringUtil::ossBase64($miniCodeResizeUrl);
  79. $posterProcess .= '/watermark,image_' . $miniCodeResizeBase64 . ',g_se,x_' . $qrRightPadding . ',y_' . $qrBottomPadding;
  80. $positions = [
  81. ['recharge' => ['x' => 240, 'y' => 665], 'give' => ['x' => 520, 'y' => 665]],
  82. ['recharge' => ['x' => 220, 'y' => 845], 'give' => ['x' => 500, 'y' => 845]],
  83. ['recharge' => ['x' => 220, 'y' => 1030], 'give' => ['x' => 500, 'y' => 1030]],
  84. ];
  85. $fontSize = 44; // 数值字号
  86. $fontColor = '333333';
  87. if (!empty($recharge)) {
  88. // 先生成同一张图片上的文字水印串,最后再与$posterProcess合并
  89. $textProcess = '';
  90. $topThree = array_slice($recharge, 0, 3);
  91. foreach ($topThree as $index => $val) {
  92. $pos = $positions[$index] ?? null;
  93. if ($pos === null) {
  94. continue;
  95. }
  96. $rechargeText = (string)(intval($val['recharge']) ?? '');
  97. $giveText = (string)(intval($val['give']) ?? '');
  98. if ($rechargeText !== '') {
  99. $rechargeBase64 = stringUtil::ossBase64($rechargeText);
  100. $textProcess .= '/watermark,text_' . $rechargeBase64 . ',g_nw,x_' . $pos['recharge']['x'] . ',y_' . $pos['recharge']['y'] . ',size_' . $fontSize . ',color_' . $fontColor;
  101. }
  102. if ($giveText !== '') {
  103. $giveBase64 = stringUtil::ossBase64($giveText);
  104. $textProcess .= '/watermark,text_' . $giveBase64 . ',g_nw,x_' . $pos['give']['x'] . ',y_' . $pos['give']['y'] . ',size_' . $fontSize . ',color_' . $fontColor;
  105. }
  106. }
  107. $posterProcess .= $textProcess;
  108. }
  109. $posterUrl .= $posterProcess;
  110. $respond = ['imgUrl' => $memberUrl, 'posterUrl' => $posterUrl];
  111. util::success($respond);
  112. }
  113. //手动给客户充值 ssh 20250301
  114. public function actionManRecharge()
  115. {
  116. $post = Yii::$app->request->post();
  117. $shopAdmin = $this->shopAdmin;
  118. if ($shopAdmin->super != 1) {
  119. util::fail('超管才能充值');
  120. }
  121. $staffId = $shopAdmin->id;
  122. $staffName = $shopAdmin->name;
  123. $connection = Yii::$app->db;
  124. $transaction = $connection->beginTransaction();
  125. try {
  126. $payWay = $post['payWay'] ?? 0;
  127. $amount = $post['amount'] ?? 0;
  128. $remark = $post['remark'] ?? '';
  129. $customId = $post['customId'] ?? 0;
  130. $shOrderId = $post['shOrderId'] ?? 0;
  131. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  132. if (empty($custom)) {
  133. util::fail('没有找到客户');
  134. }
  135. //避免重复提交
  136. util::checkRepeatCommit($customId, 4);
  137. $shOrderSn = '';
  138. if (!empty($shOrderId)) {
  139. $shOrder = OrderClass::getById($shOrderId, true);
  140. if ($shOrder->forward != 1) {
  141. util::fail('不是售后付款单');
  142. }
  143. if ($shOrder->mainId != $this->mainId) {
  144. util::fail('不是你的单');
  145. }
  146. //判断是否充值过了
  147. \bizHd\recharge\classes\RechargeClass::checkShOrderRecharge($shOrder);
  148. $shOrderSn = $shOrder->orderSn ?? '';
  149. }
  150. $refundOrderId = $post['refundOrderId'] ?? 0;
  151. $refundOrderSn = '';
  152. if (!empty($refundOrderId)) {
  153. //线下订单售后,充值
  154. $myOrder = OrderClass::getById($refundOrderId, true);
  155. if ($myOrder->mainId != $this->mainId) {
  156. util::fail('不是你的订单');
  157. }
  158. $refundOrderSn = $myOrder->orderSn ?? '';
  159. }
  160. $customName = $custom->name;
  161. $userId = $custom->userId ?? 0;
  162. $hdId = $custom->hdId;
  163. $hd = HdClass::getById($hdId, true);
  164. if (empty($hd)) {
  165. util::fail('花店信息缺失,编号9886');
  166. }
  167. $hdName = $hd->name;
  168. $shopId = $this->shopId;
  169. $mainId = $this->mainId;
  170. $orderSn = orderSn::getRechargeSn();
  171. $rechargeData = [
  172. 'orderSn' => $orderSn,
  173. 'modPrice' => 1,
  174. 'payWay' => $payWay,
  175. 'shopId' => $shopId,
  176. 'mainId' => $mainId,
  177. 'hdId' => $hdId,
  178. 'hdName' => $hdName,
  179. 'onlinePay' => 1,
  180. 'amount' => $amount,
  181. 'staffId' => $staffId,
  182. 'staffName' => $staffName,
  183. 'payStatus' => 0,
  184. 'payTime' => '0000-00-00 00:00:00',
  185. 'status' => 0,
  186. 'remark' => $remark,
  187. 'customId' => $customId,
  188. 'customName' => $customName,
  189. 'userId' => $userId,
  190. 'shOrderId' => $shOrderId,
  191. 'shOrderSn' => $shOrderSn,
  192. 'refundOrderId' => $refundOrderId,
  193. 'refundOrderSn' => $refundOrderSn,
  194. ];
  195. $respond = \bizHd\recharge\classes\RechargeClass::addRecharge($rechargeData);
  196. $id = $respond->id;
  197. $recharge = RechargeClass::getLockById($id);
  198. $params = ['onlinePay' => 1, 'side' => 0];
  199. //充值成功并且销账
  200. \bizHd\recharge\classes\RechargeClass::complete($recharge, $payWay, $params);
  201. $transaction->commit();
  202. util::success($post);
  203. } catch (\Exception $e) {
  204. $transaction->rollBack();
  205. Yii::error("充值失败原因:" . $e->getMessage());
  206. util::fail('充值失败');
  207. }
  208. }
  209. //充值记录 ssh 2021.2.21
  210. public function actionList()
  211. {
  212. $get = Yii::$app->request->get();
  213. $customId = $get['customId'] ?? 0;
  214. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  215. if (empty($custom)) {
  216. util::fail('没有找到客户');
  217. }
  218. if ($custom->shopId != $this->shopId) {
  219. util::fail('不是你的客户');
  220. }
  221. $where = ['shopId' => $this->shopId, 'customId' => $customId];
  222. $list = RechargeClass::getRechargeList($where);
  223. util::success($list);
  224. }
  225. //免费续期活动 ssh 2021.2.21
  226. public function actionRenew()
  227. {
  228. $sj = $this->sj->attributes;
  229. $shopId = $sj['parentShopId'] ?? 0;
  230. $shopName = '';
  231. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  232. if (!empty($shopId)) {
  233. $shop = ShopClass::getShopInfo($shopId);
  234. if (!empty($shop)) {
  235. $shopName = $shop['shopName'] ?? '';
  236. $num = $shop['rechargeNum'] ?? 0;
  237. $rechargeNum += $num;
  238. }
  239. }
  240. $data = [
  241. 'renew' => getenv('RENEW_PRICE') == false ? 1980 : getenv('RENEW_PRICE'),
  242. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  243. 'shopName' => $shopName,
  244. 'userNum' => $rechargeNum
  245. ];
  246. util::success($data);
  247. }
  248. //续期充值付款 ssh 2021.2.21
  249. public function actionRenewPay()
  250. {
  251. ini_set('date.timezone', 'Asia/Shanghai');
  252. $wxPay = dict::getDict('payWay', 'wxPay');
  253. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  254. $shopAdmin = $this->shopAdmin->attributes;
  255. $shopAdminName = $shopAdmin['name'] ?? '';
  256. $data = [
  257. 'amount' => $price,
  258. 'payWay' => $wxPay,
  259. 'sjId' => $this->sjId,
  260. 'sjStyle' => SjClass::STYLE_RETAIL,
  261. 'shopId' => $this->shopId,
  262. 'modPrice' => 0,
  263. 'remark' => '免费续期充值',
  264. 'shopAdminId' => $this->shopAdminId,
  265. 'shopAdminName' => $shopAdminName,
  266. ];
  267. $order = RechargeClass::addOrder($data, true);
  268. $orderSn = $order->orderSn;
  269. $name = $order->orderName ?? '充值';
  270. $totalFee = $order->amount;
  271. if (isset($this->shopAdmin['super']) == false || $this->shopAdmin['super'] != 1) {
  272. util::fail('超级管理员才有权限操作');
  273. }
  274. //强制使用小程序的miniOpenId
  275. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  276. if (empty($openId)) {
  277. util::fail('没有找到openId');
  278. }
  279. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  280. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  281. $wxPayType = 1;
  282. $ghsId = Yii::$app->request->get('ghsId', 0);
  283. $ghs = GhsClass::getById($ghsId);
  284. GhsClass::valid($ghs, $this->shopId);
  285. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1&ghsId=' . $ghsId;
  286. //订单30分钟后过期
  287. $now = time();
  288. $expireTime = $now + 1800;
  289. $wx = Yii::getAlias("@vendor/weixin");
  290. require_once($wx . '/lib/WxPay.Api.php');
  291. require_once($wx . '/example/WxPay.JsApiPay.php');
  292. $input = new \WxPayUnifiedOrder();
  293. $input->SetBody($name);
  294. $input->SetOut_trade_no($orderSn);
  295. $input->SetTotal_fee($totalFee * 100);
  296. $input->SetTime_start(date("YmdHis", $now));
  297. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  298. $input->SetTime_expire(date("YmdHis", $expireTime));
  299. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  300. $input->SetTrade_type("JSAPI");
  301. $merchantExtend = WxOpenClass::getWxInfo();
  302. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  303. $input->SetSub_openid($openId);
  304. } else {
  305. $input->SetOpenid($openId);
  306. }
  307. //强制使用小程序的AppId
  308. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  309. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  310. $tools = new \JsApiPay();
  311. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  312. $newParams = json_decode($jsApiParameters, true);
  313. util::success($newParams);
  314. }
  315. //充值送红包 ssh 2021.2.21
  316. public function actionBalance()
  317. {
  318. ini_set('date.timezone', 'Asia/Shanghai');
  319. $get = Yii::$app->request->get();
  320. $hbId = $get['id'] ?? 0;
  321. $ghsId = $get['ghsId'] ?? 0;
  322. $ghsInfo = GhsClass::getById($ghsId, true);
  323. if (empty($ghsInfo)) {
  324. util::fail('没有找到供货商');
  325. }
  326. GhsClass::valid($ghsInfo, $this->shopId);
  327. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  328. $hbInfo = RechargeHbClass::getById($hbId, true);
  329. if (empty($hbInfo)) {
  330. util::fail('无效活动');
  331. }
  332. if ($hbInfo->shopId != $ghsShopId) {
  333. util::fail('供货商与充值红包活动不一致');
  334. }
  335. if ($hbInfo->delStatus == 1) {
  336. util::fail('红包已更新,请重新发起充值');
  337. }
  338. $amount = $hbInfo->amount ?? 0;
  339. if ($amount <= 0) {
  340. util::fail('充值金额有问题');
  341. }
  342. $customId = $ghsInfo['customId'] ?? 0;
  343. $custom = CustomClass::getById($customId, true);
  344. $customName = $custom->name ?? '';
  345. $wxPay = dict::getDict('payWay', 'wxPay');
  346. $shopAdmin = $this->shopAdmin->attributes;
  347. $shopAdminName = $shopAdmin['name'] ?? '';
  348. $data = [
  349. 'amount' => $amount,
  350. 'payWay' => $wxPay,
  351. 'sjId' => $this->sjId,
  352. 'sjStyle' => SjClass::STYLE_RETAIL,
  353. 'shopId' => $this->shopId,
  354. 'modPrice' => 0,
  355. 'remark' => '',
  356. 'shopAdminId' => $this->shopAdminId,
  357. 'shopAdminName' => $shopAdminName,
  358. 'hbId' => $hbInfo->id ?? 0,
  359. 'hbAmount' => $hbInfo->hbAmount ?? 0,
  360. 'hbNum' => $hbInfo->num,
  361. 'totalHb' => $hbInfo->totalHb,
  362. 'valid' => $hbInfo->valid,
  363. 'miniExpend' => $hbInfo->miniExpend,
  364. 'ghsId' => $ghsId,
  365. 'ghsShopId' => $ghsShopId,
  366. 'customId' => $customId,
  367. 'customName' => $customName,
  368. ];
  369. $order = RechargeClass::addOrder($data, true);
  370. $orderSn = $order->orderSn;
  371. $name = $order->orderName ?? '充值';
  372. $totalFee = $order->amount;
  373. //强制使用小程序的miniOpenId
  374. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  375. if (empty($openId)) {
  376. util::fail('没有找到openId');
  377. }
  378. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  379. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  380. $wxPayType = 1;
  381. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  382. //订单30分钟后过期
  383. $now = time();
  384. $expireTime = $now + 1800;
  385. $wx = Yii::getAlias("@vendor/weixin");
  386. require_once($wx . '/lib/WxPay.Api.php');
  387. require_once($wx . '/example/WxPay.JsApiPay.php');
  388. $input = new \WxPayUnifiedOrder();
  389. $input->SetBody($name);
  390. $input->SetOut_trade_no($orderSn);
  391. $input->SetTotal_fee($totalFee * 100);
  392. $input->SetTime_start(date("YmdHis", $now));
  393. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  394. $input->SetTime_expire(date("YmdHis", $expireTime));
  395. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  396. $input->SetTrade_type("JSAPI");
  397. $merchantExtend = WxOpenClass::getWxInfo();
  398. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  399. $input->SetSub_openid($openId);
  400. } else {
  401. $input->SetOpenid($openId);
  402. }
  403. //强制使用小程序的AppId
  404. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  405. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  406. $tools = new \JsApiPay();
  407. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  408. $newParams = json_decode($jsApiParameters, true);
  409. util::success($newParams);
  410. }
  411. //续期成功 ssh 2021.2.21
  412. public function actionRenewSuccess()
  413. {
  414. $sj = $this->sj->attributes;
  415. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  416. util::success(['deadline' => $deadline]);
  417. }
  418. }