YLYService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace crmeb\services;
  3. use app\admin\model\system\SystemStorePoint;
  4. use app\models\system\Cache as CacheModel;
  5. use app\models\system\SystemStore;
  6. use crmeb\exceptions\AuthException;
  7. use crmeb\interfaces\ProviderInterface;
  8. use crmeb\traits\LogicTrait;
  9. /**
  10. * Class YLYService
  11. * @package crmeb\services
  12. */
  13. class YLYService extends HttpService implements ProviderInterface
  14. {
  15. use LogicTrait;
  16. /**
  17. * 开发者创建的应用的应用ID
  18. * @var string
  19. */
  20. protected $client_id;
  21. /**
  22. * 开发者创建的应用的秘钥
  23. * @var string
  24. */
  25. protected $apiKey;
  26. /**
  27. * 开发者id
  28. * @var string
  29. */
  30. protected $partner;
  31. /**
  32. * 易联云token
  33. * @var null
  34. */
  35. protected $access_token = null;
  36. /**
  37. * 订单编号
  38. * @var null
  39. */
  40. protected $order_id = null;
  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. $terminal_number = SystemStore::where('id', $store_id)->value('terminal_number');
  74. $this->terminal = $terminal_number;
  75. $this->getAccessToken();
  76. return $this;
  77. }
  78. /**
  79. * 获取AccessToken
  80. * */
  81. protected function getAccessToken()
  82. {
  83. $this->access_token = CacheModel::getDbCache('YLY_access_token', function () {
  84. $request = self::postRequest($this->apiUrl . 'oauth/oauth', [
  85. 'client_id' => $this->client_id,
  86. 'grant_type' => 'client_credentials',
  87. 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)),
  88. 'scope' => 'all',
  89. 'timestamp' => time(),
  90. 'id' => $this->createUuid(),
  91. ]);
  92. $request = json_decode($request, true);
  93. $request['error'] = $request['error'] ?? 0;
  94. $request['error_description'] = $request['error_description'] ?? '';
  95. if ($request['error'] == 0 && $request['error_description'] == 'success') {
  96. return $request['body']['access_token'] ?? '';
  97. }
  98. return '';
  99. }, 20 * 86400);
  100. if (!$this->access_token)
  101. throw new AuthException('获取access_token获取失败');
  102. }
  103. /**
  104. * 生成UUID4
  105. * @return string
  106. */
  107. protected function createUuid()
  108. {
  109. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  110. mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  111. }
  112. /**
  113. * 开始打印订单
  114. * @param $content
  115. * @param string $order_id
  116. * @return bool|mixed
  117. */
  118. public function orderPrinting(string $order_id = '', int $errorCount = 0, $store_id = 0)
  119. {
  120. $request = self::postRequest($this->apiUrl . 'print/index', [
  121. 'client_id' => $this->client_id,
  122. 'access_token' => $this->access_token,
  123. 'machine_code' => $this->terminal,
  124. 'content' => $this->content,
  125. 'origin_id' => $order_id ? $order_id : $this->order_id,
  126. 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)),
  127. 'id' => $this->createUuid(),
  128. 'timestamp' => time()
  129. ]);
  130. if ($request === false) return false;
  131. $request = json_decode($request, true);
  132. // var_dump($request);
  133. if (isset($request['error']) && in_array($request['error'], [18, 14]) && $errorCount == 0) {
  134. CacheModel::delectDbCache('YLY_access_token');
  135. $this->getAccessToken();
  136. return $this->orderPrinting($order_id, 1, $store_id);
  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. $orderInfo['point'] = SystemStorePoint::where('id', $orderInfo['point_id'])->value('name');
  168. $orderInfo['real_name'] = mb_substr($orderInfo['real_name'], 0, 1) . "*" . mb_substr($orderInfo['real_name'], 2);
  169. // $orderInfo['user_phone'] = substr($orderInfo['user_phone'], 0, 3) . "****" . substr($orderInfo['user_phone'], 7);
  170. $timeYmd = date('Y-m-d', time());
  171. $timeHis = date('H:i:s', time());
  172. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>金额</td><td>小计</td></tr>';
  173. foreach ($product as $item) {
  174. $goodsStr .= '<tr>';
  175. $sumPrice = bcmul($item['truePrice'], $item['cart_num'], 2);
  176. $goodsStr .= "<td>{$item['productInfo']['store_name']}</td><td>{$item['cart_num']}</td><td>{$item['truePrice']}</td><td>{$sumPrice}</td>";
  177. $goodsStr .= '</tr>';
  178. }
  179. $goodsStr .= '</table>';
  180. $this->order_id = $orderInfo['order_id'];
  181. $count = <<<CONTENT
  182. <FB><center> ** {$name} **</center></FB>
  183. <FH2><FW2>----------------</FW2></FH2>
  184. 订单编号:{$orderInfo['order_id']}\r
  185. 日 期: {$timeYmd} \r
  186. 时 间: {$timeHis}\r
  187. 姓 名: {$orderInfo['real_name']}\r
  188. 赠送积分: {$orderInfo['gain_integral']}\r
  189. 电 话: {$orderInfo['user_phone']}\r
  190. 地 址: {$orderInfo['user_address']}\r
  191. 提货时间: {$orderInfo['time_area']}\r
  192. 自 提 点: {$orderInfo['point']}\r
  193. 订单备注: {$orderInfo['mark']}\r
  194. *************商品***************\r
  195. {$goodsStr}
  196. ********************************\r
  197. <FH>
  198. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']}</LR>
  199. <LR>邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}</LR>
  200. <LR>配送:¥{$orderInfo['pay_postage_sh']}</LR>
  201. <LR>押金:¥{$orderInfo['deposit']}</LR>
  202. <right>实际支付:¥{$orderInfo['pay_price']}+{$orderInfo['deposit']}</right>
  203. </FH>
  204. <FS><center> ** 完 **</center></FS>
  205. CONTENT;
  206. $this->content = $count;
  207. return $this;
  208. }
  209. /**
  210. * 获取打印内容
  211. * @return string
  212. */
  213. public function getContent()
  214. {
  215. return $this->content;
  216. }
  217. }