Browse Source

增加一个订单状态,待确认收货

lizhen_gitee 1 month ago
parent
commit
9c20ff867f

+ 20 - 2
addons/unishop/controller/Order.php

@@ -24,6 +24,7 @@ use addons\unishop\model\Address as AddressModel;
 use think\Hook;
 use think\Loader;
 use addons\epay\library\Service;
+use app\common\server\WechatMiniExpressSendSyncServer;
 
 /**
  * 订单
@@ -702,10 +703,27 @@ class Order extends Base
 
         $order->have_received = time();
         $order->save();
-        $this->success('已确认收货', true);
+        $this->success('已确认收货');
 
     }
 
+    //微信同步发货 查询
+    public function wechatSyncCheck(){
+        $order_id = input('order_id', 0);
+        $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
+
+        $orderModel = new \addons\unishop\model\Order();
+        $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
+
+        $result = WechatMiniExpressSendSyncServer::wechatSyncCheck($order);
+
+        if (! $result) {
+            $this->success(1,'');
+        }
+
+        $this->success(1,$result);
+    }
+
     /**
      * @ApiTitle    (发表评论/评价)
      * @ApiSummary  (发表评论/评价)
@@ -912,7 +930,7 @@ class Order extends Base
                 ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
 
             if ($order) {
-                $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
+                $order = $order->append(['state', 'paidtime', 'deliveredtime',  'hexiaotime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
 
                 $order['state_text'] = $orderModel->getStatetextAttr($order['state'],$order['refund_status']);//状态中文
 

+ 1 - 0
application/admin/controller/unishop/Order.php

@@ -76,6 +76,7 @@ class Order extends Backend
 
                 $item['have_paid_status'] = $item['have_paid'];
                 $item['have_delivered_status'] = $item['have_delivered'];
+                $item['have_hexiao_status'] = $item['have_hexiao'];
                 $item['have_received_status'] = $item['have_received'];
                 $item['have_commented_status'] = $item['have_commented'];
             }

+ 2 - 1
application/admin/lang/zh-cn/unishop/order.php

@@ -24,7 +24,8 @@ return [
     'No'      => '否',
     'Have_delivered'   => '已发货',
     'Have_commented'   => '已评论',
-    'Have_received'    => '已核销',
+    'Have_hexiao'    => '已核销',
+    'Have_received'    => '已收货',
     'Pay time'          => '支付时间',
     'Time: %s'          => '时间: %s',
     'Delivered time'          => '发货时间',

+ 5 - 0
application/admin/model/unishop/Order.php

@@ -83,6 +83,11 @@ class Order extends Model
         return $value === '' ? 0 : ($value && !is_numeric($value) ? strtotime($value) : $value);
     }
 
+    protected function setHaveHexiaoAttr($value)
+    {
+        return $value === '' ? 0 : ($value && !is_numeric($value) ? strtotime($value) : $value);
+    }
+
 
     /**
      * 关联用户

+ 4 - 3
application/api/controller/Unishop.php

@@ -160,7 +160,7 @@ class Unishop extends Api
             exit;
         }
         //你可以在此编写订单逻辑
-        $rs = $this->order_notify_do($out_trade_no,$paytype);
+        $rs = $this->order_notify_do($out_trade_no,$paytype,$data['transaction_id']);
         if($rs === false){
             //不论结果都应返回success
             return $pay->success()->send();
@@ -177,7 +177,7 @@ class Unishop extends Api
     }
 
     //异步逻辑
-    private function order_notify_do($out_trade_no,$paytype){
+    private function order_notify_do($out_trade_no,$paytype,$transaction_id){
 
         Db::startTrans();
         $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
@@ -204,6 +204,7 @@ class Unishop extends Api
             'have_paid'=>$nowtime,
             'pay_type' =>$paytype_enum[$paytype],
             'pay_out_trade_no' =>$out_trade_no,
+            'pay_transaction_id' =>$transaction_id,
 
             //水饺项目自动发货
             'have_delivered' => $nowtime
@@ -216,7 +217,7 @@ class Unishop extends Api
         //逻辑结束
 
         //状态
-        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
+        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time(),'transaction_id'=>$transaction_id]);
         if($ros === false) {
             Db::rollback();
             return false;

+ 70 - 0
application/common/model/Client_.php

@@ -0,0 +1,70 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeshop开源商城系统
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | gitee下载:https://gitee.com/likeshop_gitee
+// | github下载:https://github.com/likeshop-github
+// | 访问官网:https://www.likeshop.cn
+// | 访问社区:https://home.likeshop.cn
+// | 访问手册:http://doc.likeshop.cn
+// | 微信公众号:likeshop技术社区
+// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
+// |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
+// | 禁止对系统程序代码以任何目的,任何形式的再发布
+// | likeshop团队版权所有并拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeshop.cn.team
+// +----------------------------------------------------------------------
+
+
+namespace app\common\model;
+
+
+class Client_
+{
+    const mnp = 1;//小程序
+    const oa = 2;//公众号
+    const ios = 3;
+    const android = 4;
+    const pc = 5;
+    const h5 = 6;//h5(非微信环境h5)
+
+    function getName($value)
+    {
+        switch ($value) {
+            case self::mnp:
+                $name = '小程序';
+                break;
+            case self::h5:
+                $name = 'h5';
+                break;
+            case self::ios:
+                $name = '苹果';
+                break;
+            case self::android:
+                $name = '安卓';
+                break;
+            case self::oa:
+                $name = '公众号';
+                break;
+        }
+        return $name;
+    }
+
+    public static function getClient($type = true)
+    {
+        $desc = [
+            self::pc      => 'pc商城',
+            self::h5      => 'h5商城',
+            self::oa      => '公众号商城',
+            self::mnp     => '小程序商城',
+            self::ios     => '苹果APP商城',
+            self::android => '安卓APP商城',
+        ];
+        if ($type === true) {
+            return $desc;
+        }
+        return $desc[$type] ?? '未知';
+    }
+}

+ 211 - 0
application/common/server/WeChatServer.php

@@ -0,0 +1,211 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeshop开源商城系统
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | gitee下载:https://gitee.com/likeshop_gitee
+// | github下载:https://github.com/likeshop-github
+// | 访问官网:https://www.likeshop.cn
+// | 访问社区:https://home.likeshop.cn
+// | 访问手册:http://doc.likeshop.cn
+// | 微信公众号:likeshop技术社区
+// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
+// |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
+// | 禁止对系统程序代码以任何目的,任何形式的再发布
+// | likeshop团队版权所有并拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeshop.cn.team
+// +----------------------------------------------------------------------
+
+
+namespace app\common\server;
+
+use app\common\model\Client_;
+use app\common\model\Pay;
+use think\Exception;
+
+/**
+ * 微信服务 服务类
+ * Class WeChatServer
+ * @package app\common\server
+ */
+class WeChatServer
+{
+    /**
+     * @notes 获取小程序配置
+     * @return array
+     * @author suny
+     * @date 2021/7/13 6:35 下午
+     */
+    public static function getMnpConfig()
+    {
+        $epay = get_addon_config('epay');
+
+        $config = [
+            'app_id' => $epay['wechat']['miniapp_id'],
+            'secret' => $epay['wechat']['app_secret'],
+            'mch_id' => $epay['wechat']['mch_id'],
+            'key' => $epay['wechat']['key'],
+            'response_type' => 'array',
+            'log' => [
+                'level' => 'debug',
+                'file' => 'wechat.log'
+            ],
+        ];
+        return $config;
+    }
+
+    /**
+     * @notes 获取微信公众号配置
+     * @return array
+     * @author suny
+     * @date 2021/7/13 6:35 下午
+     */
+    public static function getOaConfig()
+    {
+
+        $config = [
+            'app_id' => ConfigServer::get('oa', 'app_id'),
+            'secret' => ConfigServer::get('oa', 'secret'),
+            'mch_id' => ConfigServer::get('oa', 'mch_id'),
+            'key' => ConfigServer::get('oa', 'key'),
+            'token' => ConfigServer::get('oa', 'token', ''),
+            'response_type' => 'array',
+            'log' => [
+                'level' => 'debug',
+                'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
+            ],
+        ];
+        return $config;
+    }
+
+
+    //微信开放平台->web应用
+    public static function getOpWebConfig()
+    {
+        $config = [
+            'app_id'   => ConfigServer::get('op', 'web_app_id'),
+            'secret'   => ConfigServer::get('op', 'web_secret'),
+        ];
+        return $config;
+    }
+
+
+    /**
+     * @notes 获取url
+     * @param $str
+     * @return string
+     * @author suny
+     * @date 2021/7/13 6:35 下午
+     */
+    public static function getUrl($str)
+    {
+
+        return (string)url($str, [], false, true);
+    }
+
+    /**
+     * @notes 根据不同来源获取支付配置
+     * @param $order_source
+     * @return array
+     * @throws Exception
+     * @author suny
+     * @date 2021/7/13 6:36 下午
+     */
+    public static function getPayConfigBySource($order_source)
+    {
+
+        $notify_url = '';
+        switch ($order_source) {
+            case Client_::mnp:
+                $notify_url = self::getUrl('pay/notifyMnp');
+                break;
+            case Client_::oa:
+            case Client_::h5:
+            case Client_::pc:
+                $notify_url = self::getUrl('pay/notifyOa');
+                break;
+            case Client_::android:
+            case Client_::ios:
+                $notify_url = self::getUrl('pay/notifyApp');
+                break;
+        }
+        $config = self::getPayConfig($order_source);
+        if (empty($config) ||
+            empty($config['key']) ||
+            empty($config['mch_id']) ||
+            empty($config['app_id']) ||
+            empty($config['secret'])
+        ) {
+            throw new Exception('请在后台配置好微信支付!');
+        }
+
+        return [
+            'config' => $config,
+            'notify_url' => $notify_url,
+        ];
+    }
+
+    //===================================支付配置=======================================================
+
+    /**
+     * @notes 微信支付设置 H5支付 appid 可以是公众号appid
+     * @param $client
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author suny
+     * @date 2021/7/13 6:36 下午
+     */
+    public static function getPayConfig($client)
+    {
+
+        switch ($client) {
+            case Client_::mnp:
+                $appid = ConfigServer::get('mnp', 'app_id');
+                $secret = ConfigServer::get('mnp', 'secret');
+                break;
+            case Client_::oa:
+            case Client_::h5:
+            case Client_::pc:
+                $appid = ConfigServer::get('oa', 'app_id');
+                $secret = ConfigServer::get('oa', 'secret');
+                break;
+            case Client_::android:
+            case Client_::ios:
+                $appid = ConfigServer::get('op', 'app_id');
+                $secret = ConfigServer::get('op', 'secret');
+                break;
+            default:
+                $appid = '';
+                $secret = '';
+        }
+
+        $pay = Pay::where(['code' => 'wechat'])->find()->toArray();
+
+        $config = [
+            'app_id' => $appid,
+            'secret' => $secret,
+            'mch_id' => $pay['config']['mch_id'] ?? '',
+            'key' => $pay['config']['pay_sign_key'] ?? '',
+            'cert_path' => $pay['config']['apiclient_cert'] ?? '',
+            'key_path' => $pay['config']['apiclient_key'] ?? '',
+            'response_type' => 'array',
+            'log' => [
+                'level' => 'debug',
+                'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
+            ],
+        ];
+
+        if (is_cli()) {
+            if (!defined('ROOT_PATH')) {
+                define('ROOT_PATH', __DIR__);
+            }
+            $config['cert_path'] = ROOT_PATH . '/public/' . $pay['config']['apiclient_cert'];
+            $config['key_path'] = ROOT_PATH . '/public/' . $pay['config']['apiclient_key'];
+        }
+
+        return $config;
+    }
+}

