| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- namespace addons\Printer\common\models;
- use addons\Printer\common\enums\PrinterEnum;
- use addons\Printer\common\logic\printer\yilianyun\Lib\Api\PrintService;
- use addons\Printer\common\logic\printer\yilianyun\Lib\Config\YlyConfig;
- use addons\Printer\common\logic\printer\yilianyun\Lib\Oauth\YlyOauthClient;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use Yii;
- /**
- * This is the model class for table "{{%addons_printer}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property string $name 打印机名称
- * @property string $type 打印类型,0:不打印,1:下单打印,2:支付打印,3:确认收货打印,4:后台打印
- * @property string $terminal 终端号
- * @property string $secret_key 秘钥
- * @property int|null $store_id O2O门店id
- * @property int|null $mch_id 商户id
- * @property int|null $printer_type 1是商场,2是门店;3是多商户
- * @property string $api_key api_key
- * @property string $user_id 打印机用户ID
- * @property string $client_id 应用id
- * @property string $url url
- * @property string $source 打印来源
- * @property string $order_printer_config 下单打印设置
- * @property string $pay_printer_config 支付打印设置
- * @property string $confirm_printer_config 确认收货打印设置
- * @property string $system_printer_config 后台手动打印设置
- * @property string $print_width 打印宽度
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 编辑时间
- */
- class Printer extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_printer}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['client_id'], 'string'],
- [['mall_id', 'store_id','mch_id', 'printer_type', 'status', 'created_at', 'updated_at'], 'integer'],
- [['name', 'type', 'terminal', 'secret_key', 'api_key', 'user_id', 'url', 'source'], 'string', 'max' => 48],
- [['print_width', ], 'string', 'max' => 255],
- [['order_printer_config', 'pay_printer_config', 'confirm_printer_config', 'system_printer_config'], 'string', 'max' => 120],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'name' => '打印机名称',
- 'type' => '打印类型,0:不打印,1:下单打印,2:支付打印,3:确认收货打印,4:后台打印',
- 'terminal' => '终端号',
- 'secret_key' => '秘钥',
- 'store_id' => 'O2O门店id',
- 'mch_id' => '商户id',
- 'printer_type' => '1是商场,2是门店',
- 'api_key' => 'api_key',
- 'user_id' => '打印机用户ID',
- 'client_id' => '应用id',
- 'url' => 'url',
- 'source' => '打印来源',
- 'order_printer_config' => '下单打印设置',
- 'pay_printer_config' => '支付打印设置',
- 'confirm_printer_config' => '确认收货打印设置',
- 'system_printer_config' => '后台手动打印设置',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '编辑时间',
- 'print_width' => '打印宽度',
- ];
- }
- /**
- * 保存数据
- * @author huangpei
- * @param array $params
- * @author huangpei
- * @return bool|object
- */
- public static function setData($params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['id' => $params['id']]);
- if (empty($model) || $model->status == StatusEnum::DELETE) throw new \Exception('打印机不存在或者已删除');
- }else{
- $model = new self();
- }
- if($params['set_type']==1){
- unset($params['set_type'],$params['terminal'],$params['secret_key'],$params['api_key'],$params['user_id'],$params['url'],$params['source'],$params['order_printer_config'],$params['pay_printer_config'],$params['confirm_printer_config'],$params['system_printer_config']);
- }elseif($params['set_type']==2){
- unset($params['status']);
- $params['type'] = [];
- if($params['order_printer_config']['is_printer']){
- $params['type'][] = 1;
- }
- if($params['pay_printer_config']['is_printer']){
- $params['type'][] = 2;
- }
- if($params['confirm_printer_config']['is_printer']){
- $params['type'][] = 3;
- }
- if($params['system_printer_config']['is_printer']){
- $params['type'][] = 4;
- }
- $params['source'] = empty($params['source']) ? '[]' : json_encode($params['source']);
- $params['order_printer_config'] = json_encode($params['order_printer_config']);
- $params['pay_printer_config'] = json_encode($params['pay_printer_config']);
- $params['confirm_printer_config'] = json_encode($params['confirm_printer_config']);
- $params['system_printer_config'] = json_encode($params['system_printer_config']);
- }
- $params['type'] = json_encode($params['type']);
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- }catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 模板是否有被使用
- * @author huangpei
- * @param int $mall_id
- * @param int $template_id
- * @author huangpei
- * @return bool
- */
- public static function checkTemplateIsUse($mall_id,$template_id){
- $params = [
- 'select' => ['order_printer_config','pay_printer_config','confirm_printer_config','system_printer_config'],
- 'where' => [['=', 'mall_id', $mall_id],['<>','status',StatusEnum::DELETE]],
- 'order' => 'id desc'
- ];
- $query = self::find();
- self::paramPack($params,$query);
- $list = $query->asArray()->all();
- foreach($list as $val){
- $order_printer_config = json_decode($val['order_printer_config'], true);
- $pay_printer_config = json_decode($val['pay_printer_config'], true);
- $confirm_printer_config = json_decode($val['confirm_printer_config'], true);
- $system_printer_config = json_decode($val['system_printer_config'], true);
- if($order_printer_config['is_printer']==1 && $order_printer_config['template_id']==$template_id){
- return true;
- }
- if($pay_printer_config['is_printer']==1 && $pay_printer_config['template_id']==$template_id){
- return true;
- }
- if($confirm_printer_config['is_printer']==1 && $confirm_printer_config['template_id']==$template_id){
- return true;
- }
- if($system_printer_config['is_printer']==1 && $system_printer_config['template_id']==$template_id){
- return true;
- }
- }
- return false;
- }
- /**
- *
- */
- public static function checkPrinterStatus($mall_id){
- $list = self::lists([
- 'select' => 'system_printer_config',
- 'where' => [['=', 'mall_id', $mall_id],['=','status',StatusEnum::ENABLED]]
- ]);
- if(empty($list)){
- return false;
- }
- foreach($list as $val){ //var_dump($val);die;
- if($val){
- $val = json_decode($val['system_printer_config'], true);
- if($val['is_printer']){
- return true;
- }
- }
- }
- return false;
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/6/15 0012 12:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function isInstall(array $params){
- //插件判断 是否显示卡密信息
- $isInstall = MallAddons::isInstall($params['mall_id'], PrinterEnum::ADDONS_NAME);
- $data = $params['order'];
- if($isInstall){
- $data[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 1;
- }else{
- $data[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 0;
- }
- return $data;
- }
- public static function isShowPrinter(array $params){
- //插件判断 是否显示打印机信息
- if (!isset($params['order']) || !is_array($params['order'])) return [];
- $isInstall = MallAddons::isInstall($params['mall_id'], PrinterEnum::ADDONS_NAME);
- foreach ($params['order'] as &$item){
- if($isInstall){
- $item[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 1;
- }else{
- $item[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 0;
- }
- }
- return $params['order'];
- }
- public static function getPrintWidth($clientId,$machine_code,$clientSecret){
- $redis = \Yii::$app->redis;
- $cacheKey = RedisKeyEnum::suffix('printer_keys', $clientId);
- $access_token = $redis->get($cacheKey);
- $yilian_config = new YlyConfig($clientId, $clientSecret);
- if (empty($access_token)) {
- $client = new YlyOauthClient($yilian_config);
- $token = $client->getToken();
- $access_token = $token->access_token; //调用API凭证AccessToken 永久有效,请妥善保存.
- $redis->set($cacheKey,$access_token,'EX',30*24*3600-1);
- }
- $print = new PrintService($access_token, $yilian_config);
- return $print->printinfo($machine_code);
- }
- }
|