PointTransferController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\facades\Setting;
  12. use app\common\models\finance\PointTransfer;
  13. use app\common\services\credit\ConstService;
  14. use app\common\services\finance\PointService;
  15. use app\common\services\password\PasswordService;
  16. use app\frontend\models\Member;
  17. use Illuminate\Support\Facades\DB;
  18. class PointTransferController extends ApiController
  19. {
  20. private $transfer;
  21. private $transferModel;
  22. private $poundage;
  23. private $request_point;
  24. /**
  25. * 积分转让接口
  26. * @return \Illuminate\Http\JsonResponse
  27. */
  28. public function index()
  29. {
  30. $result = Setting::get('point.set.point_transfer') ? $this->transferStart() : '未开启积分转让';
  31. // dd($result);
  32. return $result === true ? $this->successJson('转让成功') : $this->errorJson($result);
  33. }
  34. /**
  35. * 被转让者信息接口
  36. * @return \Illuminate\Http\JsonResponse
  37. */
  38. public function getRecipientInfo()
  39. {
  40. $recipient = $this->getMemberInfo($this->getPostRecipient());
  41. return $recipient ? $this->successJson('ok', $recipient) : $this->errorJson('未获取到被转让者或被转让者不存在');
  42. }
  43. private function needPassword()
  44. {
  45. return (new PasswordService())->isNeed('point', 'transfer');
  46. }
  47. protected function memberId()
  48. {
  49. return \YunShop::app()->getMemberId();
  50. }
  51. protected function password()
  52. {
  53. return request()->input('password');
  54. }
  55. /**
  56. * 转让开始
  57. * @return bool|string
  58. */
  59. private function transferStart()
  60. {
  61. if ($this->needPassword()) (new PasswordService())->checkPayPassword($this->memberId(), $this->password());
  62. if (!$this->getTransferInfo()) {
  63. return '未获取到会员信息';
  64. }
  65. if (!$this->getMemberInfo($this->getPostRecipient())) {
  66. return '被转让者不存在';
  67. }
  68. if ($this->transfer->uid == $this->getPostRecipient()) {
  69. return '转让者不能是自己';
  70. }
  71. if (bccomp($this->getPostTransferPoint(), 0, 2) != 1) {
  72. return '转让积分必须大于 0.01';
  73. }
  74. if (bccomp($this->transfer->credit1, $this->getPostTransferPoint(), 2) == -1) {
  75. return '转让积分不能大于您的剩余积分';
  76. }
  77. $result = $this->transferRecordSave();
  78. if ($result !== true) {
  79. return '转让失败,记录出错';
  80. }
  81. DB::beginTransaction();
  82. (new PointService($this->getTransferRecordData()))->changePoint($this->transferModel->id);
  83. (new PointService($this->getRecipientRecordData()))->changePoint($this->transferModel->id);
  84. $result = $this->updateTransferRecordStatus();
  85. if ($result !== true) {
  86. DB::rollBack();
  87. return '转让失败,记录修改出错';
  88. }
  89. DB::commit();
  90. return true;
  91. }
  92. /**
  93. * 转让记录保存
  94. * @return bool|\Illuminate\Support\MessageBag
  95. */
  96. private function transferRecordSave()
  97. {
  98. $this->transferModel = new PointTransfer();
  99. $this->transferModel->fill($this->getTransferData());
  100. $validator = $this->transferModel->validator();
  101. if ($validator->fails()) {
  102. return $validator->messages();
  103. }
  104. return $this->transferModel->save();
  105. }
  106. /**
  107. * 修改转让状态
  108. * @return mixed
  109. */
  110. private function updateTransferRecordStatus()
  111. {
  112. $this->transferModel->status = ConstService::STATUS_SUCCESS;
  113. return $this->transferModel->save();
  114. }
  115. /**
  116. * 转让记录数据
  117. * @return array
  118. */
  119. private function getTransferData()
  120. {
  121. return [
  122. 'uniacid' => \YunShop::app()->uniacid,
  123. 'transferor' => \YunShop::app()->getMemberId(),
  124. 'recipient' => $this->getPostRecipient(),
  125. 'value' => $this->getPostTransferPoint(),
  126. 'status' => ConstService::STATUS_FAILURE,
  127. 'order_sn' => PointTransfer::createOrderSn('PT'),
  128. 'poundage' => $this->poundage,
  129. ];
  130. }
  131. private function getTransferRecordData()
  132. {
  133. $remark = '积分转让-转出:转入会员ID' . $this->transferModel->recipient;
  134. if (request()->input('remark')) {
  135. $remark .= ' 会员备注:'.request()->input('remark');
  136. }
  137. return [
  138. 'point_income_type' => PointService::POINT_INCOME_LOSE,
  139. 'point_mode' => PointService::POINT_MODE_TRANSFER,
  140. 'member_id' => $this->transferModel->transferor,
  141. 'point' => -$this->request_point,
  142. 'remark' => $remark,
  143. ];
  144. }
  145. private function getRecipientRecordData()
  146. {
  147. $remark = '积分转让-转入:转出会员ID' . $this->transferModel->transferor;
  148. if (request()->input('remark')) {
  149. $remark .= ' 会员备注:'.request()->input('remark');
  150. }
  151. return [
  152. 'point_income_type' => PointService::POINT_INCOME_GET,
  153. 'point_mode' => PointService::POINT_MODE_RECIPIENT,
  154. 'member_id' => $this->transferModel->recipient,
  155. 'point' => $this->transferModel->value,
  156. 'remark' => $remark,
  157. ];
  158. }
  159. private function getTransferInfo()
  160. {
  161. return $this->transfer = $this->getMemberInfo(\YunShop::app()->getMemberId());
  162. }
  163. private function getMemberInfo($memberId)
  164. {
  165. return Member::uniacid()->select('uid', 'avatar', 'nickname', 'realname', 'credit1')->where('uid', $memberId)->first();
  166. }
  167. private function getPostTransferPoint()
  168. {
  169. $this->request_point = trim(\YunShop::request()->transfer_point);
  170. if ($this->getRateSet() > 0) {
  171. $point = round($this->request_point - $this->request_point * $this->getRateSet(), 2);
  172. $this->poundage = round($this->request_point * $this->getRateSet(), 2);
  173. return $point;
  174. } else {
  175. return $this->request_point;
  176. }
  177. }
  178. private function getPostRecipient()
  179. {
  180. return trim(\YunShop::request()->recipient);
  181. }
  182. public function getRateSet()
  183. {
  184. return round(Setting::get('point.set.point_transfer_poundage') / 100, 4) ?: 0;
  185. }
  186. }