model = new StoreProductModel(); $this->uid = get_login_admin('id'); } /** * 数据列表 */ public function datalist() { if (request()->isAjax()) { $param = get_params(); $where[] = ['admin_id','=',$this->uid]; $where[] = ['status','=',1]; $where[] = ['is_del','=',0]; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['store_name','like','%'.$param['keywords'].'%']; } if (isset($param['store_cate']) && !empty($param['store_cate'])){ $where[]=['cate_id','=',$param['store_cate']]; } $list = $this->model->getStoreProductList($where,$param); foreach ($list as $k=>$v){ $www['brand_id'] = $v['brand_id']; $list[$k]['brand_id'] = Db::connect('shop')->table('eb_store_brand')->where($www)->value('brand_name'); $www2['store_category_id'] = $v['cate_id']; $list[$k]['cate_id'] = Db::connect('shop')->table('eb_store_category')->where($www2)->value('cate_name'); $list[$k]['shangjia'] = Db::table('cms_admin')->where('id',$this->uid)->value('nickname'); } return table_assign(0, '', $list); } else{ return view(); } } /** * 添加 */ public function add() { if (request()->isAjax()) { $param = get_params(); // 检验完整性 try { validate(StoreProductValidate::class)->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $param['admin_id'] = $this->uid; $this->model->addStoreProduct($param); }else{ $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1]) ->select(); View::assign('store_brand', $store_brand); return view(); } } // /** // * 添加 // */ // public function add() // { // if (request()->isAjax()) { // $param = get_params(); // $data=$param; // $data['mer_id'] = 4; // $data['status'] = 1; // $data['mer_status'] =1; // $data['rate'] = 3; // $data['spec_type'] = 0;//商品规格 // if (!$data['spec_type']) { // $data['attr'] = []; // if (count($data['skus']) > 1) throw new ValidateException('单规格商品属性错误'); // } // $content = [ // 'content' => $data['content'] , // 'type' => 0 // ]; // $productType=0; // $conType=0; // $product = $this->setProduct($data); // // event('product.create.before', compact('data','productType','conType')); // return Db::transaction(function () use ($data, $productType,$conType,$content,$product) { // $activity_id = 0; // $result = Db::connect('shop')->table()->create($product); // $settleParams = $this->setAttrValue($data, $result->product_id, $productType, 0); // $settleParams['cate'] = $this->setMerCate($data['mer_cate_id'], $result->product_id, $data['mer_id']); // $settleParams['attr'] = $this->setAttr($data['attr'], $result->product_id); // if ($productType ==0 ) app()->make(ParameterValueRepository::class)->create($result->product_id, $data['params'] ?? [],$data['mer_id']); // $this->save($result->product_id, $settleParams, $content,$product,$productType); // if (in_array($productType, [0, 1])) { // if ($productType == 1) { //秒杀商品 // $dat = $this->setSeckillProduct($data); // $dat['product_id'] = $result->product_id; // $seckill = app()->make(StoreSeckillActiveRepository::class)->create($dat); // $activity_id = $seckill->seckill_active_id; // } // $product['price'] = $settleParams['data']['price']; // $product['mer_labels'] = $data['mer_labels']; // app()->make(SpuRepository::class)->create($product, $result->product_id, $activity_id, $productType); // } // $product = $result; // event('product.create',compact('product')); // return $result->product_id; // }); // halt($data); // // // 检验完整性 // try { // validate(StoreProductValidate::class)->check($param); // } catch (ValidateException $e) { // // 验证失败 输出错误信息 // return to_assign(1, $e->getError()); // } // // $this->model->addStoreProduct($param); // }else{ // // $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1]) // ->select(); // View::assign('store_brand', $store_brand); // return view(); // } // } public function adds() { if (request()->isAjax()) { $param = get_params(); // 检验完整性 try { validate(StoreProductValidate::class)->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $param['admin_id'] = $this->uid; $this->model->addStoreProduct($param); }else{ $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1]) ->select(); View::assign('store_brand', $store_brand); return view(); } } public function category_arr($id=0){ $where[]=['is_show','=',1]; $where[]=['mer_id','=',0]; if($id!=0){ $where[1]=['mer_id','=',$id]; } $list=Db::connect('shop')->table('eb_store_category')->where($where) ->field('store_category_id id,pid,cate_name name')->select(); $category_arr=create_tree_list(0,$list,0); return to_assign(0,'操作成功', $category_arr); } /** * 格式商品主体信息 * @Author:Qinii * @Date: 2020/9/15 * @param array $data * @return array */ public function setProduct(array $data) { $give_coupon_ids = ''; if (isset($data['give_coupon_ids']) && !empty($data['give_coupon_ids'])) { $gcoupon_ids = array_unique($data['give_coupon_ids']); $give_coupon_ids = implode(',', $gcoupon_ids); } if(isset($data['integral_rate'])){ $integral_rate = $data['integral_rate']; if($data['integral_rate'] < 0) $integral_rate = -1; if($data['integral_rate'] > 100) $integral_rate = 100; } $result = [ 'store_name' => $data['store_name'], 'image' => $data['image'], 'slider_image' => is_array($data['slider_image']) ? implode(',', $data['slider_image']) : '', 'store_info' => $data['store_info'] ?? '', 'keyword' => $data['keyword']??'', 'brand_id' => $data['brand_id'] ?? 0, 'cate_id' => $data['cate_id'] ?? 0, 'unit_name' => $data['unit_name']??'件', 'sort' => $data['sort'] ?? 0, 'is_show' => $data['is_show'] ?? 0, 'is_used' => (isset($data['status']) && $data['status'] == 1) ? 1 : 0, 'is_good' => $data['is_good'] ?? 0, 'video_link' => $data['video_link']??'', 'temp_id' => $data['delivery_free'] ? 0 : ($data['temp_id'] ?? 0), 'extension_type' => $data['extension_type']??0, 'spec_type' => $data['spec_type'] ?? 0, 'status' => $data['status']??0, 'give_coupon_ids' => $give_coupon_ids, 'mer_status' => $data['mer_status'], 'guarantee_template_id' => $data['guarantee_template_id']??0, 'is_gift_bag' => $data['is_gift_bag'] ?? 0, 'integral_rate' => $integral_rate ?? 0, 'delivery_way' => implode(',',$data['delivery_way']), 'delivery_free' => $data['delivery_free'] ?? 0, 'once_min_count' => $data['once_min_count'] ?? 0, 'once_max_count' => $data['once_max_count'] ?? 0, 'pay_limit' => $data['pay_limit'] ?? 0, 'svip_price_type' => $data['svip_price_type'] ?? 0, ]; if (isset($data['mer_id'])) $result['mer_id'] = $data['mer_id']; if (isset($data['old_product_id'])) $result['old_product_id'] = $data['old_product_id']; if (isset($data['product_type'])) $result['product_type'] = $data['product_type']; if (isset($data['type']) && $data['type']) $result['type'] = $data['type']; if (isset($data['param_temp_id'])) $result['param_temp_id'] = $data['param_temp_id']; if (isset($data['extend'])) $result['extend'] = $data['extend'] ? json_encode($data['extend'], JSON_UNESCAPED_UNICODE) :[]; return $result; } /** * 编辑 */ public function edit() { $param = get_params(); if (request()->isAjax()) { // 检验完整性 try { validate(StoreProductValidate::class)->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $this->model->editStoreProduct($param); }else{ $product_id = isset($param['product_id']) ? $param['product_id'] : 0; $detail = $this->model->getStoreProductById($product_id); if (!empty($detail)) { $detail['content'] = Db::table('cms_store_product_content')->where('product_id',$detail['product_id'])->value('content'); $detail['slider_image_arr'] = explode(',',$detail['slider_image']); // halt($detail['slider_image_arr']); View::assign('detail', $detail); $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1]) ->select(); View::assign('store_brand', $store_brand); return view(); } else{ throw new \think\exception\HttpException(404, '找不到页面'); } } } /** * 查看信息 */ public function read() { $param = get_params(); $product_id = isset($param['product_id']) ? $param['product_id'] : 0; $detail = $this->model->getStoreProductById($product_id); if (!empty($detail)) { $detail['content'] = Db::table('cms_store_product_content')->where('product_id',$detail['product_id'])->value('content'); $detail['slider_image_arr'] = explode(',',$detail['slider_image']); View::assign('detail', $detail); $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1]) ->select(); View::assign('store_brand', $store_brand); return view(); } else{ throw new \think\exception\HttpException(404, '找不到页面'); } } /** * 删除 * type=0,逻辑删除,默认 * type=1,物理删除 */ public function del() { $param = get_params(); $product_id = isset($param['product_id']) ? $param['product_id'] : 0; $type = isset($param['type']) ? $param['type'] : 0; $this->model->delStoreProductById($product_id,1); } /** * 商品列表 */ public function index() { if (request()->isAjax()) { $param = get_params(); $where = []; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['store_name','like','%'.$param['keywords'].'%']; } if (isset($param['store_cate']) && !empty($param['store_cate'])){ $where[]=['cate_id','=',$param['store_cate']]; } $list = $this->model->getStoreProductList($where,$param); foreach ($list as $k=>$v){ $www['brand_id'] = $v['brand_id']; $list[$k]['brand_id'] = Db::connect('shop')->table('eb_store_brand')->where($www)->value('brand_name'); $www2['store_category_id'] = $v['cate_id']; $list[$k]['cate_id'] = Db::connect('shop')->table('eb_store_category')->where($www2)->value('cate_name'); $list[$k]['shangjia'] = Db::table('cms_admin')->where('id',$this->uid)->value('nickname'); } return table_assign(0, '', $list); } else{ return view(); } } /** * 购买下单页面 */ public function buy() { $param = get_params(); $product_id = isset($param['product_id']) ? $param['product_id'] : 0; $detail = $this->model->getStoreProductById($product_id); if (!empty($detail)) { $detail['content'] = Db::table('cms_store_product_content')->where('product_id',$detail['product_id'])->value('content'); $detail['slider_image_arr'] = explode(',',$detail['slider_image']); View::assign('detail', $detail); $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1]) ->select(); View::assign('store_brand', $store_brand); return view(); } else{ throw new \think\exception\HttpException(404, '找不到页面'); } } // 请求微信接口的公用配置, 所以单独提出来 private function payment() { // 配置信息 $config = [ 'app_id' => 'wx0b3defb62f0f910b',//注意这个APPID只能是公众号的id,没有的话要去申请,并且在微信支付平台里绑定 'mch_id' => '1635725673',//商户号 'key' => '95d195Dcf6ec66156dfeeb4E7435faef',//支付秘钥 'secret' => 'c02aa7ad9e4a5c423862e068b6cb4ad4', 'notify_url' => Request::instance()->domain().'/api/PayNotify/notify',//异步回调通知地址 // 'notify_url' => 'http://an8r22.natappfree.cc/api/PayNotify/notify',//异步回调通知地址 ]; // 这个就是 easywechat 封装的了, 一行代码搞定, 照着写就行了 $app = Factory::payment($config); return $app; } // 向微信请求统一下单接口, 创建预支付订单 public function place_order() { $post = get_params(); $id = $post['product_id']; $number = $post['number']; // 因为没有先创建订单, 所以这里先生成一个随机的订单号, 存在 pay_log 里, 用来标识订单, 支付成功后再把这个订单号存到 order 表里 $order_sn = date('ymd').substr((string)time(),-5).substr(microtime(),2,5); // 根据 id 查出价格 $where['product_id'] = $id; Db::startTrans();//开启事务 $store_product = Db::table('cms_store_product')->where($where)->lock(true)->find(); if (empty($store_product)) { return to_assign(0,'查询的数据不存在'); } // 判断库存 if($store_product['stock'] < $number){ return to_assign(0,'超过库存数量'); } $post_price = bcmul(bcmul($store_product['price'],$number),'100'); $admin_id = get_login_admin('id'); // 创建 Paylog 记录 $param =[ 'appid' => 'wx0b3defb62f0f910b', 'mch_id' => '1635725673', 'out_trade_no' => $order_sn, 'product_id' => $id, 'number' => $number, 'admin_id' =>$admin_id, 'create_time'=>time(), 'total_fee' =>$post_price ]; Db::table('cms_store_product_paylog')->strict(false)->field(true)->insert($param); add_log('buy', $id, $param); $app = $this->payment(); // $total_fee = env('APP_DEBUG') ? 1 : $post_price; $total_fee = $post_price; // 用 easywechat 封装的方法请求微信的统一下单接口 $result = $app->order->unify([ 'trade_type' => 'NATIVE', // 原生支付即扫码支付,商户根据微信支付协议格式生成的二维码,用户通过微信“扫一扫”扫描二维码后即进入付款确认界面,输入密码即完成支付。 'body' => '采购商品-订单支付', // 这个就是会展示在用户手机上巨款界面的一句话, 随便写的 'out_trade_no' => $order_sn, 'total_fee' => $total_fee, 'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址 ]); if ($result['result_code'] == 'SUCCESS') { // 如果请求成功, 微信会返回一个 'code_url' 用于生成二维码 $code_url = $result['code_url']; // 生成二维码 // 引用二维码生成方法 require '../vendor/phpqrcode/phpqrcode.php'; $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 //生成二维码图片 // 判断是否有这个文件夹 没有的话就创建一个 if(!is_dir("static/qrcode")){ // 创建文件加 mkdir("static/qrcode"); } //设置二维码文件名 $filename = 'static/qrcode/'.time().rand(10000,9999999).'.png'; //生成二维码 \QRcode::png($code_url,$filename , $errorCorrectionLevel, $matrixPointSize, 2); // 订单编号, 用于在当前页面向微信服务器发起订单状态查询请求 $data['order_sn'] = $order_sn; $data['html'] = Request::instance()->domain().'/'.$filename; Db::commit(); return to_assign(200,'操作成功',$data); } return to_assign(0,'操作失败'); } // 查询订单支付状态 public function paid(Request $request) { $out_trade_no = get_params('out_trade_no'); $app = $this->payment(); // 用 easywechat 封装的方法请求微信 $result = $app->order->queryByOutTradeNumber($out_trade_no); if ($result['trade_state'] === 'SUCCESS'){ return to_assign(200,'支付成功'); }else{ return to_assign(0,'未支付'); } } // 采购订单列表 public function order(){ if (request()->isAjax()) { $param = get_params(); if(!isset($param['type'])){ $type = 1; }else{ $type = $param['type']; } if($type == 2){ $where[] = ['a.admin_id','=',$this->uid]; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['a.order_sn','=',$param['keywords']]; } if (isset($param['store_cate']) && !empty($param['store_cate'])){ $where[]=['b.cate_id','=',$param['store_cate']]; } $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit']; $list = Db::table('cms_store_product_order') ->alias('a') ->join('cms_store_product b','a.product_id = b.product_id') ->field('a.*,b.image,b.store_name') ->withAttr('paid_at',function ($value,$data){ return date('Y-m-d H:i:s',(int)$value); }) ->withAttr('product',function ($value,$data){ if($data['cart_id']){ $product_id = Db::table('cms_store_cart')->where('cart_id','in',$data['cart_id'])->column('product_id'); $product = Db::table('cms_store_product')->where('product_id','in',$product_id)->field('image,store_name')->select()->toArray(); return $product; }else{ $product[0]['store_name'] = $data['store_name']; $product[0]['image'] = $data['image']; return $product; } }) ->withAttr('name',function ($value,$data){ return Db::table('cms_admin')->where('id','=',$data['admin_id'])->value('nickname'); }) ->where($where) ->order('a.id desc,a.paid_at desc') ->paginate($rows, false, ['query' => $param]); return table_assign(0, '', $list); }else{ $where[] = ['admin_id','=',$this->uid]; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['out_trade_no','=',$param['keywords']]; } // if (isset($param['store_cate']) && !empty($param['store_cate'])){ // $where[]=['b.cate_id','=',$param['store_cate']]; // } $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit']; $list = Db::table('cms_store_product_paylog') ->withAttr('product',function ($value,$data){ if($data['cart_id']){ $product_id = Db::table('cms_store_cart')->where('cart_id','in',$data['cart_id'])->column('product_id'); $product = Db::table('cms_store_product')->where('product_id','in',$product_id)->field('image,store_name')->select()->toArray(); return $product; }else{ $arr = Db::table('cms_store_product')->where('product_id','=',$data['product_id'])->field('image,store_name')->find(); $product[0]['store_name'] = $arr['store_name']; $product[0]['image'] = $arr['image']; return $product; } }) ->withAttr('paid_at',function ($value,$data){ return '无'; }) ->withAttr('order_sn',function ($value,$data){ return $data['out_trade_no']; }) ->withAttr('total_fee',function ($value,$data){ return bcdiv($data['total_fee'],'100',2); }) ->withAttr('status',function ($value,$data){ return 0; }) ->withAttr('name',function ($value,$data){ return Db::table('cms_admin')->where('id','=',$data['admin_id'])->value('nickname'); }) ->where($where) ->whereNull('result_code') ->whereNull('return_code') ->order('id desc') ->paginate($rows, false, ['query' => $param]); return table_assign(0, '', $list); } } else{ return view(); } } //一条龙,丧葬商品,宴席,婚礼策划,婚纱礼服,婚娶服务,婚车租赁,婚品服务,宴席,水果类,蔬菜类,粮食类,生鲜类,蛋奶类,肉禽类,种子,化肥,农药,农机,农具,禽苗,家政保洁,家电维修,保姆月嫂,洗涤护理,法律咨询,求职招聘,开锁换钥,洗漱用品,化妆品,纸巾,洗衣粉,日常调料,酒水,饮料,零食,日杂百货,休闲食品,生活用品 // 加入购物车 public function joincar(){ $data = get_params(); $result = $this->cartCheck($data,$this->uid); if ($cart = $result['cart']) { //更新购物车 $cart_id = $cart['cart_id']; $cart_num = ['cart_num' => ($cart['cart_num'] + $data['cart_num'])]; $where = [ 'cart_id' => $cart_id, ]; $storeCart = Db::table('cms_store_cart')->where($where)->update($cart_num); } else { //添加购物车 $data['uid'] = $this->uid; $data['mer_id'] = $result['product']['admin_id']; $cart = $storeCart = Db::table('cms_store_cart')->insert($data); } return to_assign(200, '操作成功',$storeCart); } /** * TODO 普通商品加入购物车检测 * @param int $prodcutId * @param string $unique * @param int $cartNum * @author Qinii * @day 2020-10-20 */ public function cartCheck(array $data, $userInfo) { $cart = null; $where = [ 'status' => 1, 'is_used' => 1, 'product_type' => 0, 'is_gift_bag' => 0, ]; $where['product_id'] = $data['product_id']; unset($where['is_gift_bag']); $product = Db::table('cms_store_product')->where($where)->find(); if (!$product) throw new ValidateException('商品已下架'); // if ($product['type'] && !$data['is_new']) throw new ValidateException('虚拟商品不可加入购物车'); // $value_make = app()->make(ProductAttrValueRepository::class); // $sku = $value_make->getOptionByUnique($data['product_attr_unique']); // if (!$sku) throw new ValidateException('SKU不存在'); //立即购买 限购 if ($data['is_new']) { $cart_num = $data['cart_num']; } else { //加入购物车 //购物车现有 $_num = $this->productOnceCountCart($where['product_id'],$data['product_attr_unique'], $userInfo); $cart_num = $_num + $data['cart_num']; } if ($product['stock'] < $cart_num ){ return to_assign(0, '库存不足'); } //添加购物车 // if (!$data['is_new']) { // $cart = app()->make(StoreCartRepository::class)->getCartByProductSku($data['product_attr_unique'], $userInfo); // } return compact('product', 'cart'); } /** * TODO 购物车单商品数量 * @param $productId * @param $uid * @param $num * @author Qinii * @day 5/26/21 */ public function productOnceCountCart($productId,$product_attr_unique,$uid) { $where = [ 'is_pay' => 0, 'is_del' => 0, 'is_new' => 0, 'is_fail' => 0, 'is_dg' => 0, 'product_type' => 0, 'product_id' => $productId, 'uid' => $uid, ]; $cart_num = Db::table('cms_store_cart')->where($where)->sum('cart_num'); return $cart_num; } // 购物车列表 public function carlist(){ if (request()->isAjax()) { $param = get_params(); $where[] = ['a.uid','=',$this->uid]; $where[] = ['a.is_pay','=',0]; $where[] = ['a.is_del','=',0]; $where[] = ['a.is_fail','=',0]; $where[] = ['a.is_save','=',0]; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['b.nickname','like','%'.$param['keywords'].'%']; } if (isset($param['product_id']) && !empty($param['product_id'])){ $where[]=['c.product_id','=',$param['product_id']]; } $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit']; $list = Db::table('cms_store_cart') ->alias('a') ->join('cms_admin b','a.uid = b.id') ->join('cms_store_product c','a.product_id = c.product_id') ->field('a.*,c.image,b.nickname,c.store_name') ->where($where) ->order('a.cart_id desc') ->paginate($rows, false, ['query' => $param]); return table_assign(0, '', $list); } else{ return view(); } } // 购物车支付 public function paycar(){ $param = get_params(); $id = $param['cart_id']; Db::startTrans();//开启事务 $return = []; if($id){ if(strpos($id,',')){ //多选购物车 $ids = explode(',',$id); $post_price = '0'; $number = 0; foreach ($ids as $k=>$v){ // 根据 id 查出价格 $where['cart_id'] = $v; $where['uid'] = $this->uid; $cart = Db::table('cms_store_cart')->where($where)->lock(true)->find(); if (empty($cart)) { return to_assign(0,'查询的数据不存在'); } $wwww['product_id'] = $cart['product_id']; $store_product = Db::table('cms_store_product')->where($wwww)->lock(true)->find(); if (empty($store_product)) { return to_assign(0,'查询的数据不存在'); } $product[$k] = $store_product; $product[$k]['number'] = $cart['cart_num']; // 判断库存 if($store_product['stock'] < $number){ return to_assign(0,'超过库存数量'); } $number += $cart['cart_num']; $post_price = bcadd($post_price,bcmul(bcmul((string)$store_product['price'],(string)$number),'100')); // 删除购物车 Db::table('cms_store_cart')->where($where)->update(['is_save'=>'1']); } $return['number'] = $number; $ss[] = ['product_id','in',$ids]; $return['product'] = $product; }else{ // 根据 id 查出价格 $where['cart_id'] = $id; $where['uid'] = $this->uid; $cart = Db::table('cms_store_cart')->where($where)->lock(true)->find(); if (empty($cart)) { return to_assign(0,'查询的数据不存在'); } $number = $cart['cart_num']; $wwww['product_id'] = $cart['product_id']; $store_product = Db::table('cms_store_product')->where($wwww)->lock(true)->find(); if (empty($store_product)) { return to_assign(0,'查询的数据不存在'); } // 判断库存 if($store_product['stock'] < $number){ return to_assign(0,'超过库存数量'); } $post_price = bcmul(bcmul((string)$store_product['price'],(string)$number),'100'); // 删除购物车 Db::table('cms_store_cart')->where($where)->update(['is_save'=>'1']); $return['number'] = $number; $store_product['number'] = $number; $return['product'] = $store_product; } }else{ return to_assign(0, '请选择需要支付的商品'); } if( count($return['product']) != count($return['product'], 1) ){ $html = ""; foreach ($return['product'] as $k =>$v){ $html .= "
商品名称:".$v['store_name']."
商品数量:".$v['number']."
商品图片: "; } $return['html'] = $html; }else{ $return['html'] = "
商品名称:".$return['product']['store_name']."
商品数量:".$return['product']['number']."
商品图片:"; } // 因为没有先创建订单, 所以这里先生成一个随机的订单号, 存在 pay_log 里, 用来标识订单, 支付成功后再把这个订单号存到 order 表里 $order_sn = date('ymd').substr((string)time(),-5).substr(microtime(),2,5); $admin_id = get_login_admin('id'); // 创建 Paylog 记录 $param =[ 'appid' => 'wx0b3defb62f0f910b', 'mch_id' => '1635725673', 'out_trade_no' => $order_sn, 'product_id' => $id, 'number' => $number, 'admin_id' =>$admin_id, 'cart_id' =>$id, 'create_time'=>time(), 'total_fee' => $post_price ]; Db::table('cms_store_product_paylog')->strict(false)->field(true)->insert($param); add_log('buy', $id, $param); $app = $this->payment(); // $total_fee = env('APP_DEBUG') ? 1 : $post_price; $total_fee = $post_price; // 用 easywechat 封装的方法请求微信的统一下单接口 $result = $app->order->unify([ 'trade_type' => 'NATIVE', // 原生支付即扫码支付,商户根据微信支付协议格式生成的二维码,用户通过微信“扫一扫”扫描二维码后即进入付款确认界面,输入密码即完成支付。 'body' => '购物车商品-订单支付', // 这个就是会展示在用户手机上巨款界面的一句话, 随便写的 'out_trade_no' => $order_sn, 'total_fee' => $total_fee, 'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址 ]); if ($result['result_code'] == 'SUCCESS') { // 如果请求成功, 微信会返回一个 'code_url' 用于生成二维码 $code_url = $result['code_url']; // 生成二维码 // 引用二维码生成方法 require '../vendor/phpqrcode/phpqrcode.php'; $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 //生成二维码图片 // 判断是否有这个文件夹 没有的话就创建一个 if(!is_dir("static/qrcode")){ // 创建文件加 mkdir("static/qrcode"); } //设置二维码文件名 $filename = 'static/qrcode/'.time().rand(10000,9999999).'.png'; //生成二维码 \QRcode::png($code_url,$filename , $errorCorrectionLevel, $matrixPointSize, 2); // 订单编号, 用于在当前页面向微信服务器发起订单状态查询请求 $data['order_sn'] = $order_sn; $data['html'] = Request::instance()->domain().'/'.$filename; $data['data'] = $return; Db::commit(); return to_assign(200,'操作成功',$data); } Db::rollback(); return to_assign(0,'操作失败'); } // 删除购物车 public function delcar(){ $param = get_params(); $where['cart_id'] = $param['cart_id']; $res = Db::table('cms_store_cart')->where($where)->delete(); if($res){ return to_assign(200, '删除成功'); }else{ return to_assign(0, '删除失败'); } } // 提现记录列表 public function tixian(){ if (request()->isAjax()) { $param = get_params(); $where[] = ['admin_id','=',$this->uid]; $where[] = ['type','=',1]; if (isset($param['status']) && !empty($param['status'])){ $where[]=['status','=',$param['status']]; } $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit']; $list = Db::table('cms_admin_money_log') ->where($where) ->withAttr('nickname',function ($value,$data){ return Db::table('cms_admin')->where('id',$this->uid)->value('nickname'); }) ->withAttr('adminUser',function ($value,$data){ return Db::table('cms_admin')->where('id',$data['sh_admin_id'])->value('nickname'); }) ->withAttr('find',function ($value,$data){ $find = Db::table('cms_store_msg')->where('admin_id',$this->uid)->find(); return $find; }) ->withAttr('create_time',function ($value,$data){ return date('Y-m-d H:i:s',$value); }) ->order('create_time desc') ->paginate($rows, false, ['query' => $param]); return table_assign(0, '', $list); } else{ return view(); } } // 申请提现 public function withdrawal(){ if (request()->isAjax()) { $param = get_params(); $find = Db::table('cms_store_msg')->where('admin_id',$this->uid)->find(); if(empty($find)){ return to_assign(0, '请先设置收款方式'); }else{ if($param['skfs'] == 1){ if(empty($find['card_name']) || empty($find['bank']) || empty($find['card_code']) ){ return to_assign(0, '请先完善银行卡收款信息'); } } if($param['skfs'] == 2){ if(empty($find['wechat_name']) || empty($find['wechat_code']) || empty($find['wechat_img']) ){ return to_assign(0, '请先完善微信收款信息'); } } if($param['skfs'] == 3){ if(empty($find['alipay_name']) || empty($find['alipay_code']) || empty($find['alipay_img']) ){ return to_assign(0, '请先完善支付宝收款信息'); } } } if($param['num'] < 100 ){ return to_assign(0, '最少提现100'); }else{ $arr = Db::table('cms_admin_money')->where('admin_id',$this->uid)->find(); if($arr && $arr['money'] >= $param['num']){ //写入后台余额记录表 $log_arr['type'] = 1; $log_arr['admin_id'] = $this->uid; $log_arr['num'] = $param['num']; $log_arr['create_time'] = time(); $log_arr['status'] = 2; $log_arr['skfs'] = $param['skfs']; $res = Db::table('cms_admin_money_log')->where('admin_id',$this->uid)->strict(false)->field(true)->insert($log_arr); if($res){ return to_assign(200, '申请成功'); }else{ return to_assign(0, '申请失败'); } }else{ return to_assign(0, '余额不足'); } } }else{ return view(); } } // 同意提现 public function pass(){ $param = get_params(); if(empty($param['id'])){ return to_assign(0, '请选择要操作的数据'); }else{ // 启动事务 Db::startTrans(); try { $find = Db::table('cms_admin_money_log')->where('id',$param['id'])->find(); if(empty($find)){ return to_assign(0, '数据未找到'); } $money = Db::table('cms_admin_money')->where('id',$find['admin_id'])->find(); if($find['num'] > $money['money']){ return to_assign(0, '余额不足'); } $data['status'] = 3; $data['sh_admin_id'] = $this->uid; Db::table('cms_admin_money_log')->where('id',$param['id'])->update($data); // 扣除余额 Db::table('cms_admin_money')->where('id',$find['admin_id'])->dec('money',$find['num'])->update(); // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); return to_assign(0, '操作失败:' . $e->getMessage()); } return to_assign(200); } } // 拒绝提现 public function afuse(){ $param = get_params(); if(empty($param['id'])){ return to_assign(0, '请选择要操作的数据'); }else{ // 启动事务 Db::startTrans(); try { $find = Db::table('cms_admin_money_log')->where('id',$param['id'])->find(); if(empty($find)){ return to_assign(0, '数据未找到'); } $data['status'] = 4; $data['sh_admin_id'] = $this->uid; $data['fail_msg'] = $param['fail_msg']; Db::table('cms_admin_money_log')->where('id',$param['id'])->update($data); Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); return to_assign(0, '操作失败:' . $e->getMessage()); } return to_assign(200); } } // 预支付订单支付 public function payorder(){ $param = get_params(); $id = $param['id']; if($id){ $paylog = Db::table('cms_store_product_paylog')->where('id',$id)->find(); if(empty($paylog)){ return to_assign(0, '数据未找到'); } }else{ return to_assign(0, '请选择需要支付的订单'); } Db::startTrans();//开启事务 $app = $this->payment(); $order_sn = $paylog['out_trade_no']; // $total_fee = env('APP_DEBUG') ? 1 : $paylog['total_fee']; $total_fee = $paylog['total_fee']; // 用 easywechat 封装的方法请求微信的统一下单接口 $result = $app->order->unify([ 'trade_type' => 'NATIVE', // 原生支付即扫码支付,商户根据微信支付协议格式生成的二维码,用户通过微信“扫一扫”扫描二维码后即进入付款确认界面,输入密码即完成支付。 'body' => '购物车商品-订单支付', // 这个就是会展示在用户手机上巨款界面的一句话, 随便写的 'out_trade_no' => $order_sn, 'total_fee' => $total_fee, 'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址 ]); if ($result['result_code'] == 'SUCCESS') { // 如果请求成功, 微信会返回一个 'code_url' 用于生成二维码 $code_url = $result['code_url']; // 生成二维码 // 引用二维码生成方法 require '../vendor/phpqrcode/phpqrcode.php'; $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 //生成二维码图片 // 判断是否有这个文件夹 没有的话就创建一个 if(!is_dir("static/qrcode")){ // 创建文件加 mkdir("static/qrcode"); } //设置二维码文件名 $filename = 'static/qrcode/'.time().rand(10000,9999999).'.png'; //生成二维码 \QRcode::png($code_url,$filename , $errorCorrectionLevel, $matrixPointSize, 2); // 订单编号, 用于在当前页面向微信服务器发起订单状态查询请求 $data['order_sn'] = $order_sn; $data['html'] = Request::instance()->domain().'/'.$filename; Db::commit(); return to_assign(200,'操作成功',$data); } return to_assign(0,'操作失败'); } // 购物车保存 public function savecar(){ $param = get_params(); $id = $param['cart_id']; if($id){ if(strpos($id,',')){ //多选购物车 $ids = explode(',',$id); $post_price = '0'; $number = 0; foreach ($ids as $k=>$v){ // 根据 id 查出价格 $where['cart_id'] = $v; $where['uid'] = $this->uid; $cart = Db::table('cms_store_cart')->where($where)->find(); if (empty($cart)) { return to_assign(0,'查询的数据不存在'); } // 删除购物车 Db::table('cms_store_cart')->where($where)->update(['is_save'=>'1']); $wwww['product_id'] = $cart['product_id']; $store_product = Db::table('cms_store_product')->where($wwww)->lock(true)->find(); if (empty($store_product)) { return to_assign(0,'查询的数据不存在'); } // 判断库存 if($store_product['stock'] < $number){ return to_assign(0,'超过库存数量'); } $number += $cart['cart_num']; $post_price = bcadd($post_price,bcmul(bcmul((string)$store_product['price'],(string)$number),'100')); } }else{ // 根据 id 查出价格 $where['cart_id'] = $id; $where['uid'] = $this->uid; $cart = Db::table('cms_store_cart')->where($where)->lock(true)->find(); if (empty($cart)) { return to_assign(0,'查询的数据不存在'); } $number = $cart['cart_num']; $wwww['product_id'] = $cart['product_id']; $store_product = Db::table('cms_store_product')->where($wwww)->lock(true)->find(); if (empty($store_product)) { return to_assign(0,'查询的数据不存在'); } // 判断库存 if($store_product['stock'] < $number){ return to_assign(0,'超过库存数量'); } $post_price = bcmul(bcmul((string)$store_product['price'],(string)$number),'100'); // 删除购物车 Db::table('cms_store_cart')->where($where)->update(['is_save'=>'1']); } }else{ return to_assign(0, '请选择需要保存的商品'); } Db::startTrans();//开启事务 // 因为没有先创建订单, 所以这里先生成一个随机的订单号, 存在 pay_log 里, 用来标识订单, 支付成功后再把这个订单号存到 order 表里 $order_sn = date('ymd').substr((string)time(),-5).substr(microtime(),2,5); $admin_id = get_login_admin('id'); // 创建 Paylog 记录 $param =[ 'appid' => 'wx0b3defb62f0f910b', 'mch_id' => '1635725673', 'out_trade_no' => $order_sn, 'product_id' => $id, 'number' => $number, 'admin_id' =>$admin_id, 'cart_id' =>$id, 'create_time'=>time(), 'total_fee' => $post_price ]; $res = Db::table('cms_store_product_paylog')->strict(false)->field(true)->insert($param); if($res){ Db::commit(); return to_assign(200,'操作成功'); }else{ Db::rollback(); return to_assign(0,'操作失败'); } } // 资金流水 public function log(){ if (request()->isAjax()) { $param = get_params(); $where[] = ['a.admin_id','=',$this->uid]; $where[] = ['a.status','=','1']; $where[] = ['a.type','=','0']; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['b.order_sn','=',$param['keywords']]; } // if (isset($param['store_cate']) && !empty($param['store_cate'])){ // $where[]=['b.cate_id','=',$param['store_cate']]; // } $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit']; $list = Db::table('cms_admin_money_log') ->alias('a') ->join('cms_store_product_order b','a.order_id = b.id') ->withAttr('type',function ($value,$data){ return '订单支付'; }) ->withAttr('name',function ($value,$data){ return Db::table('cms_admin')->where('id',$data['admin_id'])->value('nickname'); }) ->withAttr('paid_at',function ($value,$data){ return date('Y-m-d H:i:s',(int)$value); }) ->where($where) ->order('a.id desc') ->paginate($rows, false, ['query' => $param]); return table_assign(0, '', $list); } else{ return view(); } } // 转账记录 public function zzlog(){ return view('store_product/tixian'); } // 收款方式 public function sktype(){ if (request()->isAjax()) { $param = get_params(); $param['admin_id'] = $this->uid; $where[] = ['admin_id','=',$this->uid]; $is_have = Db::table('cms_store_msg')->where($where)->find(); if($is_have){ $res = Db::table('cms_store_msg')->where($where)->strict(false)->field(true)->update($param); }else{ $res = Db::table('cms_store_msg')->strict(false)->field(true)->insert($param); } if($res){ return to_assign(200,'操作成功'); }else{ return to_assign(0,'操作失败'); } } else{ $where[] = ['admin_id','=',$this->uid]; $list = Db::table('cms_store_msg')->where($where)->find(); if(empty($list)){ $list['card_name'] = ''; $list['bank'] = ''; $list['card_code'] = ''; $list['wechat_name'] = ''; $list['wechat_code'] = ''; $list['wechat_img'] = ''; $list['alipay_name'] = ''; $list['alipay_code'] = ''; $list['alipay_img'] = ''; } View::assign('arr', $list); return view(); } } // 账单管理 public function zdlist(){ if (request()->isAjax()) { $param = get_params(); if(!isset($param['type'])){ $type = 1; }else{ $type = $param['type']; } //当天 $day_start_time = strtotime(date('Y-m-d').'00:00:00'); $day_end_time = strtotime(date('Y-m-d').'23:59:59'); //本月 $yue_start_time = mktime(0,0,0,date('m'),1,date('Y')); //当月1号 $yue_end_time = mktime(23,59,59,date('m'),date('t'),date('Y')); //当月最后一天 if($type == 2){ $list[0]['id'] = '1'; $list[0]['time'] = date('Y-m'); $where[] = ['type','=','0']; $where[] = ['status','=','1']; $where[] = $www[] = ['create_time','between',[$yue_start_time,$yue_end_time]]; $shouru = Db::table('cms_admin_money_log')->where($where)->sum('num'); $www[] = ['type','=','1']; $www[] = ['status','=','3']; $zhichu = Db::table('cms_admin_money_log')->where($www)->sum('num'); $list[0]['shouru'] = $shouru; $list[0]['zhichu'] = $zhichu; $list[0]['money'] = $shouru - $zhichu; }else{ $list[0]['id'] = '1'; $list[0]['time'] = date('Y-m-d'); $where[] = ['type','=','0']; $where[] = ['status','=','1']; $where[] = $www[] = ['create_time','between',[$day_start_time,$day_end_time]]; $shouru = Db::table('cms_admin_money_log')->where($where)->sum('num'); $www[] = ['type','=','1']; $www[] = ['status','=','3']; $zhichu = Db::table('cms_admin_money_log')->where($www)->sum('num'); $list[0]['shouru'] = $shouru; $list[0]['zhichu'] = $zhichu; $list[0]['money'] = $shouru - $zhichu; } return to_assign(0, '', $list); } else{ return view(); } } // 商品库存列表 public function stock(){ if (request()->isAjax()) { $param = get_params(); // $where[] = ['admin_id','=',$this->uid]; $where[] = ['status','=',1]; $where[] = ['is_del','=',0]; if (isset($param['keywords']) && !empty($param['keywords'])){ $where[]=['store_name|bar_code','like','%'.$param['keywords'].'%']; } if (isset($param['store_cate']) && !empty($param['store_cate'])){ $www[]=['b.cate_id','=',$param['store_cate']]; } $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit']; $order = empty($param['order']) ? 'product_id desc' : $param['order']; $bar_code_arr = Db::table('cms_store_product')->where($where)->order($order)->column('bar_code'); $www[]=['a.bar_code','in',$bar_code_arr]; $list = Db::connect('shop')->table('eb_store_product_attr_value') ->alias('a') ->join('eb_store_product b','a.product_id =b.product_id') ->field('a.*,b.cate_id,b.store_name') ->withAttr('shangjia',function ($value,$data){ return Db::table('cms_admin')->where('id',$this->uid)->value('nickname'); }) ->withAttr('cate_id',function ($value,$data){ $www2['store_category_id'] = $data['cate_id']; return Db::connect('shop')->table('eb_store_category')->where($www2)->value('cate_name'); }) ->where($www) ->order($order) ->paginate($rows, false, ['query' => $param]); return table_assign(0, '', $list); } else{ return view(); } } // }