YLYService.php 7.2 KB

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