YLYService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace crmeb\services;
  3. use app\models\system\Cache as CacheModel;
  4. use crmeb\exceptions\AuthException;
  5. use crmeb\interfaces\ProviderInterface;
  6. use crmeb\traits\LogicTrait;
  7. /**
  8. * Class YLYService
  9. * @package crmeb\services
  10. */
  11. class YLYService extends HttpService implements ProviderInterface
  12. {
  13. use LogicTrait;
  14. /**
  15. * 开发者创建的应用的应用ID
  16. * @var string
  17. */
  18. protected $client_id;
  19. /**
  20. * 开发者创建的应用的秘钥
  21. * @var string
  22. */
  23. protected $apiKey;
  24. /**
  25. * 开发者id
  26. * @var string
  27. */
  28. protected $partner;
  29. /**
  30. * 易联云token
  31. * @var null
  32. */
  33. protected $access_token = null;
  34. /**
  35. * 订单编号
  36. * @var null
  37. */
  38. protected $order_id = null;
  39. /**
  40. * 终端号码
  41. * @var string
  42. */
  43. protected $terminal;
  44. /**
  45. * 打印内容
  46. * @var string
  47. */
  48. protected $content;
  49. /**
  50. * 请求地址
  51. * @var string
  52. */
  53. protected $apiUrl = 'https://open-api.10ss.net/';
  54. public function register($congig = [])
  55. {
  56. }
  57. /**
  58. * YLYService constructor.
  59. */
  60. protected function __construct()
  61. {
  62. $system = SystemConfigService::more(['develop_id', 'printing_api_key', 'printing_client_id', 'terminal_number']);
  63. $this->partner = $system['develop_id'] ?? '';
  64. $this->apiKey = $system['printing_api_key'] ?? '';
  65. $this->client_id = $system['printing_client_id'] ?? '';
  66. $this->terminal = $system['terminal_number'] ?? '';
  67. $this->getAccessToken();
  68. }
  69. /**
  70. * 获取AccessToken
  71. * */
  72. protected function getAccessToken()
  73. {
  74. $this->access_token = CacheModel::getDbCache('YLY_access_token', function () {
  75. $request = self::postRequest($this->apiUrl . 'oauth/oauth', [
  76. 'client_id' => $this->client_id,
  77. 'grant_type' => 'client_credentials',
  78. 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)),
  79. 'scope' => 'all',
  80. 'timestamp' => time(),
  81. 'id' => $this->createUuid(),
  82. ]);
  83. $request = json_decode($request, true);
  84. $request['error'] = $request['error'] ?? 0;
  85. $request['error_description'] = $request['error_description'] ?? '';
  86. if ($request['error'] == 0 && $request['error_description'] == 'success') {
  87. return $request['body']['access_token'] ?? '';
  88. }
  89. return '';
  90. }, 20 * 86400);
  91. if (!$this->access_token)
  92. throw new AuthException('获取access_token获取失败');
  93. }
  94. /**
  95. * 生成UUID4
  96. * @return string
  97. */
  98. protected function createUuid()
  99. {
  100. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  101. mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  102. }
  103. /**
  104. * 开始打印订单
  105. * @param $content
  106. * @param string $order_id
  107. * @return bool|mixed
  108. */
  109. public function orderPrinting(string $order_id = '', int $errorCount = 0)
  110. {
  111. $request = self::postRequest($this->apiUrl . 'print/index', [
  112. 'client_id' => $this->client_id,
  113. 'access_token' => $this->access_token,
  114. 'machine_code' => $this->terminal,
  115. 'content' => $this->content,
  116. 'origin_id' => $order_id ? $order_id : $this->order_id,
  117. 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)),
  118. 'id' => $this->createUuid(),
  119. 'timestamp' => time()
  120. ]);
  121. if ($request === false) return false;
  122. $request = json_decode($request, true);
  123. if (isset($request['error']) && in_array($request['error'], [18, 14]) && $errorCount == 0) {
  124. CacheModel::delectDbCache('YLY_access_token');
  125. $this->getAccessToken();
  126. return $this->orderPrinting($order_id, 1);
  127. }
  128. return $request;
  129. }
  130. /**
  131. * 删除授权终端
  132. * @param $machine_code
  133. * @return bool|mixed4
  134. */
  135. public function deletePrinter($machine_code)
  136. {
  137. $request = self::postRequest($this->apiUrl . 'printer/deleteprinter', [
  138. 'client_id' => $this->client_id,
  139. 'access_token' => $this->access_token,
  140. 'machine_code' => $machine_code,
  141. 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)),
  142. 'id' => $this->createUuid(),
  143. 'timestamp' => time(),
  144. ]);
  145. if ($request === false) return false;
  146. return json_decode($request, true);
  147. }
  148. /**
  149. * 设置订单打印模板
  150. * @param string $name 商家名称
  151. * @param array $orderInfo 下单时间
  152. * @param array $product 订单详情
  153. * @return $this
  154. */
  155. public function setContent(string $name, array $orderInfo, array $product)
  156. {
  157. $timeYmd = date('Y-m-d', time());
  158. $timeHis = date('H:i:s', time());
  159. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>金额</td><td>小计</td></tr>';
  160. foreach ($product as $item) {
  161. $goodsStr .= '<tr>';
  162. $sumPrice = bcmul($item['truePrice'], $item['cart_num'], 2);
  163. $goodsStr .= "<td>{$item['productInfo']['store_name']}</td><td>{$item['cart_num']}</td><td>{$item['truePrice']}</td><td>{$sumPrice}</td>";
  164. $goodsStr .= '</tr>';
  165. }
  166. $goodsStr .= '</table>';
  167. $this->order_id = $orderInfo['order_id'];
  168. $count = <<<CONTENT
  169. <FB><center> ** {$name} **</center></FB>
  170. <FH2><FW2>----------------</FW2></FH2>
  171. 订单编号:{$orderInfo['order_id']}\r
  172. 日 期: {$timeYmd} \r
  173. 时 间: {$timeHis}\r
  174. 姓 名: {$orderInfo['real_name']}\r
  175. 赠送积分: {$orderInfo['gain_integral']}\r
  176. 电 话: {$orderInfo['user_phone']}\r
  177. 地 址: {$orderInfo['user_address']}\r
  178. 订单备注: {$orderInfo['mark']}\r
  179. *************商品***************\r
  180. {$goodsStr}
  181. ********************************\r
  182. <FH>
  183. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']}</LR>
  184. <LR>邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}</LR>
  185. <right>实际支付:¥{$orderInfo['pay_price']}</right>
  186. </FH>
  187. <FS><center> ** 完 **</center></FS>
  188. CONTENT;
  189. $this->content = $count;
  190. return $this;
  191. }
  192. /**
  193. * 获取打印内容
  194. * @return string
  195. */
  196. public function getContent()
  197. {
  198. return $this->content;
  199. }
  200. }