Printer.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\printer\model;
  11. use app\model\BaseModel;
  12. use addon\printer\data\sdk\yilianyun\api\PrinterService;
  13. use addon\printer\data\sdk\yilianyun\config\YlyConfig;
  14. use addon\printer\data\sdk\yilianyun\oauth\YlyOauthClient;
  15. use app\model\system\Config;
  16. class Printer extends BaseModel
  17. {
  18. private $brand = [
  19. // ['brand' => '365','name' => '365'],
  20. // ['brand' => 'feie','name' => '飞鹅'],
  21. [ 'brand' => 'yilianyun', 'name' => '易联云' ],
  22. ];
  23. /**
  24. * 获取打印机品牌
  25. * @return array
  26. */
  27. public function getPrinterBrand()
  28. {
  29. return $this->brand;
  30. }
  31. /**
  32. * 添加小票打印
  33. * @param $data
  34. * @return array
  35. */
  36. public function addPrinter($data)
  37. {
  38. $data[ 'create_time' ] = time();
  39. model('printer')->startTrans();
  40. try {
  41. $res = model('printer')->add($data);
  42. //易联云
  43. if ($data[ 'brand' ] == 'yilianyun') {
  44. $result = $this->addPrinterYly($data);
  45. if ($result[ 'code' ] < 0) {
  46. model('printer')->rollback();
  47. return $result;
  48. }
  49. }
  50. model('printer')->commit();
  51. return $this->success($res);
  52. } catch (\Exception $e) {
  53. model('printer')->rollback();
  54. return $this->error('', '添加失败 ' . $e->getMessage());
  55. }
  56. }
  57. /**
  58. * 编辑小票打印
  59. * @param $data
  60. * @return array
  61. */
  62. public function editPrinter($data)
  63. {
  64. $data[ 'update_time' ] = time();
  65. $res = model('printer')->update($data, [ [ 'printer_id', '=', $data[ 'printer_id' ] ] ]);
  66. return $this->success($res);
  67. }
  68. /**
  69. * 删除
  70. * @param $condition
  71. * @return array
  72. */
  73. public function deletePrinter($condition)
  74. {
  75. model('printer')->startTrans();
  76. try {
  77. $printer_info = model('printer')->getInfo($condition, '*');
  78. $res = model('printer')->delete($condition);
  79. //易联云
  80. if ($printer_info[ 'brand' ] == 'yilianyun') {
  81. $result = $this->deletePrinterYly($printer_info);
  82. if ($result[ 'code' ] < 0) {
  83. model('printer')->rollback();
  84. return $result;
  85. }
  86. }
  87. model('printer')->commit();
  88. return $this->success($res);
  89. } catch (\Exception $e) {
  90. model('printer')->rollback();
  91. return $this->error('', $e->getMessage());
  92. }
  93. }
  94. /**
  95. * 获取小票打印信息
  96. * @param array $condition
  97. * @param string $field
  98. * @return array
  99. */
  100. public function getPrinterInfo($condition = [], $field = '*')
  101. {
  102. $res = model('printer')->getInfo($condition, $field);
  103. return $this->success($res);
  104. }
  105. /**
  106. * 获取小票打印列表
  107. * @param array $condition
  108. * @param string $field
  109. * @param string $order
  110. * @param string $limit
  111. */
  112. public function getPrinterList($condition = [], $field = '*', $order = '', $limit = null)
  113. {
  114. $list = model('printer')->getList($condition, $field, $order, '', '', '', $limit);
  115. return $this->success($list);
  116. }
  117. /**
  118. * 获取小票打印分页列表
  119. * @param array $condition
  120. * @param number $page
  121. * @param string $page_size
  122. * @param string $order
  123. * @param string $field
  124. */
  125. public function getPrinterPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  126. {
  127. $list = model('printer')->pageList($condition, $field, $order, $page, $page_size);
  128. return $this->success($list);
  129. }
  130. /**************************************************** 打印机管理(第三方) *********************************************************/
  131. /******************** 易联云 start ************************/
  132. /**
  133. * @param $data
  134. * @return array
  135. */
  136. public function setYlyTokenConfig($data)
  137. {
  138. $config = new Config();
  139. $res = $config->setConfig($data, '易联云小票打印token', 1, [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'PRINTER_YLYTOKEN' ] ]);
  140. return $this->success($res);
  141. }
  142. /**
  143. * @return array
  144. */
  145. public function getYlyTokenConfig($site_id)
  146. {
  147. $config = new Config();
  148. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'PRINTER_YLYTOKEN' ] ]);
  149. if (empty($res[ 'data' ][ 'value' ])) {
  150. $res[ 'data' ][ 'value' ] = [
  151. 'access_token' => '',
  152. 'end_time' => '0'//token有效期
  153. ];
  154. }
  155. return $res;
  156. }
  157. /**
  158. * 获取易联云token
  159. * @param $yly_config
  160. * @param $site_id
  161. * @return mixed
  162. */
  163. public function getYlyToken($yly_config, $site_id, $type = 0)
  164. {
  165. //token配置
  166. $config_data = $this->getYlyTokenConfig($site_id);
  167. $config = $config_data[ 'data' ][ 'value' ];
  168. $client = new YlyOauthClient($yly_config);
  169. if ($config[ 'end_time' ] == 0 || $config[ 'end_time' ] < time() || $type == 1) {
  170. $token = $client->getToken(); //若是开放型应用请传授权码code
  171. $access_token = $token->access_token; //调用API凭证AccessToken
  172. //更新token
  173. $expires_in = $token->expires_in;
  174. $end_time = strtotime('+' . $expires_in / 86400 . 'day');
  175. $token_data = [
  176. 'site_id' => $site_id,
  177. 'access_token' => $token->access_token,
  178. 'end_time' => $end_time
  179. ];
  180. $this->setYlyTokenConfig($token_data);
  181. } else {
  182. $access_token = $config[ 'access_token' ];
  183. }
  184. return $access_token;
  185. }
  186. /**
  187. * 添加易联云打印机授权
  188. * @param $param
  189. * @return array|mixed
  190. */
  191. public function addPrinterYly($param)
  192. {
  193. $yly_config = new YlyConfig($param[ 'open_id' ], $param[ 'apikey' ]);
  194. $access_token = $this->getYlyToken($yly_config, $param[ 'site_id' ]);
  195. //添加打印机
  196. $printer = new PrinterService($access_token, $yly_config);
  197. $data = $printer->addPrinter($param[ 'printer_code' ], $param[ 'printer_key' ], '', '');
  198. if (isset($data->error) && $data->error == 0) {
  199. return $this->success();
  200. } else {
  201. return $data;
  202. }
  203. }
  204. /**
  205. * 删除易联云打印机授权
  206. * @param $param
  207. * @return array|mixed
  208. */
  209. public function deletePrinterYly($param)
  210. {
  211. $yly_config = new YlyConfig($param[ 'open_id' ], $param[ 'apikey' ]);
  212. $access_token = $this->getYlyToken($yly_config, $param[ 'site_id' ]);
  213. //添加打印机
  214. $printer = new PrinterService($access_token, $yly_config);
  215. $data = $printer->deletePrinter($param[ 'printer_code' ]);
  216. if (isset($data->error) && $data->error == 0) {
  217. return $this->success();
  218. } else {
  219. return $data;
  220. }
  221. }
  222. /**
  223. * 重新获取易联云token
  224. * @param $site_id
  225. * @return mixed
  226. */
  227. public function refreshToken($printer_id, $site_id)
  228. {
  229. model('config')->startTrans();
  230. try {
  231. //token配置
  232. $printer_info = model('printer')->getInfo([ [ 'site_id', '=', $site_id ], [ 'printer_id', '=', $printer_id ] ], '*');
  233. $yly_config = new YlyConfig($printer_info[ 'open_id' ], $printer_info[ 'apikey' ]);
  234. $client = new YlyOauthClient($yly_config);
  235. $token = $client->getToken(); //若是开放型应用请传授权码code
  236. //更新token
  237. $expires_in = $token->expires_in;
  238. $end_time = strtotime('+' . $expires_in / 86400 . 'day');
  239. $token_data = [
  240. 'site_id' => $site_id,
  241. 'access_token' => $token->access_token,
  242. 'end_time' => $end_time
  243. ];
  244. $this->setYlyTokenConfig($token_data);
  245. model('config')->commit();
  246. return $this->success();
  247. } catch (\Exception $e) {
  248. model('config')->rollback();
  249. return $this->error('', $e->getMessage());
  250. }
  251. }
  252. /******************** 易联云 end ************************/
  253. }