Product.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $keyword = input('keyword','');
  16. $where = ['is_show'=>1];
  17. if(!empty($keyword)){
  18. $where['title'] = ['LIKE','%'.$keyword.'%'];
  19. }
  20. $list = Db::name('product')->field('content',true)->where($where)->order('weigh desc')->autopage()->select();
  21. $list = list_domain_image($list,['images']);
  22. if(!empty($list)){
  23. foreach($list as $key => &$item){
  24. //第一个图
  25. $item['image'] = isset($item['images'][0]) ? $item['images'][0] : '';
  26. }
  27. }
  28. $this->success(1,$list);
  29. }
  30. public function info(){
  31. $id = input('id',0);
  32. $info = Db::name('product')->where('is_show',1)->where('id',$id)->find();
  33. if(!$info){
  34. $this->error('不存在的商品');
  35. }
  36. $info = info_domain_image($info,['images']);
  37. //第一个图
  38. $info['image'] = isset($info['images'][0]) ? $info['images'][0] : '';
  39. $this->success(1,$info);
  40. }
  41. //兑换
  42. public function buy(){
  43. $remark = input('remark','');
  44. $product_id = input('id',0);
  45. $info = Db::name('product')->where('is_show',1)->where('id',$product_id)->find();
  46. if(!$info){
  47. $this->error('不存在的商品');
  48. }
  49. Db::startTrans();
  50. //下单
  51. $data = [
  52. 'order_no' => createUniqueNo('A',$this->auth->id),
  53. 'product_id' => $product_id,
  54. 'user_id' => $this->auth->id,
  55. 'createtime' => time(),
  56. 'pay_fee' => $info['price'],
  57. 'remark' => $remark,
  58. 'status' => 0,
  59. ];
  60. $order_id = Db::name('order')->insertGetId($data);
  61. if(!$order_id){
  62. Db::rollback();
  63. $this->error('兑换失败');
  64. }
  65. //扣钱
  66. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'score',-$info['price'],3,'兑换积分商品','order',$order_id);
  67. if($rs_wallet['status'] == false){
  68. Db::rollback();
  69. $this->error($rs_wallet['msg']);
  70. }
  71. Db::commit();
  72. $this->success('兑换成功',$order_id);
  73. }
  74. }