+ 62 - 0
application/common/server/WechatMiniExpressSendSyncServer.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace app\common\server;
+
+use EasyWeChat\Factory;
+
+/**
+ * @notes 小程序 发货信息录入
+ * @notes https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
+ * author lbzy
+ * @datetime 2023-07-14 14:51:23
+ * @class WechatMiniExpressSendSyncService
+ * @package app\common\service
+ */
+class WechatMiniExpressSendSyncServer
+{
+    static private $miniApp;
+    
+    private static function getMiniApp()
+    {
+        static::$miniApp = static::$miniApp ? : Factory::miniProgram(WeChatServer::getMnpConfig());
+        
+        return static::$miniApp;
+    }
+    
+
+    
+    private static function getToken($app)
+    {
+        return $app->access_token->getToken()['access_token'];
+    }
+    
+    static function wechatSyncCheck($order)
+    {
+        $app    = static::getMiniApp();
+    
+        $data = [
+            'transaction_id' => $order['pay_transaction_id'] ?? '',
+        ];
+    
+        $token = static::getToken($app);
+    
+        $result_content = $app->http_client->post("/wxa/sec/order/get_order?access_token={$token}", [
+            'body'   => json_encode($data, JSON_UNESCAPED_UNICODE),
+        ])->getBody()->getContents();
+    
+        $result = json_decode($result_content, true);
+    
+        if (! isset($result['errcode']) || $result['errcode'] != 0) {
+            // token失效 不标记失败 等下次执行
+            if (isset($result['errcode']) && $result['errcode'] == 40001) {
+//                Log::write("等待下次执行" . ($result_content ? : "发货录入发生错误"), 'wechat_mini_express_sync_check');
+                return false;
+            }
+//            Log::write($result_content ? : "发货录入发生错误", 'wechat_mini_express_sync_check');
+            return [];
+        }
+        
+        return $result;
+        
+    }
+}