BasePrinter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/12/14
  6. * Time: 10:17
  7. */
  8. namespace addons\Printer\common\logic\printer;
  9. use addons\Printer\common\config\BaseConfig;
  10. //use app\forms\common\prints\content\OrderContent;
  11. /**
  12. * @property BaseConfig $config
  13. */
  14. abstract class BasePrinter
  15. {
  16. public $config;
  17. /**
  18. * @param $data
  19. * @return mixed
  20. * @throws \Exception
  21. */
  22. public function print($data)
  23. {
  24. $content = '';
  25. foreach ($data as $item) {
  26. if (isset($item['show']) && $item['show'] == 0) {
  27. continue;
  28. }
  29. $getter = 'get' . $item['handle'];
  30. if (isset($item['children'])) {
  31. $children = $item['content'];
  32. foreach ($item['children'] as $value) {
  33. if (isset($value['content'])) {
  34. $childGetter = 'get' . $value['handle'];
  35. $children .= $this->$childGetter($value['content']);
  36. }
  37. }
  38. $content .= $this->$getter($children);
  39. } else {
  40. $content .= $this->$getter($item['content']);
  41. }
  42. }
  43. return $this->config->print($content);
  44. }
  45. /**
  46. * 补齐空格
  47. * 每n个中文字符长度为一个数组元素
  48. * @param string $input
  49. * @param integer $n
  50. * @return array
  51. */
  52. protected function rStrPad1($input, $n = 7)
  53. {
  54. $string = "";
  55. $count = 0;
  56. $c_count = 0;
  57. $arr = array();
  58. for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
  59. $char = mb_substr($input, $i, 1, 'UTF-8');
  60. $string .= $char;
  61. if (strlen($char) == 3) {
  62. $count += 2;
  63. $c_count++;
  64. } else {
  65. $count += 1;
  66. }
  67. if ($count >= $n * 2) {
  68. $arr[] = $string;
  69. $string = '';
  70. $count = 0;
  71. $c_count = 0;
  72. }
  73. }
  74. if ($count < $n * 2) {
  75. $string = str_pad($string, $n * 2 + $c_count);
  76. $arr[] = $string;
  77. }
  78. return $arr;
  79. }
  80. /**
  81. * 补齐空格
  82. * 截取七个中文字符长度
  83. * @param string $input
  84. * @param integer $n
  85. * @return string
  86. */
  87. protected function rStrPad($input, $n = 7)
  88. {
  89. $string = "";
  90. $count = 0;
  91. $c_count = 0;
  92. for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
  93. $char = mb_substr($input, $i, 1, 'UTF-8');
  94. $string .= $char;
  95. if (strlen($char) == 3) {
  96. $count += 2;
  97. $c_count++;
  98. } else {
  99. $count += 1;
  100. }
  101. if ($count >= $n * 2) {
  102. break;
  103. }
  104. }
  105. if ($count < $n * 2) {
  106. $string = str_pad($string, $n * 2 + $c_count);
  107. }
  108. return $string;
  109. }
  110. /**
  111. * @return string
  112. * 打印次数
  113. */
  114. public function getTimes()
  115. {
  116. return '';
  117. }
  118. /**
  119. * @param $content
  120. * @return mixed
  121. * 居中
  122. */
  123. public function getCenter($content)
  124. {
  125. return $content;
  126. }
  127. /**
  128. * @param $content
  129. * @return mixed
  130. * 加粗
  131. */
  132. public function getBold($content)
  133. {
  134. return $content;
  135. }
  136. /**
  137. * @param $content
  138. * @return string
  139. * 居中加粗
  140. */
  141. public function getCenterBold($content)
  142. {
  143. return "$content";
  144. }
  145. /**
  146. * @param $content
  147. * @return mixed
  148. * 换行
  149. */
  150. public function getBR($content)
  151. {
  152. return $content;
  153. }
  154. /**
  155. * @param OrderContent $data
  156. * @return mixed
  157. * 表格---不显示规格
  158. */
  159. public function getTableNoAttr($data)
  160. {
  161. return '';
  162. }
  163. /**
  164. * @param OrderContent $data
  165. * @return mixed
  166. * 表格--显示规格
  167. */
  168. public function getTableAttr($data)
  169. {
  170. return '';
  171. }
  172. /**
  173. * @param $content
  174. * @return string
  175. * 价格--后缀
  176. */
  177. public function getPrice($content)
  178. {
  179. return $content . '元';
  180. }
  181. /**
  182. * @return mixed
  183. * 分隔符
  184. */
  185. public function getDivide()
  186. {
  187. return $this->getBR("--------------------------------");
  188. }
  189. public function getRemarkText($text)
  190. {
  191. $arrText = $this->rStrPad1($text, 18);
  192. $newText = '';
  193. foreach ($arrText as $item) {
  194. $newText .= $this->getBR($item);
  195. }
  196. return $newText;
  197. }
  198. }