DivisionAgentApplyServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace app\services\agent;
  3. use app\dao\agent\DivisionAgentApplyDao;
  4. use app\services\BaseServices;
  5. use app\services\order\StoreOrderServices;
  6. use app\services\other\QrcodeServices;
  7. use app\services\system\attachment\SystemAttachmentServices;
  8. use app\services\user\UserServices;
  9. use crmeb\exceptions\AdminException;
  10. use crmeb\services\FormBuilder as Form;
  11. use crmeb\services\UploadService;
  12. use think\exception\ValidateException;
  13. use think\facade\Route;
  14. class DivisionAgentApplyServices extends BaseServices
  15. {
  16. /**
  17. * DivisionAgentApplyServices constructor.
  18. * @param DivisionAgentApplyDao $dao
  19. */
  20. public function __construct(DivisionAgentApplyDao $dao)
  21. {
  22. $this->dao = $dao;
  23. }
  24. /**
  25. * 申请详情
  26. * @param $uid
  27. * @return array|\think\Model|null
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function applyInfo($uid)
  33. {
  34. $data = $this->dao->get(['uid' => $uid, 'is_del' => 0]);
  35. if (!$data) return ['status' => -1];
  36. $data = $data->toArray();
  37. $data['images'] = json_decode($data['images'], true);
  38. $data['add_time'] = date('Y-m-d H:i:s', $data['add_time']);
  39. return $data;
  40. }
  41. /**
  42. * 代理商申请
  43. * @param $data
  44. * @param int $id
  45. * @return bool
  46. */
  47. public function applyAgent($data, $id = 0)
  48. {
  49. $data['images'] = json_encode($data['images']);
  50. $data['add_time'] = time();
  51. /** @var UserServices $userServices */
  52. $userServices = app()->make(UserServices::class);
  53. $divisionId = $userServices->value(['division_invite' => $data['division_invite']], 'division_id');
  54. if (!$divisionId) throw new ValidateException('邀请码无效');
  55. $data['division_id'] = $divisionId;
  56. if ($id) {
  57. $data['status'] = 0;
  58. $res = $this->dao->update(['id' => $id], $data);
  59. } else {
  60. $this->dao->update(['uid' => $data['uid']], ['is_del' => 1]);
  61. $res = $this->dao->save($data);
  62. }
  63. if (!$res) throw new ValidateException('提交失败');
  64. return true;
  65. }
  66. /**
  67. * 管理端代理商申请列表
  68. * @param $where
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function AdminApplyList($where)
  75. {
  76. $where['is_del'] = 0;
  77. [$page, $limit] = $this->getPageValue();
  78. $list = $this->dao->getList($where, $page, $limit);
  79. foreach ($list as &$item) {
  80. $item['images'] = json_decode($item['images'], true);
  81. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  82. }
  83. $count = $this->dao->count($where);
  84. return compact('list', 'count');
  85. }
  86. /**
  87. * 删除代理商审核
  88. * @param $id
  89. * @return bool
  90. */
  91. public function delApply($id)
  92. {
  93. $res = $this->dao->update($id, ['is_del' => 1]);
  94. if (!$res) throw new AdminException('删除失败');
  95. return true;
  96. }
  97. /**
  98. * 审核表单
  99. * @param $id
  100. * @param $type
  101. * @return array
  102. * @throws \FormBuilder\Exception\FormBuilderException
  103. */
  104. public function examineApply($id, $type)
  105. {
  106. if (!$id) throw new AdminException('参数错误,找不到用户');
  107. $field = [];
  108. $field[] = Form::hidden('type', $type);
  109. $field[] = Form::hidden('id', $id);
  110. if ($type) {
  111. $field[] = Form::number('division_percent', '佣金比例', '')->placeholder('代理商佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级事业部的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
  112. $field[] = Form::date('division_end_time', '到期时间', '')->placeholder('代理商代理到期时间')->required();
  113. $field[] = Form::radio('division_status', '代理状态', 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  114. $title = '同意申请';
  115. } else {
  116. $field[] = Form::textarea('refusal_reason', '拒绝原因', '')->rows(5);
  117. $title = '拒绝申请';
  118. }
  119. return create_form($title, $field, Route::buildUrl('/agent/division/apply_agent/save'), 'POST');
  120. }
  121. /**
  122. * 审核代理商
  123. * @param $data
  124. * @return mixed
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function applyAgentSave($data)
  130. {
  131. $applyInfo = $this->dao->get($data['id']);
  132. return $this->transaction(function () use ($applyInfo, $data) {
  133. if ($data['type'] == 1) {
  134. $agentData = [
  135. 'division_id' => $applyInfo['division_id'],
  136. 'agent_id' => $applyInfo['uid'],
  137. 'division_type' => 2,
  138. 'division_status' => $data['division_status'],
  139. 'is_agent' => 1,
  140. 'is_staff' => 0,
  141. 'division_percent' => $data['division_percent'],
  142. 'division_change_time' => time(),
  143. 'division_end_time' => strtotime($data['division_end_time'])
  144. ];
  145. /** @var UserServices $userServices */
  146. $userServices = app()->make(UserServices::class);
  147. $division_info = $userServices->getUserInfo($applyInfo['division_id'], 'division_end_time,division_percent');
  148. if ($applyInfo['division_id'] != 0) {
  149. if ($agentData['division_percent'] > $division_info['division_percent']) throw new AdminException('代理商佣金比例不能大于事业部佣金比例');
  150. if ($agentData['division_end_time'] > $division_info['division_end_time']) throw new AdminException('代理商到期时间不能大于事业部到期时间');
  151. }
  152. $applyInfo->status = 1;
  153. $res = $applyInfo->save();
  154. $res = $res && $userServices->update($applyInfo['uid'], $agentData);
  155. } else {
  156. $applyInfo->status = 2;
  157. $applyInfo->refusal_reason = $data['refusal_reason'];
  158. $res = $applyInfo->save();
  159. }
  160. if (!$res) throw new AdminException('审核错误');
  161. return true;
  162. });
  163. }
  164. /**
  165. * 获取员工列表
  166. * @param $userInfo
  167. * @param $where
  168. * @param string $field
  169. * @return array
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public function getStaffList($userInfo, $where, $field = '*')
  175. {
  176. /** @var UserServices $userService */
  177. $userService = app()->make(UserServices::class);
  178. /** @var StoreOrderServices $orderService */
  179. $orderService = app()->make(StoreOrderServices::class);
  180. [$page, $limit] = $this->getPageValue();
  181. $count = $userService->getCount(['agent_id' => $where['agent_id'], 'is_staff' => 1]);
  182. $list = $userService->getList(['agent_id' => $where['agent_id'], 'is_staff' => 1], $field, $page, $limit);
  183. foreach ($list as &$item) {
  184. $item['division_change_time'] = date('Y-m-d', $item['division_change_time']);
  185. $item['division_end_time'] = date('Y-m-d', $item['division_end_time']);
  186. $item['childCount'] = $userService->getCount(['agent_id' => $where['agent_id'], 'spread_uid' => $item['uid']]);
  187. $item['orderCount'] = $item['pay_count'];
  188. $item['numberCount'] = $orderService->sum(['uid' => $item['uid']], 'pay_price');
  189. }
  190. /** @var SystemAttachmentServices $systemAttachment */
  191. $systemAttachment = app()->make(SystemAttachmentServices::class);
  192. $name = 'agent_' . $where['agent_id'] . '.jpg';
  193. $siteUrl = sys_config('site_url', '');
  194. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  195. if (!$imageInfo) {
  196. /** @var QrcodeServices $qrCode */
  197. $qrCode = app()->make(QrcodeServices::class);
  198. //公众号
  199. $resCode = $qrCode->getForeverQrcode('agent', $where['agent_id']);
  200. if ($resCode) {
  201. $res = ['res' => $resCode, 'id' => $resCode['id']];
  202. } else {
  203. $res = false;
  204. }
  205. if (!$res) throw new AdminException('二维码生成失败');
  206. $imageInfo = $this->downloadImage($resCode['url'], $name);
  207. $systemAttachment->attachmentAdd($name, $imageInfo['size'], $imageInfo['type'], $imageInfo['att_dir'], $imageInfo['att_dir'], 1, $imageInfo['image_type'], time(), 2);
  208. }
  209. $codeUrl = strpos($imageInfo['att_dir'], 'http') === false ? $siteUrl . $imageInfo['att_dir'] : $imageInfo['att_dir'];
  210. return compact('list', 'count', 'codeUrl');
  211. }
  212. /**
  213. * 下载图片
  214. * @param string $url
  215. * @param string $name
  216. * @param int $type
  217. * @param int $timeout
  218. * @param int $w
  219. * @param int $h
  220. * @return string
  221. */
  222. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  223. {
  224. if (!strlen(trim($url))) return '';
  225. //TODO 获取远程文件所采用的方法
  226. if ($type) {
  227. $ch = curl_init();
  228. curl_setopt($ch, CURLOPT_URL, $url);
  229. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  230. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  231. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  232. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  233. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  234. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  235. $content = curl_exec($ch);
  236. curl_close($ch);
  237. } else {
  238. try {
  239. ob_start();
  240. readfile($url);
  241. $content = ob_get_contents();
  242. ob_end_clean();
  243. } catch (\Exception $e) {
  244. return $e->getMessage();
  245. }
  246. }
  247. $size = strlen(trim($content));
  248. if (!$content || $size <= 2) return '图片流获取失败';
  249. $upload_type = sys_config('upload_type', 1);
  250. $upload = UploadService::init();
  251. if ($upload->to('attach/spread/agent')->setAuthThumb(false)->stream($content, $name) === false) {
  252. return $upload->getError();
  253. }
  254. $imageInfo = $upload->getUploadInfo();
  255. $data['att_dir'] = $imageInfo['dir'];
  256. $data['name'] = $imageInfo['name'];
  257. $data['size'] = $imageInfo['size'];
  258. $data['type'] = $imageInfo['type'];
  259. $data['image_type'] = $upload_type;
  260. $data['is_exists'] = false;
  261. return $data;
  262. }
  263. }