RenewController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\set\classes\SetClass;
  4. use biz\sj\classes\SjClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use biz\sj\services\MerchantService;
  7. use bizGhs\shop\classes\RenewClass;
  8. use bizHd\saas\services\RenewService;
  9. use bizHd\saas\services\SetMealService;
  10. use bizHd\user\services\UserService;
  11. use bizHd\wx\classes\WxOpenClass;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\orderSn;
  15. use common\components\util;
  16. use Yii;
  17. class RenewController extends BaseController
  18. {
  19. //需要续费 ssh 20240426
  20. public function actionNeedRemind()
  21. {
  22. $need = 1;
  23. $shop = $this->shop;
  24. if ($shop->needRenew == 0) {
  25. $need = 0;
  26. }
  27. if ($shop->hasRenew == 1) {
  28. $need = 0;
  29. }
  30. $renewType = $shop->renewType ?? 0;
  31. $date = date("Y_m_d");
  32. $staff = $this->shopAdmin;
  33. $staffId = $staff->id ?? 0;
  34. $cacheKey = 'no_remind_' . $date . '_' . $staffId;
  35. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  36. if (!empty($has)) {
  37. $need = 0;
  38. }
  39. $deadline = $shop->deadline ?? '';
  40. $newTime = bcadd(strtotime($deadline), 31536000);
  41. $newDeadline = date("Y-m-d H:i:s", $newTime);
  42. $newTime5 = bcadd(strtotime($deadline), bcmul('31536000', '5'));
  43. $newDeadline5 = date("Y-m-d H:i:s", $newTime5);
  44. $old = substr($deadline, 0, 11);
  45. $new = substr($newDeadline, 0, 11);
  46. $new5 = substr($newDeadline5, 0, 11);
  47. $hasRenew = $shop->hasRenew ?? 0;
  48. util::success(['need' => $need, 'currentDeadline' => $old, 'newDeadline' => $new, 'newDeadline5' => $new5, 'hasRenew' => $hasRenew, 'renewType' => $renewType]);
  49. }
  50. //当天不需要再提醒 ssh 20240426
  51. public function actionNoRemind()
  52. {
  53. $date = date("Y_m_d");
  54. $staff = $this->shopAdmin;
  55. $staffId = $staff->id ?? 0;
  56. $cacheKey = 'no_remind_' . $date . '_' . $staffId;
  57. $mainId = $this->mainId;
  58. $time = 86400;
  59. if (getenv('YII_ENV') == 'production') {
  60. //莱漫和国恋需要更多提醒次数
  61. if ($mainId == 7704 || $mainId == 65) {
  62. $time = 15000;
  63. }
  64. } else {
  65. if ($mainId == 644) {
  66. $time = 86400;
  67. }
  68. }
  69. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, $time, 'has']);
  70. util::complete();
  71. }
  72. public function actionList()
  73. {
  74. $where = [];
  75. $list = RenewService::getRenewList($where);
  76. util::success($list);
  77. }
  78. //创建续费的订单 ssh 20240425
  79. public function actionCreate()
  80. {
  81. ini_set('date.timezone', 'Asia/Shanghai');
  82. $get = Yii::$app->request->get();
  83. $year = $get['year'] ?? 1;
  84. $type = $get['type'] ?? 0;
  85. $remark = $get['remark'] ?? '';
  86. $orderSn = orderSn::getRenewSn();
  87. $staff = $this->shopAdmin;
  88. $staffId = $staff->id ?? 0;
  89. $shopAdminName = $staff->name ?? '';
  90. $shop = $this->shop;
  91. $shopName = $shop->shopName ?? '';
  92. $shopId = $shop->id ?? 0;
  93. $sjId = $shop->sjId ?? 0;
  94. $sj = SjClass::getById($sjId, true);
  95. $sjName = $sj->name ?? '';
  96. $mainId = $shop->mainId ?? 0;
  97. $pfLevel = $shop->pfLevel ?? 0;
  98. $deadline = $shop->deadline ?? '';
  99. $beforeDeadline = $shop->beforeDeadline ?? '';
  100. if ($type == 0) {
  101. if (getenv('YII_ENV') == 'production') {
  102. $unitPrice = 1580;
  103. if ($mainId == 50) {
  104. $unitPrice = $pfLevel == 0 ? 0.5 : 1;
  105. }
  106. } else {
  107. $unitPrice = $pfLevel == 0 ? 0.1 : 0.2;
  108. }
  109. $price = bcmul($unitPrice, $year, 2);
  110. $name = "续费{$year}年";
  111. } else {
  112. $price = $get['price'] ?? 0;
  113. if ($price <= 0) {
  114. util::fail('请输入金额');
  115. }
  116. $name = "购买套餐/购买设备";
  117. }
  118. $data = [
  119. 'orderSn' => $orderSn,
  120. 'prePrice' => $price,
  121. 'actPrice' => $price,
  122. 'shopAdminId' => $staffId,
  123. 'shopAdminName' => $shopAdminName,
  124. 'sjId' => $sjId,
  125. 'sjName' => $sjName,
  126. 'mainId' => $mainId,
  127. 'shopId' => $shopId,
  128. 'shopName' => $shopName,
  129. 'pfLevel' => $pfLevel,
  130. 'year' => $year,
  131. 'deadline' => $deadline,
  132. 'beforeDeadline' => $beforeDeadline,
  133. 'status' => 0,
  134. 'type' => $type,
  135. 'remark' => $remark,
  136. ];
  137. $ret = RenewClass::add($data, true);
  138. $id = $ret->id ?? 0;
  139. $now = time();
  140. $expireTime = $now + 300;//订单5分钟后过期
  141. $totalFee = $price;
  142. $admin = $this->admin;
  143. $openId = $get['currentMiniOpenId'] ?? '';
  144. if (empty($openId)) {
  145. $openId = $admin->ghsMiniOpenId;
  146. }
  147. if (empty($openId)) {
  148. util::fail('账号有问题,无法支付');
  149. }
  150. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  151. $attach = 'capitalType=' . $capitalType;
  152. $wx = Yii::getAlias("@vendor/weixin");
  153. require_once($wx . '/lib/WxPay.Api.php');
  154. require_once($wx . '/example/WxPay.JsApiPay.php');
  155. $input = new \WxPayUnifiedOrder();
  156. $input->SetBody($name);
  157. $input->SetOut_trade_no($orderSn);
  158. $input->SetTotal_fee($totalFee * 100);
  159. $input->SetTime_start(date("YmdHis", $now));
  160. $input->SetAttach($attach);
  161. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  162. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  163. $input->SetTrade_type("JSAPI");
  164. //花卉宝代为申请的微信支付
  165. $sjExtend = WxOpenClass::getGhsWxInfo();
  166. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  167. $input->SetSub_openid($openId);
  168. } else {
  169. $input->SetOpenid($openId);
  170. }
  171. //小程序使用miniAppId
  172. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  173. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  174. $tools = new \JsApiPay();
  175. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  176. $newParams = json_decode($jsApiParameters, true);
  177. $newParams['id'] = $id;
  178. util::success($newParams);
  179. }
  180. //微信支付购买 ssh 2020.1.11
  181. public function actionWxPay()
  182. {
  183. ini_set('date.timezone', 'Asia/Shanghai');
  184. $shopAdminId = $this->shopAdminId;
  185. $addData['shopAdminId'] = $shopAdminId;
  186. $shopAdmin = $this->shopAdmin->attributes;
  187. $adminName = $shopAdmin['name'] ?? '';
  188. $sjId = $shopAdmin['sjId'];
  189. $addData['shopAdminName'] = $adminName;
  190. $set = SetClass::getByCondition(['style' => SetClass::SET_STYLE_GHS], true, 'id asc');
  191. if (empty($set)) {
  192. util::fail('没有找到套餐');
  193. }
  194. $price = $set->price;
  195. $prePrice = $price;
  196. $actPrice = $price;
  197. $setId = $set->id;
  198. $addData['actPrice'] = $actPrice;
  199. $addData['prePrice'] = $prePrice;
  200. $addData['sjId'] = $this->sjId;
  201. $now = time();
  202. $expireTime = $now + 300;//订单5分钟后过期
  203. $addData['deadline'] = date("Y-m-d H:i:s", $expireTime);
  204. $order = RenewService::addOrder($addData);
  205. $orderId = $order['id'];
  206. $orderSn = $order['orderSn'];
  207. $couponId = 0;
  208. $name = $set->name . '开通及续费';
  209. $totalFee = $actPrice;
  210. $admin = $this->admin->attributes;
  211. //小程序使用miniOpenId
  212. $openId = $admin['ghsMiniOpenId'];
  213. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  214. //将流水类型、优惠卷传过去
  215. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&setId=' . $setId . '&sjId=' . $sjId;
  216. $wx = Yii::getAlias("@vendor/weixin");
  217. require_once($wx . '/lib/WxPay.Api.php');
  218. require_once($wx . '/example/WxPay.JsApiPay.php');
  219. $input = new \WxPayUnifiedOrder();
  220. $input->SetBody($name);
  221. $input->SetOut_trade_no($orderSn);
  222. $input->SetTotal_fee($totalFee * 100);
  223. $input->SetTime_start(date("YmdHis", $now));
  224. $input->SetAttach($attach);
  225. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  226. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  227. $input->SetTrade_type("JSAPI");
  228. //花卉宝代为申请的微信支付
  229. $sjExtend = WxOpenClass::getGhsWxInfo();
  230. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  231. $input->SetSub_openid($openId);
  232. } else {
  233. $input->SetOpenid($openId);
  234. }
  235. //小程序使用miniAppId
  236. if (httpUtil::isMiniProgram()) {
  237. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  238. }
  239. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  240. $tools = new \JsApiPay();
  241. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  242. $newParams = json_decode($jsApiParameters, true);
  243. $newParams['orderId'] = $orderId;
  244. $newParams['sjId'] = $this->sjId;
  245. util::success($newParams);
  246. }
  247. }