DivisionAgentApplyServices.php 11 KB

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