UserServices.php 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  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. $f[] = Form::radio('award_switch', '级差奖无需复投', $user->getData('award_switch'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  586. $f[] = Form::radio('is_shop', '商家', $user->getData('is_shop'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  587. return create_form('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
  588. }
  589. /**
  590. * 添加用户表单
  591. * @param int $id
  592. * @return array
  593. * @throws \FormBuilder\Exception\FormBuilderException
  594. */
  595. public function saveForm()
  596. {
  597. $f = array();
  598. $f[] = Form::input('real_name', '真实姓名', '')->col(12)->placeholder('请输入真实姓名');
  599. $f[] = Form::input('phone', '手机号码', '')->col(12)->placeholder('请输入手机号码')->required();
  600. $f[] = Form::date('birthday', '生日', '')->col(12)->placeholder('请选择生日');
  601. $f[] = Form::input('card_id', '身份证号', '')->col(12)->placeholder('请输入身份证号');
  602. $f[] = Form::input('addres', '用户地址', '')->placeholder('请输入用户地址');
  603. $f[] = Form::textarea('mark', '用户备注', '')->placeholder('请输入用户备注');
  604. $f[] = Form::input('pwd', '登录密码')->col(12)->type('password')->placeholder('请输入登录密码');
  605. $f[] = Form::input('true_pwd', '确认密码')->col(12)->type('password')->placeholder('请再次确认密码');
  606. /** @var SystemUserLevelServices $systemUserLevelServices */
  607. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  608. $systemLevelList = $systemUserLevelServices->getWhereLevelList([], 'id,name');
  609. $setOptionLevel = function () use ($systemLevelList) {
  610. $menus = [];
  611. foreach ($systemLevelList as $menu) {
  612. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  613. }
  614. return $menus;
  615. };
  616. $f[] = Form::select('level', '用户等级', '')->col(12)->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  617. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  618. $setOptionGroup = function () use ($systemGroupList) {
  619. $menus = [];
  620. foreach ($systemGroupList as $menu) {
  621. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  622. }
  623. return $menus;
  624. };
  625. $f[] = Form::select('group_id', '用户分组', '')->col(12)->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  626. /** @var UserLabelServices $userLabelServices */
  627. $userLabelServices = app()->make(UserLabelServices::class);
  628. $systemLabelList = $userLabelServices->getLabelList(['type' => 0]);
  629. $setOptionLabel = function () use ($systemLabelList) {
  630. $menus = [];
  631. foreach ($systemLabelList as $menu) {
  632. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  633. }
  634. return $menus;
  635. };
  636. $f[] = Form::select('label_id', '用户标签', '')->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  637. $f[] = Form::radio('spread_open', '推广资格', 1)->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  638. //分销模式 人人分销
  639. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  640. if (in_array($storeBrokerageStatus, [1, 3])) {
  641. $f[] = Form::radio('is_promoter', '推广员权限', 0)->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  642. }
  643. $f[] = Form::radio('status', '用户状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  644. $f[] = Form::radio('award_switch', '级差奖无需复投', 0)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  645. return create_form('添加用户', $f, $this->url('/user/user'), 'POST');
  646. }
  647. /**
  648. * 获取追加信息
  649. * @param int $uid
  650. * @param $userInfo
  651. * @return \FormBuilder\UI\Iview\Components\Group
  652. */
  653. public function extendInfoForm(int $uid = 0)
  654. {
  655. $f = [];
  656. if ($uid) {
  657. $userInfo = $this->getUserInfo($uid);
  658. }
  659. $extendInfo = SystemConfigService::get('user_extend_info', []);
  660. $f = [];
  661. if ($extendInfo) {
  662. $userExtendInfo = $userInfo['extend_info'] ?? [];
  663. if ($userExtendInfo) $userExtendInfo = array_combine(array_column($userExtendInfo, 'info'), $userExtendInfo);
  664. foreach ($extendInfo as $item) {
  665. //没选择使用 跳过
  666. if (!isset($item['use']) || !$item['use']) continue;
  667. $format = $item['format'] ?? '';
  668. $field = $item['param'] ?? $item['info'] ?? '';
  669. switch ($format) {
  670. case 'num'://'数字'
  671. $form = Form::number($field, $item['info'], $userExtendInfo[$item['info']]['value'] ?? 0);
  672. break;
  673. case 'date'://'日期'
  674. $form = Form::date($field, $item['info'], $userExtendInfo[$item['info']]['value'] ?? '');
  675. break;
  676. case 'radio'://'单选项'
  677. $options = [];
  678. if (isset($item['singlearr']) && $item['singlearr']) {
  679. foreach ($item['singlearr'] as $key => $value) {
  680. $options[] = ['value' => $key, 'label' => $value];
  681. }
  682. }
  683. $form = Form::radio($field, $item['info'], (int)($userExtendInfo[$item['info']]['value'] ?? 0))->options($options);
  684. break;
  685. case 'text'://'文本'
  686. case 'id'://'身份证'
  687. case 'mail'://'邮件'
  688. case 'phone'://'手机号'
  689. case 'address'://'地址'
  690. $form = Form::input($field, $item['info'], $userExtendInfo[$item['info']]['value'] ?? '');
  691. break;
  692. }
  693. if ($item['required']) {
  694. $f[] = $form->required($item['tip'] ?? '');
  695. } else {
  696. $f[] = $form;
  697. }
  698. }
  699. }
  700. if (!$f) {
  701. throw new ValidateException('请先去用户设置:设置用户信息');
  702. }
  703. return create_form('用户补充信息', $f, $this->url('/user/user/extend_info/' . $uid), 'POST');
  704. }
  705. /**
  706. * 处理用户补充信息
  707. * @param array $inputExtendInfo
  708. * @param bool $is_all
  709. * @return array
  710. */
  711. public function handelExtendInfo(array $inputExtendInfo, bool $is_all = false)
  712. {
  713. $extendInfo = SystemConfigService::get('user_extend_info', []);
  714. if ($inputExtendInfo && $extendInfo) {
  715. if ($is_all) {//移动端全数据 处理
  716. $inputExtendInfo = array_combine(array_column($inputExtendInfo, 'info'), $inputExtendInfo);
  717. } else {//后台key=>value类型数据
  718. $inputExtendInfo = $inputExtendInfo[0] ?? $inputExtendInfo;
  719. }
  720. foreach ($extendInfo as &$item) {
  721. $value = $is_all ? ($inputExtendInfo[$item['info'] ?? '']['value'] ?? '') : ($inputExtendInfo[$item['param'] ?? $item['info'] ?? ''] ?? '');
  722. if ($value) {
  723. //验证手机号
  724. if ($item['format'] == 'phone') {
  725. if (!check_phone($value)) throw new AdminException('请填写正确的手机号');
  726. }
  727. //验证邮箱
  728. if ($item['format'] == 'mail') {
  729. if (!check_mail($value)) throw new AdminException('请填写正确的邮箱');
  730. }
  731. //验证身份证号
  732. if ($item['format'] == 'id') {
  733. try {
  734. if (!check_card($value)) throw new AdminException('请填写正确的身份证号码');
  735. } catch (\Throwable $e) {
  736. // throw new AdminException( '请填写正确的身份证号码');
  737. }
  738. }
  739. }
  740. $item['value'] = $value;
  741. }
  742. }
  743. return $extendInfo;
  744. }
  745. /**
  746. * 保存用户补充信息
  747. * @param int $uid
  748. * @param array $extend_info 补充信息
  749. * @param array $update 原本需要修改字段
  750. * @param bool $is_all
  751. * @return bool
  752. */
  753. public function saveExtendForm(int $uid, array $extend_info, array $update = [], bool $is_all = false)
  754. {
  755. $userInfo = $this->getUserInfo($uid);
  756. if (!$userInfo) {
  757. throw new ValidateException('用户不存在');
  758. }
  759. $extend_info = $this->handelExtendInfo($extend_info, $is_all) ?: [];
  760. if ($extend_info) {
  761. $default = $this->defaultExtendInfo;
  762. $params = array_column($default, 'param');
  763. $sex = $this->sex;
  764. $update['extend_info'] = $extend_info;
  765. foreach ($extend_info as $info) {
  766. if (isset($info['param']) && in_array($info['param'], $params) && isset($info['value'])) {
  767. if ($info['param'] == 'sex') {
  768. $update['sex'] = $sex[$info['value']] ?? 0;
  769. } elseif ($info['param'] == 'birthday') {
  770. $update['birthday'] = strtotime($info['value']);
  771. } elseif ($info['param'] == 'address') {
  772. $update['addres'] = $info['value'];
  773. } else {
  774. $update[$info['param']] = $info['value'];
  775. }
  776. }
  777. }
  778. }
  779. if ($update) {
  780. $this->dao->update($uid, $update);
  781. $this->dao->cacheTag()->clear();
  782. }
  783. return true;
  784. }
  785. /**
  786. * 修改提交处理
  787. * @param $id
  788. * @return mixed
  789. */
  790. public function updateInfo(int $id, array $data)
  791. {
  792. $user = $this->getUserInfo($id);
  793. if (!$user) {
  794. throw new AdminException('数据不存在!');
  795. }
  796. $res1 = false;
  797. $res2 = false;
  798. $edit = [];
  799. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  800. $data['money'] = sprintf("%.2f", $data['money']);
  801. /** @var UserMoneyServices $userMoneyServices */
  802. $userMoneyServices = app()->make(UserMoneyServices::class);
  803. if ($data['money_status'] == 1) {//增加
  804. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  805. $res1 = $userMoneyServices->income('system_add', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  806. } else if ($data['money_status'] == 2) {//减少
  807. if ($user['now_money'] > $data['money']) {
  808. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  809. } else {
  810. $edit['now_money'] = 0;
  811. $data['money'] = $user['now_money'];
  812. }
  813. $res1 = $userMoneyServices->income('system_sub', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  814. }
  815. } else {
  816. $res1 = true;
  817. }
  818. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  819. $integral_data = ['link_id' => $data['adminId'] ?? 0, 'number' => $data['integration'], 'balance' => $user['integral']];
  820. /** @var UserBillServices $userBill */
  821. $userBill = app()->make(UserBillServices::class);
  822. $balance = $user['integral'];
  823. if ($data['integration_status'] == 1) {//增加
  824. $balance = bcadd((string)$user['integral'], (string)$data['integration'], 0);
  825. $res2 = $userBill->income('system_add_integral', $id, (int)$data['integration'], (int)$balance, $data['adminId'] ?? 0);
  826. } else if ($data['integration_status'] == 2) {//减少
  827. $balance = bcsub((string)$user['integral'], (string)$data['integration'], 0);
  828. $res2 = $userBill->income('system_sub_integral', $id, (int)$data['integration'], (int)$balance, $data['adminId'] ?? 0);
  829. }
  830. $edit['integral'] = $balance;
  831. } else {
  832. $res2 = true;
  833. }
  834. //修改基本信息
  835. if (!isset($data['is_other']) || !$data['is_other']) {
  836. if ($data['phone']) {
  837. $otherUser = $this->getOne(['phone' => $data['phone']], 'uid,phone');
  838. if ($otherUser && $otherUser['uid'] != $id) {
  839. throw new AdminException('该手机号码已被注册');
  840. }
  841. }
  842. /** @var UserLabelRelationServices $userLabel */
  843. $userLabel = app()->make(UserLabelRelationServices::class);
  844. if (is_string($data['label_id'])) {
  845. $data['label_id'] = [$data['label_id']];
  846. }
  847. $userLabel->setUserLable([$id], $data['label_id'], 0, 0, true);
  848. if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
  849. $edit['pwd'] = $data['pwd'];
  850. }
  851. if (isset($data['spread_open'])) {
  852. $edit['spread_open'] = $data['spread_open'];
  853. }
  854. $edit['status'] = $data['status'];
  855. $edit['award_switch'] = $data['award_switch'];
  856. $edit['real_name'] = $data['real_name'];
  857. $edit['card_id'] = $data['card_id'];
  858. $edit['birthday'] = strtotime($data['birthday']);
  859. $edit['mark'] = $data['mark'];
  860. $edit['is_promoter'] = $data['is_promoter'];
  861. $edit['level'] = $data['level'];
  862. $edit['phone'] = $data['phone'];
  863. $edit['addres'] = $data['addres'];
  864. $edit['group_id'] = $data['group_id'];
  865. if (isset($data['area_admin']) && $data['area_admin'] >= 0) {
  866. $edit['area_admin'] = $data['area_admin'];
  867. $edit['area_admin_province'] = $data['area_admin_province'];
  868. $edit['area_admin_city'] = $data['area_admin_city'];
  869. $edit['area_admin_district'] = $data['area_admin_district'];
  870. $edit['area_provincials'] = $data['area_provincials'];
  871. }
  872. if ($data['spread_uid'] != -1) {
  873. $edit['spread_uid'] = $data['spread_uid'];
  874. $edit['spread_time'] = $data['spread_uid'] ? time() : 0;
  875. }
  876. $edit['sex'] = $data['sex'];
  877. $edit['provincials'] = $data['provincials'];
  878. $edit['province'] = $data['province'];
  879. $edit['city'] = $data['city'];
  880. $edit['area'] = $data['area'];
  881. $edit['street'] = $data['street'];
  882. $edit['is_shop'] = $data['is_shop'] ?? 0;
  883. if (isset($data['extend_info']) && $data['extend_info']) $edit['extend_info'] = $data['extend_info'];
  884. if ($user['level'] != $data['level']) {
  885. /** @var UserLevelServices $userLevelService */
  886. $userLevelService = app()->make(UserLevelServices::class);
  887. $userLevelService->setUserLevel((int)$user['uid'], (int)$data['level']);
  888. }
  889. if ($data['spread_uid'] != $user['spread_uid']) {
  890. //记录推广绑定关系
  891. UserSpreadJob::dispatch([$id, (int)$data['spread_uid'], 0, (int)$data['adminId']]);
  892. //记录好友关系
  893. UserFriendsJob::dispatch([$id, (int)$data['spread_uid']]);
  894. }
  895. /** @var WechatUserServices $wechatUser */
  896. $wechatUser = app()->make(WechatUserServices::class);
  897. $wechatUser->update(['uid' => $id], ['sex' => $data['sex']]);
  898. }
  899. if ($edit) $res3 = $this->dao->update($id, $edit);
  900. else $res3 = true;
  901. if ($res1 && $res2 && $res3) {
  902. $this->dao->cacheTag()->clear();
  903. return true;
  904. } else throw new AdminException('修改失败');
  905. }
  906. /**
  907. * 编辑其他
  908. * @param $id
  909. * @return mixed
  910. * @throws \FormBuilder\Exception\FormBuilderException
  911. */
  912. public function editOther($id)
  913. {
  914. $user = $this->getUserInfo($id);
  915. if (!$user) {
  916. throw new AdminException('数据不存在!');
  917. }
  918. $f = array();
  919. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  920. $f[] = Form::number('money', '余额', 0)->max(999999)->min(0)->style(['width' => '100px']);
  921. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  922. $f[] = Form::number('integration', '积分', 0)->max(999999)->min(0)->precision(0)->style(['width' => '100px']);
  923. return create_form('修改积分余额', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
  924. }
  925. /**
  926. * 设置会员分组
  927. * @param $id
  928. * @return mixed
  929. */
  930. public function setGroup($uids, $all, $where)
  931. {
  932. $userGroup = app()->make(UserGroupServices::class)->getGroupList();
  933. if (count($uids) == 1) {
  934. $user = $this->getUserInfo($uids[0], ['group_id']);
  935. $setOptionUserGroup = function () use ($userGroup) {
  936. $menus = [];
  937. foreach ($userGroup as $menu) {
  938. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  939. }
  940. return $menus;
  941. };
  942. $field[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  943. } else {
  944. $setOptionUserGroup = function () use ($userGroup) {
  945. $menus = [];
  946. foreach ($userGroup as $menu) {
  947. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  948. }
  949. return $menus;
  950. };
  951. $field[] = Form::select('group_id', '用户分组')->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  952. }
  953. $field[] = Form::hidden('uids', implode(',', $uids));
  954. $field[] = Form::hidden('all', $all);
  955. $field[] = Form::hidden('where', $where ? json_encode($where) : "");
  956. return create_form('设置用户分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
  957. }
  958. /**
  959. * 保存会员分组
  960. * @param $id
  961. * @return mixed
  962. */
  963. public function saveSetGroup($uids, int $group_id, $redisKey, $queueId)
  964. {
  965. /** @var UserGroupServices $userGroup */
  966. $userGroup = app()->make(UserGroupServices::class);
  967. /** @var QueueServices $queueService */
  968. $queueService = app()->make(QueueServices::class);
  969. if (!$userGroup->getGroup($group_id)) {
  970. throw new AdminException('该分组不存在');
  971. }
  972. if (!$this->setUserGroup($uids, $group_id)) {
  973. $queueService->addQueueFail($queueId['id'], $redisKey);
  974. throw new AdminException('设置分组失败或无改动');
  975. } else {
  976. $queueService->doSuccessSremRedis($uids, $redisKey, $queueId['type']);
  977. }
  978. return true;
  979. }
  980. /**
  981. * 设置用户标签
  982. * @param $uids
  983. * @param $all
  984. * @param $where
  985. * @param int $type
  986. * @param int $store_id
  987. * @return mixed
  988. * @throws \think\db\exception\DataNotFoundException
  989. * @throws \think\db\exception\DbException
  990. * @throws \think\db\exception\ModelNotFoundException
  991. */
  992. public function setLabel($uids, $all, $where, int $type = 0, int $relation_id = 0)
  993. {
  994. /** @var UserLabelServices $userlabelServices */
  995. $userlabelServices = app()->make(UserLabelServices::class);
  996. $userLabel = $userlabelServices->getLabelList(['type' => $type, 'relation_id' => $relation_id]);
  997. if (count($uids) == 1) {
  998. /** @var UserLabelRelationServices $userLabeLRelation */
  999. $userLabeLRelation = app()->make(UserLabelRelationServices::class);
  1000. $lids = $userLabeLRelation->getUserLabels($uids[0], $type, $relation_id);
  1001. $setOptionUserLabel = function () use ($userLabel) {
  1002. $menus = [];
  1003. foreach ($userLabel as $menu) {
  1004. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  1005. }
  1006. return $menus;
  1007. };
  1008. $field[] = Form::select('label_id', '用户标签', $lids)->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  1009. } else {
  1010. $setOptionUserLabel = function () use ($userLabel) {
  1011. $menus = [];
  1012. foreach ($userLabel as $menu) {
  1013. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  1014. }
  1015. return $menus;
  1016. };
  1017. $field[] = Form::select('label_id', '用户标签')->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  1018. }
  1019. $field[] = Form::hidden('uids', implode(',', $uids));
  1020. $field[] = Form::hidden('all', $all);
  1021. $field[] = Form::hidden('where', $where ? json_encode($where) : "");
  1022. return create_form('设置用户标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
  1023. }
  1024. /**
  1025. * 保存用户标签
  1026. * @param $uids
  1027. * @param $label_id
  1028. * @return bool
  1029. */
  1030. public function saveSetLabel($uids, $label_id)
  1031. {
  1032. /** @var UserLabelRelationServices $services */
  1033. $services = app()->make(UserLabelRelationServices::class);
  1034. if ($label_id) {
  1035. /** @var UserLabelServices $userlabel */
  1036. $userlabel = app()->make(UserLabelServices::class);
  1037. if (count($label_id) != $userlabel->getCount([['id', 'in', $label_id]])) {
  1038. throw new AdminException('用户标签不存在或被删除');
  1039. }
  1040. if (!$services->setUserLable($uids, $label_id)) {
  1041. throw new AdminException('设置标签失败');
  1042. }
  1043. } else {//没传入标签 默认清空
  1044. if (!is_array($uids)) {
  1045. $uids = [$uids];
  1046. }
  1047. foreach ($uids as $uid) {
  1048. $services->unUserLabel((int)$uid);
  1049. }
  1050. }
  1051. return true;
  1052. }
  1053. /**
  1054. * 批量队列设置标签
  1055. * @param $uids
  1056. * @param $lable_id
  1057. * @param $redisKey
  1058. * @param $queueId
  1059. * @return bool
  1060. */
  1061. public function saveBatchSetLabel($uids, $lable_id, $redisKey, $queueId)
  1062. {
  1063. /** @var QueueServices $queueService */
  1064. $queueService = app()->make(QueueServices::class);
  1065. foreach ($lable_id as $id) {
  1066. if (!app()->make(UserLabelServices::class)->getLable((int)$id)) {
  1067. throw new AdminException('用户标签不存在或被删除');
  1068. }
  1069. }
  1070. /** @var UserLabelRelationServices $services */
  1071. $services = app()->make(UserLabelRelationServices::class);
  1072. if (!$services->setUserLable($uids, $lable_id)) {
  1073. $queueService->addQueueFail($queueId['id'], $redisKey);
  1074. throw new AdminException('设置标签失败');
  1075. } else {
  1076. $queueService->doSuccessSremRedis($uids, $redisKey, $queueId['type']);
  1077. }
  1078. return true;
  1079. }
  1080. /**
  1081. * 赠送会员等级
  1082. * @param int $uid
  1083. * @return mixed
  1084. * */
  1085. public function giveLevel($id)
  1086. {
  1087. if (!$this->userExist($id)) {
  1088. throw new AdminException('用户不存在');
  1089. }
  1090. //查询高于当前会员的所有会员等级
  1091. $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  1092. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', $grade ?? 0], 'id,name');
  1093. $setOptionlevel = function () use ($systemLevelList) {
  1094. $menus = [];
  1095. foreach ($systemLevelList as $menu) {
  1096. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  1097. }
  1098. return $menus;
  1099. };
  1100. $field[] = Form::select('level_id', '用户等级')->setOptions(FormBuilder::setOptions($setOptionlevel))->filterable(true);
  1101. return create_form('赠送等级', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
  1102. }
  1103. /**
  1104. * 执行赠送会员等级
  1105. * @param int $uid
  1106. * @return mixed
  1107. * */
  1108. public function saveGiveLevel(int $id, int $level_id)
  1109. {
  1110. if (!$this->userExist($id)) {
  1111. throw new AdminException('用户不存在');
  1112. }
  1113. /** @var SystemUserLevelServices $systemLevelServices */
  1114. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  1115. /** @var UserLevelServices $userLevelServices */
  1116. $userLevelServices = app()->make(UserLevelServices::class);
  1117. //查询当前选择的会员等级
  1118. $systemLevel = $systemLevelServices->getLevel($level_id);
  1119. if (!$systemLevel) throw new AdminException('您选择赠送的用户等级不存在!');
  1120. //检查是否拥有此会员等级
  1121. $level = $userLevelServices->getWhereLevel(['uid' => $id, 'level_id' => $level_id], 'valid_time,is_forever');
  1122. if ($level && $level['status'] == 1 && $level['is_del'] == 0) {
  1123. throw new AdminException('此用户已有该用户等级,无法再次赠送');
  1124. }
  1125. //保存会员信息
  1126. if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
  1127. throw new AdminException('赠送失败');
  1128. }
  1129. return true;
  1130. }
  1131. /**
  1132. * 赠送付费会员时长
  1133. * @param int $uid
  1134. * @return mixed
  1135. * */
  1136. public function giveLevelTime($id)
  1137. {
  1138. $userInfo = $this->getUserCacheInfo($id);
  1139. if (!$userInfo) {
  1140. throw new AdminException('用户不存在');
  1141. }
  1142. $overdue_time = '';
  1143. if ($userInfo['is_ever_level'] == 1) {
  1144. $overdue_time = '永久';
  1145. } else {
  1146. if ($userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] > 0) {
  1147. $overdue_time = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1148. } else {
  1149. $overdue_time = '已过期/暂未开通';
  1150. }
  1151. }
  1152. $field[] = Form::input('overdue_time', '会员到期时间', $overdue_time)->disabled(true);
  1153. $field[] = Form::radio('days_status', '修改付费会员', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  1154. $field[] = Form::number('days', '调整时长(天)')->min(0)->max(999999)->precision(0)->placeholder('请输入');
  1155. return create_form('赠送付费会员时长', $field, Url::buildUrl('/user/save_give_level_time/' . $id), 'PUT');
  1156. }
  1157. /**
  1158. * 执行赠送付费会员时长
  1159. * @param int $id
  1160. * @param int $days
  1161. * @param int $days_status 1:增加 2:减少
  1162. * @return bool
  1163. * @throws \Exception
  1164. */
  1165. public function saveGiveLevelTime(int $id, int $days, int $days_status = 1)
  1166. {
  1167. $userInfo = $this->getUserInfo($id);
  1168. if (!$userInfo) {
  1169. throw new AdminException('用户不存在');
  1170. }
  1171. if ($days <= 0) throw new AdminException('赠送天数不能小于1天');
  1172. if ($userInfo['is_ever_level'] == 1) {
  1173. throw new AdminException('永久会员无需操作');
  1174. }
  1175. $update = [];
  1176. $days_time = bcmul((string)$days, '86400');
  1177. if ($userInfo['is_money_level'] == 0) {
  1178. $update['is_money_level'] = 3;
  1179. $time = time();
  1180. } else {
  1181. $time = $userInfo['overdue_time'];
  1182. }
  1183. if ($days_status == 1) {//增加
  1184. $time = (int)bcadd((string)$time, (string)$days_time);
  1185. } else {//减少
  1186. if ($time <= $days_time) {
  1187. $time = 0;
  1188. } else {
  1189. $time = (int)bcsub((string)$time, (string)$days_time);
  1190. }
  1191. }
  1192. $update['overdue_time'] = $time;
  1193. if ($time < time()) {//已经过期
  1194. $update['is_money_level'] = 0;
  1195. }
  1196. $this->dao->update($id, $update);
  1197. $userInfo->save();
  1198. /** @var StoreOrderCreateServices $storeOrderCreateService */
  1199. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  1200. $orderInfo = [
  1201. 'uid' => $id,
  1202. 'order_id' => $storeOrderCreateService->getNewOrderId(),
  1203. 'type' => 4,
  1204. 'member_type' => 0,
  1205. 'pay_type' => 'admin',
  1206. 'paid' => 1,
  1207. 'pay_time' => time(),
  1208. 'is_free' => 1,
  1209. 'overdue_time' => $time,
  1210. 'vip_day' => $days_status == 1 ? $days : bcsub('0', $days),
  1211. 'add_time' => time()
  1212. ];
  1213. /** @var OtherOrderServices $otherOrder */
  1214. $otherOrder = app()->make(OtherOrderServices::class);
  1215. $otherOrder->save($orderInfo);
  1216. return true;
  1217. }
  1218. /**
  1219. * 清除会员等级
  1220. * @paran int $uid
  1221. * @paran boolean
  1222. * */
  1223. public function cleanUpLevel($uid)
  1224. {
  1225. if (!$this->userExist($uid))
  1226. throw new AdminException('用户不存在');
  1227. /** @var UserLevelServices $services */
  1228. $services = app()->make(UserLevelServices::class);
  1229. return $this->transaction(function () use ($uid, $services) {
  1230. $res = $services->delUserLevel($uid);
  1231. $res1 = $this->dao->update($uid, ['clean_time' => time(), 'level' => 0, 'exp' => 0], 'uid');
  1232. if (!$res && !$res1)
  1233. throw new AdminException('清除失败');
  1234. return true;
  1235. });
  1236. }
  1237. /**
  1238. * 获取用户详情里面的用户消费能力和用户余额积分等
  1239. * @param $uid
  1240. * @return array[]
  1241. */
  1242. public function getHeaderList(int $uid, $userInfo = [])
  1243. {
  1244. if (!$userInfo) {
  1245. $userInfo = $this->getUserInfo($uid);
  1246. }
  1247. /** @var StoreOrderServices $orderServices */
  1248. $orderServices = app()->make(StoreOrderServices::class);
  1249. $where = ['pid' => 0, 'uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  1250. return [
  1251. [
  1252. 'title' => '余额',
  1253. 'value' => $userInfo['now_money'] ?? 0,
  1254. 'key' => '元',
  1255. ],
  1256. [
  1257. 'title' => '总计订单',
  1258. 'value' => $orderServices->count($where),
  1259. 'key' => '笔',
  1260. ],
  1261. [
  1262. 'title' => '总消费金额',
  1263. 'value' => $orderServices->together($where, 'pay_price'),
  1264. 'key' => '元',
  1265. ],
  1266. [
  1267. 'title' => '积分',
  1268. 'value' => $userInfo['integral'] ?? 0,
  1269. 'key' => '',
  1270. ],
  1271. [
  1272. 'title' => '用户经验',
  1273. 'value' => $userInfo['exp'] ?? 0,
  1274. 'key' => '',
  1275. ],
  1276. [
  1277. 'title' => '消费次数',
  1278. 'value' => $orderServices->count(['uid' => $uid, 'pid' => [0, -1], 'paid' => 1, 'is_del' => 0, 'is_system_del' => 0]),
  1279. 'key' => '',
  1280. ],
  1281. // [
  1282. // 'title' => '本月订单',
  1283. // 'value' => $orderServices->count($where + ['time' => 'month']),
  1284. // 'key' => '笔',
  1285. // ],
  1286. // [
  1287. // 'title' => '本月消费金额',
  1288. // 'value' => $orderServices->together($where + ['time' => 'month'], 'pay_price'),
  1289. // 'key' => '元',
  1290. // ]
  1291. ];
  1292. }
  1293. /**
  1294. * 用户详情
  1295. * @param int $uid
  1296. * @return array
  1297. */
  1298. public function read(int $uid)
  1299. {
  1300. $userInfo = $this->dao->getUserWithTrashedInfo($uid);
  1301. if (!$userInfo) {
  1302. throw new AdminException('数据不存在');
  1303. }
  1304. $userInfo = $userInfo->toArray();
  1305. $spread_uid_nickname = '';
  1306. if ($userInfo['spread_uid']) {
  1307. $spread_uid_nickname = $this->dao->value(['uid' => $userInfo['spread_uid']], 'nickname');
  1308. }
  1309. if ($userInfo['is_ever_level'] == 1) {
  1310. $userInfo['overdue_time'] = '永久';
  1311. } else {
  1312. if ($userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] > 0) {
  1313. $userInfo['overdue_time'] = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1314. }
  1315. }
  1316. $userInfo['spread_uid_nickname'] = $userInfo['spread_uid'] ? $spread_uid_nickname . '/' . $userInfo['spread_uid'] : '';
  1317. $userInfo['_add_time'] = date('Y-m-d H:i:s', $userInfo['add_time']);
  1318. $userInfo['_last_time'] = date('Y-m-d H:i:s', $userInfo['last_time']);
  1319. $userInfo['birthday'] = $userInfo['birthday'] ? date('Y-m-d', $userInfo['birthday']) : '';
  1320. /** @var UserLabelRelationServices $userLabelServices */
  1321. $userLabelServices = app()->make(UserLabelRelationServices::class);
  1322. $label_list = $userLabelServices->getUserLabelList([$uid]);
  1323. $label_id = [];
  1324. $userInfo['group'] = app()->make(UserGroupServices::class)->getGroup($userInfo['group_id']);
  1325. $userInfo['label_list'] = '';
  1326. if ($label_list) {
  1327. $userInfo['label_list'] = implode(',', array_column($label_list, 'label_name'));
  1328. foreach ($label_list as $item) {
  1329. $label_id[] = [
  1330. 'id' => $item['label_id'],
  1331. 'label_name' => $item['label_name']
  1332. ];
  1333. }
  1334. }
  1335. $userInfo['vip_name'] = '';
  1336. if ($userInfo['level']) {
  1337. /** @var SystemUserLevelServices $levelServices */
  1338. $levelServices = app()->make(SystemUserLevelServices::class);
  1339. $levelInfo = $levelServices->getOne(['id' => $userInfo['level']], 'id,name');
  1340. $userInfo['vip_name'] = $levelInfo['name'] ?? '';
  1341. }
  1342. $userInfo['label_id'] = $label_id;
  1343. $workClientInfo = $workMemberInfo = null;
  1344. if ($userInfo['phone']) {
  1345. /** @var WorkMemberServices $workMemberService */
  1346. $workMemberService = app()->make(WorkMemberServices::class);
  1347. $workMemberInfo = $workMemberService->get(['mobile' => $userInfo['phone']]);
  1348. if ($workMemberInfo) {
  1349. if (!$workMemberInfo->uid) {
  1350. $workMemberInfo->uid = $userInfo['uid'];
  1351. $workMemberInfo->save();
  1352. }
  1353. $workMemberInfo = $workMemberInfo->toArray();
  1354. }
  1355. }
  1356. if (!$workMemberInfo) {
  1357. /** @var WorkClientServices $workClientService */
  1358. $workClientService = app()->make(WorkClientServices::class);
  1359. $workClientInfo = $workClientService->get(['uid' => $userInfo['uid']]);
  1360. if ($workClientInfo) {
  1361. $workClientInfo = $workClientInfo->toArray();
  1362. }
  1363. }
  1364. $info = [
  1365. 'uid' => $uid,
  1366. 'userinfo' => [],
  1367. 'headerList' => $this->getHeaderList($uid, $userInfo),
  1368. 'count' => [0, 0, 0],
  1369. 'ps_info' => $userInfo,
  1370. 'workClientInfo' => $workClientInfo,
  1371. 'workMemberInfo' => $workMemberInfo,
  1372. ];
  1373. return $info;
  1374. }
  1375. /**
  1376. * 获取单个用户信息
  1377. * @param $id 用户id
  1378. * @return mixed
  1379. */
  1380. public function oneUserInfo(int $id, string $type)
  1381. {
  1382. switch ($type) {
  1383. case 'spread':
  1384. // /** @var UserSpreadServices $services */
  1385. // $services = app()->make(UserSpreadServices::class);
  1386. /** @var UserFriendsServices $services */
  1387. $services = app()->make(UserFriendsServices::class);
  1388. return $services->getFriendList($id);
  1389. break;
  1390. case 'order':
  1391. /** @var StoreOrderServices $services */
  1392. $services = app()->make(StoreOrderServices::class);
  1393. return $services->getUserOrderList($id);
  1394. break;
  1395. case 'integral':
  1396. /** @var UserBillServices $services */
  1397. $services = app()->make(UserBillServices::class);
  1398. return $services->getIntegralList($id, [], 'title,number,balance,mark,add_time');
  1399. break;
  1400. case 'sign':
  1401. /** @var UserBillServices $services */
  1402. $services = app()->make(UserBillServices::class);
  1403. return $services->getSignList($id, [], 'title,number,mark,add_time');
  1404. break;
  1405. case 'coupon':
  1406. /** @var StoreCouponUserServices $services */
  1407. $services = app()->make(StoreCouponUserServices::class);
  1408. return $services->getUserCouponList($id, 0);
  1409. break;
  1410. case 'balance_change':
  1411. /** @var UserMoneyServices $services */
  1412. $services = app()->make(UserMoneyServices::class);
  1413. return $services->getUserMoneyList($id, [], 'title,type,number,balance,mark,pm,status,add_time');
  1414. break;
  1415. case 'visit':
  1416. /** @var StoreProductLogServices $services */
  1417. $services = app()->make(StoreProductLogServices::class);
  1418. return $services->getList(['uid' => $id, 'type' => 'visit'], 'product_id', 'id,product_id,max(add_time) as add_time');
  1419. break;
  1420. case 'spread_change':
  1421. /** @var UserSpreadServices $services */
  1422. $services = app()->make(UserSpreadServices::class);
  1423. return $services->getSpreadList(['store_id' => 0, 'staff_id' => 0, 'uid' => $id], '*', ['spreadUser', 'admin'], false);
  1424. break;
  1425. case 'static_integral':
  1426. /** @var UserAwardIntegralServices $award_integral_service */
  1427. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  1428. return $award_integral_service->getIntegralList(['type' => 0, 'uid' => $id]);
  1429. break;
  1430. case 'action_integral':
  1431. /** @var UserAwardIntegralServices $award_integral_service */
  1432. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  1433. return $award_integral_service->getIntegralList(['type' => 1, 'uid' => $id]);
  1434. break;
  1435. case 'award_integral':
  1436. /** @var AwardIntegralServices $award_integral_service */
  1437. $award_integral_service = app()->make(AwardIntegralServices::class);
  1438. return $award_integral_service->getIntegralList(['uid' => $id]);
  1439. break;
  1440. case 'energy':
  1441. /** @var UserBillServices $services */
  1442. $services = app()->make(UserBillServices::class);
  1443. return $services->getEnergyList($id, [], 'title,number,balance,mark,add_time');
  1444. break;
  1445. default:
  1446. throw new AdminException('type参数错误');
  1447. }
  1448. }
  1449. /**
  1450. * 用户图表
  1451. * @return array
  1452. */
  1453. public function userChart()
  1454. {
  1455. [$starday, $yesterday, $timeType, $timeKey] = $this->timeHandle('thirtyday', true);
  1456. $user_list = $this->dao->userList($starday, $yesterday);
  1457. $chartdata = [];
  1458. $chartdata['legend'] = ['用户数'];//分类
  1459. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1460. $chartdata['xAxis'] = $timeKey;//X轴值
  1461. $chartdata['series'] = [];//分类1值
  1462. if (!empty($user_list)) {
  1463. $user_list = array_combine(array_column($user_list, 'day'), $user_list);
  1464. $chartdata['yAxis']['maxnum'] = max(array_column($user_list, 'count'));
  1465. foreach ($timeKey as $day) {
  1466. if (isset($user_list[$day])) {
  1467. $chartdata['series'][] = $user_list[$day]['count'] ?? 0;
  1468. } else {
  1469. $chartdata['series'][] = 0;
  1470. }
  1471. }
  1472. }
  1473. $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
  1474. $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  1475. $pay[0] = $this->dao->count(['pay_count' => 0]);
  1476. $pay[1] = $this->dao->count(['pay_count' => 1]);
  1477. $pay[2] = $this->dao->userCount(1);
  1478. $pay[3] = $this->dao->userCount(2);
  1479. foreach ($pay as $key => $item) {
  1480. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  1481. }
  1482. $chartdata['bing_data'] = $bing_data;
  1483. return $chartdata;
  1484. }
  1485. /***********************************************/
  1486. /************ 前端api services *****************/
  1487. /***********************************************/
  1488. /**
  1489. * 用户信息
  1490. * @param $info
  1491. * @return mixed
  1492. */
  1493. public function userInfo($info)
  1494. {
  1495. $uid = (int)$info['uid'];
  1496. $broken_time = intval(sys_config('extract_time'));
  1497. $search_time = time() - 86400 * $broken_time;
  1498. //改造时间
  1499. $search_time = '1970/01/01' . ' - ' . date('Y/m/d H:i:s', $search_time);
  1500. //可提现佣金
  1501. //返佣 +
  1502. /** @var UserBrokerageServices $userBrokerageServices */
  1503. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1504. $brokerage_commission = (string)$userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], $search_time);
  1505. //退款退的佣金 -
  1506. $refund_commission = (string)$userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 0], $search_time);
  1507. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  1508. if ($info['broken_commission'] < 0)
  1509. $info['broken_commission'] = 0;
  1510. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  1511. if ($info['commissionCount'] < 0)
  1512. $info['commissionCount'] = 0;
  1513. /** @var StoreOrderServices $storeOrder */
  1514. $storeOrder = app()->make(StoreOrderServices::class);
  1515. $orderStatusSum = (float)$storeOrder->sum(['pid' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'uid' => $info['uid'], 'is_del' => 0], 'pay_price', true);//累计有效消费
  1516. $info['spread_status'] = ($configData['brokerage_func_status'] ?? 1) && $this->checkUserPromoter($info['uid'], $info, $orderStatusSum);
  1517. if (!$info['is_promoter'] && $info['spread_status']) {
  1518. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  1519. $info['is_promoter'] = 1;
  1520. }
  1521. return $info;
  1522. }
  1523. /**
  1524. * 个人中心
  1525. * @param array $user
  1526. */
  1527. public function personalHome(array $user, $tokenData)
  1528. {
  1529. $uid = (int)$user['uid'];
  1530. event('user.login', [$uid, app('request')->ip()]);
  1531. /** @var StoreCouponUserServices $storeCoupon */
  1532. $storeCoupon = app()->make(StoreCouponUserServices::class);
  1533. /** @var UserMoneyServices $userMoneyServices */
  1534. $userMoneyServices = app()->make(UserMoneyServices::class);
  1535. /** @var UserExtractServices $userExtract */
  1536. $userExtract = app()->make(UserExtractServices::class);
  1537. /** @var StoreOrderServices $storeOrder */
  1538. $storeOrder = app()->make(StoreOrderServices::class);
  1539. /** @var UserLevelServices $userLevel */
  1540. $userLevel = app()->make(UserLevelServices::class);
  1541. /** @var StoreServiceServices $storeService */
  1542. $storeService = app()->make(StoreServiceServices::class);
  1543. /** @var WechatUserServices $wechatUser */
  1544. $wechatUser = app()->make(WechatUserServices::class);
  1545. /** @var UserRelationServices $productRelation */
  1546. $productRelation = app()->make(UserRelationServices::class);
  1547. /** @var SystemMessageServices $systemMessageServices */
  1548. $systemMessageServices = app()->make(SystemMessageServices::class);
  1549. /** @var DiyServices $diyServices */
  1550. $diyServices = app()->make(DiyServices::class);
  1551. /** @var AgentLevelServices $agentLevelServices */
  1552. $agentLevelServices = app()->make(AgentLevelServices::class);
  1553. /** @var StoreProductLogServices $storeProductLogServices */
  1554. $storeProductLogServices = app()->make(StoreProductLogServices::class);
  1555. //是否存在核销码
  1556. if (!$user['bar_code']) {
  1557. $bar_code = $this->getBarCode();
  1558. $this->dao->update($uid, ['bar_code' => $bar_code], 'uid');
  1559. $user['bar_code'] = $bar_code;
  1560. }
  1561. //获取配置参数
  1562. $configData = SystemConfigService::more([
  1563. 'member_card_status',
  1564. 'brokerage_func_status',
  1565. 'store_brokerage_statu',
  1566. 'store_brokerage_price',
  1567. 'member_func_status',
  1568. 'recharge_switch',
  1569. 'extract_time',
  1570. 'balance_func_status',
  1571. 'invoice_func_status',
  1572. 'special_invoice_status',
  1573. 'user_extract_bank_status',
  1574. 'user_extract_wechat_status',
  1575. 'user_extract_alipay_status',
  1576. 'level_activate_status'
  1577. ]);
  1578. //看付费会员是否开启
  1579. $user['is_open_member'] = $user['svip_open'] = !!($configData['member_card_status'] ?? 0);
  1580. $user['agent_level_name'] = '';
  1581. //分销等级信息
  1582. if ($user['agent_level']) {
  1583. $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name');
  1584. $user['agent_level_name'] = $levelInfo && $levelInfo['name'] ? $levelInfo['name'] : '';
  1585. }
  1586. $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
  1587. $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
  1588. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  1589. $user['like'] = $productRelation->getUserCount($uid, 0, 'like');
  1590. $collectCategory = sys_config('video_func_status', 1) ? '' : 'product';
  1591. $user['collectCount'] = $productRelation->getUserCount($uid, 0, 'collect', $collectCategory);
  1592. $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
  1593. $user['notice'] = 0;
  1594. $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
  1595. $user['orderStatusSum'] = (float)$userMoneyServices->sum(['uid' => $uid, 'pm' => 0, 'status' => 1], 'number', true);
  1596. $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
  1597. $user['extractPrice'] = $user['brokerage_price'];//可提现
  1598. $user['statu'] = (int)($configData['store_brokerage_statu'] ?? 0);
  1599. $orderStatusSum = (float)$storeOrder->sum(['pid' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'uid' => $user['uid'], 'is_del' => 0], 'pay_price', true);//累计有效消费
  1600. $user['spread_status'] = ($configData['brokerage_func_status'] ?? 1) && $this->checkUserPromoter($user['uid'], $user, $orderStatusSum);
  1601. if (!$user['is_promoter'] && $user['spread_status']) {
  1602. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  1603. $user['is_promoter'] = 1;
  1604. }
  1605. if ($user['statu'] == 3) {
  1606. $storeBrokeragePrice = $configData['store_brokerage_price'] ?? 0;
  1607. $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$user['orderStatusSum'], 2);
  1608. }
  1609. /** @var UserBrokerageServices $userBrokerageServices */
  1610. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1611. $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  1612. if ($user['broken_commission'] < 0)
  1613. $user['broken_commission'] = 0;
  1614. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  1615. if ($user['commissionCount'] < 0)
  1616. $user['commissionCount'] = 0;
  1617. //用户等级信息
  1618. $userLevelInfo = $userLevel->homeGetUserLevel((int)$user['uid'], $user);
  1619. $user = array_merge($user, $userLevelInfo);
  1620. $user['yesterDay'] = $userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
  1621. $user['recharge_switch'] = (int)($configData['recharge_switch'] ?? 0);//充值开关
  1622. $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  1623. $user['broken_day'] = (int)($configData['extract_time'] ?? 0);//佣金冻结时间
  1624. $user['balance_func_status'] = (int)($configData['balance_func_status'] ?? 0);
  1625. $user['invioce_func'] = !!($configData['invoice_func_status'] ?? 0);
  1626. $user['special_invoice'] = $user['invioce_func'] && ($configData['special_invoice_status'] ?? 0);
  1627. $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
  1628. $user['member_style'] = (int)$diyServices->getColorChange('member');
  1629. if ($user['is_ever_level']) {
  1630. $user['vip_status'] = 1;//永久会员
  1631. } else {
  1632. if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
  1633. $user['vip_status'] = -1;//开通过已过期
  1634. } else if (!$user['overdue_time'] && !$user['is_money_level']) {
  1635. $user['vip_status'] = 2;//没有开通过
  1636. } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
  1637. $user['vip_status'] = 3;//开通了,没有到期
  1638. }
  1639. }
  1640. /** @var StoreServiceRecordServices $servicesRecord */
  1641. $servicesRecord = app()->make(StoreServiceRecordServices::class);
  1642. $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
  1643. $message = $systemMessageServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  1644. $user['service_num'] = (int)bcadd((string)$service_num, (string)$message);
  1645. $user['is_agent_level'] = ($configData['brokerage_func_status'] ?? 1) && $agentLevelServices->count(['status' => 1, 'is_del' => 0]);
  1646. $user['visit_num'] = $storeProductLogServices->getDistinctCount(['uid' => $uid, 'type' => 'visit'], 'product_id');
  1647. $user['user_extract_bank_status'] = (int)($configData['user_extract_bank_status'] ?? 1);
  1648. $user['user_extract_wechat_status'] = (int)($configData['user_extract_wechat_status'] ?? 1);
  1649. $user['user_extract_alipay_status'] = (int)($configData['user_extract_alipay_status'] ?? 1);
  1650. //是否享受新人专享
  1651. /** @var StoreNewcomerServices $newcomerServices */
  1652. $newcomerServices = app()->make(StoreNewcomerServices::class);
  1653. $user['newcomer_status'] = $newcomerServices->checkUserNewcomer($uid);
  1654. $user['level_activate_status'] = $configData['level_activate_status'];
  1655. $user['member_func_status'] = $configData['member_func_status'];
  1656. $extendInfo = SystemConfigService::get('user_extend_info', []);
  1657. $user['register_extend_info'] = [];
  1658. if (!$user['level_activate_status']) {//不需要激活,用户激活状态默认为1
  1659. $user['level_status'] = 1;
  1660. }
  1661. if ($extendInfo) {
  1662. foreach ($extendInfo as $item) {
  1663. if (isset($item['use']) && $item['use'] && isset($item['user_show']) && $item['user_show']) $user['register_extend_info'][] = $item;
  1664. }
  1665. }
  1666. if (isset($user['extend_info']) && $user['extend_info']) {
  1667. $default = $this->defaultExtendInfo;
  1668. $params = array_column($default, 'param');
  1669. $sex = $this->rSex;
  1670. foreach ($user['extend_info'] as &$info) {
  1671. if (isset($info['param']) && in_array($info['param'], $params)) {
  1672. if ($info['param'] == 'sex') {
  1673. $info['value'] = $sex[$user['sex']] ?? 0;
  1674. } elseif ($info['param'] == 'birthday') {
  1675. $info['value'] = ($user['birthday'] ?? '') ? date('Y-m-d', $user['birthday']) : '';
  1676. } elseif ($info['param'] == 'address') {
  1677. $info['value'] = $user['addres'] ?? '';
  1678. } else {
  1679. $info['value'] = $user[$info['param']] ?? '';
  1680. }
  1681. }
  1682. }
  1683. }
  1684. // /** @var UserAwardIntegralServices $award_integral_service */
  1685. // $award_integral_service = app()->make(UserAwardIntegralServices::class);
  1686. // $award_lack = $award_integral_service->getLake();
  1687. // $integral_price = $award_integral_service->getPrice();
  1688. // $static_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
  1689. // $action_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
  1690. // $sum_integral = $award_integral_service->getIntegralSum(['status' => 0]);
  1691. $hidden_integral = sys_config('hidden_integral', 0);
  1692. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  1693. // $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  1694. /** @var AwardIntegralServices $award_integral_service */
  1695. $award_integral_service = app()->make(AwardIntegralServices::class);
  1696. $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  1697. $all_integral = $award_integral_service->sum(['uid' => $user['uid']], 'num');
  1698. $sent_integral = $award_integral_service->sum(['uid' => $user['uid']], 'sent_num');
  1699. $stand_integral = (float)bcsub($all_integral, $sent_integral, 2);
  1700. $extractable_integral = $award_integral_service->sum(['uid' => $user['uid']], 'extract_num');
  1701. $extracted_integral = $award_integral_service->sum(['uid' => $user['uid']], 'extracted_num');
  1702. $shop_integral_trade_commission = sys_config('shop_integral_trade_commission');//微信提现到账方式
  1703. $shop_integral_trade_switch = sys_config('shop_integral_trade_switch');//微信提现到账方式
  1704. return array_merge($user, compact('shop_integral_trade_commission', 'shop_integral_trade_switch', 'hidden_integral', 'all_integral', 'sent_integral', 'stand_integral', 'extractable_integral', 'extracted_integral'));
  1705. }
  1706. /**
  1707. * 用户资金统计
  1708. * @param int $uid `
  1709. */
  1710. public function balance(int $uid)
  1711. {
  1712. $userInfo = $this->getUserInfo($uid);
  1713. if (!$userInfo) {
  1714. throw new ValidateException('数据不存在');
  1715. }
  1716. /** @var UserMoneyServices $userMoneyServices */
  1717. $userMoneyServices = app()->make(UserMoneyServices::class);
  1718. /** @var StoreOrderServices $storeOrder */
  1719. $storeOrder = app()->make(StoreOrderServices::class);
  1720. $user['now_money'] = $userInfo['now_money'];//当前总资金
  1721. $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
  1722. $user['orderStatusSum'] = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price', true);//累计消费
  1723. return $user;
  1724. }
  1725. /**
  1726. * 用户修改信息
  1727. * @param Request $request
  1728. * @return mixed
  1729. */
  1730. public function eidtNickname(int $uid, array $data)
  1731. {
  1732. if (!$this->userExist($uid)) {
  1733. throw new ValidateException('用户不存在');
  1734. }
  1735. if (!$this->dao->update($uid, $data, 'uid')) {
  1736. throw new ValidateException('修改失败');
  1737. }
  1738. return true;
  1739. }
  1740. /**
  1741. * 获取推广人排行
  1742. * @param $data 查询条件
  1743. * @return array
  1744. * @throws \think\db\exception\DataNotFoundException
  1745. * @throws \think\db\exception\ModelNotFoundException
  1746. * @throws \think\exception\DbException
  1747. */
  1748. public function getRankList(array $data)
  1749. {
  1750. $startTime = $endTime = 0;
  1751. switch ($data['type']) {
  1752. case 'week':
  1753. $startTime = strtotime('this week Monday');
  1754. break;
  1755. case 'month':
  1756. $startTime = strtotime('first day of ' . date('F Y'));
  1757. break;
  1758. }
  1759. $endTime = time();
  1760. [$page, $limit] = $this->getPageValue();
  1761. $field = 'spread_uid,count(uid) AS count,spread_time';
  1762. /** @var UserSpreadServices $userSpreadServices */
  1763. $userSpreadServices = app()->make(UserSpreadServices::class);
  1764. $list = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  1765. $rank = 0;
  1766. foreach ($list as $key => $item) {
  1767. if ($item['spread_uid'] == $data['uid']) $rank = $key + 1;
  1768. }
  1769. $week = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('this week Monday'), time()], 'timeKey' => 'spread_time']);
  1770. $month = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('last month'), time()], 'timeKey' => 'spread_time']);
  1771. $start = date('Y-m-d H:i', $startTime);
  1772. $end = date('Y-m-d H:i', time());
  1773. return compact('list', 'rank', 'week', 'month', 'start', 'end');
  1774. }
  1775. /**
  1776. * 静默绑定推广人
  1777. * @param Request $request
  1778. * @return mixed
  1779. */
  1780. public function spread(int $uid, int $spreadUid, $code)
  1781. {
  1782. $userInfo = $this->getUserInfo($uid);
  1783. if (!$userInfo) {
  1784. throw new ValidateException('数据不存在');
  1785. }
  1786. if ($code && !$spreadUid) {
  1787. /** @var QrcodeServices $qrCode */
  1788. $qrCode = app()->make(QrcodeServices::class);
  1789. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  1790. $spreadUid = $info['third_id'];
  1791. }
  1792. }
  1793. //记录好友关系
  1794. if ($spreadUid && $uid && $spreadUid != $uid) {
  1795. /** @var UserFriendsServices $serviceFriend */
  1796. $serviceFriend = app()->make(UserFriendsServices::class);
  1797. $serviceFriend->saveFriend($uid, $spreadUid);
  1798. }
  1799. $data = [];
  1800. //永久绑定
  1801. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  1802. $spread_uid = isset($user['code']) && $user['code'] && $user['code'] != $userInfo->uid ? $user['code'] : ($userInfo['spread_uid'] ?? 0);
  1803. if ($userInfo->spread_uid && $store_brokergae_binding_status == 1) {
  1804. $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
  1805. } else {
  1806. //绑定分销关系 = 所有用户
  1807. if (sys_config('brokerage_bindind', 1) == 1) {
  1808. //分销绑定类型为时间段且过期 ||临时
  1809. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  1810. if (!$userInfo['spread_uid'] || $store_brokergae_binding_status == 3 || ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
  1811. $spreadUid = $spread_uid;
  1812. if ($spreadUid && $userInfo->uid == $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
  1813. $spreadUid = 0;
  1814. }
  1815. if ($spreadUid && $this->dao->get((int)$spreadUid)) {
  1816. $data['spread_uid'] = $spreadUid;
  1817. $data['spread_time'] = time();
  1818. }
  1819. }
  1820. }
  1821. }
  1822. if ($data && !$this->dao->update($userInfo['uid'], $data, 'uid')) {
  1823. throw new ValidateException('修改信息失败');
  1824. }
  1825. if (isset($data['spread_uid']) && $data['spread_uid']) {
  1826. /** @var UserBillServices $userBill */
  1827. $userBill = app()->make(UserBillServices::class);
  1828. //邀请新用户增加经验
  1829. $userBill->inviteUserIncExp((int)$spreadUid);
  1830. }
  1831. return true;
  1832. }
  1833. /**
  1834. * 添加访问记录
  1835. * @param Request $request
  1836. * @return mixed
  1837. */
  1838. public function setVisit(array $data)
  1839. {
  1840. $userInfo = $this->getUserInfo($data['uid']);
  1841. if (!$userInfo) {
  1842. throw new ValidateException('数据不存在');
  1843. }
  1844. if (isset($data['ip']) && $data['ip']) {
  1845. $addressArr = $this->addressHandle($this->convertIp($data['ip']));
  1846. $data['province'] = $addressArr['province'] ?? '';
  1847. }
  1848. $data['channel_type'] = $userInfo['user_type'];
  1849. $data['add_time'] = time();
  1850. /** @var UserVisitServices $userVisit */
  1851. $userVisit = app()->make(UserVisitServices::class);
  1852. if ($userVisit->save($data)) {
  1853. return true;
  1854. } else {
  1855. throw new ValidateException('添加访问记录失败');
  1856. }
  1857. }
  1858. /**
  1859. * 获取活动状态
  1860. * @return mixed
  1861. */
  1862. public function activity()
  1863. {
  1864. /** @var StoreBargainServices $storeBragain */
  1865. $storeBragain = app()->make(StoreBargainServices::class);
  1866. /** @var StoreCombinationServices $storeCombinaion */
  1867. $storeCombinaion = app()->make(StoreCombinationServices::class);
  1868. /** @var StoreSeckillServices $storeSeckill */
  1869. $storeSeckill = app()->make(StoreSeckillServices::class);
  1870. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  1871. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  1872. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  1873. return $data;
  1874. }
  1875. /**
  1876. * 获取用户下级推广人
  1877. * @param int $uid 当前用户
  1878. * @param int $grade 等级 0 一级 1 二级
  1879. * @param string $orderBy 排序
  1880. * @param string $keyword
  1881. * @return array|bool
  1882. */
  1883. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '', $time = [])
  1884. {
  1885. $user = $this->getUserInfo($uid);
  1886. if (!$user) {
  1887. throw new ValidateException('数据不存在');
  1888. }
  1889. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  1890. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  1891. $data = [
  1892. 'total' => count($spread_one_ids),
  1893. 'totalLevel' => count($spread_two_ids),
  1894. 'list' => []
  1895. ];
  1896. /** @var UserStoreOrderServices $userStoreOrder */
  1897. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  1898. $list = [];
  1899. $where = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  1900. if ($grade == 0) {
  1901. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword, $time);
  1902. $where = $where + ['spread_uid' => $uid];
  1903. } else {
  1904. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword, $time);
  1905. $where = $where + ['spread_two_uid' => $uid];
  1906. }
  1907. foreach ($list as &$item) {
  1908. if (isset($item['spread_time']) && $item['spread_time']) {
  1909. $item['time'] = date('Y/m/d', $item['spread_time']);
  1910. }
  1911. }
  1912. $data['list'] = $list;
  1913. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  1914. $data['count'] = 0;
  1915. $data['price'] = 0;
  1916. if ($list) {
  1917. $uids = array_column($list, 'uid');
  1918. $data['count'] = count($uids);
  1919. /** @var StoreOrderServices $storeOrder */
  1920. $storeOrder = app()->make(StoreOrderServices::class);
  1921. $data['price'] = $storeOrder->sum($where, $grade == 0 ? 'one_brokerage' : 'two_brokerage');
  1922. }
  1923. return $data;
  1924. }
  1925. /**
  1926. * 获取推广人uids
  1927. * @param int $uid
  1928. * @param bool $one
  1929. * @return array
  1930. */
  1931. public function getUserSpredadUids(int $uid, int $type = 0)
  1932. {
  1933. $uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
  1934. if ($type === 1) {
  1935. return $uids;
  1936. }
  1937. if ($uids) {
  1938. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids]], 'uid');
  1939. if ($type === 2) {
  1940. return $uidsTwo;
  1941. }
  1942. if ($uidsTwo) {
  1943. $uids = array_merge($uids, $uidsTwo);
  1944. }
  1945. }
  1946. return $uids;
  1947. }
  1948. /**
  1949. * 检测用户是否是推广员
  1950. * @param int $uid
  1951. * @param array $user
  1952. * @param float $sumPrice
  1953. * @return bool
  1954. */
  1955. public function checkUserPromoter(int $uid, $user = [], float $sumPrice = 0.00)
  1956. {
  1957. if (!$uid) {
  1958. return false;
  1959. }
  1960. if (!$user) {
  1961. $user = $this->getUserInfo($uid);
  1962. }
  1963. if (!$user) {
  1964. return false;
  1965. }
  1966. //分销是否开启
  1967. if (!sys_config('brokerage_func_status')) {
  1968. return false;
  1969. }
  1970. //用户分校推广资格是否开启4.0.32
  1971. if (isset($user['spread_open']) && !$user['spread_open']) {
  1972. return false;
  1973. }
  1974. $store_brokerage_statu = sys_config('store_brokerage_statu');
  1975. if ($user['is_promoter'] || $store_brokerage_statu == 2) {
  1976. return true;
  1977. }
  1978. if ($store_brokerage_statu == 3) {
  1979. if (!$sumPrice) {
  1980. /** @var StoreOrderServices $storeOrder */
  1981. $storeOrder = app()->make(StoreOrderServices::class);
  1982. $sumPrice = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price');//累计消费
  1983. }
  1984. $store_brokerage_price = sys_config('store_brokerage_price');
  1985. if ($sumPrice >= $store_brokerage_price) {
  1986. return true;
  1987. }
  1988. }
  1989. return false;
  1990. }
  1991. /**
  1992. * 同步微信粉丝用户(后台接口)
  1993. * @return bool
  1994. */
  1995. public function syncWechatUsers()
  1996. {
  1997. $key = md5('sync_wechat_users');
  1998. //一天点击一次
  1999. if (CacheService::get($key)) {
  2000. return true;
  2001. }
  2002. $next_openid = null;
  2003. do {
  2004. $result = OfficialAccount::userService()->list($next_openid);
  2005. $userOpenids = $result['data'];
  2006. //拆分大数组
  2007. $opemidArr = array_chunk($userOpenids, 100);
  2008. foreach ($opemidArr as $openids) {
  2009. //加入同步|更新用户队列
  2010. UserJob::dispatch([$openids]);
  2011. }
  2012. $next_openid = $result['next_openid'];
  2013. } while ($next_openid != null);
  2014. CacheService::set($key, 1, 3600 * 24);
  2015. return true;
  2016. }
  2017. /**
  2018. * 导入微信粉丝用户
  2019. * @param array $openids
  2020. * @return bool
  2021. */
  2022. public function importUser(array $noBeOpenids)
  2023. {
  2024. if (!$noBeOpenids) {
  2025. return true;
  2026. }
  2027. $dataAll = $data = [];
  2028. $time = time();
  2029. foreach ($noBeOpenids as $openid) {
  2030. try {
  2031. $info = OfficialAccount::userService()->get($openid);
  2032. $info = is_object($info) ? $info->toArray() : $info;
  2033. } catch (\Throwable $e) {
  2034. $info = [];
  2035. }
  2036. if (!$info) continue;
  2037. if (($info['subscribe'] ?? 1) == 1) {
  2038. $data['nickname'] = $info['nickname'] ?? '';
  2039. $data['headimgurl'] = $info['headimgurl'] ?? '';
  2040. $userInfoData = $this->setUserInfo($data);
  2041. if (!$userInfoData) {
  2042. throw new AdminException('用户信息储存失败!');
  2043. }
  2044. $data['uid'] = $userInfoData['uid'];
  2045. $data['subscribe'] = $info['subscribe'];
  2046. $data['unionid'] = $info['unionid'] ?? '';
  2047. $data['openid'] = $info['openid'] ?? '';
  2048. $data['sex'] = $info['sex'] ?? 0;
  2049. $data['language'] = $info['language'] ?? '';
  2050. $data['city'] = $info['city'] ?? '';
  2051. $data['province'] = $info['province'] ?? '';
  2052. $data['country'] = $info['country'] ?? '';
  2053. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  2054. $data['groupid'] = $info['groupid'] ?? 0;
  2055. $data['remark'] = $info['remark'] ?? '';
  2056. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  2057. $data['add_time'] = $time;
  2058. $data['is_complete'] = 1;
  2059. $dataAll[] = $data;
  2060. }
  2061. }
  2062. if ($dataAll) {
  2063. /** @var WechatUserServices $wechatUser */
  2064. $wechatUser = app()->make(WechatUserServices::class);
  2065. if (!$wechatUser->saveAll($dataAll)) {
  2066. throw new ValidateException('保存用户信息失败');
  2067. }
  2068. }
  2069. return true;
  2070. }
  2071. /**
  2072. * 修改会员的时间及是否会员状态
  2073. * @param int $vip_day 会员天数
  2074. * @param array $user_id 用户id
  2075. * @param int $is_money_level 会员来源途径
  2076. * @param bool $member_type 会员卡类型
  2077. * @return mixed
  2078. * @throws \think\db\exception\DataNotFoundException
  2079. * @throws \think\db\exception\DbException
  2080. * @throws \think\db\exception\ModelNotFoundException
  2081. */
  2082. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  2083. {
  2084. if ($vip_day == 0) throw new ValidateException('天数不能为0');
  2085. $user_info = $this->getUserInfo($user_id);
  2086. if (!$user_info) throw new ValidateException('用户数据不存在');
  2087. if (!$member_type) $member_type = "month";
  2088. if ($member_type == 'ever') {
  2089. $overdue_time = 0;
  2090. $is_ever_level = 1;
  2091. } else {
  2092. if ($user_info['is_money_level'] == 0) {
  2093. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  2094. } else {
  2095. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  2096. }
  2097. $is_ever_level = 0;
  2098. }
  2099. $setData['overdue_time'] = $overdue_time;
  2100. $setData['is_ever_level'] = $is_ever_level;
  2101. $setData['is_money_level'] = $is_money_level ? $is_money_level : 0;
  2102. // if ($user_info['level'] == 0) $setData['level'] = 1;
  2103. return $this->dao->update(['uid' => $user_id], $setData);
  2104. }
  2105. /**
  2106. * 清空到期svip(分批加入队列)
  2107. * @return bool
  2108. */
  2109. public function offUserSvip()
  2110. {
  2111. $users = $this->dao->getColumn([['is_ever_level', '=', 0], ['is_money_level', '>', 0], ['overdue_time', '<', time()]], 'uid');
  2112. if ($users) {
  2113. //拆分大数组
  2114. $uidsArr = array_chunk($users, 100);
  2115. foreach ($uidsArr as $uids) {
  2116. //加入同步|更新用户队列
  2117. UserSvipJob::dispatch([$uids]);
  2118. }
  2119. }
  2120. return true;
  2121. }
  2122. /**
  2123. * 会员过期改变状态,变为普通会员
  2124. * @param $uid
  2125. * @param null $userInfo
  2126. * @return bool
  2127. * @throws \think\db\exception\DataNotFoundException
  2128. * @throws \think\db\exception\DbException
  2129. * @throws \think\db\exception\ModelNotFoundException
  2130. */
  2131. public function offMemberLevel($uid, $userInfo = null)
  2132. {
  2133. if (!$uid) return false;
  2134. $userInfo = $userInfo ?: $this->dao->get($uid);
  2135. if (!$userInfo) return false;
  2136. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  2137. $this->dao->update(['uid' => $uid], ['is_money_level' => 0]);
  2138. return false;
  2139. }
  2140. return true;
  2141. }
  2142. /**
  2143. * @param array $where
  2144. * @return array
  2145. * @throws \think\db\exception\DataNotFoundException
  2146. * @throws \think\db\exception\DbException
  2147. * @throws \think\db\exception\ModelNotFoundException
  2148. */
  2149. public function getUserInfoList(array $where, $field = "*")
  2150. {
  2151. return $this->dao->getUserInfoList($where, $field);
  2152. }
  2153. /**
  2154. * 保存用户上级推广人
  2155. * @param int $uid
  2156. * @param int $spread_uid
  2157. * @return bool
  2158. */
  2159. public function saveUserSpreadUid(int $uid, int $spread_uid)
  2160. {
  2161. if (!$uid || !$spread_uid) {
  2162. return false;
  2163. }
  2164. if ($uid == $spread_uid) {
  2165. throw new ValidateException('上级推广人不能为自己');
  2166. }
  2167. $userInfo = $this->getUserInfo($uid);
  2168. if (!$userInfo) {
  2169. throw new ValidateException('用户不存在');
  2170. }
  2171. //上级已经是这个uid
  2172. if ($userInfo['spread_uid'] == $spread_uid) {
  2173. return true;
  2174. }
  2175. $spreadInfo = $this->getUserInfo($spread_uid);
  2176. if (!$spreadInfo) {
  2177. throw new ValidateException('上级用户不存在');
  2178. }
  2179. if ($spreadInfo['spread_uid'] == $uid) {
  2180. throw new ValidateException('上级推广人不能为自己下级');
  2181. }
  2182. $data = ['spread_uid' => $spread_uid, 'spread_time' => time()];
  2183. $this->dao->update($uid, $data);
  2184. //记录推广绑定关系
  2185. UserSpreadJob::dispatch([$uid, $spread_uid]);
  2186. //记录好友关系
  2187. UserFriendsJob::dispatch([$uid, $spread_uid]);
  2188. return true;
  2189. }
  2190. /**
  2191. * 增加推广用户佣金
  2192. * @param int $uid
  2193. * @param int $spread_uid
  2194. * @param array $userInfo
  2195. * @param array $spread_user
  2196. * @return bool|mixed
  2197. * @throws \think\db\exception\DataNotFoundException
  2198. * @throws \think\db\exception\DbException
  2199. * @throws \think\db\exception\ModelNotFoundException
  2200. */
  2201. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  2202. {
  2203. if (!$uid || !$spread_uid) {
  2204. return false;
  2205. }
  2206. //商城分销功能是否开启 0关闭1开启
  2207. if (!sys_config('brokerage_func_status')) return true;
  2208. //获取设置推广佣金单价
  2209. $brokerage_price = sys_config('uni_brokerage_price', 0);
  2210. //推广佣金是否开启
  2211. if (!sys_config('brokerage_user_status', 0)) {
  2212. return true;
  2213. }
  2214. //获取推广佣金当日限额
  2215. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  2216. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  2217. return true;
  2218. }
  2219. if (!$userInfo) {
  2220. $userInfo = $this->getUserInfo($uid);
  2221. }
  2222. if (!$userInfo) {
  2223. return false;
  2224. }
  2225. if (!$spread_user) {
  2226. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  2227. }
  2228. if (!$spread_user) {
  2229. return false;
  2230. }
  2231. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  2232. return false;
  2233. }
  2234. /** @var UserBrokerageServices $userBrokerageServices */
  2235. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  2236. // -1不限制
  2237. if ($day_brokerage_price_upper != -1) {
  2238. if ($day_brokerage_price_upper <= 0) {
  2239. return true;
  2240. } else {
  2241. //获取上级用户今日获取推广用户佣金
  2242. $spread_day_brokerage = $userBrokerageServices->getUserBillBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  2243. //超过上限
  2244. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  2245. return true;
  2246. }
  2247. }
  2248. }
  2249. $spreadPrice = $spread_user['brokerage_price'];
  2250. // 上级推广员返佣之后的金额
  2251. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  2252. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  2253. // 添加返佣记录
  2254. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  2255. 'nickname' => $userInfo['nickname'],
  2256. 'number' => floatval($brokerage_price)
  2257. ], $balance, $uid);
  2258. // 添加用户余额
  2259. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  2260. //给上级发送获得佣金的模板消息
  2261. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  2262. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  2263. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  2264. return $res1 && $res2;
  2265. });
  2266. }
  2267. /**
  2268. * 获取上级uid
  2269. * @param int $uid
  2270. * @param array $userInfo
  2271. * @param bool $is_spread
  2272. * @return int|mixed
  2273. */
  2274. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  2275. {
  2276. if (!$uid) {
  2277. return 0;
  2278. }
  2279. //商城分销功能是否开启 0关闭1开启
  2280. if (!sys_config('brokerage_func_status')) return -1;
  2281. if (!$userInfo) {
  2282. $userInfo = $this->getUserCacheInfo($uid);
  2283. }
  2284. if (!$userInfo) {
  2285. return 0;
  2286. }
  2287. //上级的上级不需要检测自购
  2288. if ($is_spread) {
  2289. //开启自购
  2290. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  2291. if ($is_self_brokerage && $is_spread) {
  2292. return $uid;
  2293. }
  2294. }
  2295. //绑定类型
  2296. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  2297. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  2298. return $userInfo['spread_uid'];
  2299. }
  2300. //分销绑定类型为时间段且没过期
  2301. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  2302. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  2303. return $userInfo['spread_uid'];
  2304. }
  2305. return -1;
  2306. }
  2307. /**
  2308. * 用户付款code
  2309. * @param int $uid
  2310. * @return bool|mixed|null
  2311. */
  2312. public function getRandCode(int $uid)
  2313. {
  2314. $key = 'user_rand_code' . $uid;
  2315. return CacheService::redisHandler()->remember($key, function () {
  2316. 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);
  2317. }, 600);
  2318. }
  2319. /**
  2320. * 获取barcode
  2321. * @return bool|int|mixed|null
  2322. */
  2323. public function getBarCode()
  2324. {
  2325. mt_srand();
  2326. $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);
  2327. if (!$this->dao->count(['bar_code' => $code])) {
  2328. return $code;
  2329. } else {
  2330. return $this->getBarCode();
  2331. }
  2332. }
  2333. /**
  2334. * 获取用户推广用户列表
  2335. * @param $uid
  2336. * @param $type
  2337. * @return array
  2338. * @throws \think\db\exception\DataNotFoundException
  2339. * @throws \think\db\exception\DbException
  2340. * @throws \think\db\exception\ModelNotFoundException
  2341. */
  2342. public function agentUserList($uid, $type)
  2343. {
  2344. $where['spread_uid'] = $uid;
  2345. if ($type == 1) {
  2346. $where['pay_count'] = -1;
  2347. }
  2348. [$page, $limit] = $this->getPageValue();
  2349. $list = $this->dao->getList($where, 'uid,nickname,avatar,FROM_UNIXTIME(spread_time, \'%Y.%m.%d %H:%m\') as spread_time', $page, $limit);
  2350. $count = $this->dao->count($where);
  2351. return compact('list', 'count');
  2352. }
  2353. /**
  2354. * 个人中心
  2355. * @param $uid
  2356. * @return array|mixed
  2357. */
  2358. public function bankInfo($uid)
  2359. {
  2360. $userService = app()->make(UserServices::class);
  2361. $user = $userService->getUserInfo($uid);
  2362. if (!$user) {
  2363. throw new ValidateException('数据不存在');
  2364. }
  2365. if (!$user['enterprise_professional_facilitator_id']) {
  2366. return [];
  2367. }
  2368. return WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  2369. }
  2370. public function cidUser($rname, $cid, $uid)
  2371. {
  2372. //找出出生年月日
  2373. $time = substr($cid, 6, 4) . '-' . substr($cid, 10, 2) . '-' . substr($cid, 12, 2);
  2374. $brithday = strtotime($time);
  2375. $rust = $this->update($uid, ['real_name' => $rname, 'card_id' => $cid, 'is_auth' => 1, 'off' => '', 'birthday' => $brithday], 'uid');
  2376. if ($rust)
  2377. return true;
  2378. else
  2379. return false;
  2380. }
  2381. }