CashController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace app\modules\client\controllers\v1;
  3. use app\constants\OptionSetting;
  4. use app\models\Cash;
  5. use app\models\CashExt;
  6. use app\models\IntegralAppreciationUser;
  7. use app\models\Lg;
  8. use app\models\Option;
  9. use app\models\SaasUser;
  10. use app\models\ShareGroupPurchaseUser;
  11. use app\models\Store;
  12. use app\models\SuperSalesUser;
  13. use app\models\TeamGrades;
  14. use app\models\User;
  15. use app\modules\client\controllers\BaseController;
  16. use app\modules\client\models\v1\CashForm;
  17. use app\modules\client\models\v1\ShareForm;
  18. use yii\helpers\Json;
  19. class CashController extends BaseController
  20. {
  21. /**
  22. * 获取提现详情
  23. */
  24. public function actionCashDetail()
  25. {
  26. $id = get_params('id');
  27. $store_id = get_store_id();
  28. $cash = Cash::findOne($id);
  29. if (!$cash) {
  30. return $this->asJson([
  31. 'code' => 1,
  32. 'msg' => '提现记录不存在'
  33. ]);
  34. }
  35. $store = Store::findOne($store_id);
  36. $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
  37. if ($store->is_platform_transfers == 1) {
  38. $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  39. }
  40. // if (empty($wechat_cash)) {
  41. // return $this->asJson([
  42. // 'code' => 1,
  43. // 'msg' => '未开启微信提现功能'
  44. // ]);
  45. // }
  46. // $wechat_cash = json_decode($wechat_cash, true);
  47. // if (empty($wechat_cash)) {
  48. // return $this->asJson([
  49. // 'code' => 1,
  50. // 'msg' => '未开启微信提现功能'
  51. // ]);
  52. // }
  53. $name = $cash->name;
  54. if (intval($cash->type) === Cash::TYPE_RECHARGE) {
  55. $user_id = $cash->user_id;
  56. $user = User::findOne($user_id);
  57. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  58. $name = $saas_user->name;
  59. }
  60. $is_platform_transfers = $cash->is_platform_transfers;
  61. $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
  62. if ($is_platform_transfers == 1) {
  63. $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  64. }
  65. $wechat_cash = json_decode($wechat_cash, true);
  66. $package = '';
  67. if (!empty($cash->wx_cash_result_info)) {
  68. $wx_cash_result_info = json_decode($cash->wx_cash_result_info, true);
  69. $package = $wx_cash_result_info['package_info'];
  70. }
  71. $is_user_confirm = 1;
  72. $status = $cash->status;
  73. //如果是微信提现且为自动打款
  74. if ($cash->type == Cash::TYPE_WX && $cash->status == Cash::STATUS_GIVEN) {
  75. //如果使用的是微信新版打款方式
  76. if (intval($cash->wx_cash_type)) {
  77. //如果微信新版状态是等待用户确认
  78. if ($cash->wx_cash_state == 'WAIT_USER_CONFIRM') {
  79. $is_user_confirm = 0;
  80. // $status = Cash::STATUS_CONFIRM;
  81. }
  82. } else {
  83. //如果是微信旧版打款方式 且为微信提交打款请求但是未成功或失败状态
  84. if ($cash->wx_cash_status == 0) {
  85. $status = Cash::STATUS_CONFIRM;
  86. }
  87. }
  88. }
  89. $price = Cash::getServiceMoney($cash);
  90. $data = [
  91. 'type' => Cash::$type[$cash->type],//提现方式
  92. 'status' => $status,//提现状态
  93. 'order_no' => $cash->order_no,
  94. 'created_at' => date('Y-m-d H:i:s', $cash->created_at),
  95. 'updated_at' => $cash->updated_at > 0 ? date('Y-m-d H:i:s', $cash->updated_at) : '',
  96. 'name' => $name,
  97. 'appid' => $wechat_cash['appid'],
  98. 'mch_id' => $wechat_cash['mch_id'],
  99. 'package' => $package,
  100. 'is_user_confirm' => $is_user_confirm,
  101. 'price' => $price,
  102. ];
  103. return $this->asJson([
  104. 'code' => 0,
  105. 'msg' => 'success',
  106. 'data' => $data
  107. ]);
  108. }
  109. public function actionGetPrice()
  110. {
  111. $cash_type = get_params('cash_type');
  112. $form = new ShareForm();
  113. $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
  114. if ($cash) {
  115. $res['data'] = json_decode($cash, true);
  116. $res['data']['is_set_new_cash_setting'] = 1;
  117. } else {
  118. $res['data']['is_set_new_cash_setting'] = 0;
  119. }
  120. $form->store_id = get_store_id();
  121. $form->user_id = get_user_id();
  122. $res['data']['price'] = $form->getPrice();
  123. if ($cash_type == Cash::IS_CASH_TYPE_TEAM_GRADES) {
  124. $userTeamGrades = TeamGrades::getUserTeamGrades(get_user_id());
  125. if ($res['data']['price']['price'] > $userTeamGrades['price']) {
  126. $res['data']['price']['price'] = $userTeamGrades['price'];
  127. }
  128. } elseif ($cash_type == Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
  129. $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => get_user_id()]);
  130. if ($res['data']['price']['price'] > $shareGroupPurchaseUser->price) {
  131. $res['data']['price']['price'] = $shareGroupPurchaseUser->price;
  132. }
  133. } elseif ($cash_type == Cash::IS_CASH_TYPE_SUPER_SALES) {
  134. $superSalesUser = SuperSalesUser::findOne(['user_id' => get_user_id()]);
  135. if ($res['data']['price']['price'] > $superSalesUser->price) {
  136. $res['data']['price']['price'] = $superSalesUser->price;
  137. }
  138. } elseif ($cash_type == Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  139. $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => get_user_id()]);
  140. //与其他不一样 增值积分需要先根据提现金额 计算一下实际的提现金额
  141. $res['data']['price']['price'] = $integralAppreciationUser->integral ?: '0.00';
  142. }
  143. if ($res['data']['cash_service_charge'] == 0) {
  144. $res['data']['service_content'] = "";
  145. } else {
  146. $res['data']['service_content'] = "提现需要加收{$res['data']['cash_service_charge']}%手续费";
  147. }
  148. $cash_last = Cash::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id(), 'is_delete' => 0])
  149. ->orderBy(['id' => SORT_DESC])->select(['name', 'mobile', 'type', 'bank_name'])->asArray()->one();
  150. $res['data']['cash_last'] = $cash_last;
  151. $cash_max_day = floatval($res['data']['cash_max_day']);
  152. if ($cash_max_day) {
  153. $query = Cash::find()->where([
  154. 'store_id' => get_store_id(),
  155. 'is_delete' => 0,
  156. 'status' => [0, 1, 2, 5],
  157. ]);
  158. $cash_sum = $query->andWhere([
  159. 'AND',
  160. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  161. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  162. ])->sum('price');
  163. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  164. $res['data']['cash_max_day'] = max(0, floatval(sprintf('%.2f', $cash_max_day)));
  165. } else {
  166. $res['data']['cash_max_day'] = -1;
  167. }
  168. if ($res['data']['cash_max_day'] !== 0) {
  169. $cash_max_single_day = $res['data']['cash_max_single_day'];
  170. if ($cash_max_single_day) {
  171. if ($res['data']['cash_max_day'] > $cash_max_single_day) {
  172. $res['data']['cash_max_day'] = $cash_max_single_day;
  173. }
  174. } else {
  175. $res['data']['cash_max_day'] = -1;
  176. }
  177. }
  178. $wxappUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/wxapp/images';
  179. $payTypeList = [
  180. [
  181. 'icon' => $wxappUrl . '/icon-share-wechat.png',
  182. 'name' => '微信',
  183. 'is_show' => false
  184. ],
  185. [
  186. 'icon' => $wxappUrl . '/icon-share-ant.png',
  187. 'name' => '支付宝',
  188. 'is_show' => false
  189. ],
  190. [
  191. 'icon' => $wxappUrl . '/icon-share-bank.png',
  192. 'name' => '银行卡',
  193. 'is_show' => false
  194. ],
  195. [
  196. 'icon' => $wxappUrl . '/gold.png',
  197. 'name' => '余额',
  198. 'is_show' => false
  199. ],
  200. [
  201. 'icon' => $wxappUrl . '/icon-share-bank.png',
  202. 'name' => '灵工提现',
  203. 'is_show' => false
  204. ],
  205. ];
  206. switch ($res['data']['pay_type']) {
  207. case 0:
  208. if(isset($res['data']['pay_type'])){
  209. $payTypeList[0]['is_show'] = true;
  210. }
  211. break;
  212. case 1:
  213. $payTypeList[1]['is_show'] = true;
  214. break;
  215. case 2:
  216. $payTypeList[0]['is_show'] = true;
  217. $payTypeList[1]['is_show'] = true;
  218. break;
  219. default:
  220. break;
  221. }
  222. if ($res['data']['pay_wechat'] == 1) {
  223. $payTypeList[0]['is_show'] = true;
  224. }
  225. if ($res['data']['pay_alipay'] == 1) {
  226. $payTypeList[1]['is_show'] = true;
  227. }
  228. if ($res['data']['bank'] == 1) {
  229. $payTypeList[2]['is_show'] = true;
  230. }
  231. if ($res['data']['remaining_sum'] == 1) {
  232. $payTypeList[3]['is_show'] = true;
  233. }
  234. //灵工提现
  235. if ($res['data']['lg'] == 1) {
  236. $payTypeList[4]['is_show'] = true;
  237. }
  238. $res['data']['pay_type_list'] = $payTypeList;
  239. $res['data']["cash_success_tpl"] = '';
  240. $res['data']["cash_fail_tpl"] = '';
  241. // 新提现方式
  242. $res['data']['cash_method_list'] = [];
  243. $saas_user = get_saas_user();
  244. $cash_method = [];
  245. if (!empty($saas_user->withdraw_method)) {
  246. $cash_method = json_decode($saas_user->withdraw_method, true);
  247. }
  248. $money = [
  249. [
  250. 'type' => 'money'
  251. ]
  252. ];
  253. $cash_method = array_merge($cash_method, $money);
  254. //判断是否灵工进件
  255. $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'store_id' => get_store_id(), 'status' => 1, 'is_delete' => 0])->one();
  256. if ($lg_info) {
  257. $lg = [
  258. [
  259. 'type' => 'lg'
  260. ]
  261. ];
  262. $cash_method = array_merge($cash_method, $lg);
  263. }
  264. if (is_array($cash_method)) {
  265. foreach ($cash_method as $index => &$item) {
  266. if ($item['type'] === 'wechat' && $payTypeList[0]['is_show'] === false) {
  267. unset($cash_method[$index]);
  268. }
  269. if ($item['type'] === 'alipay' && $payTypeList[1]['is_show'] === false) {
  270. unset($cash_method[$index]);
  271. }
  272. if ($item['type'] === 'bank_card' && $payTypeList[2]['is_show'] === false) {
  273. unset($cash_method[$index]);
  274. }
  275. if ($item['type'] === 'money' && $payTypeList[3]['is_show'] === false) {
  276. unset($cash_method[$index]);
  277. }
  278. if ($item['type'] === 'lg' && $payTypeList[4]['is_show'] === false) {
  279. unset($cash_method[$index]);
  280. }
  281. }
  282. $res['data']['cash_method_list'] = array_values($cash_method);;
  283. }
  284. return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res['data']]);
  285. }
  286. public function actionGetCashFront()
  287. {
  288. $post = post_params();
  289. $cash_price = $post['price'];
  290. try {
  291. $amount_price = 0;
  292. $integral_price = 0;
  293. $balance_price = 0;
  294. $real_amount_price = 0;
  295. $real_integral_price = 0;
  296. $real_balance_price = 0;
  297. $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
  298. if ($cash) {
  299. $cash = json_decode($cash, true);
  300. $cash_price_amount = $cash['cash_price_amount'] ?? 100;
  301. $cash_price_integral = $cash['cash_price_integral'] ?? 0;
  302. $cash_price_balance = $cash['cash_price_balance'] ?? 0;
  303. $cash_service_charge = $cash['cash_service_charge'] ?? 0;
  304. $cash_price_type = $cash['cash_price_type'] ?? '1';
  305. } else {
  306. $cash_price_amount = 0;
  307. $cash_price_integral = 0;
  308. $cash_price_balance = 0;
  309. $cash_service_charge = 0;
  310. $cash_price_type = '1';
  311. }
  312. $real_cash_price = bcsub($cash_price, bcdiv(bcmul($cash_price, $cash_service_charge, 2), 100, 2), 2);//实际可提现金额
  313. $total_profit = 0;
  314. $cash_price_type = explode(',', $cash_price_type);
  315. if (in_array(CashExt::CASH_PRICE_TYPE_AMOUNT, $cash_price_type)) {
  316. $total_profit = bcadd($total_profit, $cash_price_amount, 2);
  317. $amount_price = bcdiv(bcmul($cash_price, $cash_price_amount, 2), 100, 2);
  318. $real_amount_price = bcdiv(bcmul($real_cash_price, $cash_price_amount, 2), 100, 2);//99.4 * 1
  319. }
  320. if (in_array(CashExt::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type)) {
  321. $total_profit = bcadd($total_profit, $cash_price_integral, 2);
  322. $integral_price = bcdiv(bcmul($cash_price, $cash_price_integral, 2), 100, 2);
  323. //兼容计算精度问题
  324. if ($cash_price_balance <= 0 || !in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
  325. $real_integral_price = bcsub($real_cash_price, $real_amount_price, 2);
  326. } else {
  327. $real_integral_price = bcdiv(bcmul($real_cash_price, $cash_price_integral, 2), 100, 2);
  328. }
  329. }
  330. if (in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
  331. $total_profit = bcadd($total_profit, $cash_price_balance, 2);
  332. $balance_price = bcdiv(bcmul($cash_price, $cash_price_balance, 2), 100, 2);
  333. //兼容计算精度问题
  334. $real_balance_price = bcsub($real_cash_price, bcadd($real_amount_price, $real_integral_price, 2), 2);
  335. }
  336. if ($total_profit != 100) {
  337. throw new \Exception('计算失败,佣金配置错误');
  338. }
  339. return $this->asJson([
  340. 'code' => 0,
  341. 'msg' => '计算成功',
  342. 'data' => [
  343. 'amount_price' => $amount_price,
  344. 'integral_price' => $integral_price,
  345. 'balance_price' => $balance_price,
  346. 'cash_service_charge' => $cash_service_charge,
  347. 'real_amount_price' => $real_amount_price,
  348. 'real_integral_price' => $real_integral_price,
  349. 'real_balance_price' => $real_balance_price,
  350. ]
  351. ]);
  352. } catch (\Exception $e) {
  353. return $this->asJson([
  354. 'code' => 1,
  355. 'msg' => $e->getMessage(),
  356. ]);
  357. }
  358. }
  359. public function actionGetCashMethod()
  360. {
  361. $saas_user = get_saas_user();
  362. $payTypeList = [
  363. [
  364. 'name' => '微信',
  365. 'is_show' => false,
  366. 'type' => 'wechat',
  367. 'is_bind' => false
  368. ],
  369. [
  370. 'name' => '支付宝',
  371. 'is_show' => false,
  372. 'type' => 'alipay',
  373. 'is_bind' => false
  374. ],
  375. [
  376. 'name' => '银行卡',
  377. 'is_show' => false,
  378. 'type' => 'bank_card',
  379. 'is_bind' => false
  380. ],
  381. [
  382. 'name' => '灵工提现',
  383. 'is_show' => false,
  384. 'type' => 'lg',
  385. 'is_bind' => false
  386. ],
  387. ];
  388. $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
  389. if ($cash) {
  390. $setting = json_decode($cash, true);
  391. $is_set_new_cash_setting = 1;
  392. if ($setting['pay_wechat'] == 1) {
  393. $payTypeList[0]['is_show'] = true;
  394. }
  395. if ($setting['pay_alipay'] == 1) {
  396. $payTypeList[1]['is_show'] = true;
  397. }
  398. if ($setting['bank'] == 1) {
  399. $payTypeList[2]['is_show'] = true;
  400. }
  401. if ($setting['lg'] == 1) {
  402. $payTypeList[3]['is_show'] = true;
  403. }
  404. if (!empty($saas_user->withdraw_method)) {
  405. $decode = json_decode($saas_user->withdraw_method, true);
  406. if (is_array($decode)) {
  407. $cash_method = $decode;
  408. foreach ($payTypeList as &$pay_item) {
  409. foreach ($cash_method as $item) {
  410. if ($item['type'] === $pay_item['type']) {
  411. $pay_item['is_bind'] = true;
  412. $pay_item = array_merge($pay_item, $item);
  413. }
  414. }
  415. }
  416. }
  417. }
  418. //判断灵工是否绑定
  419. foreach ($payTypeList as &$item) {
  420. $lg = Lg::find()->where(['user_id' => $saas_user->id, 'is_delete' => 0, 'status' => 1])->one();
  421. if ($lg) {
  422. if ('lg' === $item['type']) {
  423. $item['is_bind'] = true;
  424. $item = array_merge($item, $item);
  425. }
  426. }
  427. }
  428. return $this->asJson([
  429. 'code' => 0,
  430. 'msg' => 'success',
  431. 'data' => [
  432. 'is_set_new_cash_setting' => 1,
  433. 'list' => $payTypeList,
  434. ],
  435. ]);
  436. } else {
  437. return $this->asJson([
  438. 'code' => 0,
  439. 'msg' => 'success',
  440. 'data' => [
  441. 'is_set_new_cash_setting' => 0,
  442. 'list' => [],
  443. ],
  444. ]);
  445. }
  446. }
  447. public function actionApply()
  448. {
  449. $form = new CashForm();
  450. $form->user_id = get_user_id();
  451. $form->store_id = get_store_id();
  452. $form->attributes = post_params();
  453. return $this->asJson($form->saveNew());
  454. }
  455. }