UserServices.php 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. //declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\jobs\user\UserFriendsJob;
  14. use app\jobs\user\UserJob;
  15. use app\jobs\user\UserSpreadJob;
  16. use app\jobs\user\UserSvipJob;
  17. use app\services\activity\bargain\StoreBargainServices;
  18. use app\services\activity\combination\StoreCombinationServices;
  19. use app\services\activity\newcomer\StoreNewcomerServices;
  20. use app\services\activity\seckill\StoreSeckillServices;
  21. use app\services\agent\AgentLevelServices;
  22. use app\services\BaseServices;
  23. use app\dao\user\UserDao;
  24. use app\services\activity\coupon\StoreCouponUserServices;
  25. use app\services\diy\DiyServices;
  26. use app\services\message\service\StoreServiceRecordServices;
  27. use app\services\message\service\StoreServiceServices;
  28. use app\services\order\OtherOrderServices;
  29. use app\services\order\StoreOrderCreateServices;
  30. use app\services\order\StoreOrderServices;
  31. use app\services\order\StoreOrderTakeServices;
  32. use app\services\other\QrcodeServices;
  33. use app\services\product\product\StoreProductLogServices;
  34. use app\services\other\queue\QueueServices;
  35. use app\services\message\SystemMessageServices;
  36. use app\services\user\group\UserGroupServices;
  37. use app\services\user\label\UserLabelServices;
  38. use app\services\user\label\UserLabelRelationServices;
  39. use app\services\user\level\SystemUserLevelServices;
  40. use app\services\user\level\UserLevelServices;
  41. use app\services\wechat\WechatUserServices;
  42. use app\services\work\WorkClientServices;
  43. use app\services\work\WorkMemberServices;
  44. use crmeb\exceptions\AdminException;
  45. use crmeb\services\CacheService;
  46. use crmeb\services\FormBuilder as Form;
  47. use crmeb\services\FormBuilder;
  48. use crmeb\services\SystemConfigService;
  49. use crmeb\services\wechat\OfficialAccount;
  50. use crmeb\services\WithdrawService;
  51. use think\Exception;
  52. use think\exception\ValidateException;
  53. use think\facade\Route as Url;
  54. /**
  55. *
  56. * Class UserServices
  57. * @package app\services\user
  58. * @mixin UserDao
  59. */
  60. class UserServices extends BaseServices
  61. {
  62. /**
  63. * 性别
  64. * @var array
  65. */
  66. public $sex = [
  67. '男' => 1,
  68. '女' => 2,
  69. '保密' => 0,
  70. 0 => 1,//男
  71. 1 => 2,//女
  72. 2 => 0,//保密
  73. ];
  74. /**
  75. * 逆转数据
  76. * @var int[]
  77. */
  78. public $rSex = [
  79. 0 => 2,
  80. 1 => 0,
  81. 2 => 1,
  82. ];
  83. /**
  84. * 用户默认补充信息
  85. * @var array
  86. */
  87. public $defaultExtendInfo = [
  88. ['info' => '姓名', 'tip' => '请填写真实姓名', 'format' => 'text', 'label' => '文本', 'param' => 'real_name', 'single' => '', 'singlearr' => [], 'required' => 0, 'use' => 0, 'user_show' => 0, 'sort' => 1],
  89. ['info' => '性别', 'tip' => '请选择性别', 'format' => 'radio', 'label' => '单选项', 'param' => 'sex', 'single' => '', 'singlearr' => ['男', '女', '保密'], 'required' => 0, 'use' => 0, 'user_show' => 0, 'sort' => 2],
  90. ['info' => '生日', 'tip' => '请选择出生日期', 'format' => 'date', 'label' => '日期', 'param' => 'birthday', 'single' => '', 'singlearr' => [], 'required' => 0, 'use' => 0, 'user_show' => 0, 'sort' => 3],
  91. ['info' => '身份证', 'tip' => '请填写身份证', 'format' => 'id', 'label' => '身份证', 'param' => 'card_id', 'single' => '', 'singlearr' => [], 'required' => 0, 'use' => 0, 'user_show' => 0, 'sort' => 4],
  92. ['info' => '地址', 'tip' => '请填写地址', 'format' => 'address', 'label' => '地址', 'param' => 'address', 'single' => '', 'singlearr' => [], 'required' => 0, 'use' => 0, 'user_show' => 0, 'sort' => 5],
  93. ['info' => '备注', 'tip' => '请填写补充备注内容', 'format' => 'text', 'label' => '文本', 'param' => 'mark', 'single' => '', 'singlearr' => [], 'required' => 0, 'use' => 0, 'user_show' => 0, 'sort' => 6],
  94. ];
  95. /**
  96. * UserServices constructor.
  97. * @param UserDao $dao
  98. */
  99. public function __construct(UserDao $dao)
  100. {
  101. $this->dao = $dao;
  102. }
  103. /**
  104. * 获取用户信息
  105. * @param $id
  106. * @param $field
  107. */
  108. public function getUserInfo(int $uid, $field = '*')
  109. {
  110. if (is_string($field)) $field = explode(',', $field);
  111. return $this->dao->get($uid, $field);
  112. }
  113. /**
  114. * 是否存在
  115. * @param int $uid
  116. * @return bool
  117. */
  118. public function userExist(int $uid)
  119. {
  120. return $this->dao->exist($uid);
  121. }
  122. /**
  123. * 获取用户缓存信息
  124. * @param int $uid
  125. * @param string $field
  126. * @param int $expire
  127. * @return bool|mixed|null
  128. */
  129. public function getUserCacheInfo(int $uid, int $expire = 60)
  130. {
  131. return $this->dao->cacheTag()->remember('user_info_' . $uid, function () use ($uid) {
  132. return $this->dao->get($uid);
  133. }, $expire);
  134. }
  135. /**
  136. * 获取用户列表
  137. * @param array $where
  138. * @param string $field
  139. * @return array
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function getUserList(array $where, string $field = '*'): array
  145. {
  146. [$page, $limit] = $this->getPageValue();
  147. $list = $this->dao->getList($where, $field, $page, $limit);
  148. $count = $this->count($where);
  149. return compact('list', 'count');
  150. }
  151. /**
  152. * 列表条数
  153. * @param array $where
  154. * @return int
  155. */
  156. public function getCount(array $where, bool $is_list = false)
  157. {
  158. return $this->dao->getCount($where, $is_list);
  159. }
  160. /**
  161. * 保存用户信息
  162. * @param $user
  163. * @param int $spreadUid
  164. * @param string $userType
  165. * @return User|\think\Model
  166. */
  167. public function setUserInfo($user, int $spreadUid = 0, string $userType = 'wechat')
  168. {
  169. mt_srand();
  170. $ip = app()->request->ip();
  171. $data = [
  172. 'account' => $user['account'] ?? 'wx' . rand(1, 9999) . time(),
  173. 'pwd' => $user['pwd'] ?? md5('123456'),
  174. 'nickname' => $user['nickname'] ?? '',
  175. 'avatar' => !empty($user['headimgurl']) ? $user['headimgurl'] : sys_config('h5_avatar'),
  176. 'phone' => $user['phone'] ?? '',
  177. 'birthday' => $user['birthday'] ?? '',
  178. 'add_time' => time(),
  179. 'add_ip' => $ip,
  180. 'last_time' => time(),
  181. 'last_ip' => $ip,
  182. 'user_type' => $userType
  183. ];
  184. if ($spreadUid && $this->dao->get((int)$spreadUid)) {
  185. $data['spread_uid'] = $spreadUid;
  186. $data['spread_time'] = time();
  187. }
  188. $res = $this->dao->save($data);
  189. if (!$res)
  190. throw new AdminException('保存用户信息失败');
  191. //用户注册成功事件
  192. $userInfo = $res->toArray();
  193. $userInfo['unionid'] = $user['unionid'] ?? '';
  194. event('user.register', [$userInfo, true, $spreadUid]);
  195. return $res;
  196. }
  197. /**
  198. * 某些条件用户佣金总和
  199. * @param array $where
  200. * @return mixed
  201. */
  202. public function getSumBrokerage(array $where)
  203. {
  204. return $this->dao->getWhereSumField($where, 'brokerage_price');
  205. }
  206. /**
  207. * 根据条件获取用户指定字段列表
  208. * @param array $where
  209. * @param string $field
  210. * @param string $key
  211. * @return array
  212. */
  213. public function getColumn(array $where, string $field = '*', string $key = '')
  214. {
  215. return $this->dao->getColumn($where, $field, $key);
  216. }
  217. /**
  218. * 获取分销用户
  219. * @param array $where
  220. * @param string $field
  221. * @return array
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public function getAgentUserList(array $where = [], string $field = '*', $is_page = true)
  227. {
  228. $where_data['status'] = 1;
  229. if (sys_config('store_brokerage_statu') != 2) $where_data['is_promoter'] = 1;
  230. $where_data['spread_open'] = 1;
  231. if (isset($where['nickname']) && $where['nickname'] !== '') {
  232. $where_data['like'] = $where['nickname'];
  233. }
  234. if (isset($where['data']) && $where['data']) {
  235. $where_data['time'] = $where['data'];
  236. }
  237. [$page, $limit] = $this->getPageValue($is_page);
  238. $list = $this->dao->getAgentUserList($where_data, $field, $page, $limit);
  239. $count = $this->dao->count($where_data);
  240. return compact('count', 'list');
  241. }
  242. /**
  243. * 获取分销员ids
  244. * @param array $where
  245. * @return array
  246. */
  247. public function getAgentUserIds(array $where)
  248. {
  249. $where['status'] = 1;
  250. if (sys_config('store_brokerage_statu') != 2) $where['is_promoter'] = 1;
  251. $where['spread_open'] = 1;
  252. if (isset($where['nickname']) && $where['nickname'] !== '') {
  253. $where['like'] = $where['nickname'];
  254. }
  255. if (isset($where['data']) && $where['data']) {
  256. $where['time'] = $where['data'];
  257. }
  258. return $this->dao->getAgentUserIds($where);
  259. }
  260. /**
  261. * 获取推广人列表
  262. * @param array $where
  263. * @param string $field
  264. * @param int $page
  265. * @param int $limit
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\DbException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. */
  270. public function getSairList(array $where, string $field = '*')
  271. {
  272. $where_data = [];
  273. if (isset($where['uid'])) {
  274. if (isset($where['type'])) {
  275. $type = (int)$where['type'];
  276. $type = in_array($type, [1, 2]) ? $type : 0;
  277. $uids = $this->getUserSpredadUids((int)$where['uid'], $type);
  278. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  279. }
  280. if (isset($where['data']) && $where['data']) {
  281. $where_data['time'] = $where['data'];
  282. }
  283. if (isset($where['nickname']) && $where['nickname']) {
  284. $where_data['like'] = $where['nickname'];
  285. }
  286. $where_data['status'] = 1;
  287. }
  288. [$page, $limit] = $this->getPageValue();
  289. $list = $this->dao->getSairList($where_data, '*', $page, $limit);
  290. $count = $this->dao->count($where_data);
  291. return compact('list', 'count');
  292. }
  293. /**
  294. * 获取推广人统计
  295. * @param array $where
  296. * @param string $field
  297. * @param int $page
  298. * @param int $limit
  299. * @throws \think\db\exception\DataNotFoundException
  300. * @throws \think\db\exception\DbException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. */
  303. public function getSairCount(array $where)
  304. {
  305. $where_data = [];
  306. if (isset($where['uid'])) {
  307. if (isset($where['type'])) {
  308. $uids = $this->getColumn(['spread_uid' => $where['uid']], 'uid');
  309. switch ((int)$where['type']) {
  310. case 1:
  311. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  312. break;
  313. case 2:
  314. if (count($uids))
  315. $spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid');
  316. else
  317. $spread_uid_two = [];
  318. $where_data['uid'] = count($spread_uid_two) > 0 ? $spread_uid_two : 0;
  319. break;
  320. default:
  321. if (count($uids)) {
  322. if ($spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid')) {
  323. $uids = array_merge($uids, $spread_uid_two);
  324. $uids = array_unique($uids);
  325. $uids = array_merge($uids);
  326. }
  327. }
  328. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  329. break;
  330. }
  331. }
  332. if (isset($where['data']) && $where['data']) {
  333. $where_data['time'] = $where['data'];
  334. }
  335. if (isset($where['nickname']) && $where['nickname']) {
  336. $where_data['like'] = $where['nickname'];
  337. }
  338. $where_data['status'] = 1;
  339. }
  340. return $this->dao->count($where_data);
  341. }
  342. /**
  343. * 写入用户信息
  344. * @param array $data
  345. * @return bool
  346. */
  347. public function create(array $data)
  348. {
  349. if (!$this->dao->save($data))
  350. throw new AdminException('写入失败');
  351. return true;
  352. }
  353. /**
  354. * 设置用户登录类型
  355. * @param int $uid
  356. * @param string $type
  357. * @return bool
  358. * @throws Exception
  359. */
  360. public function setLoginType(int $uid, string $type = 'h5')
  361. {
  362. if (!$this->dao->update($uid, ['login_type' => $type]))
  363. throw new Exception('设置登录类型失败');
  364. return true;
  365. }
  366. /**
  367. * 设置用户分组
  368. * @param $uids
  369. * @param int $group_id
  370. */
  371. public function setUserGroup($uids, int $group_id)
  372. {
  373. return $this->dao->batchUpdate($uids, ['group_id' => $group_id], 'uid');
  374. }
  375. /**
  376. * 获取用户标签
  377. * @param array $uids
  378. * @param int $type
  379. * @param int $relation_id
  380. * @return array
  381. * @throws \think\db\exception\DataNotFoundException
  382. * @throws \think\db\exception\DbException
  383. * @throws \think\db\exception\ModelNotFoundException
  384. */
  385. public function getUserLablel(array $uids, int $type = 0, int $relation_id = 0)
  386. {
  387. /** @var UserLabelRelationServices $services */
  388. $services = app()->make(UserLabelRelationServices::class);
  389. $userlabels = $services->getUserLabelList($uids, $type, $relation_id);
  390. $data = [];
  391. foreach ($uids as $uid) {
  392. $labels = array_filter($userlabels, function ($item) use ($uid) {
  393. if ($item['uid'] == $uid && $item['label_id'] && isset($item['label_name']) && $item['label_name']) {
  394. return true;
  395. }
  396. });
  397. $data[$uid] = implode(',', array_column($labels, 'label_name'));
  398. }
  399. return $data;
  400. }
  401. /**
  402. * 显示资源列表头部
  403. * @return array[]
  404. */
  405. public function typeHead()
  406. {
  407. //全部会员
  408. $all = $this->getCount([]);
  409. /** @var UserWechatuserServices $userWechatUser */
  410. $userWechatUser = app()->make(UserWechatuserServices::class);
  411. //小程序会员
  412. $routine = $userWechatUser->getCount(['w.user_type' => 'routine']);
  413. //公众号会员
  414. $wechat = $userWechatUser->getCount(['w.user_type' => 'wechat']);
  415. //H5会员
  416. $h5 = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'h5']);
  417. //pc会员
  418. $pc = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'pc']);
  419. //app会员
  420. $app = $userWechatUser->getCount(['w.user_type' => 'app']);
  421. return [
  422. ['user_type' => '', 'name' => '全部会员', 'count' => $all],
  423. ['user_type' => 'routine', 'name' => '小程序会员', 'count' => $routine],
  424. ['user_type' => 'wechat', 'name' => '公众号会员', 'count' => $wechat],
  425. ['user_type' => 'h5', 'name' => 'H5会员', 'count' => $h5],
  426. ['user_type' => 'pc', 'name' => 'PC会员', 'count' => $pc],
  427. ['user_type' => 'app', 'name' => 'APP会员', 'count' => $app],
  428. ];
  429. }
  430. /**
  431. * 会员列表
  432. * @param array $where
  433. * @return array
  434. */
  435. public function index(array $where)
  436. {
  437. /** @var UserWechatuserServices $userWechatUser */
  438. $userWechatUser = app()->make(UserWechatuserServices::class);
  439. /** @var UserAwardIntegralServices $award_integral_service */
  440. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  441. $fields = 'u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.user_type as w_user_type,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time';
  442. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  443. if ($list) {
  444. $uids = array_column($list, 'uid');
  445. $userlabel = $this->getUserLablel($uids);
  446. $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
  447. $userExtract = app()->make(UserExtractServices::class)->getUsersSumList($uids);
  448. // $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
  449. // $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
  450. $spreadUids = array_unique(array_column($list, 'spread_uid'));
  451. $spread_names = $this->dao->getColumn([['uid', 'in', $spreadUids]], 'nickname', 'uid');
  452. /** @var WorkClientServices $workClientService */
  453. $workClientService = app()->make(WorkClientServices::class);
  454. $clientData = $workClientService->getList(['uid' => $uids], ['id', 'uid', 'name', 'external_userid', 'corp_id', 'unionid'], false);
  455. $clientlist = $clientData['list'];
  456. //补充信息
  457. $extendInfo = SystemConfigService::get('user_extend_info', []);
  458. $is_extend_info = false;
  459. if ($extendInfo) {
  460. foreach ($extendInfo as $item) {
  461. //没选择使用 跳过
  462. if (isset($item['use']) && $item['use']) {
  463. $is_extend_info = true;
  464. break;
  465. }
  466. }
  467. }
  468. foreach ($list as &$item) {
  469. if (empty($item['addres'])) {
  470. if (!empty($item['country']) || !empty($item['province']) || !empty($item['city'])) {
  471. $item['addres'] = $item['country'] . $item['province'] . $item['city'];
  472. }
  473. }
  474. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  475. $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
  476. $item['extract_count_price'] = $userExtract[$item['uid']] ?? 0;//累计提现
  477. $item['spread_uid_nickname'] = $item['spread_uid'] ? (($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid']) : '无';
  478. //用户类型
  479. if ($item['user_type'] == 'routine') {
  480. $item['user_type'] = '小程序';
  481. } else if ($item['user_type'] == 'wechat') {
  482. $item['user_type'] = '公众号';
  483. } else if ($item['user_type'] == 'h5') {
  484. $item['user_type'] = 'H5';
  485. } else if ($item['user_type'] == 'pc') {
  486. $item['user_type'] = 'PC';
  487. } else if ($item['user_type'] == 'app') {
  488. $item['user_type'] = 'APP';
  489. } else $item['user_type'] = '其他';
  490. if ($item['sex'] == 1) {
  491. $item['sex'] = '男';
  492. } else if ($item['sex'] == 2) {
  493. $item['sex'] = '女';
  494. } else $item['sex'] = '保密';
  495. //等级名称
  496. $levelinfo = app()->make(UserLevelServices::class)->getUserLevel($item['uid']);
  497. $item['level'] = $levelinfo['levelInfo']['name'] ?? '无';
  498. //分组名称
  499. $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
  500. //用户等级
  501. $item['vip_name'] = false;
  502. // $levelinfo = $userLevel[$item['uid']] ?? null;
  503. if ($levelinfo) {
  504. if ($levelinfo && ($levelinfo['is_forever'] || time() < $levelinfo['valid_time'])) {
  505. $item['vip_name'] = $item['level'] != '无' ? $item['level'] : false;
  506. }
  507. }
  508. $item['labels'] = $userlabel[$item['uid']] ?? '';
  509. $item['isMember'] = $item['is_money_level'] > 0 ? 1 : 0;
  510. $item['follow_list'] = [];
  511. if ($clientlist) {
  512. foreach ($clientlist as $value) {
  513. if (!empty($value['followOne']) && $value['uid'] == $item['uid']) {
  514. $item['follow_list'][] = $value['followOne'];
  515. }
  516. }
  517. }
  518. $item['is_extend_info'] = $is_extend_info;
  519. $item['static_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $item['uid']]);
  520. $item['action_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $item['uid']]);
  521. }
  522. }
  523. return compact('count', 'list');
  524. }
  525. /**
  526. * 获取修改页面数据
  527. * @param int $id
  528. * @return array
  529. * @throws \FormBuilder\Exception\FormBuilderException
  530. */
  531. public function edit(int $id)
  532. {
  533. $user = $this->getUserInfo($id);
  534. if (!$user)
  535. throw new AdminException('数据不存在');
  536. $f = array();
  537. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(true);
  538. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'))->col(12);
  539. $f[] = Form::input('phone', '手机号码', $user->getData('phone'))->col(12);
  540. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : '')->col(12);
  541. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'))->col(12);
  542. $f[] = Form::input('addres', '用户地址', $user->getData('addres'));
  543. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  544. $f[] = Form::input('pwd', '登录密码')->type('password')->col(12)->placeholder('不改密码请留空');
  545. $f[] = Form::input('true_pwd', '确认密码')->type('password')->col(12)->placeholder('不改密码请留空');
  546. //查询高于当前会员的所有会员等级
  547. // $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  548. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  549. $setOptionLevel = function () use ($systemLevelList) {
  550. $menus = [];
  551. foreach ($systemLevelList as $menu) {
  552. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  553. }
  554. return $menus;
  555. };
  556. $f[] = Form::select('level', '用户等级', (int)$user->getData('level'))->col(12)->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  557. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  558. $setOptionGroup = function () use ($systemGroupList) {
  559. $menus = [];
  560. foreach ($systemGroupList as $menu) {
  561. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  562. }
  563. return $menus;
  564. };
  565. $f[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->col(12)->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  566. /** @var UserLabelServices $userlabelServices */
  567. $userlabelServices = app()->make(UserLabelServices::class);
  568. $systemLabelList = $userlabelServices->getLabelList(['type' => 0]);
  569. $labels = app()->make(UserLabelRelationServices::class)->getUserLabels($user['uid']);
  570. $setOptionLabel = function () use ($systemLabelList) {
  571. $menus = [];
  572. foreach ($systemLabelList as $menu) {
  573. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  574. }
  575. return $menus;
  576. };
  577. $f[] = Form::select('label_id', '用户标签', $labels)->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  578. $f[] = Form::radio('spread_open', '推广资格', $user->getData('spread_open'))->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  579. //分销模式 人人分销
  580. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  581. if (in_array($storeBrokerageStatus, [1, 3])) {
  582. $f[] = Form::radio('is_promoter', '推广员权限', $user->getData('is_promoter'))->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  583. }
  584. $f[] = Form::radio('status', '用户状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  585. return create_form('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
  586. }
  587. /**
  588. * 添加用户表单
  589. * @param int $id
  590. * @return array
  591. * @throws \FormBuilder\Exception\FormBuilderException
  592. */
  593. public function saveForm()
  594. {
  595. $f = array();
  596. $f[] = Form::input('real_name', '真实姓名', '')->col(12)->placeholder('请输入真实姓名');
  597. $f[] = Form::input('phone', '手机号码', '')->col(12)->placeholder('请输入手机号码')->required();
  598. $f[] = Form::date('birthday', '生日', '')->col(12)->placeholder('请选择生日');
  599. $f[] = Form::input('card_id', '身份证号', '')->col(12)->placeholder('请输入身份证号');
  600. $f[] = Form::input('addres', '用户地址', '')->placeholder('请输入用户地址');
  601. $f[] = Form::textarea('mark', '用户备注', '')->placeholder('请输入用户备注');
  602. $f[] = Form::input('pwd', '登录密码')->col(12)->type('password')->placeholder('请输入登录密码');
  603. $f[] = Form::input('true_pwd', '确认密码')->col(12)->type('password')->placeholder('请再次确认密码');
  604. /** @var SystemUserLevelServices $systemUserLevelServices */
  605. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  606. $systemLevelList = $systemUserLevelServices->getWhereLevelList([], 'id,name');
  607. $setOptionLevel = function () use ($systemLevelList) {
  608. $menus = [];
  609. foreach ($systemLevelList as $menu) {
  610. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  611. }
  612. return $menus;
  613. };
  614. $f[] = Form::select('level', '用户等级', '')->col(12)->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  615. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  616. $setOptionGroup = function () use ($systemGroupList) {
  617. $menus = [];
  618. foreach ($systemGroupList as $menu) {
  619. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  620. }
  621. return $menus;
  622. };
  623. $f[] = Form::select('group_id', '用户分组', '')->col(12)->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  624. /** @var UserLabelServices $userLabelServices */
  625. $userLabelServices = app()->make(UserLabelServices::class);
  626. $systemLabelList = $userLabelServices->getLabelList(['type' => 0]);
  627. $setOptionLabel = function () use ($systemLabelList) {
  628. $menus = [];
  629. foreach ($systemLabelList as $menu) {
  630. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  631. }
  632. return $menus;
  633. };
  634. $f[] = Form::select('label_id', '用户标签', '')->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  635. $f[] = Form::radio('spread_open', '推广资格', 1)->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  636. //分销模式 人人分销
  637. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  638. if (in_array($storeBrokerageStatus, [1, 3])) {
  639. $f[] = Form::radio('is_promoter', '推广员权限', 0)->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  640. }
  641. $f[] = Form::radio('status', '用户状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  642. return create_form('添加用户', $f, $this->url('/user/user'), 'POST');
  643. }
  644. /**
  645. * 获取追加信息
  646. * @param int $uid
  647. * @param $userInfo
  648. * @return \FormBuilder\UI\Iview\Components\Group
  649. */
  650. public function extendInfoForm(int $uid = 0)
  651. {
  652. $f = [];
  653. if ($uid) {
  654. $userInfo = $this->getUserInfo($uid);
  655. }
  656. $extendInfo = SystemConfigService::get('user_extend_info', []);
  657. $f = [];
  658. if ($extendInfo) {
  659. $userExtendInfo = $userInfo['extend_info'] ?? [];
  660. if ($userExtendInfo) $userExtendInfo = array_combine(array_column($userExtendInfo, 'info'), $userExtendInfo);
  661. foreach ($extendInfo as $item) {
  662. //没选择使用 跳过
  663. if (!isset($item['use']) || !$item['use']) continue;
  664. $format = $item['format'] ?? '';
  665. $field = $item['param'] ?? $item['info'] ?? '';
  666. switch ($format) {
  667. case 'num'://'数字'
  668. $form = Form::number($field, $item['info'], $userExtendInfo[$item['info']]['value'] ?? 0);
  669. break;
  670. case 'date'://'日期'
  671. $form = Form::date($field, $item['info'], $userExtendInfo[$item['info']]['value'] ?? '');
  672. break;
  673. case 'radio'://'单选项'
  674. $options = [];
  675. if (isset($item['singlearr']) && $item['singlearr']) {
  676. foreach ($item['singlearr'] as $key => $value) {
  677. $options[] = ['value' => $key, 'label' => $value];
  678. }
  679. }
  680. $form = Form::radio($field, $item['info'], (int)($userExtendInfo[$item['info']]['value'] ?? 0))->options($options);
  681. break;
  682. case 'text'://'文本'
  683. case 'id'://'身份证'
  684. case 'mail'://'邮件'
  685. case 'phone'://'手机号'
  686. case 'address'://'地址'
  687. $form = Form::input($field, $item['info'], $userExtendInfo[$item['info']]['value'] ?? '');
  688. break;
  689. }
  690. if ($item['required']) {
  691. $f[] = $form->required($item['tip'] ?? '');
  692. } else {
  693. $f[] = $form;
  694. }
  695. }
  696. }
  697. if (!$f) {
  698. throw new ValidateException('请先去用户设置:设置用户信息');
  699. }
  700. return create_form('用户补充信息', $f, $this->url('/user/user/extend_info/' . $uid), 'POST');
  701. }
  702. /**
  703. * 处理用户补充信息
  704. * @param array $inputExtendInfo
  705. * @param bool $is_all
  706. * @return array
  707. */
  708. public function handelExtendInfo(array $inputExtendInfo, bool $is_all = false)
  709. {
  710. $extendInfo = SystemConfigService::get('user_extend_info', []);
  711. if ($inputExtendInfo && $extendInfo) {
  712. if ($is_all) {//移动端全数据 处理
  713. $inputExtendInfo = array_combine(array_column($inputExtendInfo, 'info'), $inputExtendInfo);
  714. } else {//后台key=>value类型数据
  715. $inputExtendInfo = $inputExtendInfo[0] ?? $inputExtendInfo;
  716. }
  717. foreach ($extendInfo as &$item) {
  718. $value = $is_all ? ($inputExtendInfo[$item['info'] ?? '']['value'] ?? '') : ($inputExtendInfo[$item['param'] ?? $item['info'] ?? ''] ?? '');
  719. if ($value) {
  720. //验证手机号
  721. if ($item['format'] == 'phone') {
  722. if (!check_phone($value)) throw new AdminException('请填写正确的手机号');
  723. }
  724. //验证邮箱
  725. if ($item['format'] == 'mail') {
  726. if (!check_mail($value)) throw new AdminException('请填写正确的邮箱');
  727. }
  728. //验证身份证号
  729. if ($item['format'] == 'id') {
  730. try {
  731. if (!check_card($value)) throw new AdminException('请填写正确的身份证号码');
  732. } catch (\Throwable $e) {
  733. // throw new AdminException( '请填写正确的身份证号码');
  734. }
  735. }
  736. }
  737. $item['value'] = $value;
  738. }
  739. }
  740. return $extendInfo;
  741. }
  742. /**
  743. * 保存用户补充信息
  744. * @param int $uid
  745. * @param array $extend_info 补充信息
  746. * @param array $update 原本需要修改字段
  747. * @param bool $is_all
  748. * @return bool
  749. */
  750. public function saveExtendForm(int $uid, array $extend_info, array $update = [], bool $is_all = false)
  751. {
  752. $userInfo = $this->getUserInfo($uid);
  753. if (!$userInfo) {
  754. throw new ValidateException('用户不存在');
  755. }
  756. $extend_info = $this->handelExtendInfo($extend_info, $is_all) ?: [];
  757. if ($extend_info) {
  758. $default = $this->defaultExtendInfo;
  759. $params = array_column($default, 'param');
  760. $sex = $this->sex;
  761. $update['extend_info'] = $extend_info;
  762. foreach ($extend_info as $info) {
  763. if (isset($info['param']) && in_array($info['param'], $params) && isset($info['value'])) {
  764. if ($info['param'] == 'sex') {
  765. $update['sex'] = $sex[$info['value']] ?? 0;
  766. } elseif ($info['param'] == 'birthday') {
  767. $update['birthday'] = strtotime($info['value']);
  768. } elseif ($info['param'] == 'address') {
  769. $update['addres'] = $info['value'];
  770. } else {
  771. $update[$info['param']] = $info['value'];
  772. }
  773. }
  774. }
  775. }
  776. if ($update) {
  777. $this->dao->update($uid, $update);
  778. $this->dao->cacheTag()->clear();
  779. }
  780. return true;
  781. }
  782. /**
  783. * 修改提交处理
  784. * @param $id
  785. * @return mixed
  786. */
  787. public function updateInfo(int $id, array $data)
  788. {
  789. $user = $this->getUserInfo($id);
  790. if (!$user) {
  791. throw new AdminException('数据不存在!');
  792. }
  793. $res1 = false;
  794. $res2 = false;
  795. $edit = [];
  796. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  797. $data['money'] = sprintf("%.2f", $data['money']);
  798. /** @var UserMoneyServices $userMoneyServices */
  799. $userMoneyServices = app()->make(UserMoneyServices::class);
  800. if ($data['money_status'] == 1) {//增加
  801. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  802. $res1 = $userMoneyServices->income('system_add', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  803. } else if ($data['money_status'] == 2) {//减少
  804. if ($user['now_money'] > $data['money']) {
  805. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  806. } else {
  807. $edit['now_money'] = 0;
  808. $data['money'] = $user['now_money'];
  809. }
  810. $res1 = $userMoneyServices->income('system_sub', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  811. }
  812. } else {
  813. $res1 = true;
  814. }
  815. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  816. $integral_data = ['link_id' => $data['adminId'] ?? 0, 'number' => $data['integration'], 'balance' => $user['integral']];
  817. /** @var UserBillServices $userBill */
  818. $userBill = app()->make(UserBillServices::class);
  819. $balance = $user['integral'];
  820. if ($data['integration_status'] == 1) {//增加
  821. $balance = bcadd((string)$user['integral'], (string)$data['integration'], 0);
  822. $res2 = $userBill->income('system_add_integral', $id, (int)$data['integration'], (int)$balance, $data['adminId'] ?? 0);
  823. } else if ($data['integration_status'] == 2) {//减少
  824. $balance = bcsub((string)$user['integral'], (string)$data['integration'], 0);
  825. $res2 = $userBill->income('system_sub_integral', $id, (int)$data['integration'], (int)$balance, $data['adminId'] ?? 0);
  826. }
  827. $edit['integral'] = $balance;
  828. } else {
  829. $res2 = true;
  830. }
  831. //修改基本信息
  832. if (!isset($data['is_other']) || !$data['is_other']) {
  833. if ($data['phone']) {
  834. $otherUser = $this->getOne(['phone' => $data['phone']], 'uid,phone');
  835. if ($otherUser && $otherUser['uid'] != $id) {
  836. throw new AdminException('该手机号码已被注册');
  837. }
  838. }
  839. /** @var UserLabelRelationServices $userLabel */
  840. $userLabel = app()->make(UserLabelRelationServices::class);
  841. if (is_string($data['label_id'])) {
  842. $data['label_id'] = [$data['label_id']];
  843. }
  844. $userLabel->setUserLable([$id], $data['label_id'], 0, 0, true);
  845. if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
  846. $edit['pwd'] = $data['pwd'];
  847. }
  848. if (isset($data['spread_open'])) {
  849. $edit['spread_open'] = $data['spread_open'];
  850. }
  851. $edit['status'] = $data['status'];
  852. $edit['real_name'] = $data['real_name'];
  853. $edit['card_id'] = $data['card_id'];
  854. $edit['birthday'] = strtotime($data['birthday']);
  855. $edit['mark'] = $data['mark'];
  856. $edit['is_promoter'] = $data['is_promoter'];
  857. $edit['level'] = $data['level'];
  858. $edit['phone'] = $data['phone'];
  859. $edit['addres'] = $data['addres'];
  860. $edit['group_id'] = $data['group_id'];
  861. if (isset($data['area_admin']) && $data['area_admin'] >= 0) {
  862. $edit['area_admin'] = $data['area_admin'];
  863. $edit['area_admin_province'] = $data['area_admin_province'];
  864. $edit['area_admin_city'] = $data['area_admin_city'];
  865. $edit['area_admin_district'] = $data['area_admin_district'];
  866. $edit['area_provincials'] = $data['area_provincials'];
  867. }
  868. if ($data['spread_uid'] != -1) {
  869. $edit['spread_uid'] = $data['spread_uid'];
  870. $edit['spread_time'] = $data['spread_uid'] ? time() : 0;
  871. }
  872. $edit['sex'] = $data['sex'];
  873. $edit['provincials'] = $data['provincials'];
  874. $edit['province'] = $data['province'];
  875. $edit['city'] = $data['city'];
  876. $edit['area'] = $data['area'];
  877. $edit['street'] = $data['street'];
  878. if (isset($data['extend_info']) && $data['extend_info']) $edit['extend_info'] = $data['extend_info'];
  879. if ($user['level'] != $data['level']) {
  880. /** @var UserLevelServices $userLevelService */
  881. $userLevelService = app()->make(UserLevelServices::class);
  882. $userLevelService->setUserLevel((int)$user['uid'], (int)$data['level']);
  883. }
  884. if ($data['spread_uid'] != $user['spread_uid']) {
  885. //记录推广绑定关系
  886. UserSpreadJob::dispatch([$id, (int)$data['spread_uid'], 0, (int)$data['adminId']]);
  887. //记录好友关系
  888. UserFriendsJob::dispatch([$id, (int)$data['spread_uid']]);
  889. }
  890. /** @var WechatUserServices $wechatUser */
  891. $wechatUser = app()->make(WechatUserServices::class);
  892. $wechatUser->update(['uid' => $id], ['sex' => $data['sex']]);
  893. }
  894. if ($edit) $res3 = $this->dao->update($id, $edit);
  895. else $res3 = true;
  896. if ($res1 && $res2 && $res3) {
  897. $this->dao->cacheTag()->clear();
  898. return true;
  899. } else throw new AdminException('修改失败');
  900. }
  901. /**
  902. * 编辑其他
  903. * @param $id
  904. * @return mixed
  905. * @throws \FormBuilder\Exception\FormBuilderException
  906. */
  907. public function editOther($id)
  908. {
  909. $user = $this->getUserInfo($id);
  910. if (!$user) {
  911. throw new AdminException('数据不存在!');
  912. }
  913. $f = array();
  914. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  915. $f[] = Form::number('money', '余额', 0)->max(999999)->min(0)->style(['width' => '100px']);
  916. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  917. $f[] = Form::number('integration', '积分', 0)->max(999999)->min(0)->precision(0)->style(['width' => '100px']);
  918. return create_form('修改积分余额', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
  919. }
  920. /**
  921. * 设置会员分组
  922. * @param $id
  923. * @return mixed
  924. */
  925. public function setGroup($uids, $all, $where)
  926. {
  927. $userGroup = app()->make(UserGroupServices::class)->getGroupList();
  928. if (count($uids) == 1) {
  929. $user = $this->getUserInfo($uids[0], ['group_id']);
  930. $setOptionUserGroup = function () use ($userGroup) {
  931. $menus = [];
  932. foreach ($userGroup as $menu) {
  933. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  934. }
  935. return $menus;
  936. };
  937. $field[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  938. } else {
  939. $setOptionUserGroup = function () use ($userGroup) {
  940. $menus = [];
  941. foreach ($userGroup as $menu) {
  942. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  943. }
  944. return $menus;
  945. };
  946. $field[] = Form::select('group_id', '用户分组')->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  947. }
  948. $field[] = Form::hidden('uids', implode(',', $uids));
  949. $field[] = Form::hidden('all', $all);
  950. $field[] = Form::hidden('where', $where ? json_encode($where) : "");
  951. return create_form('设置用户分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
  952. }
  953. /**
  954. * 保存会员分组
  955. * @param $id
  956. * @return mixed
  957. */
  958. public function saveSetGroup($uids, int $group_id, $redisKey, $queueId)
  959. {
  960. /** @var UserGroupServices $userGroup */
  961. $userGroup = app()->make(UserGroupServices::class);
  962. /** @var QueueServices $queueService */
  963. $queueService = app()->make(QueueServices::class);
  964. if (!$userGroup->getGroup($group_id)) {
  965. throw new AdminException('该分组不存在');
  966. }
  967. if (!$this->setUserGroup($uids, $group_id)) {
  968. $queueService->addQueueFail($queueId['id'], $redisKey);
  969. throw new AdminException('设置分组失败或无改动');
  970. } else {
  971. $queueService->doSuccessSremRedis($uids, $redisKey, $queueId['type']);
  972. }
  973. return true;
  974. }
  975. /**
  976. * 设置用户标签
  977. * @param $uids
  978. * @param $all
  979. * @param $where
  980. * @param int $type
  981. * @param int $store_id
  982. * @return mixed
  983. * @throws \think\db\exception\DataNotFoundException
  984. * @throws \think\db\exception\DbException
  985. * @throws \think\db\exception\ModelNotFoundException
  986. */
  987. public function setLabel($uids, $all, $where, int $type = 0, int $relation_id = 0)
  988. {
  989. /** @var UserLabelServices $userlabelServices */
  990. $userlabelServices = app()->make(UserLabelServices::class);
  991. $userLabel = $userlabelServices->getLabelList(['type' => $type, 'relation_id' => $relation_id]);
  992. if (count($uids) == 1) {
  993. /** @var UserLabelRelationServices $userLabeLRelation */
  994. $userLabeLRelation = app()->make(UserLabelRelationServices::class);
  995. $lids = $userLabeLRelation->getUserLabels($uids[0], $type, $relation_id);
  996. $setOptionUserLabel = function () use ($userLabel) {
  997. $menus = [];
  998. foreach ($userLabel as $menu) {
  999. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  1000. }
  1001. return $menus;
  1002. };
  1003. $field[] = Form::select('label_id', '用户标签', $lids)->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  1004. } else {
  1005. $setOptionUserLabel = function () use ($userLabel) {
  1006. $menus = [];
  1007. foreach ($userLabel as $menu) {
  1008. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  1009. }
  1010. return $menus;
  1011. };
  1012. $field[] = Form::select('label_id', '用户标签')->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  1013. }
  1014. $field[] = Form::hidden('uids', implode(',', $uids));
  1015. $field[] = Form::hidden('all', $all);
  1016. $field[] = Form::hidden('where', $where ? json_encode($where) : "");
  1017. return create_form('设置用户标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
  1018. }
  1019. /**
  1020. * 保存用户标签
  1021. * @param $uids
  1022. * @param $label_id
  1023. * @return bool
  1024. */
  1025. public function saveSetLabel($uids, $label_id)
  1026. {
  1027. /** @var UserLabelRelationServices $services */
  1028. $services = app()->make(UserLabelRelationServices::class);
  1029. if ($label_id) {
  1030. /** @var UserLabelServices $userlabel */
  1031. $userlabel = app()->make(UserLabelServices::class);
  1032. if (count($label_id) != $userlabel->getCount([['id', 'in', $label_id]])) {
  1033. throw new AdminException('用户标签不存在或被删除');
  1034. }
  1035. if (!$services->setUserLable($uids, $label_id)) {
  1036. throw new AdminException('设置标签失败');
  1037. }
  1038. } else {//没传入标签 默认清空
  1039. if (!is_array($uids)) {
  1040. $uids = [$uids];
  1041. }
  1042. foreach ($uids as $uid) {
  1043. $services->unUserLabel((int)$uid);
  1044. }
  1045. }
  1046. return true;
  1047. }
  1048. /**
  1049. * 批量队列设置标签
  1050. * @param $uids
  1051. * @param $lable_id
  1052. * @param $redisKey
  1053. * @param $queueId
  1054. * @return bool
  1055. */
  1056. public function saveBatchSetLabel($uids, $lable_id, $redisKey, $queueId)
  1057. {
  1058. /** @var QueueServices $queueService */
  1059. $queueService = app()->make(QueueServices::class);
  1060. foreach ($lable_id as $id) {
  1061. if (!app()->make(UserLabelServices::class)->getLable((int)$id)) {
  1062. throw new AdminException('用户标签不存在或被删除');
  1063. }
  1064. }
  1065. /** @var UserLabelRelationServices $services */
  1066. $services = app()->make(UserLabelRelationServices::class);
  1067. if (!$services->setUserLable($uids, $lable_id)) {
  1068. $queueService->addQueueFail($queueId['id'], $redisKey);
  1069. throw new AdminException('设置标签失败');
  1070. } else {
  1071. $queueService->doSuccessSremRedis($uids, $redisKey, $queueId['type']);
  1072. }
  1073. return true;
  1074. }
  1075. /**
  1076. * 赠送会员等级
  1077. * @param int $uid
  1078. * @return mixed
  1079. * */
  1080. public function giveLevel($id)
  1081. {
  1082. if (!$this->userExist($id)) {
  1083. throw new AdminException('用户不存在');
  1084. }
  1085. //查询高于当前会员的所有会员等级
  1086. $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  1087. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', $grade ?? 0], 'id,name');
  1088. $setOptionlevel = function () use ($systemLevelList) {
  1089. $menus = [];
  1090. foreach ($systemLevelList as $menu) {
  1091. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  1092. }
  1093. return $menus;
  1094. };
  1095. $field[] = Form::select('level_id', '用户等级')->setOptions(FormBuilder::setOptions($setOptionlevel))->filterable(true);
  1096. return create_form('赠送等级', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
  1097. }
  1098. /**
  1099. * 执行赠送会员等级
  1100. * @param int $uid
  1101. * @return mixed
  1102. * */
  1103. public function saveGiveLevel(int $id, int $level_id)
  1104. {
  1105. if (!$this->userExist($id)) {
  1106. throw new AdminException('用户不存在');
  1107. }
  1108. /** @var SystemUserLevelServices $systemLevelServices */
  1109. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  1110. /** @var UserLevelServices $userLevelServices */
  1111. $userLevelServices = app()->make(UserLevelServices::class);
  1112. //查询当前选择的会员等级
  1113. $systemLevel = $systemLevelServices->getLevel($level_id);
  1114. if (!$systemLevel) throw new AdminException('您选择赠送的用户等级不存在!');
  1115. //检查是否拥有此会员等级
  1116. $level = $userLevelServices->getWhereLevel(['uid' => $id, 'level_id' => $level_id], 'valid_time,is_forever');
  1117. if ($level && $level['status'] == 1 && $level['is_del'] == 0) {
  1118. throw new AdminException('此用户已有该用户等级,无法再次赠送');
  1119. }
  1120. //保存会员信息
  1121. if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
  1122. throw new AdminException('赠送失败');
  1123. }
  1124. return true;
  1125. }
  1126. /**
  1127. * 赠送付费会员时长
  1128. * @param int $uid
  1129. * @return mixed
  1130. * */
  1131. public function giveLevelTime($id)
  1132. {
  1133. $userInfo = $this->getUserCacheInfo($id);
  1134. if (!$userInfo) {
  1135. throw new AdminException('用户不存在');
  1136. }
  1137. $overdue_time = '';
  1138. if ($userInfo['is_ever_level'] == 1) {
  1139. $overdue_time = '永久';
  1140. } else {
  1141. if ($userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] > 0) {
  1142. $overdue_time = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1143. } else {
  1144. $overdue_time = '已过期/暂未开通';
  1145. }
  1146. }
  1147. $field[] = Form::input('overdue_time', '会员到期时间', $overdue_time)->disabled(true);
  1148. $field[] = Form::radio('days_status', '修改付费会员', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  1149. $field[] = Form::number('days', '调整时长(天)')->min(0)->max(999999)->precision(0)->placeholder('请输入');
  1150. return create_form('赠送付费会员时长', $field, Url::buildUrl('/user/save_give_level_time/' . $id), 'PUT');
  1151. }
  1152. /**
  1153. * 执行赠送付费会员时长
  1154. * @param int $id
  1155. * @param int $days
  1156. * @param int $days_status 1:增加 2:减少
  1157. * @return bool
  1158. * @throws \Exception
  1159. */
  1160. public function saveGiveLevelTime(int $id, int $days, int $days_status = 1)
  1161. {
  1162. $userInfo = $this->getUserInfo($id);
  1163. if (!$userInfo) {
  1164. throw new AdminException('用户不存在');
  1165. }
  1166. if ($days <= 0) throw new AdminException('赠送天数不能小于1天');
  1167. if ($userInfo['is_ever_level'] == 1) {
  1168. throw new AdminException('永久会员无需操作');
  1169. }
  1170. $update = [];
  1171. $days_time = bcmul((string)$days, '86400');
  1172. if ($userInfo['is_money_level'] == 0) {
  1173. $update['is_money_level'] = 3;
  1174. $time = time();
  1175. } else {
  1176. $time = $userInfo['overdue_time'];
  1177. }
  1178. if ($days_status == 1) {//增加
  1179. $time = (int)bcadd((string)$time, (string)$days_time);
  1180. } else {//减少
  1181. if ($time <= $days_time) {
  1182. $time = 0;
  1183. } else {
  1184. $time = (int)bcsub((string)$time, (string)$days_time);
  1185. }
  1186. }
  1187. $update['overdue_time'] = $time;
  1188. if ($time < time()) {//已经过期
  1189. $update['is_money_level'] = 0;
  1190. }
  1191. $this->dao->update($id, $update);
  1192. $userInfo->save();
  1193. /** @var StoreOrderCreateServices $storeOrderCreateService */
  1194. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  1195. $orderInfo = [
  1196. 'uid' => $id,
  1197. 'order_id' => $storeOrderCreateService->getNewOrderId(),
  1198. 'type' => 4,
  1199. 'member_type' => 0,
  1200. 'pay_type' => 'admin',
  1201. 'paid' => 1,
  1202. 'pay_time' => time(),
  1203. 'is_free' => 1,
  1204. 'overdue_time' => $time,
  1205. 'vip_day' => $days_status == 1 ? $days : bcsub('0', $days),
  1206. 'add_time' => time()
  1207. ];
  1208. /** @var OtherOrderServices $otherOrder */
  1209. $otherOrder = app()->make(OtherOrderServices::class);
  1210. $otherOrder->save($orderInfo);
  1211. return true;
  1212. }
  1213. /**
  1214. * 清除会员等级
  1215. * @paran int $uid
  1216. * @paran boolean
  1217. * */
  1218. public function cleanUpLevel($uid)
  1219. {
  1220. if (!$this->userExist($uid))
  1221. throw new AdminException('用户不存在');
  1222. /** @var UserLevelServices $services */
  1223. $services = app()->make(UserLevelServices::class);
  1224. return $this->transaction(function () use ($uid, $services) {
  1225. $res = $services->delUserLevel($uid);
  1226. $res1 = $this->dao->update($uid, ['clean_time' => time(), 'level' => 0, 'exp' => 0], 'uid');
  1227. if (!$res && !$res1)
  1228. throw new AdminException('清除失败');
  1229. return true;
  1230. });
  1231. }
  1232. /**
  1233. * 获取用户详情里面的用户消费能力和用户余额积分等
  1234. * @param $uid
  1235. * @return array[]
  1236. */
  1237. public function getHeaderList(int $uid, $userInfo = [])
  1238. {
  1239. if (!$userInfo) {
  1240. $userInfo = $this->getUserInfo($uid);
  1241. }
  1242. /** @var StoreOrderServices $orderServices */
  1243. $orderServices = app()->make(StoreOrderServices::class);
  1244. $where = ['pid' => 0, 'uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  1245. return [
  1246. [
  1247. 'title' => '余额',
  1248. 'value' => $userInfo['now_money'] ?? 0,
  1249. 'key' => '元',
  1250. ],
  1251. [
  1252. 'title' => '总计订单',
  1253. 'value' => $orderServices->count($where),
  1254. 'key' => '笔',
  1255. ],
  1256. [
  1257. 'title' => '总消费金额',
  1258. 'value' => $orderServices->together($where, 'pay_price'),
  1259. 'key' => '元',
  1260. ],
  1261. [
  1262. 'title' => '积分',
  1263. 'value' => $userInfo['integral'] ?? 0,
  1264. 'key' => '',
  1265. ],
  1266. [
  1267. 'title' => '用户经验',
  1268. 'value' => $userInfo['exp'] ?? 0,
  1269. 'key' => '',
  1270. ],
  1271. [
  1272. 'title' => '消费次数',
  1273. 'value' => $orderServices->count(['uid' => $uid, 'pid' => [0, -1], 'paid' => 1, 'is_del' => 0, 'is_system_del' => 0]),
  1274. 'key' => '',
  1275. ],
  1276. // [
  1277. // 'title' => '本月订单',
  1278. // 'value' => $orderServices->count($where + ['time' => 'month']),
  1279. // 'key' => '笔',
  1280. // ],
  1281. // [
  1282. // 'title' => '本月消费金额',
  1283. // 'value' => $orderServices->together($where + ['time' => 'month'], 'pay_price'),
  1284. // 'key' => '元',
  1285. // ]
  1286. ];
  1287. }
  1288. /**
  1289. * 用户详情
  1290. * @param int $uid
  1291. * @return array
  1292. */
  1293. public function read(int $uid)
  1294. {
  1295. $userInfo = $this->dao->getUserWithTrashedInfo($uid);
  1296. if (!$userInfo) {
  1297. throw new AdminException('数据不存在');
  1298. }
  1299. $userInfo = $userInfo->toArray();
  1300. $spread_uid_nickname = '';
  1301. if ($userInfo['spread_uid']) {
  1302. $spread_uid_nickname = $this->dao->value(['uid' => $userInfo['spread_uid']], 'nickname');
  1303. }
  1304. if ($userInfo['is_ever_level'] == 1) {
  1305. $userInfo['overdue_time'] = '永久';
  1306. } else {
  1307. if ($userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] > 0) {
  1308. $userInfo['overdue_time'] = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1309. }
  1310. }
  1311. $userInfo['spread_uid_nickname'] = $userInfo['spread_uid'] ? $spread_uid_nickname . '/' . $userInfo['spread_uid'] : '';
  1312. $userInfo['_add_time'] = date('Y-m-d H:i:s', $userInfo['add_time']);
  1313. $userInfo['_last_time'] = date('Y-m-d H:i:s', $userInfo['last_time']);
  1314. $userInfo['birthday'] = $userInfo['birthday'] ? date('Y-m-d', $userInfo['birthday']) : '';
  1315. /** @var UserLabelRelationServices $userLabelServices */
  1316. $userLabelServices = app()->make(UserLabelRelationServices::class);
  1317. $label_list = $userLabelServices->getUserLabelList([$uid]);
  1318. $label_id = [];
  1319. $userInfo['group'] = app()->make(UserGroupServices::class)->getGroup($userInfo['group_id']);
  1320. $userInfo['label_list'] = '';
  1321. if ($label_list) {
  1322. $userInfo['label_list'] = implode(',', array_column($label_list, 'label_name'));
  1323. foreach ($label_list as $item) {
  1324. $label_id[] = [
  1325. 'id' => $item['label_id'],
  1326. 'label_name' => $item['label_name']
  1327. ];
  1328. }
  1329. }
  1330. $userInfo['vip_name'] = '';
  1331. if ($userInfo['level']) {
  1332. /** @var SystemUserLevelServices $levelServices */
  1333. $levelServices = app()->make(SystemUserLevelServices::class);
  1334. $levelInfo = $levelServices->getOne(['id' => $userInfo['level']], 'id,name');
  1335. $userInfo['vip_name'] = $levelInfo['name'] ?? '';
  1336. }
  1337. $userInfo['label_id'] = $label_id;
  1338. $workClientInfo = $workMemberInfo = null;
  1339. if ($userInfo['phone']) {
  1340. /** @var WorkMemberServices $workMemberService */
  1341. $workMemberService = app()->make(WorkMemberServices::class);
  1342. $workMemberInfo = $workMemberService->get(['mobile' => $userInfo['phone']]);
  1343. if ($workMemberInfo) {
  1344. if (!$workMemberInfo->uid) {
  1345. $workMemberInfo->uid = $userInfo['uid'];
  1346. $workMemberInfo->save();
  1347. }
  1348. $workMemberInfo = $workMemberInfo->toArray();
  1349. }
  1350. }
  1351. if (!$workMemberInfo) {
  1352. /** @var WorkClientServices $workClientService */
  1353. $workClientService = app()->make(WorkClientServices::class);
  1354. $workClientInfo = $workClientService->get(['uid' => $userInfo['uid']]);
  1355. if ($workClientInfo) {
  1356. $workClientInfo = $workClientInfo->toArray();
  1357. }
  1358. }
  1359. $info = [
  1360. 'uid' => $uid,
  1361. 'userinfo' => [],
  1362. 'headerList' => $this->getHeaderList($uid, $userInfo),
  1363. 'count' => [0, 0, 0],
  1364. 'ps_info' => $userInfo,
  1365. 'workClientInfo' => $workClientInfo,
  1366. 'workMemberInfo' => $workMemberInfo,
  1367. ];
  1368. return $info;
  1369. }
  1370. /**
  1371. * 获取单个用户信息
  1372. * @param $id 用户id
  1373. * @return mixed
  1374. */
  1375. public function oneUserInfo(int $id, string $type)
  1376. {
  1377. switch ($type) {
  1378. case 'spread':
  1379. // /** @var UserSpreadServices $services */
  1380. // $services = app()->make(UserSpreadServices::class);
  1381. /** @var UserFriendsServices $services */
  1382. $services = app()->make(UserFriendsServices::class);
  1383. return $services->getFriendList($id);
  1384. break;
  1385. case 'order':
  1386. /** @var StoreOrderServices $services */
  1387. $services = app()->make(StoreOrderServices::class);
  1388. return $services->getUserOrderList($id);
  1389. break;
  1390. case 'integral':
  1391. /** @var UserBillServices $services */
  1392. $services = app()->make(UserBillServices::class);
  1393. return $services->getIntegralList($id, [], 'title,number,balance,mark,add_time');
  1394. break;
  1395. case 'sign':
  1396. /** @var UserBillServices $services */
  1397. $services = app()->make(UserBillServices::class);
  1398. return $services->getSignList($id, [], 'title,number,mark,add_time');
  1399. break;
  1400. case 'coupon':
  1401. /** @var StoreCouponUserServices $services */
  1402. $services = app()->make(StoreCouponUserServices::class);
  1403. return $services->getUserCouponList($id, 0);
  1404. break;
  1405. case 'balance_change':
  1406. /** @var UserMoneyServices $services */
  1407. $services = app()->make(UserMoneyServices::class);
  1408. return $services->getUserMoneyList($id, [], 'title,type,number,balance,mark,pm,status,add_time');
  1409. break;
  1410. case 'visit':
  1411. /** @var StoreProductLogServices $services */
  1412. $services = app()->make(StoreProductLogServices::class);
  1413. return $services->getList(['uid' => $id, 'type' => 'visit'], 'product_id', 'id,product_id,max(add_time) as add_time');
  1414. break;
  1415. case 'spread_change':
  1416. /** @var UserSpreadServices $services */
  1417. $services = app()->make(UserSpreadServices::class);
  1418. return $services->getSpreadList(['store_id' => 0, 'staff_id' => 0, 'uid' => $id], '*', ['spreadUser', 'admin'], false);
  1419. break;
  1420. case 'static_integral':
  1421. /** @var UserAwardIntegralServices $award_integral_service */
  1422. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  1423. return $award_integral_service->getIntegralList(['type' => 0, 'uid' => $id]);
  1424. break;
  1425. case 'action_integral':
  1426. /** @var UserAwardIntegralServices $award_integral_service */
  1427. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  1428. return $award_integral_service->getIntegralList(['type' => 1, 'uid' => $id]);
  1429. break;
  1430. case 'energy':
  1431. /** @var UserBillServices $services */
  1432. $services = app()->make(UserBillServices::class);
  1433. return $services->getEnergyList($id, [], 'title,number,balance,mark,add_time');
  1434. break;
  1435. default:
  1436. throw new AdminException('type参数错误');
  1437. }
  1438. }
  1439. /**
  1440. * 用户图表
  1441. * @return array
  1442. */
  1443. public function userChart()
  1444. {
  1445. [$starday, $yesterday, $timeType, $timeKey] = $this->timeHandle('thirtyday', true);
  1446. $user_list = $this->dao->userList($starday, $yesterday);
  1447. $chartdata = [];
  1448. $chartdata['legend'] = ['用户数'];//分类
  1449. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1450. $chartdata['xAxis'] = $timeKey;//X轴值
  1451. $chartdata['series'] = [];//分类1值
  1452. if (!empty($user_list)) {
  1453. $user_list = array_combine(array_column($user_list, 'day'), $user_list);
  1454. $chartdata['yAxis']['maxnum'] = max(array_column($user_list, 'count'));
  1455. foreach ($timeKey as $day) {
  1456. if (isset($user_list[$day])) {
  1457. $chartdata['series'][] = $user_list[$day]['count'] ?? 0;
  1458. } else {
  1459. $chartdata['series'][] = 0;
  1460. }
  1461. }
  1462. }
  1463. $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
  1464. $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  1465. $pay[0] = $this->dao->count(['pay_count' => 0]);
  1466. $pay[1] = $this->dao->count(['pay_count' => 1]);
  1467. $pay[2] = $this->dao->userCount(1);
  1468. $pay[3] = $this->dao->userCount(2);
  1469. foreach ($pay as $key => $item) {
  1470. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  1471. }
  1472. $chartdata['bing_data'] = $bing_data;
  1473. return $chartdata;
  1474. }
  1475. /***********************************************/
  1476. /************ 前端api services *****************/
  1477. /***********************************************/
  1478. /**
  1479. * 用户信息
  1480. * @param $info
  1481. * @return mixed
  1482. */
  1483. public function userInfo($info)
  1484. {
  1485. $uid = (int)$info['uid'];
  1486. $broken_time = intval(sys_config('extract_time'));
  1487. $search_time = time() - 86400 * $broken_time;
  1488. //改造时间
  1489. $search_time = '1970/01/01' . ' - ' . date('Y/m/d H:i:s', $search_time);
  1490. //可提现佣金
  1491. //返佣 +
  1492. /** @var UserBrokerageServices $userBrokerageServices */
  1493. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1494. $brokerage_commission = (string)$userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], $search_time);
  1495. //退款退的佣金 -
  1496. $refund_commission = (string)$userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 0], $search_time);
  1497. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  1498. if ($info['broken_commission'] < 0)
  1499. $info['broken_commission'] = 0;
  1500. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  1501. if ($info['commissionCount'] < 0)
  1502. $info['commissionCount'] = 0;
  1503. return $info;
  1504. }
  1505. /**
  1506. * 个人中心
  1507. * @param array $user
  1508. */
  1509. public function personalHome(array $user, $tokenData)
  1510. {
  1511. $uid = (int)$user['uid'];
  1512. event('user.login', [$uid, app('request')->ip()]);
  1513. /** @var StoreCouponUserServices $storeCoupon */
  1514. $storeCoupon = app()->make(StoreCouponUserServices::class);
  1515. /** @var UserMoneyServices $userMoneyServices */
  1516. $userMoneyServices = app()->make(UserMoneyServices::class);
  1517. /** @var UserExtractServices $userExtract */
  1518. $userExtract = app()->make(UserExtractServices::class);
  1519. /** @var StoreOrderServices $storeOrder */
  1520. $storeOrder = app()->make(StoreOrderServices::class);
  1521. /** @var UserLevelServices $userLevel */
  1522. $userLevel = app()->make(UserLevelServices::class);
  1523. /** @var StoreServiceServices $storeService */
  1524. $storeService = app()->make(StoreServiceServices::class);
  1525. /** @var WechatUserServices $wechatUser */
  1526. $wechatUser = app()->make(WechatUserServices::class);
  1527. /** @var UserRelationServices $productRelation */
  1528. $productRelation = app()->make(UserRelationServices::class);
  1529. /** @var SystemMessageServices $systemMessageServices */
  1530. $systemMessageServices = app()->make(SystemMessageServices::class);
  1531. /** @var DiyServices $diyServices */
  1532. $diyServices = app()->make(DiyServices::class);
  1533. /** @var AgentLevelServices $agentLevelServices */
  1534. $agentLevelServices = app()->make(AgentLevelServices::class);
  1535. /** @var StoreProductLogServices $storeProductLogServices */
  1536. $storeProductLogServices = app()->make(StoreProductLogServices::class);
  1537. //是否存在核销码
  1538. if (!$user['bar_code']) {
  1539. $bar_code = $this->getBarCode();
  1540. $this->dao->update($uid, ['bar_code' => $bar_code], 'uid');
  1541. $user['bar_code'] = $bar_code;
  1542. }
  1543. //获取配置参数
  1544. $configData = SystemConfigService::more([
  1545. 'member_card_status',
  1546. 'brokerage_func_status',
  1547. 'store_brokerage_statu',
  1548. 'store_brokerage_price',
  1549. 'member_func_status',
  1550. 'recharge_switch',
  1551. 'extract_time',
  1552. 'balance_func_status',
  1553. 'invoice_func_status',
  1554. 'special_invoice_status',
  1555. 'user_extract_bank_status',
  1556. 'user_extract_wechat_status',
  1557. 'user_extract_alipay_status',
  1558. 'level_activate_status'
  1559. ]);
  1560. //看付费会员是否开启
  1561. $user['is_open_member'] = $user['svip_open'] = !!($configData['member_card_status'] ?? 0);
  1562. $user['agent_level_name'] = '';
  1563. //分销等级信息
  1564. if ($user['agent_level']) {
  1565. $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name');
  1566. $user['agent_level_name'] = $levelInfo && $levelInfo['name'] ? $levelInfo['name'] : '';
  1567. }
  1568. $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
  1569. $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
  1570. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  1571. $user['like'] = $productRelation->getUserCount($uid, 0, 'like');
  1572. $collectCategory = sys_config('video_func_status', 1) ? '' : 'product';
  1573. $user['collectCount'] = $productRelation->getUserCount($uid, 0, 'collect', $collectCategory);
  1574. $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
  1575. $user['notice'] = 0;
  1576. $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
  1577. $user['orderStatusSum'] = (float)$userMoneyServices->sum(['uid' => $uid, 'pm' => 0, 'status' => 1], 'number', true);
  1578. $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
  1579. $user['extractPrice'] = $user['brokerage_price'];//可提现
  1580. $user['statu'] = (int)($configData['store_brokerage_statu'] ?? 0);
  1581. $orderStatusSum = (float)$storeOrder->sum(['pid' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'uid' => $user['uid'], 'is_del' => 0], 'pay_price', true);//累计有效消费
  1582. $user['spread_status'] = ($configData['brokerage_func_status'] ?? 1) && $this->checkUserPromoter($user['uid'], $user, $orderStatusSum);
  1583. if (!$user['is_promoter'] && $user['spread_status']) {
  1584. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  1585. $user['is_promoter'] = 1;
  1586. }
  1587. if ($user['statu'] == 3) {
  1588. $storeBrokeragePrice = $configData['store_brokerage_price'] ?? 0;
  1589. $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$user['orderStatusSum'], 2);
  1590. }
  1591. /** @var UserBrokerageServices $userBrokerageServices */
  1592. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1593. $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  1594. if ($user['broken_commission'] < 0)
  1595. $user['broken_commission'] = 0;
  1596. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  1597. if ($user['commissionCount'] < 0)
  1598. $user['commissionCount'] = 0;
  1599. //用户等级信息
  1600. $userLevelInfo = $userLevel->homeGetUserLevel((int)$user['uid'], $user);
  1601. $user = array_merge($user, $userLevelInfo);
  1602. $user['yesterDay'] = $userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
  1603. $user['recharge_switch'] = (int)($configData['recharge_switch'] ?? 0);//充值开关
  1604. $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  1605. $user['broken_day'] = (int)($configData['extract_time'] ?? 0);//佣金冻结时间
  1606. $user['balance_func_status'] = (int)($configData['balance_func_status'] ?? 0);
  1607. $user['invioce_func'] = !!($configData['invoice_func_status'] ?? 0);
  1608. $user['special_invoice'] = $user['invioce_func'] && ($configData['special_invoice_status'] ?? 0);
  1609. $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
  1610. $user['member_style'] = (int)$diyServices->getColorChange('member');
  1611. if ($user['is_ever_level']) {
  1612. $user['vip_status'] = 1;//永久会员
  1613. } else {
  1614. if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
  1615. $user['vip_status'] = -1;//开通过已过期
  1616. } else if (!$user['overdue_time'] && !$user['is_money_level']) {
  1617. $user['vip_status'] = 2;//没有开通过
  1618. } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
  1619. $user['vip_status'] = 3;//开通了,没有到期
  1620. }
  1621. }
  1622. /** @var StoreServiceRecordServices $servicesRecord */
  1623. $servicesRecord = app()->make(StoreServiceRecordServices::class);
  1624. $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
  1625. $message = $systemMessageServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  1626. $user['service_num'] = (int)bcadd((string)$service_num, (string)$message);
  1627. $user['is_agent_level'] = ($configData['brokerage_func_status'] ?? 1) && $agentLevelServices->count(['status' => 1, 'is_del' => 0]);
  1628. $user['visit_num'] = $storeProductLogServices->getDistinctCount(['uid' => $uid, 'type' => 'visit'], 'product_id');
  1629. $user['user_extract_bank_status'] = (int)($configData['user_extract_bank_status'] ?? 1);
  1630. $user['user_extract_wechat_status'] = (int)($configData['user_extract_wechat_status'] ?? 1);
  1631. $user['user_extract_alipay_status'] = (int)($configData['user_extract_alipay_status'] ?? 1);
  1632. //是否享受新人专享
  1633. /** @var StoreNewcomerServices $newcomerServices */
  1634. $newcomerServices = app()->make(StoreNewcomerServices::class);
  1635. $user['newcomer_status'] = $newcomerServices->checkUserNewcomer($uid);
  1636. $user['level_activate_status'] = $configData['level_activate_status'];
  1637. $user['member_func_status'] = $configData['member_func_status'];
  1638. $extendInfo = SystemConfigService::get('user_extend_info', []);
  1639. $user['register_extend_info'] = [];
  1640. if (!$user['level_activate_status']) {//不需要激活,用户激活状态默认为1
  1641. $user['level_status'] = 1;
  1642. }
  1643. if ($extendInfo) {
  1644. foreach ($extendInfo as $item) {
  1645. if (isset($item['use']) && $item['use'] && isset($item['user_show']) && $item['user_show']) $user['register_extend_info'][] = $item;
  1646. }
  1647. }
  1648. if (isset($user['extend_info']) && $user['extend_info']) {
  1649. $default = $this->defaultExtendInfo;
  1650. $params = array_column($default, 'param');
  1651. $sex = $this->rSex;
  1652. foreach ($user['extend_info'] as &$info) {
  1653. if (isset($info['param']) && in_array($info['param'], $params)) {
  1654. if ($info['param'] == 'sex') {
  1655. $info['value'] = $sex[$user['sex']] ?? 0;
  1656. } elseif ($info['param'] == 'birthday') {
  1657. $info['value'] = ($user['birthday'] ?? '') ? date('Y-m-d', $user['birthday']) : '';
  1658. } elseif ($info['param'] == 'address') {
  1659. $info['value'] = $user['addres'] ?? '';
  1660. } else {
  1661. $info['value'] = $user[$info['param']] ?? '';
  1662. }
  1663. }
  1664. }
  1665. }
  1666. /** @var UserAwardIntegralServices $award_integral_service */
  1667. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  1668. $award_lack = $award_integral_service->getLake();
  1669. $integral_price = $award_integral_service->getPrice();
  1670. $static_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
  1671. $action_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
  1672. $sum_integral = $award_integral_service->getIntegralSum(['status' => 0]);
  1673. $hidden_integral = sys_config('hidden_integral', 0);
  1674. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  1675. $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  1676. return array_merge($user, compact('hidden_integral', 'award_lack', 'integral_price', 'static_integral', 'action_integral', 'sum_integral'));
  1677. }
  1678. /**
  1679. * 用户资金统计
  1680. * @param int $uid `
  1681. */
  1682. public function balance(int $uid)
  1683. {
  1684. $userInfo = $this->getUserInfo($uid);
  1685. if (!$userInfo) {
  1686. throw new ValidateException('数据不存在');
  1687. }
  1688. /** @var UserMoneyServices $userMoneyServices */
  1689. $userMoneyServices = app()->make(UserMoneyServices::class);
  1690. /** @var StoreOrderServices $storeOrder */
  1691. $storeOrder = app()->make(StoreOrderServices::class);
  1692. $user['now_money'] = $userInfo['now_money'];//当前总资金
  1693. $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
  1694. $user['orderStatusSum'] = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price', true);//累计消费
  1695. return $user;
  1696. }
  1697. /**
  1698. * 用户修改信息
  1699. * @param Request $request
  1700. * @return mixed
  1701. */
  1702. public function eidtNickname(int $uid, array $data)
  1703. {
  1704. if (!$this->userExist($uid)) {
  1705. throw new ValidateException('用户不存在');
  1706. }
  1707. if (!$this->dao->update($uid, $data, 'uid')) {
  1708. throw new ValidateException('修改失败');
  1709. }
  1710. return true;
  1711. }
  1712. /**
  1713. * 获取推广人排行
  1714. * @param $data 查询条件
  1715. * @return array
  1716. * @throws \think\db\exception\DataNotFoundException
  1717. * @throws \think\db\exception\ModelNotFoundException
  1718. * @throws \think\exception\DbException
  1719. */
  1720. public function getRankList(array $data)
  1721. {
  1722. $startTime = $endTime = 0;
  1723. switch ($data['type']) {
  1724. case 'week':
  1725. $startTime = strtotime('this week Monday');
  1726. break;
  1727. case 'month':
  1728. $startTime = strtotime('first day of ' . date('F Y'));
  1729. break;
  1730. }
  1731. $endTime = time();
  1732. [$page, $limit] = $this->getPageValue();
  1733. $field = 'spread_uid,count(uid) AS count,spread_time';
  1734. /** @var UserSpreadServices $userSpreadServices */
  1735. $userSpreadServices = app()->make(UserSpreadServices::class);
  1736. $list = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  1737. $rank = 0;
  1738. foreach ($list as $key => $item) {
  1739. if ($item['spread_uid'] == $data['uid']) $rank = $key + 1;
  1740. }
  1741. $week = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('this week Monday'), time()], 'timeKey' => 'spread_time']);
  1742. $month = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('last month'), time()], 'timeKey' => 'spread_time']);
  1743. $start = date('Y-m-d H:i', $startTime);
  1744. $end = date('Y-m-d H:i', time());
  1745. return compact('list', 'rank', 'week', 'month', 'start', 'end');
  1746. }
  1747. /**
  1748. * 静默绑定推广人
  1749. * @param Request $request
  1750. * @return mixed
  1751. */
  1752. public function spread(int $uid, int $spreadUid, $code)
  1753. {
  1754. $userInfo = $this->getUserInfo($uid);
  1755. if (!$userInfo) {
  1756. throw new ValidateException('数据不存在');
  1757. }
  1758. if ($code && !$spreadUid) {
  1759. /** @var QrcodeServices $qrCode */
  1760. $qrCode = app()->make(QrcodeServices::class);
  1761. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  1762. $spreadUid = $info['third_id'];
  1763. }
  1764. }
  1765. //记录好友关系
  1766. if ($spreadUid && $uid && $spreadUid != $uid) {
  1767. /** @var UserFriendsServices $serviceFriend */
  1768. $serviceFriend = app()->make(UserFriendsServices::class);
  1769. $serviceFriend->saveFriend($uid, $spreadUid);
  1770. }
  1771. $data = [];
  1772. //永久绑定
  1773. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  1774. $spread_uid = isset($user['code']) && $user['code'] && $user['code'] != $userInfo->uid ? $user['code'] : ($userInfo['spread_uid'] ?? 0);
  1775. if ($userInfo->spread_uid && $store_brokergae_binding_status == 1) {
  1776. $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
  1777. } else {
  1778. //绑定分销关系 = 所有用户
  1779. if (sys_config('brokerage_bindind', 1) == 1) {
  1780. //分销绑定类型为时间段且过期 ||临时
  1781. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  1782. if (!$userInfo['spread_uid'] || $store_brokergae_binding_status == 3 || ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
  1783. $spreadUid = $spread_uid;
  1784. if ($spreadUid && $userInfo->uid == $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
  1785. $spreadUid = 0;
  1786. }
  1787. if ($spreadUid && $this->dao->get((int)$spreadUid)) {
  1788. $data['spread_uid'] = $spreadUid;
  1789. $data['spread_time'] = time();
  1790. }
  1791. }
  1792. }
  1793. }
  1794. if ($data && !$this->dao->update($userInfo['uid'], $data, 'uid')) {
  1795. throw new ValidateException('修改信息失败');
  1796. }
  1797. if (isset($data['spread_uid']) && $data['spread_uid']) {
  1798. /** @var UserBillServices $userBill */
  1799. $userBill = app()->make(UserBillServices::class);
  1800. //邀请新用户增加经验
  1801. $userBill->inviteUserIncExp((int)$spreadUid);
  1802. }
  1803. return true;
  1804. }
  1805. /**
  1806. * 添加访问记录
  1807. * @param Request $request
  1808. * @return mixed
  1809. */
  1810. public function setVisit(array $data)
  1811. {
  1812. $userInfo = $this->getUserInfo($data['uid']);
  1813. if (!$userInfo) {
  1814. throw new ValidateException('数据不存在');
  1815. }
  1816. if (isset($data['ip']) && $data['ip']) {
  1817. $addressArr = $this->addressHandle($this->convertIp($data['ip']));
  1818. $data['province'] = $addressArr['province'] ?? '';
  1819. }
  1820. $data['channel_type'] = $userInfo['user_type'];
  1821. $data['add_time'] = time();
  1822. /** @var UserVisitServices $userVisit */
  1823. $userVisit = app()->make(UserVisitServices::class);
  1824. if ($userVisit->save($data)) {
  1825. return true;
  1826. } else {
  1827. throw new ValidateException('添加访问记录失败');
  1828. }
  1829. }
  1830. /**
  1831. * 获取活动状态
  1832. * @return mixed
  1833. */
  1834. public function activity()
  1835. {
  1836. /** @var StoreBargainServices $storeBragain */
  1837. $storeBragain = app()->make(StoreBargainServices::class);
  1838. /** @var StoreCombinationServices $storeCombinaion */
  1839. $storeCombinaion = app()->make(StoreCombinationServices::class);
  1840. /** @var StoreSeckillServices $storeSeckill */
  1841. $storeSeckill = app()->make(StoreSeckillServices::class);
  1842. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  1843. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  1844. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  1845. return $data;
  1846. }
  1847. /**
  1848. * 获取用户下级推广人
  1849. * @param int $uid 当前用户
  1850. * @param int $grade 等级 0 一级 1 二级
  1851. * @param string $orderBy 排序
  1852. * @param string $keyword
  1853. * @return array|bool
  1854. */
  1855. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '', $time = [])
  1856. {
  1857. $user = $this->getUserInfo($uid);
  1858. if (!$user) {
  1859. throw new ValidateException('数据不存在');
  1860. }
  1861. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  1862. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  1863. $data = [
  1864. 'total' => count($spread_one_ids),
  1865. 'totalLevel' => count($spread_two_ids),
  1866. 'list' => []
  1867. ];
  1868. /** @var UserStoreOrderServices $userStoreOrder */
  1869. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  1870. $list = [];
  1871. $where = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  1872. if ($grade == 0) {
  1873. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword, $time);
  1874. $where = $where + ['spread_uid' => $uid];
  1875. } else {
  1876. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword, $time);
  1877. $where = $where + ['spread_two_uid' => $uid];
  1878. }
  1879. foreach ($list as &$item) {
  1880. if (isset($item['spread_time']) && $item['spread_time']) {
  1881. $item['time'] = date('Y/m/d', $item['spread_time']);
  1882. }
  1883. }
  1884. $data['list'] = $list;
  1885. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  1886. $data['count'] = 0;
  1887. $data['price'] = 0;
  1888. if ($list) {
  1889. $uids = array_column($list, 'uid');
  1890. $data['count'] = count($uids);
  1891. /** @var StoreOrderServices $storeOrder */
  1892. $storeOrder = app()->make(StoreOrderServices::class);
  1893. $data['price'] = $storeOrder->sum($where, $grade == 0 ? 'one_brokerage' : 'two_brokerage');
  1894. }
  1895. return $data;
  1896. }
  1897. /**
  1898. * 获取推广人uids
  1899. * @param int $uid
  1900. * @param bool $one
  1901. * @return array
  1902. */
  1903. public function getUserSpredadUids(int $uid, int $type = 0)
  1904. {
  1905. $uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
  1906. if ($type === 1) {
  1907. return $uids;
  1908. }
  1909. if ($uids) {
  1910. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids]], 'uid');
  1911. if ($type === 2) {
  1912. return $uidsTwo;
  1913. }
  1914. if ($uidsTwo) {
  1915. $uids = array_merge($uids, $uidsTwo);
  1916. }
  1917. }
  1918. return $uids;
  1919. }
  1920. /**
  1921. * 检测用户是否是推广员
  1922. * @param int $uid
  1923. * @param array $user
  1924. * @param float $sumPrice
  1925. * @return bool
  1926. */
  1927. public function checkUserPromoter(int $uid, $user = [], float $sumPrice = 0.00)
  1928. {
  1929. if (!$uid) {
  1930. return false;
  1931. }
  1932. if (!$user) {
  1933. $user = $this->getUserInfo($uid);
  1934. }
  1935. if (!$user) {
  1936. return false;
  1937. }
  1938. //分销是否开启
  1939. if (!sys_config('brokerage_func_status')) {
  1940. return false;
  1941. }
  1942. //用户分校推广资格是否开启4.0.32
  1943. if (isset($user['spread_open']) && !$user['spread_open']) {
  1944. return false;
  1945. }
  1946. $store_brokerage_statu = sys_config('store_brokerage_statu');
  1947. if ($user['is_promoter'] || $store_brokerage_statu == 2) {
  1948. return true;
  1949. }
  1950. if ($store_brokerage_statu == 3) {
  1951. if ($sumPrice) {
  1952. /** @var StoreOrderServices $storeOrder */
  1953. $storeOrder = app()->make(StoreOrderServices::class);
  1954. $sumPrice = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price');//累计消费
  1955. }
  1956. $store_brokerage_price = sys_config('store_brokerage_price');
  1957. if ($sumPrice > $store_brokerage_price) {
  1958. return true;
  1959. }
  1960. }
  1961. return false;
  1962. }
  1963. /**
  1964. * 同步微信粉丝用户(后台接口)
  1965. * @return bool
  1966. */
  1967. public function syncWechatUsers()
  1968. {
  1969. $key = md5('sync_wechat_users');
  1970. //一天点击一次
  1971. if (CacheService::get($key)) {
  1972. return true;
  1973. }
  1974. $next_openid = null;
  1975. do {
  1976. $result = OfficialAccount::userService()->list($next_openid);
  1977. $userOpenids = $result['data'];
  1978. //拆分大数组
  1979. $opemidArr = array_chunk($userOpenids, 100);
  1980. foreach ($opemidArr as $openids) {
  1981. //加入同步|更新用户队列
  1982. UserJob::dispatch([$openids]);
  1983. }
  1984. $next_openid = $result['next_openid'];
  1985. } while ($next_openid != null);
  1986. CacheService::set($key, 1, 3600 * 24);
  1987. return true;
  1988. }
  1989. /**
  1990. * 导入微信粉丝用户
  1991. * @param array $openids
  1992. * @return bool
  1993. */
  1994. public function importUser(array $noBeOpenids)
  1995. {
  1996. if (!$noBeOpenids) {
  1997. return true;
  1998. }
  1999. $dataAll = $data = [];
  2000. $time = time();
  2001. foreach ($noBeOpenids as $openid) {
  2002. try {
  2003. $info = OfficialAccount::userService()->get($openid);
  2004. $info = is_object($info) ? $info->toArray() : $info;
  2005. } catch (\Throwable $e) {
  2006. $info = [];
  2007. }
  2008. if (!$info) continue;
  2009. if (($info['subscribe'] ?? 1) == 1) {
  2010. $data['nickname'] = $info['nickname'] ?? '';
  2011. $data['headimgurl'] = $info['headimgurl'] ?? '';
  2012. $userInfoData = $this->setUserInfo($data);
  2013. if (!$userInfoData) {
  2014. throw new AdminException('用户信息储存失败!');
  2015. }
  2016. $data['uid'] = $userInfoData['uid'];
  2017. $data['subscribe'] = $info['subscribe'];
  2018. $data['unionid'] = $info['unionid'] ?? '';
  2019. $data['openid'] = $info['openid'] ?? '';
  2020. $data['sex'] = $info['sex'] ?? 0;
  2021. $data['language'] = $info['language'] ?? '';
  2022. $data['city'] = $info['city'] ?? '';
  2023. $data['province'] = $info['province'] ?? '';
  2024. $data['country'] = $info['country'] ?? '';
  2025. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  2026. $data['groupid'] = $info['groupid'] ?? 0;
  2027. $data['remark'] = $info['remark'] ?? '';
  2028. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  2029. $data['add_time'] = $time;
  2030. $data['is_complete'] = 1;
  2031. $dataAll[] = $data;
  2032. }
  2033. }
  2034. if ($dataAll) {
  2035. /** @var WechatUserServices $wechatUser */
  2036. $wechatUser = app()->make(WechatUserServices::class);
  2037. if (!$wechatUser->saveAll($dataAll)) {
  2038. throw new ValidateException('保存用户信息失败');
  2039. }
  2040. }
  2041. return true;
  2042. }
  2043. /**
  2044. * 修改会员的时间及是否会员状态
  2045. * @param int $vip_day 会员天数
  2046. * @param array $user_id 用户id
  2047. * @param int $is_money_level 会员来源途径
  2048. * @param bool $member_type 会员卡类型
  2049. * @return mixed
  2050. * @throws \think\db\exception\DataNotFoundException
  2051. * @throws \think\db\exception\DbException
  2052. * @throws \think\db\exception\ModelNotFoundException
  2053. */
  2054. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  2055. {
  2056. if ($vip_day == 0) throw new ValidateException('天数不能为0');
  2057. $user_info = $this->getUserInfo($user_id);
  2058. if (!$user_info) throw new ValidateException('用户数据不存在');
  2059. if (!$member_type) $member_type = "month";
  2060. if ($member_type == 'ever') {
  2061. $overdue_time = 0;
  2062. $is_ever_level = 1;
  2063. } else {
  2064. if ($user_info['is_money_level'] == 0) {
  2065. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  2066. } else {
  2067. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  2068. }
  2069. $is_ever_level = 0;
  2070. }
  2071. $setData['overdue_time'] = $overdue_time;
  2072. $setData['is_ever_level'] = $is_ever_level;
  2073. $setData['is_money_level'] = $is_money_level ? $is_money_level : 0;
  2074. // if ($user_info['level'] == 0) $setData['level'] = 1;
  2075. return $this->dao->update(['uid' => $user_id], $setData);
  2076. }
  2077. /**
  2078. * 清空到期svip(分批加入队列)
  2079. * @return bool
  2080. */
  2081. public function offUserSvip()
  2082. {
  2083. $users = $this->dao->getColumn([['is_ever_level', '=', 0], ['is_money_level', '>', 0], ['overdue_time', '<', time()]], 'uid');
  2084. if ($users) {
  2085. //拆分大数组
  2086. $uidsArr = array_chunk($users, 100);
  2087. foreach ($uidsArr as $uids) {
  2088. //加入同步|更新用户队列
  2089. UserSvipJob::dispatch([$uids]);
  2090. }
  2091. }
  2092. return true;
  2093. }
  2094. /**
  2095. * 会员过期改变状态,变为普通会员
  2096. * @param $uid
  2097. * @param null $userInfo
  2098. * @return bool
  2099. * @throws \think\db\exception\DataNotFoundException
  2100. * @throws \think\db\exception\DbException
  2101. * @throws \think\db\exception\ModelNotFoundException
  2102. */
  2103. public function offMemberLevel($uid, $userInfo = null)
  2104. {
  2105. if (!$uid) return false;
  2106. $userInfo = $userInfo ?: $this->dao->get($uid);
  2107. if (!$userInfo) return false;
  2108. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  2109. $this->dao->update(['uid' => $uid], ['is_money_level' => 0]);
  2110. return false;
  2111. }
  2112. return true;
  2113. }
  2114. /**
  2115. * @param array $where
  2116. * @return array
  2117. * @throws \think\db\exception\DataNotFoundException
  2118. * @throws \think\db\exception\DbException
  2119. * @throws \think\db\exception\ModelNotFoundException
  2120. */
  2121. public function getUserInfoList(array $where, $field = "*")
  2122. {
  2123. return $this->dao->getUserInfoList($where, $field);
  2124. }
  2125. /**
  2126. * 保存用户上级推广人
  2127. * @param int $uid
  2128. * @param int $spread_uid
  2129. * @return bool
  2130. */
  2131. public function saveUserSpreadUid(int $uid, int $spread_uid)
  2132. {
  2133. if (!$uid || !$spread_uid) {
  2134. return false;
  2135. }
  2136. if ($uid == $spread_uid) {
  2137. throw new ValidateException('上级推广人不能为自己');
  2138. }
  2139. $userInfo = $this->getUserInfo($uid);
  2140. if (!$userInfo) {
  2141. throw new ValidateException('用户不存在');
  2142. }
  2143. //上级已经是这个uid
  2144. if ($userInfo['spread_uid'] == $spread_uid) {
  2145. return true;
  2146. }
  2147. $spreadInfo = $this->getUserInfo($spread_uid);
  2148. if (!$spreadInfo) {
  2149. throw new ValidateException('上级用户不存在');
  2150. }
  2151. if ($spreadInfo['spread_uid'] == $uid) {
  2152. throw new ValidateException('上级推广人不能为自己下级');
  2153. }
  2154. $data = ['spread_uid' => $spread_uid, 'spread_time' => time()];
  2155. $this->dao->update($uid, $data);
  2156. //记录推广绑定关系
  2157. UserSpreadJob::dispatch([$uid, $spread_uid]);
  2158. //记录好友关系
  2159. UserFriendsJob::dispatch([$uid, $spread_uid]);
  2160. return true;
  2161. }
  2162. /**
  2163. * 增加推广用户佣金
  2164. * @param int $uid
  2165. * @param int $spread_uid
  2166. * @param array $userInfo
  2167. * @param array $spread_user
  2168. * @return bool|mixed
  2169. * @throws \think\db\exception\DataNotFoundException
  2170. * @throws \think\db\exception\DbException
  2171. * @throws \think\db\exception\ModelNotFoundException
  2172. */
  2173. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  2174. {
  2175. if (!$uid || !$spread_uid) {
  2176. return false;
  2177. }
  2178. //商城分销功能是否开启 0关闭1开启
  2179. if (!sys_config('brokerage_func_status')) return true;
  2180. //获取设置推广佣金单价
  2181. $brokerage_price = sys_config('uni_brokerage_price', 0);
  2182. //推广佣金是否开启
  2183. if (!sys_config('brokerage_user_status', 0)) {
  2184. return true;
  2185. }
  2186. //获取推广佣金当日限额
  2187. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  2188. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  2189. return true;
  2190. }
  2191. if (!$userInfo) {
  2192. $userInfo = $this->getUserInfo($uid);
  2193. }
  2194. if (!$userInfo) {
  2195. return false;
  2196. }
  2197. if (!$spread_user) {
  2198. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  2199. }
  2200. if (!$spread_user) {
  2201. return false;
  2202. }
  2203. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  2204. return false;
  2205. }
  2206. /** @var UserBrokerageServices $userBrokerageServices */
  2207. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  2208. // -1不限制
  2209. if ($day_brokerage_price_upper != -1) {
  2210. if ($day_brokerage_price_upper <= 0) {
  2211. return true;
  2212. } else {
  2213. //获取上级用户今日获取推广用户佣金
  2214. $spread_day_brokerage = $userBrokerageServices->getUserBillBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  2215. //超过上限
  2216. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  2217. return true;
  2218. }
  2219. }
  2220. }
  2221. $spreadPrice = $spread_user['brokerage_price'];
  2222. // 上级推广员返佣之后的金额
  2223. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  2224. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  2225. // 添加返佣记录
  2226. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  2227. 'nickname' => $userInfo['nickname'],
  2228. 'number' => floatval($brokerage_price)
  2229. ], $balance, $uid);
  2230. // 添加用户余额
  2231. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  2232. //给上级发送获得佣金的模板消息
  2233. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  2234. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  2235. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  2236. return $res1 && $res2;
  2237. });
  2238. }
  2239. /**
  2240. * 获取上级uid
  2241. * @param int $uid
  2242. * @param array $userInfo
  2243. * @param bool $is_spread
  2244. * @return int|mixed
  2245. */
  2246. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  2247. {
  2248. if (!$uid) {
  2249. return 0;
  2250. }
  2251. //商城分销功能是否开启 0关闭1开启
  2252. if (!sys_config('brokerage_func_status')) return -1;
  2253. if (!$userInfo) {
  2254. $userInfo = $this->getUserCacheInfo($uid);
  2255. }
  2256. if (!$userInfo) {
  2257. return 0;
  2258. }
  2259. //上级的上级不需要检测自购
  2260. if ($is_spread) {
  2261. //开启自购
  2262. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  2263. if ($is_self_brokerage && $is_spread) {
  2264. return $uid;
  2265. }
  2266. }
  2267. //绑定类型
  2268. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  2269. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  2270. return $userInfo['spread_uid'];
  2271. }
  2272. //分销绑定类型为时间段且没过期
  2273. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  2274. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  2275. return $userInfo['spread_uid'];
  2276. }
  2277. return -1;
  2278. }
  2279. /**
  2280. * 用户付款code
  2281. * @param int $uid
  2282. * @return bool|mixed|null
  2283. */
  2284. public function getRandCode(int $uid)
  2285. {
  2286. $key = 'user_rand_code' . $uid;
  2287. return CacheService::redisHandler()->remember($key, function () {
  2288. return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 9), 1))), 0, 3) . str_pad((string)mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
  2289. }, 600);
  2290. }
  2291. /**
  2292. * 获取barcode
  2293. * @return bool|int|mixed|null
  2294. */
  2295. public function getBarCode()
  2296. {
  2297. mt_srand();
  2298. $code = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 9), 1))), 0, 4) . str_pad((string)mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  2299. if (!$this->dao->count(['bar_code' => $code])) {
  2300. return $code;
  2301. } else {
  2302. return $this->getBarCode();
  2303. }
  2304. }
  2305. /**
  2306. * 获取用户推广用户列表
  2307. * @param $uid
  2308. * @param $type
  2309. * @return array
  2310. * @throws \think\db\exception\DataNotFoundException
  2311. * @throws \think\db\exception\DbException
  2312. * @throws \think\db\exception\ModelNotFoundException
  2313. */
  2314. public function agentUserList($uid, $type)
  2315. {
  2316. $where['spread_uid'] = $uid;
  2317. if ($type == 1) {
  2318. $where['pay_count'] = -1;
  2319. }
  2320. [$page, $limit] = $this->getPageValue();
  2321. $list = $this->dao->getList($where, 'uid,nickname,avatar,FROM_UNIXTIME(spread_time, \'%Y.%m.%d %H:%m\') as spread_time', $page, $limit);
  2322. $count = $this->dao->count($where);
  2323. return compact('list', 'count');
  2324. }
  2325. /**
  2326. * 个人中心
  2327. * @param $uid
  2328. * @return array|mixed
  2329. */
  2330. public function bankInfo($uid)
  2331. {
  2332. $userService = app()->make(UserServices::class);
  2333. $user = $userService->getUserInfo($uid);
  2334. if (!$user) {
  2335. throw new ValidateException('数据不存在');
  2336. }
  2337. if (!$user['enterprise_professional_facilitator_id']) {
  2338. return [];
  2339. }
  2340. return WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  2341. }
  2342. }