Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

232 wiersze
7.3 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/10/27
  6. * Time: 5:37 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\unishop\extend\Hashids;
  10. use addons\unishop\model\Config;
  11. use addons\unishop\model\Product;
  12. use addons\unishop\model\Cart as CartModel;
  13. use think\Exception;
  14. /**
  15. * 购物车接口
  16. * Class Cart
  17. * @package addons\unishop\controller
  18. */
  19. class Cart extends Base
  20. {
  21. /**
  22. * 允许频繁访问的接口
  23. * @var array
  24. */
  25. protected $frequently = ['number_change', 'choose_change'];
  26. /**
  27. * 获取购物车列表
  28. *
  29. * @ApiTitle (获取购物车列表)
  30. * @ApiSummary (获取购物车列表)
  31. * @ApiMethod (GET)
  32. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  33. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  34. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  35. * @ApiReturnParams (name="data", type="object", sample="{'cart_id'='int','choose'='int','image'='string','isset'='bool','nowPrice'='float','number'='int','oldPrice'='float','spec'='string','title'='string'",description="扩展数据返回")
  36. * @ApiReturn ({
  37. 'code':'1',
  38. 'mesg':'返回成功'
  39. 'data':[
  40. 0:{
  41. cart_id: 3
  42. choose: 1
  43. image: "/uploads/20190715/1aa2c058d4dd7f0d64edb992d9aeccee.jpeg"
  44. isset: true
  45. nowPrice: "122"
  46. number: 2
  47. oldPrice: "122"
  48. spec: "深空灰,64G"
  49. title: "苹果X",
  50. stock:12
  51. }]
  52. })
  53. */
  54. public function index()
  55. {
  56. $carts = (new CartModel)->where(['user_id' => $this->auth->id])
  57. ->with([
  58. 'product' => function ($query) {
  59. $query->field(['id', 'image', 'title', 'specTableList','sales','market_price','sales_price','stock','use_spec', 'switch']);
  60. }
  61. ])
  62. ->order(['createtime' => 'desc'])
  63. ->select();
  64. if (!$carts) {
  65. $this->success('', []);
  66. }
  67. $data = [];
  68. $productExtend = new \addons\unishop\extend\Product;
  69. foreach ($carts as $item) {
  70. $oldProduct = json_decode($item['snapshot'], true);
  71. $oldData = $productExtend->getBaseData($oldProduct, $item['spec'] ?? '');
  72. if (empty($item['product'])) {
  73. $tempData = $oldData;
  74. $tempData['isset'] = false; // 失效
  75. $tempData['title'] = $oldProduct['title'];
  76. $tempData['choose'] = 0;
  77. } else {
  78. $productData = $item['product']->getData();
  79. $tempData = $productExtend->getBaseData($productData, $item['spec'] ?? '');
  80. $tempData['title'] = $item['product']['title'];
  81. $tempData['choose'] = $item['choose']; //是否选中
  82. $tempData['isset'] = true;
  83. if ($productData['switch'] == Product::SWITCH_OFF) {
  84. $tempData['isset'] = false; // 失效
  85. $tempData['choose'] = 0;
  86. }
  87. }
  88. $tempData['cart_id'] = $item['id'];
  89. $tempData['spec'] = $item['spec'];
  90. $tempData['number'] = $item['number'];
  91. $tempData['image'] = Config::getImagesFullUrl($oldData['image']);
  92. $tempData['oldPrice'] = number_format($oldData['sales_price'], 2);
  93. $tempData['nowPrice'] = number_format($tempData['sales_price'], 2);
  94. $tempData['product_id'] = Hashids::encodeHex($item['product_id']);
  95. $data[] = $tempData;
  96. }
  97. $this->success('', $data);
  98. }
  99. /**
  100. * 添加
  101. */
  102. public function add()
  103. {
  104. $id = $this->request->get('id', 0);
  105. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  106. $product = (new Product)->where(['id' => $id, 'switch' => Product::SWITCH_ON])->find();
  107. if (!$product) {
  108. $this->error('产品不存在或已下架');
  109. }
  110. $spec = $this->request->get('spec', '');
  111. $productBase = (new \addons\unishop\extend\Product())->getBaseData($product->getData(), $spec);
  112. if (!$productBase['stock'] || $productBase['stock'] <= 0) {
  113. $this->error('库存不足');
  114. }
  115. $user_id = $this->auth->id;
  116. $cartModel = new \addons\unishop\model\Cart();
  117. $cartModel->where(['user_id' => $user_id, 'product_id' => $id]);
  118. $spec && $cartModel->where('spec', $spec);
  119. $oldCart = $cartModel->find();
  120. if ($oldCart) {
  121. $this->error('商品已存在购物车');
  122. // $oldCart->number++;
  123. // $result = $oldCart->save();
  124. } else {
  125. $cartModel->user_id = $user_id;
  126. $cartModel->product_id = $id;
  127. $spec && $cartModel->spec = $spec;
  128. $cartModel->number = 1;
  129. $cartModel->snapshot = json_encode($product->getData(), true);
  130. $result = $cartModel->save();
  131. }
  132. if ($result) {
  133. $this->success('添加成功', 1);
  134. } else {
  135. $this->error('添加失败');
  136. }
  137. }
  138. /**
  139. * 删除
  140. */
  141. public function delete()
  142. {
  143. $id = $this->request->post('id', 0);
  144. $userId = $this->auth->id;
  145. $result = CartModel::destroy(function ($query) use ($id, $userId) {
  146. $query->whereIn('id', $id)->where(['user_id' => $userId]);
  147. });
  148. if ($result) {
  149. $this->success('删除成功', 1);
  150. } else {
  151. $this->error('删除失败', 0);
  152. }
  153. }
  154. /**
  155. * 修改购物车数量
  156. * @ApiTitle (获取购物车列表)
  157. * @ApiSummary (获取购物车列表)
  158. * @ApiMethod (GET)
  159. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  160. */
  161. public function number_change()
  162. {
  163. $cart_id = $this->request->get('id', 0);
  164. $number = $this->request->get('number', 1);
  165. $cart = CartModel::get(['id' => $cart_id, 'user_id' => $this->auth->id]);
  166. if (empty($cart)) {
  167. $this->error('此商品不存在购物车');
  168. }
  169. $cart->number = $number;
  170. $result = $cart->save();
  171. if ($result) {
  172. $this->success('更改成功', $number);
  173. } else {
  174. $this->error('更改失败', $number);
  175. }
  176. }
  177. /**
  178. * 修改购物车选中状态
  179. * @ApiTitle (修改购物车选中状态)
  180. * @ApiMethod (GET)
  181. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  182. */
  183. public function choose_change()
  184. {
  185. $trueArr = $this->request->post('trueArr', false);
  186. $falseArr = $this->request->post('falseArr', false);
  187. $user_id = $this->auth->id;
  188. try {
  189. $cart = new CartModel();
  190. if ($trueArr) {
  191. $cart->save(['choose' => CartModel::CHOOSE_ON], function ($query) use ($user_id, $trueArr) {
  192. $query->where('user_id', $user_id)->where('id', 'IN', $trueArr);
  193. });
  194. }
  195. if ($falseArr) {
  196. $cart->save(['choose' => CartModel::CHOOSE_OFF], function ($query) use ($user_id, $falseArr) {
  197. $query->where('user_id', $user_id)->where('id', 'IN', $falseArr);
  198. });
  199. }
  200. } catch (Exception $e) {
  201. $this->error('更新失败', 0);
  202. }
  203. $this->success('', 1);
  204. }
  205. }