getToken("", $params);
return $token;
}
//获取打印机当前状态
public function getPrinterStatus($printer)
{
$client = new \YlyOauthClient();
//获取签名
$time = time();
$params['appid'] = $printer['api_name'];
$params['appsecret'] = $printer['api_key'];
$sign = $client->getSign($time, $params);
$token = $this->getAccessToken($printer['api_name'], $printer['api_key']);
$checkData['client_id'] = $printer['api_name'];//应用id,在易联云开放平台创建应用时获取
$checkData['access_token'] = $token;//通过请求获取令牌接口获取到的令牌信息
$checkData['machine_code'] = $printer['printer_sn'];//打印机终端号
$checkData['sign'] = $sign;//获取到的签名
$checkData['id'] = $client->uuid4();//UUID4;详情参考官方文档
$checkData['timestamp'] = $time;//当前服务器时间戳
$check = $client->send(self::$checkStatusUrl, $checkData);
//返回的数组为state时,表示打印机处于参数没有错误 0离线 1在线 2缺纸
//返回的数组非state时,打印机出现错误,详细的错误信息请参考官方文档
return $check;
}
//打印
/*
* $type 1线上小程序 2收银机
*/
public function prints($storetag, $orderInfo, $sell_goods, $paytype, $addressInfo, $printerSet)
{
$allmoney = $orderInfo['allmoney'];
if (!empty($printerSet['title'])) {
$content = "--" . $printerSet['title'] . "--";
} else {
$printerSet['title'] = '欢迎光临';
$content = "--" . $printerSet['title'] . "--";
}
$content .= "" . $storetag . "";
$content .= "订单时间:" . date("Y-m-d H:i", $orderInfo['addtime']) . "\n";
$content .= "订单编号:" . $orderInfo['order_id'] . "\n";
$content .= '名称 数量 单价' . "\n";
$content .= str_repeat('*', 14) . str_repeat("*", 17);
$content .= "
";
//此处对商品进行处理
foreach ($sell_goods as &$value) {
$value['goods_name'] = $value['goods_name'] ? $value['goods_name'] : '无码商品';
if (!empty($value['promotion_info'])) {
$promotion_info = json_decode($value['promotion_info'], true);
if (!empty($value['skuinfo'])) {
$sku = json_decode($value['skuinfo'], true)[0]['sku'];
$goods_name = $value['goods_name'] . ":" . $sku . '(促销' . $promotion_info['bundle_number'] . '件)';
} else if (!empty($value['sku_json_info'])) {
$sku = json_decode($value['sku_json_info'], true)['name'];
$goods_name = $value['goods_name'] . ":" . $sku . '(促销' . $promotion_info['bundle_number'] . '件)';
} else {
$goods_name = "{$value['goods_name']}" . '(促销' . $promotion_info['bundle_number'] . '件)';//商品名
}
} else {
if (!empty($value['skuinfo'])) {
$sku = json_decode($value['skuinfo'], true)[0]['sku'];
$goods_name = $value['goods_name'] . ":" . $sku;
} else if (!empty($value['sku_json_info'])) {
$sku = json_decode($value['sku_json_info'], true)['name'];
$goods_name = $value['goods_name'] . ":" . $sku;
} else {
$goods_name = "{$value['goods_name']}";//商品名
}
}
$content .= "| " . $goods_name . " |
";
if ($value['can_exchange_weigh'] == 1) {
$value['num'] = round($value['num']);
} else {
$value['num'] = round($value['num'], 2) . 'kg';
}
$content .= " | x" . $value['num'] . " | " . $value['price'] . " |
";
}
$content .= "
";
$content .= str_repeat('.', 32);
// $content .= "这是二维码内容";
$content .= "@@2合计:" . $allmoney . "元" . "\n";
$content .= "优惠券:" . $orderInfo['exchange_money'] . "元" . " \n";
if (!empty($orderInfo['vipid'])) {
$content .= "会员折扣:" . $orderInfo['dismoney'] . "元" . "\n";
}
if ($orderInfo['is_meet_reduce'] == 2 && $orderInfo['reduce_money']) {
$content .= "满减优惠:" . $orderInfo['reduce_money'] . "元" . "\n";
}
if ($orderInfo['isneedsend'] == 2) {
$content .= "配送费:" . $orderInfo['distribution_fee'] . "元" . "\n";
}
$content .= "抹零:" . $orderInfo['small_changes_money'] . "元" . "\n";
$content .= "支付方式:" . $paytype . " \n";
$PayOrder = new PayOrder();
if ($paytype == '组合支付') {
$combinationInfo = $PayOrder->getFindPayList($orderInfo['pay_type'], $orderInfo['all_money'], $orderInfo['id']);
foreach ($combinationInfo as &$val) {
$content .= $val['name'] . ":" . $val['money'] . "\n";
}
if (!empty($orderInfo['integral_dis_money'])) {
$content .= "积分抵扣:" . $orderInfo['integral_dis_money'] . "\n";
$content .= "扣除积分:" . $orderInfo['integral'] . "\n";
}
}
if ($paytype == '会员卡支付') {
$vip = M('retail_vip')->where(array('id' => $orderInfo['vipid']))->find();
$vipname = mb_substr($vip['nickname'], 0, 1, 'utf-8');
$name = '';
for ($i = 1; $i < mb_strlen($vip['nickname'], 'utf-8'); $i++) {
$name .= '*';
}
$vipname = $vipname . $name;
$vipname = $vipname ? $vipname : $vip['name'];
$vipphone = str_replace(substr($vip['mobile_phone'], 3, 4), '****', $vip['mobile_phone']);
$content .= str_repeat('-', 14) . str_repeat("-", 17);
$content .= "会员姓名:" . $vipname . " \n";
$content .= "会员手机号:" . $vipphone . " \n";
$content .= "会员余额:" . $vip['balance'] . " \n";
}
//根据不同订单打印不同内容
if (!empty($addressInfo)) {
$content .= str_repeat('.', 14) . str_repeat(".", 17);
$content .= "地址:" . $addressInfo['address'] . "\n";
$content .= "@@2联系方式:" . $addressInfo['phone'] . "\n";
$content .= $addressInfo['name'] . "先生/女士\n";
}
if (!empty($orderInfo['remark'])) {
$content .= str_repeat('.', 14) . str_repeat(".", 17);
$content .= "@@2买家留言:" . $orderInfo['remark'] . "\n";
}
if (!empty($printerSet['footer'])) {
$content .= "--" . $printerSet['footer'] . "--";
}
//以下不用修改
$client = new \YlyOauthClient();
//获取签名
$time = time();
// $params['appid'] = self::$appid;
// $params['appsecret'] = self::$appsecret;
$params['appid'] = $printerSet['api_name'];
$params['appsecret'] = $printerSet['api_key'];
$sign = $client->getSign($time, $params);
$token = $this->getAccessToken($printerSet['api_name'], $printerSet['api_key']);
// print_r($token);
$checkData['client_id'] = $printerSet['api_name'];//应用id,在易联云开放平台创建应用时获取
$checkData['access_token'] = $token;//通过请求获取令牌接口获取到的令牌信息
$checkData['machine_code'] = $printerSet['printer_sn'];//打印机终端号
$checkData['content'] = $content;
$checkData['sign'] = $sign;//获取到的签名
$checkData['id'] = $client->uuid4();//UUID4;详情参考官方文档
$checkData['timestamp'] = $time;//当前服务器时间戳
$checkData['origin_id'] = $time;//当前服务器时间戳
$check = $client->send(self::$printUrl, $checkData);
// print_r($check);die;
}
/*打印退款订单
* @param $storetag 门店名称
* @param $orderInfo 订单信息
* @param $sell_goods 商品信息
* @param $paytype 支付方式
* @param $addressInfo 地址
* @param $printerSet 打印机配置
* @param $stime 打印时间
*/
public function printsRefundOrder($storetag, $orderInfo, $sell_goods, $paytype, $addressInfo, $printerSet)
{
$allmoney = $orderInfo['allmoney'];
$stime = date('Y/m/d H:i:s', time());
if (!empty($printerSet['title'])) {
$content = "--" . $printerSet['title'] . "--";
} else {
$printerSet['title'] = '欢迎光临';
$content = "--" . $printerSet['title'] . "--";
}
$content .= "" . $storetag . "";
$content .= "退款时间:" . date("Y-m-d H:i", $orderInfo['addtime']) . "\n";
$content .= "订单编号:" . $orderInfo['order_id'] . "\n";
$content .= '名称 数量 单价' . "\n";
$content .= str_repeat('*', 14) . str_repeat("*", 17);
$content .= "";
//此处对商品进行处理
foreach ($sell_goods as &$value) {
$value['goods_name'] = $value['goods_name'] ? $value['goods_name'] : '无码商品';
if (!empty($value['skuinfo'])) {
$sku = json_decode($value['skuinfo'], true)[0]['sku'];
$goods_name = $value['goods_name'] . ":" . $sku;
} else if (!empty($value['sku_json_info'])) {
$sku = json_decode($value['sku_json_info'], true)['name'];
$goods_name = $value['goods_name'] . ":" . $sku;
} else {
$goods_name = "{$value['goods_name']}";//商品名
}
$content .= "| " . $goods_name . " |
";
if ($value['can_exchange_weigh'] == 1) {
$value['num'] = round($value['num']);
} else {
$value['num'] = round($value['num'], 2) . 'kg';
}
$content .= " | x" . $value['num'] . " | " . $value['price'] . " |
";
}
$content .= "
";
$content .= str_repeat('.', 32);
$content .= "支付方式:" . $paytype . " \n";
$PayOrder = new PayOrder();
if ($paytype == '组合支付') {
$combinationInfo = $PayOrder->getFindPayList($orderInfo['pay_type'], $orderInfo['all_money'], $orderInfo['id']);
foreach ($combinationInfo as &$val) {
$content .= $val['name'] . ":" . $val['money'] . "\n";
}
}
$content .= "实退金额:" . $allmoney . "\n";
$content .= "打印时间:" . $stime . "\n";
if (!empty($printerSet['footer'])) {
$content .= "--" . $printerSet['footer'] . "--";
}
//以下不用修改
$client = new \YlyOauthClient();
//获取签名
$time = time();
// $params['appid'] = self::$appid;
// $params['appsecret'] = self::$appsecret;
$params['appid'] = $printerSet['api_name'];
$params['appsecret'] = $printerSet['api_key'];
$sign = $client->getSign($time, $params);
$token = $this->getAccessToken($printerSet['api_name'], $printerSet['api_key']);
// print_r($token);
$checkData['client_id'] = $printerSet['api_name'];//应用id,在易联云开放平台创建应用时获取
$checkData['access_token'] = $token;//通过请求获取令牌接口获取到的令牌信息
$checkData['machine_code'] = $printerSet['printer_sn'];//打印机终端号
$checkData['content'] = $content;
$checkData['sign'] = $sign;//获取到的签名
$checkData['id'] = $client->uuid4();//UUID4;详情参考官方文档
$checkData['timestamp'] = $time;//当前服务器时间戳
$checkData['origin_id'] = $time;//当前服务器时间戳
$check = $client->send(self::$printUrl, $checkData);
// print_r($check);die;
}
public function getAccessToken($appid, $appsecret)
{
$map['appid'] = $appid;
$map['appsecret'] = $appsecret;
$find = M("yiliancloud")->where($map)->find();
if ($find) {
return $find['access_token'];
}
}
public function dovipRechargePrints($info, $printerSet)
{
$content = "{$printerSet['title']}";//title为自定义头部信息,如果没有显示门店名
$content .= "订单类型:会员充值" . "\n";
$content .= "支付方式:{$info['paytype']}" . "\n";
$content .= "订单实付:{$info['money']}" . "\n";
$content .= "充值获得:{$info['allmoney']}" . "\n";
$content .= "会员账号:{$info['phone']}" . "\n";
$content .= "会员余额:{$info['balance']}" . "\n";
$content .= "充值时间:{$info['addtime']}" . "\n";
$content .= "操作人员:{$info['user']}" . "\n";
$content .= "打印时间:{$info['ctime']}" . "\n";
$content .= "{$printerSet['footer']}";//title为自定义头部信息,如果没有显示门店名
//以下不用修改
$client = new \YlyOauthClient();
//获取签名
$time = time();
// $params['appid'] = self::$appid;
// $params['appsecret'] = self::$appsecret;
$params['appid'] = $printerSet['api_name'];
$params['appsecret'] = $printerSet['api_key'];
$sign = $client->getSign($time, $params);
$token = $this->getAccessToken($printerSet['api_name'], $printerSet['api_key']);
$checkData['client_id'] = $printerSet['api_name'];//应用id,在易联云开放平台创建应用时获取
$checkData['access_token'] = $token;//通过请求获取令牌接口获取到的令牌信息
$checkData['machine_code'] = $printerSet['printer_sn'];//打印机终端号
$checkData['content'] = $content;
$checkData['sign'] = $sign;//获取到的签名
$checkData['id'] = $client->uuid4();//UUID4;详情参考官方文档
$checkData['timestamp'] = $time;//当前服务器时间戳
$checkData['origin_id'] = $time;//当前服务器时间戳
$check = $client->send(self::$printUrl, $checkData);
}
/* 获取更新token */
public function refreshToken($refreshToken, $printer)
{
$client = new \YlyOauthClient();
$result = $client->refreshToken($refreshToken, $printer);
if ($result['error']) {
//更新token出错
$data['status'] = 0;
$data['msg'] = $result['error_description'];
} else {
//更新成功,强制更新配置表
$where['appid'] = $printer['appid'];
$where['appsecret'] = $printer['appsecret'];
$updateData['access_token'] = $result['access_token'];
$updateData['refresh_token'] = $result['refresh_token'];
$updateData['updatetime'] = time();
$update = M("yiliancloud")->where($where)->save($updateData);
if ($update) {
$data['status'] = 1;
$data['token'] = $result['access_token'];
} else {
$data['status'] = 0;
$data['msg'] = '更新失败';
}
}
return $data;
}
}