RechargeController.php 18 KB

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