UserInvoiceController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v2\user;
  12. use app\services\user\UserInvoiceServices;
  13. use think\Request;
  14. /**
  15. * Class UserInvoiceController
  16. * @package app\api\controller\v2\user
  17. */
  18. class UserInvoiceController
  19. {
  20. /**
  21. * @var UserInvoiceServices
  22. */
  23. protected $services;
  24. /**
  25. * UserInvoiceController constructor.
  26. * @param UserInvoiceServices $services
  27. */
  28. public function __construct(UserInvoiceServices $services)
  29. {
  30. $this->services = $services;
  31. }
  32. /**
  33. * 获取单个发票信息
  34. * @param $id
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function invoice($id)
  41. {
  42. if (!$id) {
  43. return app('json')->fail(100100);
  44. }
  45. return app('json')->success($this->services->getInvoice((int)$id));
  46. }
  47. /**
  48. * 发票列表
  49. * @param Request $request
  50. * @return mixed
  51. */
  52. public function invoiceList(Request $request)
  53. {
  54. $data = $request->postMore([
  55. ['header_type', ''],
  56. ['type', '']
  57. ]);
  58. $uid = (int)$request->uid();
  59. return app('json')->success($this->services->getUserList($uid, $data));
  60. }
  61. /**
  62. * 设置默认发票
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function setDefaultInvoice(Request $request)
  67. {
  68. list($id) = $request->getMore([['id', 0]], true);
  69. if (!$id || !is_numeric($id)) return app('json')->fail(100100);
  70. $uid = (int)$request->uid();
  71. $this->services->setDefaultInvoice($uid, (int)$id);
  72. return app('json')->success(100014);
  73. }
  74. /**
  75. * 获取默认发票
  76. * @param Request $request
  77. * @return mixed
  78. */
  79. public function getDefaultInvoice(Request $request)
  80. {
  81. [$type] = $request->postMore(['type', 1], true);
  82. $uid = (int)$request->uid();
  83. $defaultInvoice = $this->services->getUserDefaultInvoice($uid, (int)$type);
  84. if ($defaultInvoice) {
  85. $defaultInvoice = $defaultInvoice->toArray();
  86. return app('json')->success($defaultInvoice);
  87. }
  88. return app('json')->success([]);
  89. }
  90. /**
  91. * 修改 添加发票
  92. * @param Request $request
  93. * @return mixed
  94. */
  95. public function saveInvoice(Request $request)
  96. {
  97. $data = $request->postMore([
  98. [['id', 'd'], 0],
  99. [['header_type', 'd'], 1],
  100. [['type', 'd'], 1],
  101. ['drawer_phone', ''],
  102. ['email', ''],
  103. ['name', ''],
  104. ['duty_number', ''],
  105. ['tell', ''],
  106. ['address', ''],
  107. ['bank', ''],
  108. ['card_number', ''],
  109. ['is_default', 0]
  110. ]);
  111. if (!$data['drawer_phone']) return app('json')->fail(410144);
  112. if (!check_phone($data['drawer_phone'])) return app('json')->fail(410018);
  113. if (!$data['name']) return app('json')->fail(410145);
  114. if (!in_array($data['header_type'], [1, 2])) {
  115. $data['header_type'] = empty($data['duty_number']) ? 1 : 2;
  116. }
  117. if ($data['header_type'] == 1 && !preg_match('/^[\x80-\xff]{2,60}$/', $data['name'])) {
  118. return app('json')->fail(410146);
  119. }
  120. if ($data['header_type'] == 2 && !preg_match('/^[0-9a-zA-Z&\(\)\(\)\x80-\xff]{2,150}$/', $data['name'])) {
  121. return app('json')->fail(410146);
  122. }
  123. if ($data['header_type'] == 2 && !$data['duty_number']) {
  124. return app('json')->fail(410147);
  125. }
  126. if ($data['header_type'] == 2 && !preg_match('/^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/', $data['duty_number'])) {
  127. return app('json')->fail(410148);
  128. }
  129. if ($data['card_number'] && !preg_match('/^[1-9]\d{11,19}$/', $data['card_number'])) {
  130. return app('json')->fail(410149);
  131. }
  132. $uid = (int)$request->uid();
  133. $re = $this->services->saveInvoice($uid, $data);
  134. if ($re) {
  135. if ($re['type'] == 'edit') {
  136. return app('json')->success(100001);
  137. } else {
  138. return app('json')->success(100021, $re['data']);
  139. }
  140. } else {
  141. return app('json')->fail(100005);
  142. }
  143. }
  144. /**
  145. * 删除发票
  146. * @param Request $request
  147. * @return mixed
  148. */
  149. public function delInvoice(Request $request)
  150. {
  151. [$id] = $request->postMore([['id', 0]], true);
  152. if (!$id || !is_numeric($id)) return app('json')->fail(100100);
  153. $uid = (int)$request->uid();
  154. $re = $this->services->delInvoice($uid, (int)$id);
  155. if ($re)
  156. return app('json')->success(100002);
  157. else
  158. return app('json')->fail(100008);
  159. }
  160. }