Product.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 商品
  8. */
  9. class Product extends Api
  10. {
  11. protected $noNeedLogin = ['lists'];
  12. protected $noNeedRight = ['*'];
  13. public function lists()
  14. {
  15. $where = ['is_show'=>1];
  16. $list = Db::name('product')->field('content',true)->where($where)->order('weigh desc')->autopage()->select();
  17. $list = list_domain_image($list,['images']);
  18. if(!empty($list)){
  19. foreach($list as $key => &$item){
  20. //第一个图
  21. $item['image'] = isset($item['images'][0]) ? $item['images'][0] : '';
  22. }
  23. }
  24. $this->success(1,$list);
  25. }
  26. public function info(){
  27. $id = input('id',0);
  28. $info = Db::name('product')->where('is_show',1)->where('id',$id)->find();
  29. if(!$info){
  30. $this->error('不存在的商品');
  31. }
  32. $info = info_domain_image($info,['images']);
  33. //第一个图
  34. $info['image'] = isset($info['images'][0]) ? $info['images'][0] : '';
  35. $this->success(1,$info);
  36. }
  37. //兑换
  38. public function buy(){
  39. $remark = input('remark','');
  40. $product_id = input('id',0);
  41. $info = Db::name('product')->where('is_show',1)->where('id',$product_id)->find();
  42. if(!$info){
  43. $this->error('不存在的商品');
  44. }
  45. Db::startTrans();
  46. //下单
  47. $data = [
  48. 'order_no' => createUniqueNo('A',$this->auth->id),
  49. 'product_id' => $product_id,
  50. 'user_id' => $this->auth->id,
  51. 'createtime' => time(),
  52. 'pay_fee' => $info['price'],
  53. 'remark' => $remark,
  54. 'status' => 0,
  55. ];
  56. $order_id = Db::name('order')->insertGetId($data);
  57. if(!$order_id){
  58. Db::rollback();
  59. $this->error('兑换失败');
  60. }
  61. //扣钱
  62. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'score',-$info['price'],3,'积分兑换'.$info['title'],'order',$order_id);
  63. if($rs_wallet['status'] == false){
  64. Db::rollback();
  65. $this->error($rs_wallet['msg']);
  66. }
  67. Db::commit();
  68. $this->success('兑换成功',$order_id);
  69. }
  70. }