Printer.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace addons\Printer\common\models;
  3. use addons\Printer\common\enums\PrinterEnum;
  4. use addons\Printer\common\logic\printer\yilianyun\Lib\Api\PrintService;
  5. use addons\Printer\common\logic\printer\yilianyun\Lib\Config\YlyConfig;
  6. use addons\Printer\common\logic\printer\yilianyun\Lib\Oauth\YlyOauthClient;
  7. use common\enums\RedisKeyEnum;
  8. use common\enums\StatusEnum;
  9. use common\models\mall\MallAddons;
  10. use Yii;
  11. /**
  12. * This is the model class for table "{{%addons_printer}}".
  13. *
  14. * @property int $id
  15. * @property int $mall_id 商城ID
  16. * @property string $name 打印机名称
  17. * @property string $type 打印类型,0:不打印,1:下单打印,2:支付打印,3:确认收货打印,4:后台打印
  18. * @property string $terminal 终端号
  19. * @property string $secret_key 秘钥
  20. * @property int|null $store_id O2O门店id
  21. * @property int|null $mch_id 商户id
  22. * @property int|null $printer_type 1是商场,2是门店;3是多商户
  23. * @property string $api_key api_key
  24. * @property string $user_id 打印机用户ID
  25. * @property string $client_id 应用id
  26. * @property string $url url
  27. * @property string $source 打印来源
  28. * @property string $order_printer_config 下单打印设置
  29. * @property string $pay_printer_config 支付打印设置
  30. * @property string $confirm_printer_config 确认收货打印设置
  31. * @property string $system_printer_config 后台手动打印设置
  32. * @property string $print_width 打印宽度
  33. * @property int $status 状态[-1:删除;0:禁用;1启用]
  34. * @property int $created_at 创建时间
  35. * @property int $updated_at 编辑时间
  36. */
  37. class Printer extends \common\models\base\BaseActiveRecord
  38. {
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%addons_printer}}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['mall_id'], 'required'],
  53. [['client_id'], 'string'],
  54. [['mall_id', 'store_id','mch_id', 'printer_type', 'status', 'created_at', 'updated_at'], 'integer'],
  55. [['name', 'type', 'terminal', 'secret_key', 'api_key', 'user_id', 'url', 'source'], 'string', 'max' => 48],
  56. [['print_width', ], 'string', 'max' => 255],
  57. [['order_printer_config', 'pay_printer_config', 'confirm_printer_config', 'system_printer_config'], 'string', 'max' => 120],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => 'ID',
  67. 'mall_id' => '商城ID',
  68. 'name' => '打印机名称',
  69. 'type' => '打印类型,0:不打印,1:下单打印,2:支付打印,3:确认收货打印,4:后台打印',
  70. 'terminal' => '终端号',
  71. 'secret_key' => '秘钥',
  72. 'store_id' => 'O2O门店id',
  73. 'mch_id' => '商户id',
  74. 'printer_type' => '1是商场,2是门店',
  75. 'api_key' => 'api_key',
  76. 'user_id' => '打印机用户ID',
  77. 'client_id' => '应用id',
  78. 'url' => 'url',
  79. 'source' => '打印来源',
  80. 'order_printer_config' => '下单打印设置',
  81. 'pay_printer_config' => '支付打印设置',
  82. 'confirm_printer_config' => '确认收货打印设置',
  83. 'system_printer_config' => '后台手动打印设置',
  84. 'status' => '状态[-1:删除;0:禁用;1启用]',
  85. 'created_at' => '创建时间',
  86. 'updated_at' => '编辑时间',
  87. 'print_width' => '打印宽度',
  88. ];
  89. }
  90. /**
  91. * 保存数据
  92. * @author huangpei
  93. * @param array $params
  94. * @author huangpei
  95. * @return bool|object
  96. */
  97. public static function setData($params){
  98. try{
  99. if(!empty($params['id'])){
  100. $model = self::findOne(['id' => $params['id']]);
  101. if (empty($model) || $model->status == StatusEnum::DELETE) throw new \Exception('打印机不存在或者已删除');
  102. }else{
  103. $model = new self();
  104. }
  105. if($params['set_type']==1){
  106. 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']);
  107. }elseif($params['set_type']==2){
  108. unset($params['status']);
  109. $params['type'] = [];
  110. if($params['order_printer_config']['is_printer']){
  111. $params['type'][] = 1;
  112. }
  113. if($params['pay_printer_config']['is_printer']){
  114. $params['type'][] = 2;
  115. }
  116. if($params['confirm_printer_config']['is_printer']){
  117. $params['type'][] = 3;
  118. }
  119. if($params['system_printer_config']['is_printer']){
  120. $params['type'][] = 4;
  121. }
  122. $params['source'] = empty($params['source']) ? '[]' : json_encode($params['source']);
  123. $params['order_printer_config'] = json_encode($params['order_printer_config']);
  124. $params['pay_printer_config'] = json_encode($params['pay_printer_config']);
  125. $params['confirm_printer_config'] = json_encode($params['confirm_printer_config']);
  126. $params['system_printer_config'] = json_encode($params['system_printer_config']);
  127. }
  128. $params['type'] = json_encode($params['type']);
  129. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  130. return $model;
  131. }catch (\Exception $e) {
  132. self::$static_error = $e->getMessage();
  133. return false;
  134. }
  135. }
  136. /**
  137. * 模板是否有被使用
  138. * @author huangpei
  139. * @param int $mall_id
  140. * @param int $template_id
  141. * @author huangpei
  142. * @return bool
  143. */
  144. public static function checkTemplateIsUse($mall_id,$template_id){
  145. $params = [
  146. 'select' => ['order_printer_config','pay_printer_config','confirm_printer_config','system_printer_config'],
  147. 'where' => [['=', 'mall_id', $mall_id],['<>','status',StatusEnum::DELETE]],
  148. 'order' => 'id desc'
  149. ];
  150. $query = self::find();
  151. self::paramPack($params,$query);
  152. $list = $query->asArray()->all();
  153. foreach($list as $val){
  154. $order_printer_config = json_decode($val['order_printer_config'], true);
  155. $pay_printer_config = json_decode($val['pay_printer_config'], true);
  156. $confirm_printer_config = json_decode($val['confirm_printer_config'], true);
  157. $system_printer_config = json_decode($val['system_printer_config'], true);
  158. if($order_printer_config['is_printer']==1 && $order_printer_config['template_id']==$template_id){
  159. return true;
  160. }
  161. if($pay_printer_config['is_printer']==1 && $pay_printer_config['template_id']==$template_id){
  162. return true;
  163. }
  164. if($confirm_printer_config['is_printer']==1 && $confirm_printer_config['template_id']==$template_id){
  165. return true;
  166. }
  167. if($system_printer_config['is_printer']==1 && $system_printer_config['template_id']==$template_id){
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. *
  175. */
  176. public static function checkPrinterStatus($mall_id){
  177. $list = self::lists([
  178. 'select' => 'system_printer_config',
  179. 'where' => [['=', 'mall_id', $mall_id],['=','status',StatusEnum::ENABLED]]
  180. ]);
  181. if(empty($list)){
  182. return false;
  183. }
  184. foreach($list as $val){ //var_dump($val);die;
  185. if($val){
  186. $val = json_decode($val['system_printer_config'], true);
  187. if($val['is_printer']){
  188. return true;
  189. }
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * @Author: ltm
  196. * @DateTime: 2022/6/15 0012 12:45
  197. * @Copyright: copyright (c) 2021 广东七件事集团
  198. * @param $data
  199. * @return string
  200. */
  201. public static function isInstall(array $params){
  202. //插件判断 是否显示卡密信息
  203. $isInstall = MallAddons::isInstall($params['mall_id'], PrinterEnum::ADDONS_NAME);
  204. $data = $params['order'];
  205. if($isInstall){
  206. $data[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 1;
  207. }else{
  208. $data[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 0;
  209. }
  210. return $data;
  211. }
  212. public static function isShowPrinter(array $params){
  213. //插件判断 是否显示打印机信息
  214. if (!isset($params['order']) || !is_array($params['order'])) return [];
  215. $isInstall = MallAddons::isInstall($params['mall_id'], PrinterEnum::ADDONS_NAME);
  216. foreach ($params['order'] as &$item){
  217. if($isInstall){
  218. $item[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 1;
  219. }else{
  220. $item[PrinterEnum::ADDONS_NAME]['is_show_printer'] = 0;
  221. }
  222. }
  223. return $params['order'];
  224. }
  225. public static function getPrintWidth($clientId,$machine_code,$clientSecret){
  226. $redis = \Yii::$app->redis;
  227. $cacheKey = RedisKeyEnum::suffix('printer_keys', $clientId);
  228. $access_token = $redis->get($cacheKey);
  229. $yilian_config = new YlyConfig($clientId, $clientSecret);
  230. if (empty($access_token)) {
  231. $client = new YlyOauthClient($yilian_config);
  232. $token = $client->getToken();
  233. $access_token = $token->access_token; //调用API凭证AccessToken 永久有效,请妥善保存.
  234. $redis->set($cacheKey,$access_token,'EX',30*24*3600-1);
  235. }
  236. $print = new PrintService($access_token, $yilian_config);
  237. return $print->printinfo($machine_code);
  238. }
  239. }