DivisionAgentApplyServices.php 13 KB

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