Exchange.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\Exchange as ExchangeModel;
  4. use addons\shop\model\ExchangeOrder;
  5. use addons\shop\model\Address;
  6. use think\Db;
  7. /**
  8. * 积分兑换接口
  9. */
  10. class Exchange extends Base
  11. {
  12. protected $noNeedLogin = ['index', 'show'];
  13. /**
  14. * 积分兑换列表
  15. */
  16. public function index()
  17. {
  18. // 判断是否跳转移动端
  19. $this->checkredirect('exchange/list');
  20. $list = ExchangeModel::tableList($this->request->param());
  21. $this->assign('exchangeList', $list);
  22. $this->assign('type', $this->request->param('type'));
  23. return $this->view->fetch('/exchange_index');
  24. }
  25. /**
  26. * 积分兑换详情
  27. */
  28. public function show()
  29. {
  30. $id = $this->request->param('id/d');
  31. if (!$id) {
  32. $this->error('参数错误');
  33. }
  34. $row = ExchangeModel::get($id);
  35. if (empty($row)) {
  36. $this->error('兑换商品未找到');
  37. }
  38. if ($row->stocks <= 0) {
  39. $this->error('兑换商品库存不足');
  40. }
  41. $addressList = [];
  42. if ($this->auth->isLogin()) {
  43. $addressList = Address::where('status', 'normal')->where('user_id', $this->auth->id)->select();
  44. }
  45. $this->assign('addressList', $addressList);
  46. $this->assign('row', $row);
  47. return $this->view->fetch('/exchange_show');
  48. }
  49. /**
  50. * 积分兑换
  51. */
  52. public function exchange()
  53. {
  54. $address_id = $this->request->post('address_id/d');
  55. $memo = $this->request->post('memo');
  56. $nums = (int)$this->request->post('nums/d', 1);
  57. $exchange_id = $this->request->post('exchange_id/d');
  58. $row = ExchangeModel::get($exchange_id);
  59. if (empty($row)) {
  60. $this->error('兑换物品不存在');
  61. }
  62. $data = [];
  63. if ($row->type == 'reality') {
  64. $address = Address::where('status', 'normal')->where('id', $address_id)->find();
  65. if (empty($address)) {
  66. $this->error('未找到正确地址');
  67. }
  68. if ($address->user_id != $this->auth->id) {
  69. $this->error('不能越权操作');
  70. }
  71. $data = array_merge($data, [
  72. 'receiver' => $address->receiver,
  73. 'mobile' => $address->mobile,
  74. 'address' => $address->address
  75. ]);
  76. }
  77. if ($nums <= 0) {
  78. $nums = 1;
  79. }
  80. $score = bcmul($nums, $row['score']);
  81. if ($this->auth->score < $score) {
  82. $this->error('积分不足,无法兑换');
  83. }
  84. // 启动事务
  85. Db::startTrans();
  86. try {
  87. $data = array_merge($data, [
  88. 'type' => $row->type,
  89. 'memo' => $memo,
  90. 'score' => $score,
  91. 'nums' => $nums,
  92. 'user_id' => $this->auth->id,
  93. 'exchange_id' => $exchange_id
  94. ]);
  95. ExchangeOrder::createOrder($data);
  96. $row->setDec('stocks');
  97. $row->setInc('sales');
  98. \app\common\model\User::score(-$score, $this->auth->id, '积分兑换商品');
  99. // 提交事务
  100. Db::commit();
  101. } catch (\Exception $e) {
  102. // 回滚事务
  103. Db::rollback();
  104. $this->error('提交失败');
  105. }
  106. $this->success('提交成功,等待管理员处理!');
  107. }
  108. }