DivisionAgentApplyServices.php 15 KB

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