User.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\user;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\special\Special;
  14. use app\admin\model\special\SpecialSubject;
  15. use app\admin\model\special\SpecialSource;
  16. use app\admin\model\special\SpecialBuy;
  17. use service\FormBuilder as Form;
  18. use service\JsonService;
  19. use think\Db;
  20. use traits\CurdControllerTrait;
  21. use service\JsonService as Json;
  22. use think\Request;
  23. use think\Url;
  24. use app\admin\model\user\User as UserModel;
  25. use app\admin\model\user\UserBill;
  26. use basic\ModelBasic;
  27. use service\HookService;
  28. use behavior\wap\UserBehavior;
  29. use app\admin\model\store\StoreVisit;
  30. use app\admin\model\wechat\WechatMessage;
  31. use app\admin\model\order\StoreOrder;
  32. use app\admin\model\order\DataDownloadOrder;
  33. use app\admin\model\download\DataDownload;
  34. use app\wap\model\store\StoreOrder as StoreOrderModel;
  35. use service\SystemConfigService;
  36. use app\admin\model\user\MemberRecord as MemberRecordModel;
  37. use app\admin\model\user\MemberShip;
  38. use app\admin\model\questions\TestPaperObtain;
  39. use app\admin\model\questions\TestPaper;
  40. use app\admin\model\download\DataDownloadBuy;
  41. use app\admin\model\merchant\Merchant;
  42. /**
  43. * 用户管理控制器
  44. * Class User
  45. * @package app\admin\controller\user
  46. */
  47. class User extends AuthController
  48. {
  49. use CurdControllerTrait;
  50. /**
  51. * 显示资源列表
  52. *
  53. * @return \think\Response
  54. */
  55. public function index()
  56. {
  57. $this->assign(['count_user' => UserModel::count(), 'count_vip' => UserModel::where('level', 1)->count()]);
  58. $this->assign('gold_name', SystemConfigService::get("gold_name"));
  59. return $this->fetch();
  60. }
  61. /**
  62. * 修改user表状态
  63. *
  64. * @return json
  65. */
  66. public function set_status($status = '', $uid = 0, $is_echo = 0)
  67. {
  68. if ($is_echo == 0) {
  69. if ($status == '' || $uid == 0) return Json::fail('参数错误');
  70. UserModel::where(['uid' => $uid])->update(['status' => $status]);
  71. } else {
  72. $uids = parent::postMore([
  73. ['uids', []]
  74. ]);
  75. UserModel::destrSyatus($uids['uids'], $status);
  76. }
  77. return Json::successful($status == 0 ? '禁用成功' : '解禁成功');
  78. }
  79. public function get_member_list()
  80. {
  81. $list = MemberShip::where(['is_publish' => 1, 'is_del' => 0])->field('title,id')->order('sort desc,add_time desc')->select();
  82. $list = count($list) > 0 ? $list->toArray() : [];
  83. return Json::successful($list);
  84. }
  85. /**
  86. * 赠送会员
  87. */
  88. public function members($uid = 0)
  89. {
  90. if (!$uid) return Json::fail('参数错误');
  91. $this->assign('uid', $uid);
  92. return $this->fetch();
  93. }
  94. /**赠送会员
  95. * @param int $uid
  96. */
  97. public function gift_members()
  98. {
  99. $data = parent::postMore([
  100. ['uid', 0],
  101. ['type', 0],
  102. ['day', 0],
  103. ['member', 0],
  104. ]);
  105. if (!$data['uid']) return Json::fail('参数错误');
  106. $uid = $data['uid'];
  107. $userInfo = UserModel::where('uid', $uid)->find();
  108. if (!$userInfo) return Json::fail('用户不存在!');
  109. if ($userInfo['level'] && $userInfo['is_permanent']) return Json::fail('该用户是永久会员,无需续费!');
  110. if (!$data['type']) {
  111. if (!$data['member']) return Json::fail('请选择会员等级');
  112. $member = MemberShip::where(['id' => $data['member'], 'is_publish' => 1, 'is_del' => 0, 'type' => 1])->find();
  113. $orderInfo = [
  114. 'uid' => $uid,
  115. 'order_id' => StoreOrderModel::getNewOrderId(),
  116. 'type' => 1,
  117. 'member_id' => $data['member'],
  118. 'total_num' => 1,
  119. 'total_price' => $member['original_price'],
  120. 'pay_price' => 0,
  121. 'pay_type' => 'yue',
  122. 'combination_id' => 0,
  123. 'is_gift' => 0,
  124. 'pink_time' => 0,
  125. 'paid' => 0,
  126. 'pink_id' => 0,
  127. 'unique' => md5(time() . '' . $uid . $data['member']),
  128. 'cost' => $member['original_price'],
  129. 'link_pay_uid' => 0,
  130. 'spread_uid' => $userInfo['spread_uid'] ? $userInfo['spread_uid'] : 0,
  131. 'is_del' => 0,
  132. ];
  133. $order = StoreOrderModel::set($orderInfo);
  134. if ($order) {
  135. $res1 = UserBill::expend('赠送会员', $uid, 'now_money', 'give_vip', $order['pay_price'], $order['id'], 0, '会员价值' . floatval($order['pay_price']) . '元');
  136. $res2 = StoreOrderModel::payMeSuccess($order['order_id']);
  137. if ($res1 && $res2) {
  138. return Json::successful('赠送成功');
  139. } else {
  140. return Json::fail('赠送失败');
  141. }
  142. } else {
  143. return Json::fail('赠送失败');
  144. }
  145. } else {
  146. if (!$data['day']) return Json::fail('请填写会员天数');
  147. $resMer = MemberShip::setUserCustomMember($data['day'], $userInfo);
  148. if ($resMer) {
  149. return Json::successful('赠送成功');
  150. } else {
  151. return Json::fail('赠送失败');
  152. }
  153. }
  154. }
  155. /**
  156. * 清除会员等级
  157. */
  158. public function del_vip($uid = 0)
  159. {
  160. if (!$uid) return Json::fail('参数错误!');
  161. $user = UserModel::where('uid', $uid)->where('level', 1)->find();
  162. if (!$user) return Json::fail('用户不存在或不是会员!');
  163. $res = UserModel::edit(['level' => 0, 'is_permanent' => 0, 'overdue_time' => 0, 'member_time' => 0], $uid, 'uid');
  164. if ($res) {
  165. return Json::successful('清除会员成功');
  166. } else {
  167. return Json::fail('清除会员失败');
  168. }
  169. }
  170. /**设置用户的推广人
  171. * @param string $uid
  172. */
  173. public function add_superior($uid = '')
  174. {
  175. if (!$uid) return Json::fail('参数错误!');
  176. $form = Form::create(Url::build('set_superior', ['uid' => $uid]), [
  177. Form::input('spread_uid', '推广人uid')
  178. ]);
  179. $form->setMethod('post')->setTitle('设置用户的推广人')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  180. $this->assign(compact('form'));
  181. return $this->fetch('public/form-builder');
  182. }
  183. public function set_superior($uid = '')
  184. {
  185. if (!$uid) return Json::fail('参数错误');
  186. $data = parent::postMore([
  187. ['spread_uid', 0],
  188. ]);
  189. if (!$data['spread_uid']) return Json::fail('请填写推广人uid');
  190. $userInfo = UserModel::where('uid', $data['spread_uid'])->find();
  191. if (!$userInfo) return Json::fail('推广人不存在');
  192. $uids = explode(',', $uid);
  193. if (in_array($data['spread_uid'], $uids)) {
  194. $key = array_search($data['spread_uid'], $uids);
  195. unset($uids[$key]);
  196. }
  197. $res = UserModel::where('uid', 'in', $uids)->update(['spread_uid' => $data['spread_uid']]);
  198. if ($res) {
  199. return Json::successful('设置用户的推广人成功');
  200. } else {
  201. return Json::fail('设置用户的推广人失败');
  202. }
  203. }
  204. /**发送试卷
  205. * @param int $uid
  206. * @return mixed
  207. */
  208. public function send($uid = 0)
  209. {
  210. $mer_list = Merchant::getMerchantList();
  211. $this->assign(['uid' => $uid,'mer_list' => $mer_list]);
  212. return $this->fetch();
  213. }
  214. /**发送提交
  215. * @param $uid
  216. * @param $tid
  217. */
  218. public function sendTestPaper($uid, $tid)
  219. {
  220. $tid = explode(',', $tid);
  221. $res = TestPaperObtain::addUidSend($uid, $tid);
  222. if ($res) {
  223. return Json::successful('发送成功');
  224. } else {
  225. return Json::fail('发送失败');
  226. }
  227. }
  228. /**赠送专题
  229. * @param int $uid
  230. * @return mixed
  231. */
  232. public function relation($uid = 0)
  233. {
  234. $mer_list = Merchant::getMerchantList();
  235. $this->assign(['uid' => $uid, 'mer_list' => $mer_list]);
  236. return $this->fetch();
  237. }
  238. /**
  239. * 图文、音频、视频、专栏专题素材列表获取 关联
  240. * @return json
  241. * */
  242. public function get_relation_source_list()
  243. {
  244. $where = parent::getMore([
  245. ['page', 1],
  246. ['is_show', 1],
  247. ['limit', 20],
  248. ['title', ''],
  249. ['pid', ''],
  250. ['type', ''],
  251. ['mer_id', 0],
  252. ['special_type', 0],
  253. ]);
  254. $special_source = [];
  255. $special_task = Special::getRelationList($where, $special_source);
  256. if (isset($special_task['data']) && $special_task['data']) {
  257. foreach ($special_task['data'] as $k => $v) {
  258. $special_task['data'][$k]['is_check'] = 0;
  259. $special_task['data'][$k]['pay_status'] = PAY_MONEY;
  260. if ($v['type'] == 6) $v['type'] = $v['light_type'];
  261. $special_task['data'][$k]['types'] = parent::specialTaskType($v['type']);
  262. }
  263. }
  264. return Json::successlayui($special_task);
  265. }
  266. public function user_data($uid = 0)
  267. {
  268. $spread = UserModel::where(['spread_uid' => $uid])->column('uid');
  269. $count['pay_count'] = SpecialBuy::where(['uid' => $uid, 'is_del' => 0])->group('special_id')->count();
  270. $count['bill_count'] = UserBill::where(['category' => 'now_money'])->where('uid', $uid)
  271. ->where('type', 'in', ['extract'])
  272. ->count();
  273. $count['order_count'] = UserBill::where('u.uid', $uid)->alias('u')->join('__STORE_ORDER__ a', 'a.id=u.link_id')
  274. ->where('u.category', 'now_money')->where('u.type', 'in', ['brokerage'])
  275. ->where(['a.paid' => 1, 'a.is_gift' => 0, 'a.is_receive_gift' => 0])->count();
  276. $count['spread_count'] = UserModel::where('uid', 'in', $spread)->count();
  277. $this->assign('gradeList', json_encode(SpecialSubject::specialCategoryAll(1)));
  278. $this->assign('uid', $uid);
  279. $this->assign('count', json_encode($count));
  280. return $this->fetch();
  281. }
  282. public function member_record($uid = 0)
  283. {
  284. if (!$uid) return Json::fail('缺少参数');
  285. $this->assign('uid', $uid);
  286. return $this->fetch();
  287. }
  288. public function get_member_record($uid)
  289. {
  290. $data = MemberRecordModel::userOneRecord($uid);
  291. return Json::successlayui($data);
  292. }
  293. public function get_subjec_list($grade_id = 0)
  294. {
  295. return Json::successful(SpecialSubject::where(['grade_id' => $grade_id, 'is_show' => 1, 'is_del' => 0])->order('sort desc,add_time desc')->field('id,name')->select());
  296. }
  297. public function get_special_list($subjec_id = 0)
  298. {
  299. return Json::successful(Special::PreWhere()->where('subject_id', $subjec_id)->order('sort desc,add_time desc')->field('id,title')->select());
  300. }
  301. /**赠送专题
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. * @throws \think\exception\DbException
  305. */
  306. public function save_give()
  307. {
  308. $data = parent::getMore([
  309. ['uid', 0],
  310. ['special_id', 0],
  311. ]);
  312. if (!$data['uid']) return Json::fail('请选择被赠送的用户');
  313. if (!$data['special_id']) return Json::fail('请选择要赠送的专题');
  314. $special = Special::PreWhere()->where(['id' => $data['special_id']])->find();
  315. if (SpecialBuy::be(['uid' => $data['uid'], 'special_id' => $data['special_id'], 'is_del' => 0, 'type' => 3])) return Json::fail('此用户已经拥有此专题无需赠送');
  316. if ($special['type'] == SPECIAL_COLUMN) {
  317. $special_source = SpecialSource::getSpecialSource($special['id']);
  318. if ($special_source) {
  319. foreach ($special_source as $k => $v) {
  320. $task_special = Special::PreWhere()->where(['id' => $v['source_id']])->find();
  321. if ($task_special['is_show'] == 1) {
  322. SpecialBuy::setBuySpecial('', $data['uid'], $v['source_id'], 3, $task_special['validity'], $data['special_id']);
  323. }
  324. }
  325. }
  326. }
  327. $res = SpecialBuy::setBuySpecial('', $data['uid'], $data['special_id'], 3, $special['validity']);
  328. if ($res) {
  329. TestPaperObtain::setTestPaper('', $data['uid'], $data['special_id'], 3);
  330. DataDownloadBuy::setDataDownload('', $data['uid'], $data['special_id'], 2);
  331. return Json::successful('赠送成功');
  332. } else
  333. return Json::fail('赠送失败');
  334. }
  335. public function get_user_info($uid = 0)
  336. {
  337. if (!$uid) return Json::fail('缺少用户参数');
  338. return Json::successful(UserModel::getUserinfoV1($uid));
  339. }
  340. public function get_pay_list()
  341. {
  342. $where = parent::getMore([
  343. ['uid', 0],
  344. ['limit', 10],
  345. ['page', 1],
  346. ]);
  347. return Json::successful(SpecialBuy::getPayList($where));
  348. }
  349. public function get_spread_list()
  350. {
  351. $where = parent::getMore([
  352. ['uid', 0],
  353. ['limit', 10],
  354. ['page', 1],
  355. ]);
  356. return Json::successful(UserModel::getSpreadListV1($where));
  357. }
  358. public function get_order_list()
  359. {
  360. $where = parent::getMore([
  361. ['uid', 0],
  362. ['limit', 10],
  363. ['page', 1],
  364. ['excel', 0],
  365. ['start_date', ''],
  366. ['end_date', ''],
  367. ]);
  368. return Json::successful(StoreOrder::getOrderList($where));
  369. }
  370. public function get_bill_list()
  371. {
  372. $where = parent::getMore([
  373. ['limit', 10],
  374. ['page', 1],
  375. ['uid', 0],
  376. ['excel', 0],
  377. ['start_date', ''],
  378. ['end_date', ''],
  379. ]);
  380. return Json::successful(UserBill::getBillList($where, $where['uid']));
  381. }
  382. public function update_user_spread($uid = 0, $type = 0)
  383. {
  384. if (!$uid || !$type) return Json::fail('缺少参数');
  385. $user = UserModel::get($uid);
  386. switch ($type) {
  387. case '1':
  388. case "2":
  389. case "3":
  390. case '4':
  391. $user->is_promoter = (int)$type;
  392. break;
  393. case "5":
  394. $user->is_promoter = 1;
  395. $user->spread_uid = 0;
  396. break;
  397. }
  398. if ($user->save())
  399. return Json::successful('修改成功');
  400. else
  401. return Json::fail('修改失败');
  402. }
  403. /**
  404. * 获取user表
  405. * @return json
  406. */
  407. public function get_user_list()
  408. {
  409. $where = parent::getMore([
  410. ['page', 1],
  411. ['limit', 20],
  412. ['nickname', ''],
  413. ['status', ''],
  414. ['pay_count', ''],
  415. ['is_promoter', ''],
  416. ['order', ''],
  417. ['data', ''],
  418. ['country', ''],
  419. ['province', ''],
  420. ['city', ''],
  421. ['user_time_type', ''],
  422. ['user_time', ''],
  423. ]);
  424. return Json::successlayui(UserModel::getUserList($where));
  425. }
  426. /**
  427. * 编辑模板消息
  428. * @param $id
  429. * @return mixed|\think\response\Json|void
  430. */
  431. public function edit($uid)
  432. {
  433. if (!$uid) return $this->failed('数据不存在');
  434. $user = UserModel::get($uid);
  435. if (!$user) return Json::fail('数据不存在!');
  436. $f = array();
  437. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(1);
  438. $f[] = Form::input('nickname', '用户姓名', $user->getData('nickname'));
  439. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  440. $f[] = Form::number('nowMoney', '当前余额', $user->getData('now_money'))->disabled(1);
  441. $f[] = Form::number('money', '金额')->min(0);
  442. $f[] = Form::radio('is_promoter', '推广员', $user->getData('is_promoter'))->options([
  443. ['value' => 0, 'label' => '关闭'],
  444. ['value' => 1, 'label' => '推广员'],
  445. ]);
  446. $f[] = Form::radio('is_write_off', '核销员', $user->getData('is_write_off'))->options([
  447. ['value' => 0, 'label' => '关闭'],
  448. ['value' => 1, 'label' => '开启'],
  449. ]);
  450. $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  451. $form = Form::make_post_form('添加用户通知', $f, Url::build('update', array('id' => $uid)), 2);
  452. $this->assign(compact('form'));
  453. return $this->fetch('public/form-builder');
  454. }
  455. public function update(Request $request, $uid)
  456. {
  457. $data = parent::postMore([
  458. ['money_status', 0],
  459. ['is_promoter', 1],
  460. ['is_write_off', 1],
  461. ['is_senior', 0],
  462. ['money', 0],
  463. ['nickname', ''],
  464. ['integration_status', 0],
  465. ['integration', 0],
  466. ['status', 0],
  467. ], $request);
  468. if (!$uid) return $this->failed('数据不存在');
  469. $user = UserModel::get($uid);
  470. if (!$user) return Json::fail('数据不存在!');
  471. ModelBasic::beginTrans();
  472. $res1 = false;
  473. $res2 = false;
  474. $edit = array();
  475. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  476. if ($data['money_status'] == 1) {//增加
  477. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  478. $res1 = UserBill::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  479. try {
  480. HookService::listen('admin_add_money', $user, $data['money'], false, UserBehavior::class);
  481. } catch (\Exception $e) {
  482. ModelBasic::rollbackTrans();
  483. return Json::fail($e->getMessage());
  484. }
  485. } else if ($data['money_status'] == 2) {//减少
  486. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  487. $res1 = UserBill::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  488. try {
  489. HookService::listen('admin_sub_money', $user, $data['money'], false, UserBehavior::class);
  490. } catch (\Exception $e) {
  491. ModelBasic::rollbackTrans();
  492. return Json::fail($e->getMessage());
  493. }
  494. }
  495. } else {
  496. $res1 = true;
  497. }
  498. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  499. if ($data['integration_status'] == 1) {//增加
  500. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  501. $res2 = UserBill::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $user['integral'], '系统增加了' . floatval($data['integration']) . '积分');
  502. try {
  503. HookService::listen('admin_add_integral', $user, $data['integration'], false, UserBehavior::class);
  504. } catch (\Exception $e) {
  505. ModelBasic::rollbackTrans();
  506. return Json::fail($e->getMessage());
  507. }
  508. } else if ($data['integration_status'] == 2) {//减少
  509. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  510. $res2 = UserBill::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $user['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
  511. try {
  512. HookService::listen('admin_sub_integral', $user, $data['integration'], false, UserBehavior::class);
  513. } catch (\Exception $e) {
  514. ModelBasic::rollbackTrans();
  515. return Json::fail($e->getMessage());
  516. }
  517. }
  518. } else {
  519. $res2 = true;
  520. }
  521. $edit['status'] = $data['status'];
  522. $edit['nickname'] = $data['nickname'];
  523. $edit['is_promoter'] = $data['is_promoter'];
  524. $edit['is_write_off'] = $data['is_write_off'];
  525. $edit['is_senior'] = $data['is_senior'];
  526. if ($edit) $res3 = UserModel::edit($edit, $uid);
  527. else $res3 = true;
  528. if ($res1 && $res2 && $res3) $res = true;
  529. else $res = false;
  530. ModelBasic::checkTrans($res);
  531. if ($res) return Json::successful('修改成功!');
  532. else return Json::fail('修改失败');
  533. }
  534. /**
  535. * 用户图表
  536. * @return mixed
  537. */
  538. public function user_analysis()
  539. {
  540. $where = parent::getMore([
  541. ['nickname', ''],
  542. ['status', ''],
  543. ['is_promoter', ''],
  544. ['date', ''],
  545. ['export', 0]
  546. ], $this->request);
  547. $user_count = UserModel::consume($where, '', true);
  548. //头部信息
  549. $header = [
  550. [
  551. 'name' => '新增用户',
  552. 'class' => 'fa-line-chart',
  553. 'value' => $user_count,
  554. 'color' => 'red'
  555. ],
  556. [
  557. 'name' => '用户留存',
  558. 'class' => 'fa-area-chart',
  559. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true), $where) . '%',
  560. 'color' => 'lazur'
  561. ],
  562. [
  563. 'name' => '新增用户总消费',
  564. 'class' => 'fa-bar-chart',
  565. 'value' => '¥' . UserModel::consume($where),
  566. 'color' => 'navy'
  567. ],
  568. [
  569. 'name' => '用户活跃度',
  570. 'class' => 'fa-pie-chart',
  571. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true)) . '%',
  572. 'color' => 'yellow'
  573. ],
  574. ];
  575. $name = ['新增用户', '用户消费'];
  576. $dates = $this->get_user_index($where, $name);
  577. $user_index = ['name' => json_encode($name), 'date' => json_encode($dates['time']), 'series' => json_encode($dates['series'])];
  578. //用户浏览分析
  579. $view = StoreVisit::getVisit($where['date'], ['', 'warning', 'info', 'danger']);
  580. $view_v1 = WechatMessage::getViweList($where['date'], ['', 'warning', 'info', 'danger']);
  581. $view = array_merge($view, $view_v1);
  582. $view_v2 = [];
  583. foreach ($view as $val) {
  584. $view_v2['color'][] = '#' . rand(100000, 339899);
  585. $view_v2['name'][] = $val['name'];
  586. $view_v2['value'][] = $val['value'];
  587. }
  588. $view = $view_v2;
  589. //消费会员排行用户分析
  590. $user_null = UserModel::getUserSpend($where['date']);
  591. //消费数据
  592. $now_number = UserModel::getUserSpend($where['date'], true);
  593. list($paren_number, $title) = UserModel::getPostNumber($where['date']);
  594. if ($paren_number == 0) {
  595. $rightTitle = [
  596. 'number' => $now_number > 0 ? $now_number : 0,
  597. 'icon' => 'fa-level-up',
  598. 'title' => $title
  599. ];
  600. } else {
  601. $number = (float)bcsub($now_number, $paren_number, 4);
  602. if ($now_number == 0) {
  603. $icon = 'fa-level-down';
  604. } else {
  605. $icon = $now_number > $paren_number ? 'fa-level-up' : 'fa-level-down';
  606. }
  607. $rightTitle = ['number' => $number, 'icon' => $icon, 'title' => $title];
  608. }
  609. unset($title, $paren_number, $now_number);
  610. list($paren_user_count, $title) = UserModel::getPostNumber($where['date'], true, 'add_time', '');
  611. if ($paren_user_count == 0) {
  612. $count = $user_count == 0 ? 0 : $user_count;
  613. $icon = $user_count == 0 ? 'fa-level-down' : 'fa-level-up';
  614. } else {
  615. $count = (float)bcsub($user_count, $paren_user_count, 4);
  616. $icon = $user_count < $paren_user_count ? 'fa-level-down' : 'fa-level-up';
  617. }
  618. $leftTitle = [
  619. 'count' => $count,
  620. 'icon' => $icon,
  621. 'title' => $title
  622. ];
  623. unset($count, $icon, $title);
  624. $consume = [
  625. 'title' => '消费金额为¥' . UserModel::consume($where),
  626. 'series' => UserModel::consume($where, 'xiaofei'),
  627. 'rightTitle' => $rightTitle,
  628. 'leftTitle' => $leftTitle,
  629. ];
  630. $form = UserModel::consume($where, 'form');
  631. $grouping = UserModel::consume($where, 'grouping');
  632. $this->assign(compact('header', 'user_index', 'view', 'user_null', 'consume', 'form', 'grouping', 'where'));
  633. return $this->fetch();
  634. }
  635. public function gethreaderValue($chart, $where = [])
  636. {
  637. if ($where) {
  638. switch ($where['date']) {
  639. case null:
  640. case 'today':
  641. case 'week':
  642. case 'year':
  643. if ($where['date'] == null) {
  644. $where['date'] = 'month';
  645. }
  646. $sum_user = UserModel::whereTime('add_time', $where['date'])->count();
  647. if ($sum_user == 0) return 0;
  648. $counts = bcdiv($chart, $sum_user, 4) * 100;
  649. return $counts;
  650. break;
  651. case 'quarter':
  652. $quarter = UserModel::getMonth('n');
  653. $quarter[0] = strtotime($quarter[0]);
  654. $quarter[1] = strtotime($quarter[1]);
  655. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  656. if ($sum_user == 0) return 0;
  657. $counts = bcdiv($chart, $sum_user, 4) * 100;
  658. return $counts;
  659. default:
  660. //自定义时间
  661. $quarter = explode('-', $where['date']);
  662. $quarter[0] = strtotime($quarter[0]);
  663. $quarter[1] = strtotime($quarter[1]);
  664. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  665. if ($sum_user == 0) return 0;
  666. $counts = bcdiv($chart, $sum_user, 4) * 100;
  667. return $counts;
  668. break;
  669. }
  670. } else {
  671. $num = UserModel::count();
  672. $chart = $num != 0 ? bcdiv($chart, $num, 5) * 100 : 0;
  673. return $chart;
  674. }
  675. }
  676. public function get_user_index($where, $name)
  677. {
  678. switch ($where['date']) {
  679. case null:
  680. $days = date("t", strtotime(date('Y-m', time())));
  681. $dates = [];
  682. $series = [];
  683. $times_list = [];
  684. foreach ($name as $key => $val) {
  685. for ($i = 1; $i <= $days; $i++) {
  686. if (!in_array($i . '号', $times_list)) {
  687. array_push($times_list, $i . '号');
  688. }
  689. $time = $this->gettime(date("Y-m", time()) . '-' . $i);
  690. if ($key == 0) {
  691. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  692. } else if ($key == 1) {
  693. $dates['data'][] = UserModel::consume(true, $time);
  694. }
  695. }
  696. $dates['name'] = $val;
  697. $dates['type'] = 'line';
  698. $series[] = $dates;
  699. unset($dates);
  700. }
  701. return ['time' => $times_list, 'series' => $series];
  702. case 'today':
  703. $dates = [];
  704. $series = [];
  705. $times_list = [];
  706. foreach ($name as $key => $val) {
  707. for ($i = 0; $i <= 24; $i++) {
  708. $strtitle = $i . '点';
  709. if (!in_array($strtitle, $times_list)) {
  710. array_push($times_list, $strtitle);
  711. }
  712. $time = $this->gettime(date("Y-m-d ", time()) . $i);
  713. if ($key == 0) {
  714. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  715. } else if ($key == 1) {
  716. $dates['data'][] = UserModel::consume(true, $time);
  717. }
  718. }
  719. $dates['name'] = $val;
  720. $dates['type'] = 'line';
  721. $series[] = $dates;
  722. unset($dates);
  723. }
  724. return ['time' => $times_list, 'series' => $series];
  725. case "week":
  726. $dates = [];
  727. $series = [];
  728. $times_list = [];
  729. foreach ($name as $key => $val) {
  730. for ($i = 0; $i <= 6; $i++) {
  731. if (!in_array('星期' . ($i + 1), $times_list)) {
  732. array_push($times_list, '星期' . ($i + 1));
  733. }
  734. $time = UserModel::getMonth('h', $i);
  735. if ($key == 0) {
  736. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($time[0]), strtotime($time[1])])->count();
  737. } else if ($key == 1) {
  738. $dates['data'][] = UserModel::consume(true, [strtotime($time[0]), strtotime($time[1])]);
  739. }
  740. }
  741. $dates['name'] = $val;
  742. $dates['type'] = 'line';
  743. $series[] = $dates;
  744. unset($dates);
  745. }
  746. return ['time' => $times_list, 'series' => $series];
  747. case 'year':
  748. $dates = [];
  749. $series = [];
  750. $times_list = [];
  751. $year = date('Y');
  752. foreach ($name as $key => $val) {
  753. for ($i = 1; $i <= 12; $i++) {
  754. if (!in_array($i . '月', $times_list)) {
  755. array_push($times_list, $i . '月');
  756. }
  757. $t = strtotime($year . '-' . $i . '-01');
  758. $arr = explode('/', date('Y-m-01', $t) . '/' . date('Y-m-', $t) . date('t', $t));
  759. if ($key == 0) {
  760. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  761. } else if ($key == 1) {
  762. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  763. }
  764. }
  765. $dates['name'] = $val;
  766. $dates['type'] = 'line';
  767. $series[] = $dates;
  768. unset($dates);
  769. }
  770. return ['time' => $times_list, 'series' => $series];
  771. case 'quarter':
  772. $dates = [];
  773. $series = [];
  774. $times_list = [];
  775. foreach ($name as $key => $val) {
  776. for ($i = 1; $i <= 4; $i++) {
  777. $arr = $this->gettime('quarter', $i);
  778. if (!in_array(implode('--', $arr) . '季度', $times_list)) {
  779. array_push($times_list, implode('--', $arr) . '季度');
  780. }
  781. if ($key == 0) {
  782. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  783. } else if ($key == 1) {
  784. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  785. }
  786. }
  787. $dates['name'] = $val;
  788. $dates['type'] = 'line';
  789. $series[] = $dates;
  790. unset($dates);
  791. }
  792. return ['time' => $times_list, 'series' => $series];
  793. default:
  794. $list = UserModel::consume($where, 'default');
  795. $dates = [];
  796. $series = [];
  797. $times_list = [];
  798. foreach ($name as $k => $v) {
  799. foreach ($list as $val) {
  800. $date = $val['add_time'];
  801. if (!in_array($date, $times_list)) {
  802. array_push($times_list, $date);
  803. }
  804. if ($k == 0) {
  805. $dates['data'][] = $val['num'];
  806. } else if ($k == 1) {
  807. $dates['data'][] = UserBill::where(['uid' => $val['uid'], 'type' => 'pay_product'])->sum('number');
  808. }
  809. }
  810. $dates['name'] = $v;
  811. $dates['type'] = 'line';
  812. $series[] = $dates;
  813. unset($dates);
  814. }
  815. return ['time' => $times_list, 'series' => $series];
  816. }
  817. }
  818. public function gettime($time = '', $season = '')
  819. {
  820. if (!empty($time) && empty($season)) {
  821. $timestamp0 = strtotime($time);
  822. $timestamp24 = strtotime($time) + 86400;
  823. return [$timestamp0, $timestamp24];
  824. } else if (!empty($time) && !empty($season)) {
  825. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  826. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  827. return [$firstday, $lastday];
  828. }
  829. }
  830. /**
  831. * 会员等级首页
  832. */
  833. public function group()
  834. {
  835. return $this->fetch();
  836. }
  837. public function access($uid = 0)
  838. {
  839. $this->assign([
  840. 'uid' => $uid,
  841. 'count' => UserModel::getCountData($uid),
  842. ]);
  843. return $this->fetch();
  844. }
  845. public function getUserBuySpecilList($uid, $page = 1, $limit = 20)
  846. {
  847. $list = SpecialBuy::where(['a.uid' => $uid, 'a.is_del' => 0])
  848. ->join("__SPECIAL__ s", 's.id=a.special_id')->order('a.add_time desc')
  849. ->alias('a')->field(['a.*', 's.title'])->page((int)$page, (int)$limit)->select();
  850. $list = count($list) ? $list->toArray() : [];
  851. return Json::successful($list);
  852. }
  853. public function getUserBuyDataList($uid, $page = 1, $limit = 20)
  854. {
  855. $list = DataDownloadBuy::where(['a.uid' => $uid, 'a.is_del' => 0])
  856. ->join("DataDownload d", 'd.id=a.data_id', 'left')->order('a.add_time desc')
  857. ->alias('a')->field(['a.*', 'd.title'])
  858. ->page((int)$page, (int)$limit)->select();
  859. $list = count($list) ? $list->toArray() : [];
  860. return Json::successful($list);
  861. }
  862. public function getUserBuyTestPaperList($uid, $page = 1, $limit = 20)
  863. {
  864. $list = TestPaperObtain::where(['a.uid' => $uid, 'a.is_del' => 0])
  865. ->join("TestPaper t", 't.id=a.test_id', 'left')->order('a.add_time desc')
  866. ->alias('a')->field(['a.*', 't.title'])
  867. ->page((int)$page, (int)$limit)->select();
  868. $list = count($list) ? $list->toArray() : [];
  869. return Json::successful($list);
  870. }
  871. public function del_data_buy($id = 0)
  872. {
  873. if ($id == 0) return Json::fail('缺少参数');
  874. $dataBuy = DataDownloadBuy::get($id);
  875. if (!$dataBuy) return Json::fail('记录不存在');
  876. $res = DataDownloadBuy::where('id', $id)->update(['is_del' => 1]);
  877. if ($res) {
  878. return Json::successful('删除成功');
  879. } else
  880. return Json::fail('删除失败');
  881. }
  882. public function del_test_buy($id = 0)
  883. {
  884. if ($id == 0) return Json::fail('缺少参数');
  885. $testBuy = TestPaperObtain::get($id);
  886. if (!$testBuy) return Json::fail('记录不存在');
  887. $res = TestPaperObtain::where('id', $id)->update(['is_del' => 1]);
  888. if ($res) {
  889. return Json::successful('删除成功');
  890. } else
  891. return Json::fail('删除失败');
  892. }
  893. public function get_order_buy_list($uid, $page = 1, $limit = 20)
  894. {
  895. $data = SpecialBuy::getOrderPayList($uid, $page, $limit);
  896. return JsonService::successful($data);
  897. }
  898. /**
  899. * 会员详情
  900. */
  901. public function see($uid = '')
  902. {
  903. $this->assign([
  904. 'uid' => $uid,
  905. 'userinfo' => UserModel::getUserDetailed($uid),
  906. 'is_layui' => true,
  907. 'headerList' => UserModel::getHeaderList($uid),
  908. 'count' => UserModel::getCountInfo($uid),
  909. 'gold_name' => SystemConfigService::get("gold_name")
  910. ]);
  911. return $this->fetch();
  912. }
  913. public function getBuySpecilList($uid, $page = 1, $limit = 20)
  914. {
  915. $list = SpecialBuy::where(['a.uid' => $uid, 'a.is_del' => 0])
  916. ->join('__STORE_ORDER__ o', 'a.order_id=o.order_id', 'left')
  917. ->join("__SPECIAL__ s", 's.id=a.special_id')->order('a.add_time desc')
  918. ->alias('a')->field(['a.*', 'o.total_num', 'o.pay_price', 's.title'])->page((int)$page, (int)$limit)->select();
  919. $list = count($list) ? $list->toArray() : [];
  920. foreach ($list as &$value) {
  921. if ($value['type'] == '赠送获得') {
  922. $value['order_id'] = '赠送获得';
  923. $value['total_num'] = '1';
  924. $value['pay_price'] = '0';
  925. } else if ($value['type'] == '买商品赠送') {
  926. $value['order_id'] = '买商品赠送';
  927. $value['total_num'] = '1';
  928. $value['pay_price'] = '0';
  929. } else if ($value['type'] == '兑换获得') {
  930. $value['order_id'] = '兑换获得';
  931. $value['total_num'] = '1';
  932. $value['pay_price'] = '0';
  933. }
  934. }
  935. return Json::successful($list);
  936. }
  937. public function del_special_buy($id = 0)
  938. {
  939. if ($id == 0) return Json::fail('缺少参数');
  940. $specialBuy = SpecialBuy::get($id);
  941. if (!$specialBuy) return Json::fail('记录不存在');
  942. $special = Special::get($specialBuy['special_id']);
  943. $res = SpecialBuy::where('id', $id)->update(['is_del' => 1]);
  944. if ($special['type'] == SPECIAL_COLUMN) {
  945. $res2 = SpecialBuy::where(['uid' => $specialBuy['uid'], 'type' => $specialBuy['type'], 'column_id' => $specialBuy['special_id'], 'is_del' => 0])->update(['is_del' => 1]);
  946. $res = $res && $res2;
  947. }
  948. if ($res) {
  949. switch ($specialBuy['type']) {
  950. case '支付获得':
  951. case '拼团获得':
  952. case '领取礼物获得':
  953. case '兑换获得':
  954. $source = 2;
  955. $type = 1;
  956. break;
  957. case '赠送获得':
  958. case '买商品赠送':
  959. $source = 3;
  960. $type = 2;
  961. break;
  962. }
  963. TestPaperObtain::delTestPaper($specialBuy['order_id'], $specialBuy['uid'], $specialBuy['special_id'], $source);
  964. DataDownloadBuy::delDataDownload($specialBuy['order_id'], $specialBuy['uid'], $specialBuy['special_id'], $type);
  965. return Json::successful('删除成功');
  966. } else
  967. return Json::fail('删除失败');
  968. }
  969. /**
  970. * 获取某个用户的推广下线
  971. * */
  972. public function getSpreadList($uid, $page = 1, $limit = 20)
  973. {
  974. return Json::successful(UserModel::getSpreadList($uid, (int)$page, (int)$limit));
  975. }
  976. /**
  977. * 获取某用户的订单列表
  978. */
  979. public function getOneorderList($uid, $page = 1, $limit = 20)
  980. {
  981. return Json::successful(StoreOrder::getOneorderList(compact('uid', 'page', 'limit')));
  982. }
  983. /**
  984. * 获取某用户的签到列表
  985. */
  986. public function getOneSignList($uid, $page = 1, $limit = 20)
  987. {
  988. return Json::successful(UserBill::getOneSignList(compact('uid', 'page', 'limit')));
  989. }
  990. /**
  991. * 获取某用户的余额变动记录
  992. */
  993. public function getOneBalanceChangList($uid, $page = 1, $limit = 20)
  994. {
  995. return Json::successful(UserBill::getOneBalanceChangList(compact('uid', 'page', 'limit')));
  996. }
  997. }