| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 | <?phpnamespace app\api\controller;use app\common\controller\Api;use think\Db;use addons\epay\library\Service;/** * 商品 */class Product extends Api{    protected $noNeedLogin = ['lists','info'];    protected $noNeedRight = ['*'];    public function lists()    {        $keyword = input('keyword','');        $where = ['is_show'=>1];        if(!empty($keyword)){            $where['title'] = ['LIKE','%'.$keyword.'%'];        }        $list = Db::name('product')->field('content',true)->where($where)->order('weigh desc')->autopage()->select();        $list = list_domain_image($list,['images']);        if(!empty($list)){            foreach($list as $key => &$item){                //第一个图                $item['image'] = isset($item['images'][0]) ? $item['images'][0] : '';            }        }        $this->success(1,$list);    }    public function info(){        $id = input('id',0);        $info = Db::name('product')->where('is_show',1)->where('id',$id)->find();        if(!$info){            $this->error('不存在的商品');        }        $info = info_domain_image($info,['images']);        //第一个图        $info['image'] = isset($info['images'][0]) ? $info['images'][0] : '';        $this->success(1,$info);    }    //兑换    public function buy(){        $remark = input('remark','');        $product_id = input('id',0);        $info = Db::name('product')->where('is_show',1)->where('id',$product_id)->find();        if(!$info){            $this->error('不存在的商品');        }        Db::startTrans();        //下单        $data = [            'order_no' => createUniqueNo('A',$this->auth->id),            'product_id' => $product_id,            'user_id'    => $this->auth->id,            'createtime' => time(),            'pay_fee' => $info['price'],            'remark' => $remark,            'status' => 0,        ];        $order_id = Db::name('order')->insertGetId($data);        if(!$order_id){            Db::rollback();            $this->error('兑换失败');        }        //扣钱        $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'score',-$info['price'],3,'兑换积分商品','order',$order_id);        if($rs_wallet['status'] == false){            Db::rollback();            $this->error($rs_wallet['msg']);        }        Db::commit();        $this->success('兑换成功',$order_id);    }}
 |