DivisionAgentApplyServices.php 11 KB

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