YLYService.php 7.2 KB

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