PrinterService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\sys;
  12. use app\dict\sys\PrinterDict;
  13. use app\model\sys\SysPrinter;
  14. use app\service\core\printer\CorePrinterService;
  15. use core\base\BaseAdminService;
  16. use core\exception\CommonException;
  17. use think\facade\Db;
  18. /**
  19. * 小票打印机服务层
  20. * Class PrinterService
  21. * @package app\service\admin\sys
  22. */
  23. class PrinterService extends BaseAdminService
  24. {
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->model = new SysPrinter();
  29. }
  30. /**
  31. * 获取小票打印机分页列表
  32. * @param array $where
  33. * @return array
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function getPage(array $where = [])
  37. {
  38. $field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,print_width,status,create_time';
  39. $order = 'create_time desc';
  40. $search_model = $this->model->where([ [ 'site_id', "=", $this->site_id ] ])->withSearch([ "printer_name" ], $where)->field($field)->order($order)->append([ 'brand_name' ]);
  41. $list = $this->pageQuery($search_model);
  42. return $list;
  43. }
  44. /**
  45. * 获取小票打印机列表
  46. * @param array $where
  47. * @param string $field
  48. * @return array
  49. * @throws \think\db\exception\DbException
  50. */
  51. public function getList(array $where = [], $field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,print_width,status,create_time')
  52. {
  53. return ( new CorePrinterService() )->getList($where, $field);
  54. }
  55. /**
  56. * 获取小票打印机信息
  57. * @param int $id
  58. * @return array
  59. */
  60. public function getInfo(int $id)
  61. {
  62. $field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,value,print_width,status';
  63. $info = $this->model->field($field)->where([ [ 'printer_id', "=", $id ] ])->findOrEmpty()->toArray();
  64. return $info;
  65. }
  66. /**
  67. * 添加小票打印机
  68. * @param array $data
  69. * @return mixed
  70. */
  71. public function add(array $data)
  72. {
  73. try {
  74. Db::startTrans();
  75. $data[ 'site_id' ] = $this->site_id;
  76. $res = $this->model->create($data);
  77. // 绑定易联云设备授权
  78. if ($data[ 'brand' ] == PrinterDict::YI_LIAN_YUN) {
  79. $result = ( new CorePrinterService() )->addPrinterYly($this->site_id, $data);
  80. if ($result[ 'code' ] != 0) {
  81. Db::rollback();
  82. throw new CommonException($result[ 'message' ]);
  83. }
  84. }
  85. Db::commit();
  86. return $res->printer_id;
  87. } catch (\Exception $e) {
  88. Db::rollback();
  89. throw new CommonException($e->getMessage());
  90. }
  91. }
  92. /**
  93. * 小票打印机编辑
  94. * @param int $id
  95. * @param array $data
  96. * @return bool
  97. */
  98. public function edit(int $id, array $data)
  99. {
  100. try {
  101. Db::startTrans();
  102. // 绑定易联云设备授权
  103. if ($data[ 'brand' ] == PrinterDict::YI_LIAN_YUN) {
  104. $result = ( new CorePrinterService() )->addPrinterYly($this->site_id, $data);
  105. if ($result[ 'code' ] != 0) {
  106. Db::rollback();
  107. throw new CommonException($result[ 'message' ]);
  108. }
  109. }
  110. $this->model->where([ [ 'printer_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
  111. Db::commit();
  112. return true;
  113. } catch (\Exception $e) {
  114. Db::rollback();
  115. throw new CommonException($e->getMessage());
  116. }
  117. }
  118. /**
  119. * 修改小票打印机状态
  120. * @param $data
  121. * @return mixed
  122. */
  123. public function modifyStatus($data)
  124. {
  125. return $this->model->where([
  126. [ 'printer_id', '=', $data[ 'printer_id' ] ],
  127. [ 'site_id', '=', $this->site_id ]
  128. ])->update([ 'status' => $data[ 'status' ] ]);
  129. }
  130. /**
  131. * 删除小票打印机
  132. * @param int $printer_id
  133. * @return bool
  134. */
  135. public function del(int $printer_id)
  136. {
  137. try {
  138. Db::startTrans();
  139. $field = 'printer_id,brand,printer_code,open_id,apikey';
  140. $printer_info = $this->model->field($field)->where([ [ 'site_id', '=', $this->site_id ], [ 'printer_id', "=", $printer_id ] ])->findOrEmpty()->toArray();
  141. // 删除易联云打印机授权
  142. if ($printer_info[ 'brand' ] == PrinterDict::YI_LIAN_YUN) {
  143. $result = ( new CorePrinterService() )->deletePrinterYly($this->site_id, $printer_info);
  144. if ($result[ 'code' ] != 0) {
  145. Db::rollback();
  146. throw new CommonException($result[ 'message' ]);
  147. }
  148. }
  149. $model = $this->model->where([ [ 'printer_id', '=', $printer_id ], [ 'site_id', '=', $this->site_id ] ])->find();
  150. $res = $model->delete();
  151. Db::commit();
  152. return $res;
  153. } catch (\Exception $e) {
  154. Db::rollback();
  155. throw new CommonException($e->getMessage());
  156. }
  157. }
  158. /**
  159. * 获取小票打印模板类型
  160. * @return array|null
  161. */
  162. public function getType()
  163. {
  164. return ( new CorePrinterService() )->getType();
  165. }
  166. /**
  167. * 获取打印机设备品牌
  168. * @param $brand
  169. * @return array|mixed|string
  170. */
  171. public function getBrand($brand)
  172. {
  173. return ( new CorePrinterService() )->getBrand($brand);
  174. }
  175. /**************************************************** 打印机管理(第三方) *********************************************************/
  176. /******************** 易联云 start ************************/
  177. /**
  178. * 设置易联云小票打印token
  179. * @param $data
  180. * @return \app\model\sys\SysConfig|bool|\think\Model
  181. */
  182. public function setYlyTokenConfig($data)
  183. {
  184. return ( new CorePrinterService() )->setYlyTokenConfig($this->site_id, $data);
  185. }
  186. /**
  187. * 获取易联云配置
  188. * @return array
  189. */
  190. public function getYlyTokenConfig()
  191. {
  192. return ( new CorePrinterService() )->getYlyTokenConfig($this->site_id);
  193. }
  194. /**
  195. * 重新获取易联云token
  196. * @param $printer_id
  197. * @return mixed
  198. */
  199. public function refreshToken($printer_id)
  200. {
  201. return ( new CorePrinterService() )->refreshToken($this->site_id, $printer_id);
  202. }
  203. /**
  204. * 测试打印
  205. * @param $printer_id
  206. * @return array
  207. */
  208. public function testPrint($printer_id)
  209. {
  210. return ( new CorePrinterService() )->testPrint($this->site_id, $printer_id);
  211. }
  212. /**
  213. * 打印小票内容
  214. * @param $params
  215. */
  216. public function printTicket($params)
  217. {
  218. $params[ 'site_id' ] = $this->site_id;
  219. return ( new CorePrinterService() )->printTicket($params);
  220. }
  221. /******************** 易联云 end ************************/
  222. }