RechargeController.php 18 KB

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