PointTransferIntegralController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/9/1 下午2:22
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\frontend\modules\finance\controllers;
  10. use app\common\components\ApiController;
  11. use app\common\exceptions\ShopException;
  12. use app\common\facades\Setting;
  13. use app\common\services\finance\PointService;
  14. use app\common\services\password\PasswordService;
  15. use app\frontend\models\Member;
  16. use Illuminate\Support\Facades\DB;
  17. use Yunshop\Integral\Backend\Models\IntegralRechargeModel;
  18. use Yunshop\Integral\Common\Services\IntegralChangeServer;
  19. class PointTransferIntegralController extends ApiController
  20. {
  21. private $set;//积分设置
  22. private $transfer;//转化者信息
  23. private $transfer_point;//转化积分
  24. public function preAction()
  25. {
  26. parent::preAction();
  27. $this->set = Setting::get('point.set');
  28. $this->transfer = Member::uniacid()->select('uid', 'avatar', 'nickname', 'realname', 'credit1')->where('uid', $this->memberId())->first();
  29. $this->transfer_point = trim(\YunShop::request()->transfer_point);//转化积分
  30. if (!app('plugins')->isEnabled('integral') || !$this->set['is_transfer_integral']) {
  31. throw new ShopException('未开启积分转化消费积分');
  32. };
  33. }
  34. private function needPassword()
  35. {
  36. return (new PasswordService())->isNeed('point', 'transfer');
  37. }
  38. protected function memberId()
  39. {
  40. return \YunShop::app()->getMemberId();
  41. }
  42. protected function password()
  43. {
  44. return request()->input('password');
  45. }
  46. //积分比例
  47. protected function transferPointRatio()
  48. {
  49. return (float)$this->set['transfer_point_ratio'] ?: 1;
  50. }
  51. //消费积分比例
  52. protected function transferIntegralRatio()
  53. {
  54. return (float)$this->set['transfer_integral_ratio'] ?: 1;
  55. }
  56. /**
  57. * 积分转化接口
  58. * @return \Illuminate\Http\JsonResponse
  59. */
  60. public function index()
  61. {
  62. $result = $this->transferStart();
  63. return $result === true ? $this->successJson('转化成功') : $this->errorJson($result);
  64. }
  65. //页面预处理
  66. public function pagePer()
  67. {
  68. $integralName = app('plugins')->isEnabled('integral') ? \Yunshop\Integral\Common\Services\SetService::getIntegralName() : '消费积分';
  69. $data = [
  70. 'credit1' => $this->transfer->credit1,//现积分
  71. 'point_name' => Setting::get('shop.shop')['credit1'] ?: "积分",//积分自定义名称
  72. 'integral_name' => $integralName,//消费积分自定义名称
  73. 'transfer_point_ratio' => $this->transferPointRatio(),//积分比例
  74. 'transfer_integral_ratio' => $this->transferIntegralRatio(),//消费积分比例
  75. ];
  76. return $this->successJson('ok', $data);
  77. }
  78. /**
  79. * 转化开始
  80. * @return bool|string
  81. */
  82. private function transferStart()
  83. {
  84. // if ($this->needPassword()) (new PasswordService())->checkPayPassword($this->memberId(), $this->password());
  85. $pointName=Setting::get('shop.shop')['credit1'] ?: "积分"; //积分自定义名称
  86. if (!$this->transfer) {
  87. return '未获取到会员信息';
  88. }
  89. if (bccomp($this->transfer_point, 0, 2) != 1) {
  90. return '转化'.$pointName.'必须大于 0.01';
  91. }
  92. if (bccomp($this->transfer->credit1, $this->transfer_point, 2) == -1) {
  93. return '转化'.$pointName.'不能大于您的剩余'.$pointName;
  94. }
  95. DB::beginTransaction();
  96. try {
  97. //转出积分
  98. (new PointService($this->getTransferRecordData()))->changePoint();
  99. //转入消费积分
  100. (new IntegralChangeServer())->universal($this->getRecipientRecordData($this->getTransferIntegral()));
  101. DB::commit();
  102. return true;
  103. } catch (\Exception $e) {
  104. DB::rollBack();
  105. return $e->getMessage();
  106. }
  107. }
  108. private function getTransferRecordData()
  109. {
  110. return [
  111. 'point_income_type' => PointService::POINT_INCOME_LOSE,
  112. 'point_mode' => PointService::POINT_MODE_TRANSFER_INTEGRAL,
  113. 'member_id' => $this->memberId(),
  114. 'point' => -$this->transfer_point,
  115. 'remark' => '积分转化消费积分-转出积分'
  116. ];
  117. }
  118. private function getRecipientRecordData($value)
  119. {
  120. $order_sn = (new IntegralRechargeModel())->createOrderSn('PTI');//积分转化消费积分
  121. return [
  122. 'uid' => $this->memberId(),
  123. 'uniacid' => \Yunshop::app()->uniacid,
  124. 'change_value' => $value,
  125. 'source_type' => 1,
  126. 'source' => \Yunshop\Integral\Common\Services\ConstService::POINT_TRANSFER_INTEGRAL,
  127. 'order_sn' => $order_sn,
  128. 'remark' => "积分转化消费积分",
  129. 'type' => 1,
  130. ];
  131. }
  132. //获取转化后的消费积分
  133. private function getTransferIntegral()
  134. {
  135. return bcmul(bcdiv($this->transferIntegralRatio(), $this->transferPointRatio(), 4), $this->transfer_point, 2);
  136. }
  137. }