DivisionAgentApplyServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\agent;
  12. use app\dao\agent\DivisionAgentApplyDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\other\QrcodeServices;
  16. use app\services\system\attachment\SystemAttachmentServices;
  17. use app\services\user\UserServices;
  18. use crmeb\exceptions\AdminException;
  19. use crmeb\exceptions\ApiException;
  20. use crmeb\services\app\MiniProgramService;
  21. use crmeb\services\FormBuilder as Form;
  22. use app\services\other\UploadService;
  23. use think\facade\Config;
  24. use think\facade\Log;
  25. use think\facade\Route;
  26. class DivisionAgentApplyServices extends BaseServices
  27. {
  28. /**
  29. * DivisionAgentApplyServices constructor.
  30. * @param DivisionAgentApplyDao $dao
  31. */
  32. public function __construct(DivisionAgentApplyDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 申请详情
  38. * @param $uid
  39. * @return array|\think\Model|null
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function applyInfo($uid)
  45. {
  46. $data = $this->dao->get(['uid' => $uid, 'is_del' => 0]);
  47. if (!$data) return ['status' => -1];
  48. $data = $data->toArray();
  49. $data['images'] = json_decode($data['images'], true);
  50. $data['add_time'] = date('Y-m-d H:i:s', $data['add_time']);
  51. return $data;
  52. }
  53. /**
  54. * 代理商申请
  55. * @param $data
  56. * @param int $id
  57. * @return bool
  58. */
  59. public function applyAgent($data, $id = 0)
  60. {
  61. $data['images'] = json_encode($data['images']);
  62. $data['add_time'] = time();
  63. /** @var UserServices $userServices */
  64. $userServices = app()->make(UserServices::class);
  65. $divisionId = $userServices->value(['division_invite' => $data['division_invite']], 'division_id');
  66. if (!$divisionId) throw new ApiException('邀请码无效');
  67. $data['division_id'] = $divisionId;
  68. if ($id) {
  69. $data['status'] = 0;
  70. $res = $this->dao->update(['id' => $id], $data);
  71. } else {
  72. $this->dao->update(['uid' => $data['uid']], ['is_del' => 1]);
  73. $res = $this->dao->save($data);
  74. }
  75. if (!$res) throw new ApiException('提交失败');
  76. return true;
  77. }
  78. /**
  79. * 管理端代理商申请列表
  80. * @param $where
  81. * @return array
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function AdminApplyList($where)
  87. {
  88. $where['is_del'] = 0;
  89. [$page, $limit] = $this->getPageValue();
  90. $list = $this->dao->getList($where, $page, $limit);
  91. $divisionUids = array_column($list, 'division_id');
  92. $divisionArr = app()->make(UserServices::class)->getColumn([['division_id', 'in', $divisionUids]], 'division_name', 'uid');
  93. foreach ($list as &$item) {
  94. $item['images'] = json_decode($item['images'], true);
  95. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  96. $item['division_name'] = $divisionArr[$item['division_id']] ?? '';
  97. }
  98. $count = $this->dao->count($where);
  99. return compact('list', 'count');
  100. }
  101. /**
  102. * 删除代理商审核
  103. * @param $id
  104. * @return bool
  105. */
  106. public function delApply($id)
  107. {
  108. $res = $this->dao->update($id, ['is_del' => 1]);
  109. if (!$res) throw new AdminException('删除失败');
  110. return true;
  111. }
  112. /**
  113. * 审核表单
  114. * @param $id
  115. * @param $type
  116. * @return array
  117. * @throws \FormBuilder\Exception\FormBuilderException
  118. */
  119. public function examineApply($id, $type)
  120. {
  121. if (!$id) throw new AdminException('参数错误');
  122. $field = [];
  123. $field[] = Form::hidden('type', $type);
  124. $field[] = Form::hidden('id', $id);
  125. if ($type) {
  126. $field[] = Form::number('division_percent', '佣金比例', '')->placeholder('代理商佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级事业部的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
  127. $field[] = Form::date('division_end_time', '到期时间', '')->placeholder('代理商代理到期时间');
  128. $field[] = Form::radio('division_status', '代理状态', 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  129. $title = '同意申请';
  130. } else {
  131. $field[] = Form::textarea('refusal_reason', '拒绝原因', '')->rows(5);
  132. $title = '拒绝申请';
  133. }
  134. return create_form($title, $field, Route::buildUrl('/agent/division/apply_agent/save'), 'POST');
  135. }
  136. /**
  137. * 审核代理商
  138. * @param $data
  139. * @return mixed
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function applyAgentSave($data)
  145. {
  146. $applyInfo = $this->dao->get($data['id']);
  147. return $this->transaction(function () use ($applyInfo, $data) {
  148. if ($data['type'] == 1) {
  149. $agentData = [
  150. 'division_name' => $applyInfo['agent_name'],
  151. 'division_id' => $applyInfo['division_id'],
  152. 'agent_id' => $applyInfo['uid'],
  153. 'division_type' => 2,
  154. 'division_status' => $data['division_status'],
  155. 'is_agent' => 1,
  156. 'is_staff' => 0,
  157. 'division_percent' => $data['division_percent'],
  158. 'division_change_time' => time(),
  159. 'division_end_time' => strtotime($data['division_end_time']),
  160. 'spread_uid' => $applyInfo['division_id'],
  161. 'spread_time' => time()
  162. ];
  163. /** @var UserServices $userServices */
  164. $userServices = app()->make(UserServices::class);
  165. $division_info = $userServices->getUserInfo($applyInfo['division_id'], 'division_end_time,division_percent');
  166. if ($applyInfo['division_id'] != 0) {
  167. if ($agentData['division_percent'] > $division_info['division_percent']) throw new AdminException('代理商佣金比例不能大于事业部佣金比例');
  168. if ($agentData['division_end_time'] > $division_info['division_end_time']) throw new AdminException('代理商到期时间不能大于事业部到期时间');
  169. }
  170. $applyInfo->status = 1;
  171. $res = $applyInfo->save();
  172. $res = $res && $userServices->update($applyInfo['uid'], $agentData);
  173. } else {
  174. $applyInfo->status = 2;
  175. $applyInfo->refusal_reason = $data['refusal_reason'];
  176. $res = $applyInfo->save();
  177. }
  178. if (!$res) throw new AdminException('操作失败');
  179. return true;
  180. });
  181. }
  182. /**
  183. * 获取员工列表
  184. * @param $userInfo
  185. * @param $where
  186. * @param string $field
  187. * @return array
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function getStaffList($isRoutine, $where, $field = '*')
  193. {
  194. /** @var UserServices $userService */
  195. $userService = app()->make(UserServices::class);
  196. /** @var StoreOrderServices $orderService */
  197. $orderService = app()->make(StoreOrderServices::class);
  198. [$page, $limit] = $this->getPageValue();
  199. $count = $userService->getCount(['agent_id' => $where['agent_id'], 'is_staff' => 1, 'is_del' => 0]);
  200. $list = $userService->getList(['agent_id' => $where['agent_id'], 'is_staff' => 1, 'is_del' => 0], $field, $page, $limit);
  201. foreach ($list as &$item) {
  202. $item['division_change_time'] = date('Y-m-d', $item['division_change_time']);
  203. $item['division_end_time'] = date('Y-m-d', $item['division_end_time']);
  204. $item['childCount'] = $userService->getCount(['agent_id' => $where['agent_id'], 'spread_uid' => $item['uid']]);
  205. $item['orderCount'] = $item['pay_count'];
  206. $item['numberCount'] = $orderService->sum(['uid' => $item['uid']], 'pay_price');
  207. }
  208. $codeUrl = '';
  209. if ($isRoutine) {
  210. /** @var SystemAttachmentServices $systemAttachment */
  211. $systemAttachment = app()->make(SystemAttachmentServices::class);
  212. $name = 'routine_agent_' . $where['agent_id'] . '.jpg';
  213. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  214. //检测远程文件是否存在
  215. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  216. $imageInfo = null;
  217. $systemAttachment->delete(['name' => $name]);
  218. }
  219. $siteUrl = sys_config('site_url');
  220. if (!$imageInfo) {
  221. /** @var QrcodeServices $qrCode */
  222. $qrCode = app()->make(QrcodeServices::class);
  223. $resForever = $qrCode->qrCodeForever($where['agent_id'], 'agent', '', '');
  224. $resCode = MiniProgramService::appCodeUnlimitService($resForever->id, '', 280);
  225. if ($resCode) {
  226. $res = ['res' => $resCode, 'id' => $resForever->id];
  227. } else {
  228. $res = false;
  229. }
  230. if (!$res) return compact('list', 'count', 'codeUrl');
  231. $uploadType = (int)sys_config('upload_type', 1);
  232. $upload = UploadService::init();
  233. $uploadRes = $upload->to('routine/agent/code')->validate()->setAuthThumb(false)->stream($res['res'], $name);
  234. if ($uploadRes === false) return compact('list', 'count', 'codeUrl');
  235. $imageInfo = $upload->getUploadInfo();
  236. $imageInfo['image_type'] = $uploadType;
  237. $systemAttachment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  238. $qrCode->setQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  239. $codeUrl = $imageInfo['dir'];
  240. } else $codeUrl = $imageInfo['att_dir'];
  241. if ($imageInfo['image_type'] == 1) $codeUrl = $siteUrl . $codeUrl;
  242. }
  243. return compact('list', 'count', 'codeUrl');
  244. //代理商邀请员工二维码为公众号渠道码,需要配置公众号并开启关注自动生成用户使用
  245. // try {
  246. // /** @var SystemAttachmentServices $systemAttachment */
  247. // $systemAttachment = app()->make(SystemAttachmentServices::class);
  248. // $name = 'agent_' . $where['agent_id'] . '.jpg';
  249. // $siteUrl = sys_config('site_url', '');
  250. // $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  251. // if (!$imageInfo) {
  252. // /** @var QrcodeServices $qrCode */
  253. // $qrCode = app()->make(QrcodeServices::class);
  254. // //公众号
  255. // $resCode = $qrCode->getForeverQrcode('agent', $where['agent_id']);
  256. // if ($resCode) {
  257. // $res = ['res' => $resCode, 'id' => $resCode['id']];
  258. // } else {
  259. // $res = false;
  260. // }
  261. // if (!$res) throw new ApiException('二维码生成失败');
  262. // $imageInfo = $this->downloadImage($resCode['url'], $name);
  263. // $systemAttachment->attachmentAdd($name, $imageInfo['size'], $imageInfo['type'], $imageInfo['att_dir'], $imageInfo['att_dir'], 1, $imageInfo['image_type'], time(), 2);
  264. // }
  265. // $codeUrl = strpos($imageInfo['att_dir'], 'http') === false ? $siteUrl . $imageInfo['att_dir'] : $imageInfo['att_dir'];
  266. // } catch (\Exception $e) {
  267. // Log::error('邀请员工二维码生成失败,失败原因' . $e->getMessage());
  268. // }
  269. }
  270. /**
  271. * 下载图片
  272. * @param string $url
  273. * @param string $name
  274. * @param int $type
  275. * @param int $timeout
  276. * @param int $w
  277. * @param int $h
  278. * @return string
  279. */
  280. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  281. {
  282. if (!strlen(trim($url))) return '';
  283. if (!strlen(trim($name))) {
  284. //TODO 获取要下载的文件名称
  285. $downloadImageInfo = $this->getImageExtname($url);
  286. $ext = $downloadImageInfo['ext_name'];
  287. $name = $downloadImageInfo['file_name'];
  288. if (!strlen(trim($name))) return '';
  289. } else {
  290. $ext = $this->getImageExtname($name)['ext_name'];
  291. }
  292. if (!in_array($ext, Config::get('upload.fileExt'))) {
  293. throw new AdminException('格式错误');
  294. }
  295. //TODO 获取远程文件所采用的方法
  296. if ($type) {
  297. $ch = curl_init();
  298. curl_setopt($ch, CURLOPT_URL, $url);
  299. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  300. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  301. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  302. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  303. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  304. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  305. $content = curl_exec($ch);
  306. curl_close($ch);
  307. } else {
  308. try {
  309. ob_start();
  310. readfile($url);
  311. $content = ob_get_contents();
  312. ob_end_clean();
  313. } catch (\Exception $e) {
  314. return $e->getMessage();
  315. }
  316. }
  317. $size = strlen(trim($content));
  318. if (!$content || $size <= 2) return '图片流获取失败';
  319. $upload_type = sys_config('upload_type', 1);
  320. $upload = UploadService::init();
  321. if ($upload->to('attach/spread/agent')->setAuthThumb(false)->stream($content, $name) === false) {
  322. return $upload->getError();
  323. }
  324. $imageInfo = $upload->getUploadInfo();
  325. $data['att_dir'] = $imageInfo['dir'];
  326. $data['name'] = $imageInfo['name'];
  327. $data['size'] = $imageInfo['size'];
  328. $data['type'] = $imageInfo['type'];
  329. $data['image_type'] = $upload_type;
  330. $data['is_exists'] = false;
  331. return $data;
  332. }
  333. /**
  334. * 获取即将要下载的图片扩展名
  335. * @param string $url
  336. * @param string $ex
  337. * @return array|string[]
  338. */
  339. public function getImageExtname($url = '', $ex = 'jpg')
  340. {
  341. $_empty = ['file_name' => '', 'ext_name' => $ex];
  342. if (!$url) return $_empty;
  343. if (strpos($url, '?')) {
  344. $_tarr = explode('?', $url);
  345. $url = trim($_tarr[0]);
  346. }
  347. $arr = explode('.', $url);
  348. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  349. $ext_name = trim($arr[count($arr) - 1]);
  350. $ext_name = !$ext_name ? $ex : $ext_name;
  351. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  352. }
  353. }