DivisionAgentApplyServices.php 15 KB

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