| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils;
- use app\models\Cloud;
- use app\models\CloudSetting;
- use app\models\Goods;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\PtActivityOrder;
- use app\models\PtActivityOrderDetail;
- /**
- * 云打印
- * Class CloudPrint
- * @package app\utils
- */
- class CloudPrint
- {
- /**
- * TODO: 后续补充
- * @var array
- * 订单类型 0--普通订单 1--秒杀订单 2--拼团订单 3--砍价订单
- */
- private static $validType = [0, 1, 2, 3];
- /**
- * 下单时云打印订单
- * @param $order_id
- * @param $order_type
- * @param int $store_id
- */
- public static function doPrint($order_id, $order_type, $store_id = 1)
- {
- try {
- if (empty($order_id) || !in_array($order_type, self::$validType)) {
- \Yii::warning(['CLOUD PRINT NOT VALID, PARAMS: ', [$order_id, $order_type, $store_id]]);
- return;
- }
- switch ($order_type) {
- case 0:
- $order_info = Order::find()->select(['is_pay','mch_id','order_no','created_at','total_price', 'name',
- 'mobile', 'address', 'pay_price'])->where(['id' => $order_id])->asArray()->one();
- $order_detail_list = OrderDetail::find()
- ->alias('o')
- ->where(['o.order_id' => $order_id])
- ->select('o.goods_id,o.total_price,o.attr as o_attr,o.num,o.goods_id,o.order_id')
- ->asArray()
- ->all();
- break;
- case 1:
- $order_info = MsOrder::find()->select(['is_pay','mch_id','order_no','created_at','total_price', 'name',
- 'mobile', 'address', 'pay_price'])->where(['id' => $order_id])->asArray()->one();
- $order_detail_list = MsOrder::find()
- ->alias('o')
- ->where(['o.order_id' => $order_id])
- ->select('o.goods_id,o.total_price,o.attr as o_attr,o.num,o.goods_id,o.order_id')
- ->asArray()
- ->all();
- break;
- case 2:
- $order_info = PtActivityOrder::find()->select(['is_pay','store_id','order_no','created_at','total_price', 'name',
- 'mobile', 'address', 'pay_price'])->where(['id' => $order_id])->asArray()->one();
- $order_detail_list = PtActivityOrderDetail::find()
- ->alias('o')
- ->where(['o.order_id' => $order_id])
- ->select('o.goods_id,o.total_price,o.attr as o_attr,o.num,o.goods_id,o.order_id')
- ->asArray()
- ->all();
- break;
- case 3:
- $order_info = BargainOrder::find()->select(['is_pay','mch_id','order_no','created_at','total_price', 'name',
- 'mobile', 'address', 'pay_price'])->where(['id' => $order_id])->asArray()->one();
- $order_detail_list = BargainOrderDetail::find()
- ->alias('o')
- ->where(['o.order_id' => $order_id])
- ->select('o.goods_id,o.total_price,o.attr as o_attr,o.num,o.goods_id,o.order_id')
- ->asArray()
- ->all();
- break;
- default:
- return;
- break;
- }
- if($order_info['is_pay'] == 1){
- $order_info['is_pay'] = '已支付';
- }else{
- $order_info['is_pay'] = '货到付款';
- }
- $mch_id = $order_info['mch_id'];
- // 查询订单下单时是否开启打印
- $cloud_on = CloudSetting::find()->select(['type', 'cloud_id'])->where(['store_id' => $store_id,
- 'mch_id' => $mch_id,'is_delete' => 0])->asArray()->one();
- if (empty($cloud_on)) {
- return;
- }
- $is_res = json_decode($cloud_on['type']);
- $is_on = $is_res->pay;
- if ($is_on == 1 && $order_info['is_pay'] == 1) {
- foreach($order_detail_list as & $arr1){
- $g_id = $arr1['goods_id'];
- if ($order_type == 1) {
- $g_info = MsGoods::find()->alias('g')->where(['id' => $g_id])->select(['g.id','g.name',
- 'g.unit','g.price','g.attr'])->asArray()->one();
- } else if ($order_type == 2) {
- $g_info = PtGoods::find()->alias('g')->where(['id' => $g_id])->select(['g.id','g.name',
- 'g.unit','g.price','g.attr'])->asArray()->one();
- } else if ($order_type == 3) {
- $g_info = BargainGoods::find()->alias('g')->where(['id' => $g_id])->select(['g.id','g.name',
- 'g.unit','g.price','g.attr'])->asArray()->one();
- } else {
- $g_info = Goods::find()->alias('g')->where(['id' => $g_id])->select(['g.id','g.name','g.unit',
- 'g.price','g.attr'])->asArray()->one();
- }
- $arr1['id'] = $g_info['id'];
- $arr1['name'] = $g_info['name'];
- $arr1['unit'] = $g_info['unit'];
- $arr1['price'] = $g_info['price'];
- $arr1['attr'] = $g_info['attr'];
- }
- $cloud_id = $cloud_on['cloud_id'];
- $setting = Cloud::find()->select(['cloud_setting'])->where(['id' => $cloud_id])->asArray()->one();
- $setting_json = json_decode($setting['cloud_setting']);
-
- $send_html = self::createHtml($setting_json, $order_info, $order_detail_list, $order_type);
-
- // 抓取设置的云打印机参数
- if ($order_info['total_price'] > $setting_json->minimoney) {
- $name = $setting_json->name;
- $app_id = $setting_json->app_id;
- $app_key = $setting_json->app_key;
- $time_stamp = time();
- $sign = md5('app_id=' . $app_id . '&sign_type=MD5&time_stamp=' . $time_stamp . '&key=' . $app_key);
- $sign = strtoupper($sign);
- $token = self::getToken($sign,$app_id,$time_stamp);
- $token_arr = json_decode($token,true);
- $token = $token_arr['return_data']['access_token'];
- $cus_order_id = time().rand(100,999);
- //初始化
- $curl = curl_init();
- //设置抓取的ur
- curl_setopt($curl, CURLOPT_URL, 'https://mcp.jolimark.com/mcp/v3/sys/PrintRichHtmlCode');
- //设置头文件的信息作为数据流输出
- curl_setopt($curl, CURLOPT_HEADER, 1);
- //设置获取的信息以文件流的形式返回,而不是直接输出。
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
- //设置post方式提交
- curl_setopt($curl, CURLOPT_POST, 1);
-
- //设置post数据
- $post_data = array(
- "app_id" => $app_id,
- "access_token" => $token,
- "device_ids" => $name,
- "copies" => 1,
- "cus_orderid" => $cus_order_id,
- "bill_content" => $send_html,
- "paper_type" => 1,
- );
- // post提交的数据
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
- // 执行命令
- curl_exec($curl);
- curl_close($curl);
- }
- }
- } catch (\Throwable $e) {
- \Yii::warning($e->getMessage());
- }
- }
- /**
- * @param $attrStr
- * @param $searchAttrIds
- * @return bool
- */
- public static function getNo($attrStr, $searchAttrIds){
- $attr = json_decode($attrStr,true);
- foreach ($attr as $item){
- $attrIds = array_column($item, 'attr_id');
- if ($attrIds == $searchAttrIds){
- return $item['no'];
- }
- }
- return false;
- }
- /**
- * @param $setting_json
- * @param $order_info
- * @param $order_detail_list
- * @param $order_type
- * @return string
- */
- public static function createHtml($setting_json, $order_info, $order_detail_list, $order_type)
- {
- $order_title = $setting_json->order_title;
- $order_address = $setting_json->order_address;
- $order_mobile = $setting_json->order_mobile;
- $order_desc = $setting_json->order_desc;
- $no = 0;
- $order_count_price = 0;
- foreach ($order_detail_list as & $arr) {
- $no = $no + 1;
- $push_arr = [];
- $search = $arr['o_attr'];
- $search_arr = json_decode($search, true);
- foreach ($search_arr as & $vv) {
- array_push($push_arr, $vv['attr_id']);
- }
- $arr['no'] = self::getNo($arr['o_attr'], $push_arr);
- $g_id = $arr['goods_id'];
- $order_id = $arr['order_id'];
- if ($order_type == 1) {
- $g_info_attr = MsOrder::find()->where(['is_delete' => 0,'order_id' => $order_id,'goods_id' => $g_id])
- ->select(['attr'])->asArray()->one();
- } else if ($order_type == 2) {
- $g_info_attr = PtOrderDetail::find()->where(['is_delete' => 0,'order_id' => $order_id,'goods_id' => $g_id])
- ->select(['attr'])->asArray()->one();
- } else if ($order_type == 3) {
- $g_info_attr = BargainOrderDetail::find()->where(['is_delete' => 0,'order_id' => $order_id,'goods_id' => $g_id])
- ->select(['attr'])->asArray()->one();
- } else {
- $g_info_attr = OrderDetail::find()->where(['is_delete' => 0,'order_id' => $order_id,'goods_id' => $g_id])
- ->select(['attr'])->asArray()->one();
- }
- $attr_list = json_decode($g_info_attr['attr']);
- if (is_array($attr_list)){
- foreach ($attr_list as $attr){
- $attr_str=$attr->attr_group_name.":".$attr->attr_name;
- }
- }
- $tr2 = "<tr>
- <td class='fontsize20 text-cont style='font-size: 23px'>" . $no . "</td>"
- . "<td style='height: 45px;font-size: 23px;overflow: hidden'>" . $arr['name'] . "</td>"
- . "<td style='height: 45px;font-size: 23px;'>" . $attr_str . "</td>"
- . "<td style='height: 45px;font-size: 23px'>" . $arr['no'] . "</td>"
- . "<td style='height: 45px;font-size: 23px'>" . $arr['unit'] . "</td>"
- . "<td style='height: 45px;font-size: 23px'>" . $arr['num'] . "</td>"
- . "<td style='height: 45px;font-size: 23px'>" . $arr['total_price'] / $arr['num'] . "</td>"
- . "<td style='height: 45px;font-size: 23px'>" . $arr['total_price']
- . "<td style='height: 45px;font-size: 23px'></td style='height: 45px;font-size: 23px'></tr>";
- $order_count_price = $order_count_price + $arr['total_price'];
- }
- $html = "
- <!DOCTYPE html>
- <html lang='en'>
- <style>
- html, body, div, ul, li, h1, h2, h3, h4, h5, h6, p, dl, dt, dd, ol, form, input, textarea, th, td, select {
- margin: 0;
- padding: 0;
- }
- *{box-sizing: border-box;}
- html, body {
- min-height: 100%;
- }
-
- body {
- font-family: 'Microsoft YaHei';
- font-size:14px;
- color:#333;
- }
- h1, h2, h3, h4, h5, h6{font-weight:normal;}
- ul,ol {
- list-style: none;
- }
-
- img {
- border: none;
- vertical-align: middle;
- }
-
- a {
- text-decoration: none;
- color: #232323;
- }
-
- table {
- border-collapse: collapse;
- table-layout: fixed;
- }
-
- input, textarea {
- outline: none;
- border: none;
- }
-
- textarea {
- resize: none;
- overflow: auto;
- }
-
- .clearfix {
- zoom: 1;
- }
-
- .clearfix:after {
- content: '.';
- width: 0;
- height: 0;
- visibility: hidden;
- display: block;
- clear: both;
- overflow:hidden;
- }
-
- .fl {
- float: left
- }
-
- .fr {
- float: right
- }
-
- .tl {
- text-align: left;
- }
-
- .tc {
- text-align: center
- }
-
- .tr {
- text-align: right;
- }
-
- .ellipse {
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .inline{
- display: inline-block;
- *display: inline;
- *zoom: 1;
- }
- body {
- font-size: 16px;
- }
- .title {
- font-size: 36px;
- margin-left: 30%;
- }
- table {
- margin: 0 auto;
- }
- .column div {
- display: flex;
- justify-content: space-between;
- }
-
- .info td {
- padding-right: 10px;
- }
- td {
- height: 34px;
- }
- .serial {
- width: 5%;
- }
- .name {
- width: 25%;
- }
- .bar_code {
- width: 18%;
- }
- .unit{
- width: 7%;
- }
-
- .num {
- width: 7%;
- }
- .unit_price {
- width: 7%;
- }
- .money {
- width: 9%;
- }
-
- .desc{
- width: 12%;
- }
- .text-cont {
- text-align: center;
- }
- .fontsize20 {
- font-size: 20px;
- }
- .fontsize15 {
- font-size: 15px;
- white-space: nowrap;
- -moz-text-overflow: ellipsis;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .header {
- font-size: 18px;
- background-color: #CCC;
- }
-
- .img_box {
- display: flex;
- }
- .img_box img {
- width: 110px;
- height: 110px;
- background-color: #ccc;
- }
- .phone span {
- padding-right: 10px;
- }
- .flex {
- display: flex;
- }
- .flex_name {
- width: 74px;
- }
- .info{
- width: 96%;
- }
- .info2{
- width: 96%;
- float: left;
- margin-left: 2%;
- }
- .footer {
- width: 96%;
- margin: 0 auto;
- display: flex;
- }
- </style>
- <body style='width: 100%;margin-top: 5%'>
- <h2 class='title'>".$order_title."</h2>
- <table border='0' class='info'>
- <tr>
- <td style='width: 40%;font-size: 20px;' >订单号:" . $order_info['order_no'] . "</td>
- <td style='width: 30%;font-size: 20px;'>客户电话:" . $order_info['mobile'] . "</td>
- <td style='width: 30%;font-size: 20px;'>下单时间:" . date('Y-m-d', $order_info['created_at']) . "</td>
- </tr>
- <tr>
- <td style='width: 40%;font-size: 20px;'>客户:" . $order_info['name'] . "</td>
- <td style='width: 30%;font-size: 20px;'>客户地址:" . $order_info['address'] . "</td>
- <td style='width: 30%;font-size: 20px;'>支付方式:" . $order_info['is_pay'] . "</td>
- </tr>
- </table>
- <table border='1' class='info2'>
- <tr class='text-cont header'>
- <td class='serial' style='height: 45px;font-size: 25px'>序</td>
- <td class='name' style='height: 45px;font-size: 25px'>产品名称</td>
- <td class='name' style='height: 45px;font-size: 25px' >规格</td>
- <td class='bar_code' style='height: 45px;font-size: 25px' >条码</td>
- <td class='unit' style='height: 45px;font-size: 25px'>单位</td>
- <td class='num' style='height: 45px;font-size: 25px'>数量</td>
- <td class='unit_price' style='height: 45px;font-size: 25px'>单价</td>
- <td class='money' style='height: 45px;font-size: 25px'>金额</td>
- <td class='desc' style='height: 45px;font-size: 25px'>备注</td>
- </tr>
- " . $tr2 . "
- <tr>
- <td colspan='6' style='padding-left: 10px;height: 45px;font-size: 25px'>合计金额:</td>
- <td style='height: 45px;font-size: 25px'>" . $order_count_price . "</td>
- <td style='height: 45px;font-size: 25px'>" . "</td>
- </tr>
- </table>
- <div class='footer'>
- <div>
- <div class='phone' style='font-size: 20px;'>
- 联系电话:<span>".$order_mobile."</span>
- </div>
- <div class='flex' style='font-size: 20px;'>
- <div class='flex_name'>
- 地址:
- </div>
- <div style='font-size: 20px;'>
- ".$order_address."
- </div>
- </div>
- <div style='font-size: 20px;'>
- 产品注明:".$order_desc."
- </div>
- </div>
- </div>
- </body>
- </html>
-
- ";
- return $html;
- }
- /**
- * @param $sign
- * @param $app_id
- * @param $time_stamp
- * @return mixed
- */
- public static function getToken($sign, $app_id, $time_stamp)
- {
- // 初始化
- $curl = curl_init();
- $url = 'https://mcp.jolimark.com/mcp/v3/sys/GetAccessToken?app_id=' . $app_id . '&time_stamp=' . $time_stamp
- . '&sign=' . $sign . '&sign_type=MD5';
- // 设置抓取的url
- curl_setopt($curl, CURLOPT_URL, $url);
- // 设置头文件的信息作为数据流输出
- curl_setopt($curl, CURLOPT_HEADER, 0);
- //设置获取的信息以文件流的形式返回,而不是直接输出。
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
- // 执行命令
- $data = curl_exec($curl);
- // 关闭URL请求
- curl_close($curl);
- return $data;
- }
- }
|