Initial commit

This commit is contained in:
易焱
2021-03-13 18:42:01 +08:00
commit d9a3c376d5
3243 changed files with 627198 additions and 0 deletions
@@ -0,0 +1,17 @@
<?php
namespace Shop\Model;
use Think\Model\RelationModel;
class ShopCartModel extends RelationModel{
//关联模型
Protected $_link=array(
"cms_product"=>array(
'mapping_type' => self::BELONGS_TO,
'foreign_key' => 'product_id',
),
"cat"=>array(
'mapping_type' => self::BELONGS_TO,
'foreign_key' => 'catid',
),
);
}
@@ -0,0 +1,306 @@
<?php
namespace Shop\Model;
use Think\Model\RelationModel;
class ShopOrderModel extends RelationModel{
private $order_status_config=array(
/*
* status_code为订单状态码
* set_table 为查询的对象表
* sql_where 为查询的对象表的判断条件
*/
/*******************以1开头的是正常购买状态************************/
//待付款
array(
"status_code"=>101,
"status_name"=>"待付款",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_pay"=>0,
),
),
//待付款
array(
"status_code"=>111,
"status_name"=>"对公转账",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_pay"=>0,
"is_offline_pay"=>1,
),
),
//待发货
array(
"status_code"=>102,
"status_name"=>"待发货",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_pay"=>1,
"is_delivery"=>0,
),
),
//待收货
array(
"status_code"=>103,
"status_name"=>"待收货",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_delivery"=>1,
"is_receipt"=>0,
),
),
//已收货
array(
"status_code"=>104,
"status_name"=>"已收货",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_delivery"=>1,
"is_receipt"=>1,
),
),
/*******************以2为开头的为取消订单************************/
//取消订单(包含用户取消,超时取消)
array(
"status_code"=>201,
"status_name"=>"已取消",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_cancel"=>1,
),
),
/*******************以3为开头的为退货状态 状态表为副表:order_refund(退款表)************************/
//用户申请退款中
array(
"status_code"=>301,
"status_name"=>"申请退款中",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>0,//(is_rerund_agree为1表示管理员同意,为0表示还未操作但不代表不同意)
"is_rerund_disagree"=>0,
),
),
//平台同意退款
array(
"status_code"=>302,
"status_name"=>"平台同意退款",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>1,
),
),
//费用已原路返还
array(
"status_code"=>303,
"status_name"=>"退款成功,费用已原路返还",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>1,
"is_back_money"=>1,
),
),
//平台不同意退款
array(
"status_code"=>311,
"status_name"=>"平台不同意退款",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>0,
"is_rerund_disagree"=>1,
),
),
//用户取消退款
array(
"status_code"=>321,
"status_name"=>"用户取消退款",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund_cancel"=>1,
),
),
);
//关联模型
Protected $_link=array(
//收货地址表
"shop_order_address"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//发票信息
"shop_order_invoice"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//收票地址表
"shop_order_invoice_address"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//商品详情表
"shop_order_detail"=>array(
'mapping_type' => self::HAS_MANY,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//退款表
"shop_order_refund"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//日志表
"shop_order_log"=>array(
'mapping_type' => self::HAS_MANY,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//会员表
"member" => array(
'mapping_type' => self::BELONGS_TO,
"parent_key" => 'member_id', //parent_key一定是主键,否则会列不出子表的数据
'foreign_key' => 'member_id',
),
);
/*设置订单状态
* $order(Array) 是一个订单信息的所有字段,包含关联的数据表
*/
public function setOrderState($order_id){
//这里一定要将数组倒序一下,订单最初状态判断条件很少。否则会造成判断不准确
$order_status_config = array_reverse($this->order_status_config);
foreach($order_status_config as $v){
$sql_where=$v["sql_where"];
$sql_where["order_id"]=$order_id;
//这里一定是配置里的表名,因为退款表是shop_order_refund表
$count=M($v["set_table"])->where($sql_where)->count();
if($count){
M("shop_order")->save(array(
"order_id"=>$order_id,
"status_code"=>$v["status_code"],
"status_name"=>$v["status_name"],
));
break;
}
}
}
//前台的增加订单
public function add_order($data,$sh_address,$order_invoice,$sp_address){
$member_id = session("member.member_id");
// 创建订单 计算金额
$money=0;
foreach($data as $k=>$v){
$money += ($v[cms_product]['price2']?$v[cms_product]['price2']:$v[cms_product]['price'])* $v["quantity"];
}
$data_order["money"] = number_format($money,2, '.', '');//这里第4个参数一定要加一个空,否则金额超出1000会转换为类似于2,155.00的,会增加一个千分位,存入数据只保存了前面2
$data_order["pay_money"] = number_format($money,2, '.', '');
$data_order["order_num"] = date("YmdHis").$member_id.rand(1000,9999);
$data_order["member_id"] = $member_id;
$data_order["addtime"] = time();
$data_order["remark"] = I("remark");
//收货地址表
$data_order["shop_order_address"] = D("shop_order_address")->create($sh_address);
//发票表
if($order_invoice){
$data_order["shop_order_invoice"] = D("shop_order_invoice")->create($order_invoice);
}
//收票地址表
if($order_invoice){
$data_order["shop_order_invoice_address"] = D("shop_order_invoice_address")->create($sp_address);
}
//商品详情表
foreach ($data as $k=>$v){
$price=($v[cms_product]['price2']?$v[cms_product]['price2']:$v[cms_product]['price']);
$money=($v[cms_product]['price2']?$v[cms_product]['price2']:$v[cms_product]['price']) * $v["quantity"];
$data_order["shop_order_detail"][$k]["thumb"] = $v["cms_product"]["thumb"];
$data_order["shop_order_detail"][$k]["title"] = $v["cms_product"]["title"];
$data_order["shop_order_detail"][$k]["type_no"] = $v["cms_product"]["type_no"];
$data_order["shop_order_detail"][$k]["price"] = number_format($price,2);
$data_order["shop_order_detail"][$k]["product_id"] = $v["cms_product"]["id"];
$data_order["shop_order_detail"][$k]["catid"] = $v["cat"]["catid"];
$data_order["shop_order_detail"][$k]["quantity"] = $v["quantity"];
$data_order["shop_order_detail"][$k]["money"] = number_format($money,2);
//更新订单销量
M("cms_product")->where(array("id"=>$v["cms_product"]["id"]))->setInc('sales',$v["quantity"]);
//
}
$order_id = $this->relation(true)->add($data_order);
//订单日志
$this->log($order_id,"用户创建订单");
//修改订单状态
$this->setOrderState($order_id);
// 删除购物车数据
$ids = session("confirmation_order_ids");
$ids[] = 0;
$where["id"] = array("in",$ids);
M("shop_cart")->where($where)->delete();
session("confirmation_order_ids",null);
return $order_id;
}
//取消订单
public function cancelOrder($order_id,$content){
$this->save(array(
"order_id"=>$order_id,
"is_cancel"=>1,
));
//订单日志
$this->log($order_id,$content);
//更新状态
$this->setOrderState($order_id);
}
//确认收货
public function confirmReceipt($order_id,$content){
$this->save(array(
"order_id"=>$order_id,
"is_receipt"=>1,
));
//订单日志
$this->log($order_id,$content);
//更新状态
$this->setOrderState($order_id);
}
//删除订单
public function deleteOrder($order_id){
$this->relation(true)->delete($order_id);
}
//订单日志
public function log($order_id,$content){
M("shop_order_log")->add(array(
"addtime"=>time(),
"order_id"=>$order_id,
"content"=>$content,
));
}
//自动收货
public function autoReceipt(){
$auto_time=C("shop.auto_receipt_time");
$auto_time=$auto_time?$auto_time:5;
$auto_time=$auto_time*3600*24;
$ago_time=time()-$auto_time;
$data=$this->where(array(
"status_code"=>103,
"delivery_time"=>array("lt",$ago_time),
))->getField("order_id",true);
foreach($data as $v){
$this->confirmReceipt($v,"超时系统自动收货");
}
}
//自动取消
public function autoCancelOrder(){
$auto_time=C("shop.order_timeout");
//$auto_time=$auto_time?$auto_time:60;
$auto_time=$auto_time*3600;
$ago_time=time()-$auto_time;
$data=$this->where(array(
"status_code"=>101,
"addtime"=>array("lt",$ago_time),
))->getField("order_id",true);
foreach($data as $v){
$this->cancelOrder($v,"超时系统取消订单");
}
}
}
@@ -0,0 +1,169 @@
<?php
namespace Shop\Model;
use Think\Model\RelationModel;
use Org\pay\lib;
class ShopOrderModel extends RelationModel {
public function __construct() {
parent::__construct();
ini_set('date.timezone', 'Asia/Shanghai');
//error_reporting(E_ERROR);
require_once "./ThinkPHP/Library/Vendor/Wxpay/lib/WxPay.Api.php";
require_once "./ThinkPHP/Library/Vendor/Wxpay/example/WxPay.NativePay.php";
require_once './ThinkPHP/Library/Vendor/Wxpay/example/WxPay.JsApiPay.php';
require_once './ThinkPHP/Library/Vendor/Wxpay/example/log.php';
require_once './ThinkPHP/Library/Vendor/Wxpay/example/phpqrcode/phpqrcode.php';
require_once './ThinkPHP/Library/Vendor/Wxpay/example/WxpayService.php';
}
//关联模型
Protected $_link = array(
"shop_order_detail" => array(
'mapping_type' => self::HAS_MANY,
"parent_key" => 'order_id', //parent_key一定是主键,否则会列不出子表的数据
'foreign_key' => 'order_id',
),
);
//验证
protected $_validate = array(
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
array("provinceName", "require", "送货地址不能为空"),
array("cityName", "require", "送货地址不能为空"),
array("countryName", "require", "送货地址不能为空"),
array("detailInfo", "require", "送货地址不能为空"),
array("userName", "require", "送货地址不能为空"),
array("telNumber", "require", "送货地址不能为空"),
);
public function Wxpay_order_h5($order) {
$input = new \WxpayService();
$input->SetAttach($order["order_id"]);
$input->setTotalFee($order["money"]*100);
$input->setOutTradeNo($order["order_num"]."1");
$input->setOrderName("维健乐");
$input->setWapUrl("http://" . $_SERVER['HTTP_HOST']);
$input->setWapName("维健乐");
$input->setNotifyUrl("http://" . $_SERVER['HTTP_HOST'] . "/payment.php");
$where["order_id"] = $order["order_id"];
$where["order_num"] = $order["order_num"];
$rs = M("transaction")->where($where)->find();
if (!$rs) {
$data = array();
$data["order_type"] = 1;
$data["pay_type"] = 1;
$data["order_num"] = $order["order_num"];
$data["order_id"] = $order["order_id"];
$data["money"] = $order["money"];
$data["addtime"] = $order["addtime"];
M("transaction")->add($data);
}
return $input->createJsBizPackage();
}
public function Wxpay_order_jssdk($order) {
//①、获取用户openid
$tools = new \JsApiPay();
$openId = session("member_r.openid");
// ②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody("维健乐");
$input->SetAttach($order["order_id"]);
$input->SetOut_trade_no($order["order_num"]."2");
$input->SetTotal_fee($order["money"]*100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("维健乐");
$input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/payment.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$orders = \WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($orders);
$where["order_id"] = $order["order_id"];
$where["order_num"] = $order["order_num"];
$rs = M("transaction")->where($where)->find();
if(!$rs){
$data = array();
$data["order_type"] = 1;
$data["pay_type"] = 1;
$data["order_num"] = $order["order_num"];
$data["order_id"] = $order["order_id"];
$data["money"] = $order["money"];
$data["addtime"] = $order["addtime"];
M("transaction")->add($data);
}
return $jsApiParameters;
}
public function Wxpay_order($order) {
//模式二
/**
* 流程:
* 1、调用统一下单,取得code_url,生成二维码
* 2、用户扫描二维码,进行支付
* 3、支付完成之后,微信服务器会通知支付成功 $order["money"]*100
* 4、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php)
*/
$notify = new \NativePay();
$input = new \WxPayUnifiedOrder();
$input->SetBody("维健乐");
$input->SetAttach($order["order_id"]);
$input->SetOut_trade_no($order["order_num"]."3");
$input->SetTotal_fee($order["money"]*100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("维健乐");
$input->SetNotify_url("http://" . $_SERVER['HTTP_HOST'] . "/payment.php");
$input->SetTrade_type("NATIVE");
$input->SetProduct_id();
$result = $notify->GetPayUrl($input);
$url = $result["code_url"];
$where["order_id"] = $order["order_id"];
$where["order_num"] = $order["order_num"];
$rs = M("transaction")->where($where)->find();
if (!$rs) {
$data = array();
$data["order_type"] = 1;
$data["pay_type"] = 1;
$data["order_num"] = $order["order_num"];
$data["order_id"] = $order["order_id"];
$data["money"] = $order["money"];
$data["addtime"] = $order["addtime"];
M("transaction")->add($data);
}
return $url;
}
public function qrcode($url) {
\QRcode::png($url);
}
function getIPaddress(){
$IPaddress='';
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$IPaddress = $_SERVER["HTTP_CLIENT_IP"];
} else {
$IPaddress = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$IPaddress = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$IPaddress = getenv("HTTP_CLIENT_IP");
} else {
$IPaddress = getenv("REMOTE_ADDR");
}
}
return $IPaddress;
}
}