RechargeController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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' => 220, 'y' => 655], 'give' => ['x' => 500, 'y' => 655]],
  82. ['recharge' => ['x' => 220, 'y' => 840], 'give' => ['x' => 500, 'y' => 840]],
  83. ['recharge' => ['x' => 220, 'y' => 1020], 'give' => ['x' => 500, 'y' => 1020]],
  84. ];
  85. $fontSize = 50; // 数值字号
  86. if (!empty($recharge)) {
  87. // 先生成同一张图片上的文字水印串,最后再与$posterProcess合并
  88. $textProcess = '';
  89. $topThree = array_slice($recharge, 0, 3);
  90. foreach ($topThree as $index => $val) {
  91. $pos = $positions[$index] ?? null;
  92. if ($pos === null) {
  93. continue;
  94. }
  95. $rechargeText = (string)(intval($val['recharge']) ?? '');
  96. $giveText = (string)(intval($val['give']) ?? '');
  97. if ($rechargeText !== '') {
  98. $rechargeBase64 = stringUtil::ossBase64($rechargeText);
  99. $textProcess .= '/watermark,text_' . $rechargeBase64 . ',g_nw,x_' . $pos['recharge']['x'] . ',y_' . $pos['recharge']['y'] . ',size_' . $fontSize . ',color_333333,type_' . stringUtil::ossBase64('fangzhengheiti');
  100. }
  101. if ($giveText !== '') {
  102. $giveBase64 = stringUtil::ossBase64($giveText);
  103. $textProcess .= '/watermark,text_' . $giveBase64 . ',g_nw,x_' . $pos['give']['x'] . ',y_' . $pos['give']['y'] . ',size_' . $fontSize . ',color_8B0000,type_' . stringUtil::ossBase64('fangzhengheiti');
  104. }
  105. }
  106. $posterProcess .= $textProcess;
  107. }
  108. $posterUrl .= $posterProcess;
  109. $respond = ['imgUrl' => $memberUrl, 'posterUrl' => $posterUrl];
  110. util::success($respond);
  111. }
  112. //手动给客户充值 ssh 20250301
  113. public function actionManRecharge()
  114. {
  115. $post = Yii::$app->request->post();
  116. $shopAdmin = $this->shopAdmin;
  117. if ($shopAdmin->super != 1) {
  118. util::fail('超管才能充值');
  119. }
  120. $staffId = $shopAdmin->id;
  121. $staffName = $shopAdmin->name;
  122. $shop = $this->shop;
  123. if ($shop->rechargeWeal == 0) {
  124. util::fail('请先设置充值规则');
  125. }
  126. $connection = Yii::$app->db;
  127. $transaction = $connection->beginTransaction();
  128. try {
  129. $payWay = $post['payWay'] ?? 0;
  130. $amount = $post['amount'] ?? 0;
  131. $remark = $post['remark'] ?? '';
  132. $customId = $post['customId'] ?? 0;
  133. $shOrderId = $post['shOrderId'] ?? 0;
  134. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  135. if (empty($custom)) {
  136. util::fail('没有找到客户');
  137. }
  138. //避免重复提交
  139. util::checkRepeatCommit($customId, 4);
  140. $shOrderSn = '';
  141. if (!empty($shOrderId)) {
  142. $shOrder = OrderClass::getById($shOrderId, true);
  143. if ($shOrder->forward != 1) {
  144. util::fail('不是售后付款单');
  145. }
  146. if ($shOrder->mainId != $this->mainId) {
  147. util::fail('不是你的单');
  148. }
  149. //判断是否充值过了
  150. \bizHd\recharge\classes\RechargeClass::checkShOrderRecharge($shOrder);
  151. $shOrderSn = $shOrder->orderSn ?? '';
  152. }
  153. $refundOrderId = $post['refundOrderId'] ?? 0;
  154. $refundOrderSn = '';
  155. if (!empty($refundOrderId)) {
  156. //线下订单售后,充值
  157. $myOrder = OrderClass::getById($refundOrderId, true);
  158. if ($myOrder->mainId != $this->mainId) {
  159. util::fail('不是你的订单');
  160. }
  161. $refundOrderSn = $myOrder->orderSn ?? '';
  162. }
  163. $customName = $custom->name;
  164. $userId = $custom->userId ?? 0;
  165. $hdId = $custom->hdId;
  166. $hd = HdClass::getById($hdId, true);
  167. if (empty($hd)) {
  168. util::fail('花店信息缺失,编号9886');
  169. }
  170. $hdName = $hd->name;
  171. $shopId = $this->shopId;
  172. $mainId = $this->mainId;
  173. $orderSn = orderSn::getRechargeSn();
  174. $rechargeData = [
  175. 'orderSn' => $orderSn,
  176. 'modPrice' => 1,
  177. 'payWay' => $payWay,
  178. 'shopId' => $shopId,
  179. 'mainId' => $mainId,
  180. 'hdId' => $hdId,
  181. 'hdName' => $hdName,
  182. 'onlinePay' => 1,
  183. 'amount' => $amount,
  184. 'staffId' => $staffId,
  185. 'staffName' => $staffName,
  186. 'payStatus' => 0,
  187. 'payTime' => '0000-00-00 00:00:00',
  188. 'status' => 0,
  189. 'remark' => $remark,
  190. 'customId' => $customId,
  191. 'customName' => $customName,
  192. 'userId' => $userId,
  193. 'shOrderId' => $shOrderId,
  194. 'shOrderSn' => $shOrderSn,
  195. 'refundOrderId' => $refundOrderId,
  196. 'refundOrderSn' => $refundOrderSn,
  197. ];
  198. $respond = \bizHd\recharge\classes\RechargeClass::addRecharge($rechargeData);
  199. $id = $respond->id;
  200. $recharge = RechargeClass::getLockById($id);
  201. $params = ['onlinePay' => 1, 'side' => 0];
  202. //充值成功并且销账
  203. \bizHd\recharge\classes\RechargeClass::complete($recharge, $payWay, $params);
  204. $transaction->commit();
  205. util::success($post);
  206. } catch (\Exception $e) {
  207. $transaction->rollBack();
  208. Yii::error("充值失败原因:" . $e->getMessage());
  209. util::fail('充值失败');
  210. }
  211. }
  212. //充值记录 ssh 2021.2.21
  213. public function actionList()
  214. {
  215. $get = Yii::$app->request->get();
  216. $customId = $get['customId'] ?? 0;
  217. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  218. if (empty($custom)) {
  219. util::fail('没有找到客户');
  220. }
  221. if ($custom->shopId != $this->shopId) {
  222. util::fail('不是你的客户');
  223. }
  224. $where = ['shopId' => $this->shopId, 'customId' => $customId];
  225. $list = RechargeClass::getRechargeList($where);
  226. util::success($list);
  227. }
  228. //免费续期活动 ssh 2021.2.21
  229. public function actionRenew()
  230. {
  231. $sj = $this->sj->attributes;
  232. $shopId = $sj['parentShopId'] ?? 0;
  233. $shopName = '';
  234. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  235. if (!empty($shopId)) {
  236. $shop = ShopClass::getShopInfo($shopId);
  237. if (!empty($shop)) {
  238. $shopName = $shop['shopName'] ?? '';
  239. $num = $shop['rechargeNum'] ?? 0;
  240. $rechargeNum += $num;
  241. }
  242. }
  243. $data = [
  244. 'renew' => getenv('RENEW_PRICE') == false ? 1980 : getenv('RENEW_PRICE'),
  245. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  246. 'shopName' => $shopName,
  247. 'userNum' => $rechargeNum
  248. ];
  249. util::success($data);
  250. }
  251. //续期充值付款 ssh 2021.2.21
  252. public function actionRenewPay()
  253. {
  254. ini_set('date.timezone', 'Asia/Shanghai');
  255. $wxPay = dict::getDict('payWay', 'wxPay');
  256. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  257. $shopAdmin = $this->shopAdmin->attributes;
  258. $shopAdminName = $shopAdmin['name'] ?? '';
  259. $data = [
  260. 'amount' => $price,
  261. 'payWay' => $wxPay,
  262. 'sjId' => $this->sjId,
  263. 'sjStyle' => SjClass::STYLE_RETAIL,
  264. 'shopId' => $this->shopId,
  265. 'modPrice' => 0,
  266. 'remark' => '免费续期充值',
  267. 'shopAdminId' => $this->shopAdminId,
  268. 'shopAdminName' => $shopAdminName,
  269. ];
  270. $order = RechargeClass::addOrder($data, true);
  271. $orderSn = $order->orderSn;
  272. $name = $order->orderName ?? '充值';
  273. $totalFee = $order->amount;
  274. if (isset($this->shopAdmin['super']) == false || $this->shopAdmin['super'] != 1) {
  275. util::fail('超级管理员才有权限操作');
  276. }
  277. //强制使用小程序的miniOpenId
  278. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  279. if (empty($openId)) {
  280. util::fail('没有找到openId');
  281. }
  282. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  283. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  284. $wxPayType = 1;
  285. $ghsId = Yii::$app->request->get('ghsId', 0);
  286. $ghs = GhsClass::getById($ghsId);
  287. GhsClass::valid($ghs, $this->shopId);
  288. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1&ghsId=' . $ghsId;
  289. //订单30分钟后过期
  290. $now = time();
  291. $expireTime = $now + 1800;
  292. $wx = Yii::getAlias("@vendor/weixin");
  293. require_once($wx . '/lib/WxPay.Api.php');
  294. require_once($wx . '/example/WxPay.JsApiPay.php');
  295. $input = new \WxPayUnifiedOrder();
  296. $input->SetBody($name);
  297. $input->SetOut_trade_no($orderSn);
  298. $input->SetTotal_fee($totalFee * 100);
  299. $input->SetTime_start(date("YmdHis", $now));
  300. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  301. $input->SetTime_expire(date("YmdHis", $expireTime));
  302. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  303. $input->SetTrade_type("JSAPI");
  304. $merchantExtend = WxOpenClass::getWxInfo();
  305. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  306. $input->SetSub_openid($openId);
  307. } else {
  308. $input->SetOpenid($openId);
  309. }
  310. //强制使用小程序的AppId
  311. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  312. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  313. $tools = new \JsApiPay();
  314. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  315. $newParams = json_decode($jsApiParameters, true);
  316. util::success($newParams);
  317. }
  318. //充值送红包 ssh 2021.2.21
  319. public function actionBalance()
  320. {
  321. ini_set('date.timezone', 'Asia/Shanghai');
  322. $get = Yii::$app->request->get();
  323. $hbId = $get['id'] ?? 0;
  324. $ghsId = $get['ghsId'] ?? 0;
  325. $ghsInfo = GhsClass::getById($ghsId, true);
  326. if (empty($ghsInfo)) {
  327. util::fail('没有找到供货商');
  328. }
  329. GhsClass::valid($ghsInfo, $this->shopId);
  330. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  331. $hbInfo = RechargeHbClass::getById($hbId, true);
  332. if (empty($hbInfo)) {
  333. util::fail('无效活动');
  334. }
  335. if ($hbInfo->shopId != $ghsShopId) {
  336. util::fail('供货商与充值红包活动不一致');
  337. }
  338. if ($hbInfo->delStatus == 1) {
  339. util::fail('红包已更新,请重新发起充值');
  340. }
  341. $amount = $hbInfo->amount ?? 0;
  342. if ($amount <= 0) {
  343. util::fail('充值金额有问题');
  344. }
  345. $customId = $ghsInfo['customId'] ?? 0;
  346. $custom = CustomClass::getById($customId, true);
  347. $customName = $custom->name ?? '';
  348. $wxPay = dict::getDict('payWay', 'wxPay');
  349. $shopAdmin = $this->shopAdmin->attributes;
  350. $shopAdminName = $shopAdmin['name'] ?? '';
  351. $data = [
  352. 'amount' => $amount,
  353. 'payWay' => $wxPay,
  354. 'sjId' => $this->sjId,
  355. 'sjStyle' => SjClass::STYLE_RETAIL,
  356. 'shopId' => $this->shopId,
  357. 'modPrice' => 0,
  358. 'remark' => '',
  359. 'shopAdminId' => $this->shopAdminId,
  360. 'shopAdminName' => $shopAdminName,
  361. 'hbId' => $hbInfo->id ?? 0,
  362. 'hbAmount' => $hbInfo->hbAmount ?? 0,
  363. 'hbNum' => $hbInfo->num,
  364. 'totalHb' => $hbInfo->totalHb,
  365. 'valid' => $hbInfo->valid,
  366. 'miniExpend' => $hbInfo->miniExpend,
  367. 'ghsId' => $ghsId,
  368. 'ghsShopId' => $ghsShopId,
  369. 'customId' => $customId,
  370. 'customName' => $customName,
  371. ];
  372. $order = RechargeClass::addOrder($data, true);
  373. $orderSn = $order->orderSn;
  374. $name = $order->orderName ?? '充值';
  375. $totalFee = $order->amount;
  376. //强制使用小程序的miniOpenId
  377. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  378. if (empty($openId)) {
  379. util::fail('没有找到openId');
  380. }
  381. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  382. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  383. $wxPayType = 1;
  384. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  385. //订单30分钟后过期
  386. $now = time();
  387. $expireTime = $now + 1800;
  388. $wx = Yii::getAlias("@vendor/weixin");
  389. require_once($wx . '/lib/WxPay.Api.php');
  390. require_once($wx . '/example/WxPay.JsApiPay.php');
  391. $input = new \WxPayUnifiedOrder();
  392. $input->SetBody($name);
  393. $input->SetOut_trade_no($orderSn);
  394. $input->SetTotal_fee($totalFee * 100);
  395. $input->SetTime_start(date("YmdHis", $now));
  396. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  397. $input->SetTime_expire(date("YmdHis", $expireTime));
  398. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  399. $input->SetTrade_type("JSAPI");
  400. $merchantExtend = WxOpenClass::getWxInfo();
  401. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  402. $input->SetSub_openid($openId);
  403. } else {
  404. $input->SetOpenid($openId);
  405. }
  406. //强制使用小程序的AppId
  407. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  408. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  409. $tools = new \JsApiPay();
  410. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  411. $newParams = json_decode($jsApiParameters, true);
  412. util::success($newParams);
  413. }
  414. //续期成功 ssh 2021.2.21
  415. public function actionRenewSuccess()
  416. {
  417. $sj = $this->sj->attributes;
  418. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  419. util::success(['deadline' => $deadline]);
  420. }
  421. }