User.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\user;
  8. use app\admin\controller\AuthController;
  9. use crmeb\repositories\UserRepository;
  10. use crmeb\traits\CurdControllerTrait;
  11. use think\facade\Route as Url;
  12. use crmeb\basic\BaseModel;
  13. use app\models\user\UserLevel as Level;
  14. use app\admin\model\order\StoreOrder;
  15. use app\admin\model\wechat\WechatMessage;
  16. use app\admin\model\store\{StoreVisit, StoreCouponUser};
  17. use app\admin\model\system\{SystemUserLevel, SystemUserTask};
  18. use crmeb\services\{FormBuilder as Form, UtilService as Util, JsonService as Json};
  19. use app\admin\model\user\{User as UserModel, UserBill as UserBillAdmin, UserLevel, UserGroup, UserTaskFinish};
  20. /**
  21. * 用户管理控制器
  22. * Class User
  23. * @package app\admin\controller\user
  24. */
  25. class User extends AuthController
  26. {
  27. use CurdControllerTrait;
  28. /**
  29. * 显示资源列表
  30. *
  31. * @return \think\Response
  32. */
  33. public function index()
  34. {
  35. $level = SystemUserLevel::where('is_del', 0)->where('is_show', 1)->order('grade asc')->field(['id', 'name'])->select();
  36. $group = UserGroup::select();
  37. $this->assign(compact('group'));
  38. $this->assign(compact('level'));
  39. $this->assign('count_user', UserModel::getcount());
  40. return $this->fetch();
  41. }
  42. /**
  43. * 设置分组
  44. * @param int $uid
  45. */
  46. public function set_group($uid = 0)
  47. {
  48. if (!$uid) return $this->failed('缺少参数');
  49. $userGroup = UserGroup::select();
  50. $field[] = Form::select('group_id', '会员分组')->setOptions(function () use ($userGroup) {
  51. $menus = [];
  52. foreach ($userGroup as $menu) {
  53. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  54. }
  55. return $menus;
  56. })->filterable(1);
  57. $form = Form::make_post_form('设置分组', $field, Url::buildUrl('save_set_group', ['uid' => $uid]), 2);
  58. $this->assign(compact('form'));
  59. return $this->fetch('public/form-builder');
  60. }
  61. public function save_set_group($uid = 0)
  62. {
  63. if (!$uid) return Json::fail('缺少参数');
  64. list($group_id) = Util::postMore([
  65. ['group_id', 0],
  66. ], $this->request, true);
  67. $uids = explode(',',$uid);
  68. $res = UserModel::whereIn('uid', $uids)->update(['group_id' => $group_id]);
  69. if ($res) {
  70. return Json::successful('设置成功');
  71. } else {
  72. return Json::successful('设置失败');
  73. }
  74. }
  75. /**
  76. * 赠送会员等级
  77. * @param int $uid
  78. * @return string|void
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function give_level($uid = 0)
  84. {
  85. if (!$uid) return $this->failed('缺少参数');
  86. $level = Level::getUserLevel($uid);
  87. //获取当前会员等级
  88. if ($level === false)
  89. $grade = 0;
  90. else
  91. $grade = Level::getUserLevelInfo($level, 'grade');
  92. //查询高于当前会员的所有会员等级
  93. $systemLevelList = SystemUserLevel::where('grade', '>', $grade)->where(['is_show' => 1, 'is_del' => 0])->field(['name', 'id'])->select();
  94. $field[] = Form::select('level_id', '会员等级')->setOptions(function () use ($systemLevelList) {
  95. $menus = [];
  96. foreach ($systemLevelList as $menu) {
  97. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  98. }
  99. return $menus;
  100. })->filterable(1);
  101. $form = Form::make_post_form('赠送会员', $field, Url::buildUrl('save_give_level', ['uid' => $uid]), 2);
  102. $this->assign(compact('form'));
  103. return $this->fetch('public/form-builder');
  104. }
  105. public function edit_other($uid)
  106. {
  107. if (!$uid) return $this->failed('数据不存在');
  108. $user = UserModel::get($uid);
  109. if (!$user) return Json::fail('数据不存在!');
  110. $f = array();
  111. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  112. $f[] = Form::number('money', '余额')->min(0);
  113. $f[] = Form::radio('integration_status', '修改趣豆', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  114. $f[] = Form::number('integration', '趣豆')->min(0);
  115. $f[] = Form::radio('anticipate_status', '修改广告值', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  116. $f[] = Form::number('anticipate', '广告值')->min(0);
  117. $f[] = Form::radio('shop_integral_status', '修改购物券', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  118. $f[] = Form::number('shop_integral', '购物券')->min(0);
  119. $form = Form::make_post_form('修改其他', $f, Url::buildUrl('update_other', array('uid' => $uid)));
  120. $this->assign(compact('form'));
  121. return $this->fetch('public/form-builder');
  122. }
  123. public function update_other($uid = 0)
  124. {
  125. $data = Util::postMore([
  126. ['money_status', 0],
  127. ['money', 0],
  128. ['integration_status', 0],
  129. ['integration', 0],
  130. ['anticipate_status', 0],
  131. ['anticipate', 0],
  132. ['shop_integral_status', 0],
  133. ['shop_integral', 0],
  134. ], $this->request);
  135. if (!$uid) return $this->failed('数据不存在');
  136. $user = UserModel::get($uid);
  137. if (!$user) return Json::fail('数据不存在!');
  138. BaseModel::beginTrans();
  139. $res1 = false;
  140. $res2 = false;
  141. $edit = array();
  142. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  143. if ($data['money_status'] == 1) {//增加
  144. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  145. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  146. try {
  147. UserRepository::adminAddMoney($user, $data['money']);
  148. } catch (\Exception $e) {
  149. BaseModel::rollbackTrans();
  150. return Json::fail($e->getMessage());
  151. }
  152. } else if ($data['money_status'] == 2) {//减少
  153. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  154. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  155. try {
  156. UserRepository::adminSubMoney($user, $data['money']);
  157. } catch (\Exception $e) {
  158. BaseModel::rollbackTrans();
  159. return Json::fail($e->getMessage());
  160. }
  161. }
  162. } else {
  163. $res1 = true;
  164. }
  165. if ($data['integration_status'] && $data['integration']) {//趣豆增加或者减少
  166. if ($data['integration_status'] == 1) {//增加
  167. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  168. $res2 = UserBillAdmin::income('系统增加趣豆', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '趣豆');
  169. try {
  170. UserRepository::adminAddIntegral($user, $data['integration']);
  171. } catch (\Exception $e) {
  172. BaseModel::rollbackTrans();
  173. return Json::fail($e->getMessage());
  174. }
  175. } else if ($data['integration_status'] == 2) {//减少
  176. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  177. $res2 = UserBillAdmin::expend('系统减少趣豆', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '趣豆');
  178. try {
  179. UserRepository::adminSubIntegral($user, $data['integration']);
  180. } catch (\Exception $e) {
  181. BaseModel::rollbackTrans();
  182. return Json::fail($e->getMessage());
  183. }
  184. }
  185. } else {
  186. $res2 = true;
  187. }
  188. if ($data['anticipate_status'] && $data['anticipate']) {//趣豆增加或者减少
  189. if ($data['anticipate_status'] == 1) {//增加
  190. $edit['anticipate'] = bcadd($user['anticipate'], $data['anticipate'], 2);
  191. $res3 = UserBillAdmin::income('系统增加广告值', $user['uid'], 'anticipate', 'add_anticipate', $data['anticipate'], $this->adminId, $edit['anticipate'], '系统增加了' . floatval($data['anticipate']) . '广告值');
  192. try {
  193. UserRepository::adminAddIntegral($user, $data['integration']);
  194. } catch (\Exception $e) {
  195. BaseModel::rollbackTrans();
  196. return Json::fail($e->getMessage());
  197. }
  198. } else if ($data['anticipate_status'] == 2) {//减少
  199. $edit['anticipate'] = bcsub($user['anticipate'], $data['anticipate'], 2);
  200. $res3 = UserBillAdmin::expend('系统减少广告值', $user['uid'], 'anticipate', 'reduce_anticipate', $data['anticipate'], $this->adminId, $edit['anticipate'], '系统扣除了' . floatval($data['anticipate']) . '广告值');
  201. try {
  202. UserRepository::adminSubIntegral($user, $data['anticipate']);
  203. } catch (\Exception $e) {
  204. BaseModel::rollbackTrans();
  205. return Json::fail($e->getMessage());
  206. }
  207. }
  208. } else {
  209. $res3 = true;
  210. }
  211. if ($data['shop_integral_status'] && $data['shop_integral']) {//趣豆增加或者减少
  212. if ($data['shop_integral_status'] == 1) {//增加
  213. $edit['shop_integral'] = bcadd($user['shop_integral'], $data['shop_integral'], 2);
  214. $res5 = UserBillAdmin::income('系统增加购物券', $user['uid'], 'shop_integral', 'add_shop_integral', $data['shop_integral'], $this->adminId, $edit['shop_integral'], '系统增加了' . floatval($data['shop_integral']) . '购物券');
  215. try {
  216. UserRepository::adminAddIntegral($user, $data['integration']);
  217. } catch (\Exception $e) {
  218. BaseModel::rollbackTrans();
  219. return Json::fail($e->getMessage());
  220. }
  221. } else if ($data['shop_integral_status'] == 2) {//减少
  222. $edit['shop_integral'] = bcsub($user['shop_integral'], $data['shop_integral'], 2);
  223. $res5 = UserBillAdmin::expend('系统减少购物券', $user['uid'], 'shop_integral', 'reduce_shop_integral', $data['shop_integral'], $this->adminId, $edit['shop_integral'], '系统扣除了' . floatval($data['shop_integral']) . '购物券');
  224. try {
  225. UserRepository::adminSubIntegral($user, $data['anticipate']);
  226. } catch (\Exception $e) {
  227. BaseModel::rollbackTrans();
  228. return Json::fail($e->getMessage());
  229. }
  230. }
  231. } else {
  232. $res5 = true;
  233. }
  234. if ($edit) $res4 = UserModel::edit($edit, $uid);
  235. else $res4 = true;
  236. if ($res1 && $res2 && $res3 && $res4 && $res5) $res = true;
  237. else $res = false;
  238. BaseModel::checkTrans($res);
  239. if ($res) return Json::successful('修改成功!');
  240. else return Json::fail('修改失败');
  241. }
  242. /*
  243. * 赠送会员等级
  244. * @paran int $uid
  245. * @return json
  246. * */
  247. public function save_give_level($uid = 0)
  248. {
  249. if (!$uid) return Json::fail('缺少参数');
  250. list($level_id) = Util::postMore([
  251. ['level_id', 0],
  252. ], $this->request, true);
  253. //查询当前选择的会员等级
  254. $systemLevel = SystemUserLevel::where(['is_show' => 1, 'is_del' => 0, 'id' => $level_id])->find();
  255. if (!$systemLevel) return Json::fail('您选择赠送的会员等级不存在!');
  256. //检查是否拥有此会员等级
  257. $level = UserLevel::where(['uid' => $uid, 'level_id' => $level_id, 'is_del' => 0])->field('valid_time,is_forever')->find();
  258. if ($level) if (!$level['is_forever'] && time() < $level['valid_time']) return Json::fail('此用户已有该会员等级,无法再次赠送');
  259. //设置会员过期时间
  260. $add_valid_time = (int)$systemLevel->valid_date * 86400;
  261. UserModel::commitTrans();
  262. try {
  263. //保存会员信息
  264. $res = UserLevel::create([
  265. 'is_forever' => $systemLevel->is_forever,
  266. 'status' => 1,
  267. 'is_del' => 0,
  268. 'grade' => $systemLevel->grade,
  269. 'uid' => $uid,
  270. 'add_time' => time(),
  271. 'level_id' => $level_id,
  272. 'discount' => $systemLevel->discount,
  273. 'valid_time' => $systemLevel->discount ? $add_valid_time + time() : 0,
  274. 'mark' => '尊敬的用户【' . UserModel::where('uid', $uid)->value('nickname') . '】在' . date('Y-m-d H:i:s', time()) . '赠送会员等级成为' . $systemLevel['name'] . '会员',
  275. ]);
  276. //提取等级任务并记录完成情况
  277. $levelIds = [$level_id];
  278. $lowGradeLevelIds = SystemUserLevel::where('grade', '<', $systemLevel->grade)->where(['is_show' => 1, 'is_del' => 0])->column('id', 'id');
  279. if (count($lowGradeLevelIds)) $levelIds = array_merge($levelIds, $lowGradeLevelIds);
  280. $taskIds = SystemUserTask::where('level_id', 'in', $levelIds)->column('id', 'id');
  281. $inserValue = [];
  282. foreach ($taskIds as $id) {
  283. $inserValue[] = ['uid' => $uid, 'task_id' => $id, 'status' => 1, 'add_time' => time()];
  284. }
  285. $res = $res && UserTaskFinish::insertAll($inserValue) && UserModel::where('uid', $uid)->update(['level' => $level_id]);
  286. if ($res) {
  287. UserModel::commitTrans();
  288. return Json::successful('赠送成功');
  289. } else {
  290. UserModel::rollbackTrans();
  291. return Json::successful('赠送失败');
  292. }
  293. } catch (\Exception $e) {
  294. UserModel::rollbackTrans();
  295. return Json::fail('赠送失败');
  296. }
  297. }
  298. /*
  299. * 清除会员等级
  300. * @param int $uid
  301. * @return json
  302. * */
  303. public function del_level($uid = 0)
  304. {
  305. if (!$uid) return Json::fail('缺少参数');
  306. if (UserLevel::cleanUpLevel($uid))
  307. return Json::successful('清除成功');
  308. else
  309. return Json::fail('清除失败');
  310. }
  311. /**
  312. * 修改实名认证状态状态
  313. *
  314. * @return json
  315. */
  316. /**
  317. * 修改user表状态
  318. *
  319. * @return json
  320. */
  321. public function set_status($status = '', $uid = 0, $is_echo = 0)
  322. {
  323. if ($is_echo == 0) {
  324. if ($status == '' || $uid == 0) return Json::fail('参数错误');
  325. UserModel::where(['uid' => $uid])->update(['status' => $status]);
  326. } else {
  327. $uids = Util::postMore([
  328. ['uids', []]
  329. ]);
  330. UserModel::destrSyatus($uids['uids'], $status);
  331. }
  332. return Json::successful($status == 0 ? '禁用成功' : '解禁成功');
  333. }
  334. /**
  335. * 获取user表
  336. *
  337. * @return json
  338. */
  339. public function get_user_list()
  340. {
  341. $where = Util::getMore([
  342. ['page', 1],
  343. ['limit', 20],
  344. ['nickname', ''],
  345. ['status', ''],
  346. ['pay_count', ''],
  347. ['is_promoter', ''],
  348. ['order', ''],
  349. ['data', ''],
  350. ['user_type', ''],
  351. ['country', ''],
  352. ['province', ''],
  353. ['city', ''],
  354. ['user_time_type', ''],
  355. ['user_time', ''],
  356. ['sex', ''],
  357. ['level', ''],
  358. ['group_id', ''],
  359. ['excel', 0],
  360. ]);
  361. return Json::successlayui(UserModel::getUserList($where));
  362. }
  363. /**
  364. * 编辑模板消息
  365. * @param $id
  366. * @return mixed|\think\response\Json|void
  367. */
  368. public function edit($uid)
  369. {
  370. if (!$uid) return $this->failed('数据不存在');
  371. $user = UserModel::get($uid);
  372. if (!$user) return Json::fail('数据不存在!');
  373. $f = array();
  374. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(1);
  375. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
  376. $f[] = Form::text('phone', '手机号', $user->getData('phone'));
  377. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : 0);
  378. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
  379. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  380. $f[] = Form::radio('is_promoter', '推广员', $user->getData('is_promoter'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  381. $f[] = Form::radio('is_agent', '代理', $user->getData('is_agent'))->options([['value' => 1, 'label' => '是'], ['value' => 0, 'label' => '否']]);
  382. $st[] = ['is_promoter','=',1];
  383. $st[] = ['uid','<>',$uid];
  384. $f[] = Form::input('spread_time', '绑定推荐人时间', $user->getData('spread_time') ? date('Y-m-d-h:i:s', $user->getData('spread_time')) : 0)->disabled(1);
  385. $f[] = Form::select('spread_uid', '绑定推荐人', $user->getData('spread_uid'))->options(UserModel::where([$st])->field('uid as value,nickname as label')->select()->toArray())->filterable(true);
  386. $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  387. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', array('uid' => $uid)), 5);
  388. $this->assign(compact('form'));
  389. return $this->fetch('public/form-builder');
  390. }
  391. public function update($uid)
  392. {
  393. $data = Util::postMore([
  394. ['money_status', 0],
  395. ['is_promoter', 1],
  396. ['real_name', ''],
  397. ['phone', 0],
  398. ['card_id', ''],
  399. ['birthday', ''],
  400. ['mark', ''],
  401. ['money', 0],
  402. ['integration_status', 0],
  403. ['integration', 0],
  404. ['spread_uid', 0],
  405. ['status', 0],
  406. ['is_agent', 0],
  407. ['spread_time', ''],
  408. ]);
  409. if (!$uid) return $this->failed('数据不存在');
  410. $user = UserModel::get($uid);
  411. if (!$user) return Json::fail('数据不存在!');
  412. BaseModel::beginTrans();
  413. $res1 = false;
  414. $res2 = false;
  415. $edit = array();
  416. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  417. if ($data['money_status'] == 1) {//增加
  418. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  419. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  420. try {
  421. UserRepository::adminAddMoney($user, $data['money']);
  422. } catch (\Exception $e) {
  423. BaseModel::rollbackTrans();
  424. return Json::fail($e->getMessage());
  425. }
  426. } else if ($data['money_status'] == 2) {//减少
  427. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  428. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  429. try {
  430. UserRepository::adminSubMoney($user, $data['money']);
  431. } catch (\Exception $e) {
  432. BaseModel::rollbackTrans();
  433. return Json::fail($e->getMessage());
  434. }
  435. }
  436. } else {
  437. $res1 = true;
  438. }
  439. if ($data['integration_status'] && $data['integration']) {//趣豆增加或者减少
  440. if ($data['integration_status'] == 1) {//增加
  441. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  442. $res2 = UserBillAdmin::income('系统增加趣豆', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '趣豆');
  443. try {
  444. UserRepository::adminAddIntegral($user, $data['integration']);
  445. } catch (\Exception $e) {
  446. BaseModel::rollbackTrans();
  447. return Json::fail($e->getMessage());
  448. }
  449. } else if ($data['integration_status'] == 2) {//减少
  450. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  451. $res2 = UserBillAdmin::expend('系统减少趣豆', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '趣豆');
  452. try {
  453. UserRepository::adminSubIntegral($user, $data['integration']);
  454. } catch (\Exception $e) {
  455. BaseModel::rollbackTrans();
  456. return Json::fail($e->getMessage());
  457. }
  458. }
  459. } else {
  460. $res2 = true;
  461. }
  462. $edit['status'] = $data['status'];
  463. $edit['real_name'] = $data['real_name'];
  464. $edit['phone'] = $data['phone'];
  465. $edit['card_id'] = $data['card_id'];
  466. $edit['birthday'] = strtotime($data['birthday']);
  467. $edit['mark'] = $data['mark'];
  468. $edit['is_agent'] = $data['is_agent'];
  469. if($data['is_promoter']==1){
  470. // if($user['level']<3){
  471. // return $this->failed('等级未达到店长!不可为推广员');
  472. // die;
  473. // }
  474. $edit['is_promoter'] = $data['is_promoter'];
  475. }else{
  476. $edit['is_promoter'] = $data['is_promoter'];
  477. }
  478. $userlist=UserModel::where('uid','<>',$uid)->column('uid,spread_uid','uid');
  479. $sp =$data['spread_uid'];
  480. while ($sp) {
  481. if ($sp == $uid) {
  482. $data['spread_uid'] = 0;
  483. return Json::fail('推荐人不合法');
  484. break;
  485. }
  486. $sp = $userlist[$sp]['spread_uid'];
  487. $edit['spread_uid'] = $data['spread_uid'];
  488. }
  489. if($user['spread_uid']!=$data['spread_uid']){
  490. $edit['spread_time'] = time();
  491. }
  492. if ($edit) $res3 = UserModel::edit($edit, $uid);
  493. else $res3 = true;
  494. if ($res1 && $res2 && $res3) $res = true;
  495. else $res = false;
  496. BaseModel::checkTrans($res);
  497. if ($res) return Json::successful('修改成功!');
  498. else return Json::fail('修改失败');
  499. }
  500. /**
  501. * 用户图表
  502. * @return mixed
  503. */
  504. public function user_analysis()
  505. {
  506. $where = Util::getMore([
  507. ['nickname', ''],
  508. ['status', ''],
  509. ['is_promoter', ''],
  510. ['date', ''],
  511. ['user_type', ''],
  512. ['export', 0]
  513. ], $this->request);
  514. $user_count = UserModel::consume($where, '', true);
  515. //头部信息
  516. $header = [
  517. [
  518. 'name' => '新增用户',
  519. 'class' => 'fa-line-chart',
  520. 'value' => $user_count,
  521. 'color' => 'red'
  522. ],
  523. [
  524. 'name' => '用户留存',
  525. 'class' => 'fa-area-chart',
  526. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true), $where) . '%',
  527. 'color' => 'lazur'
  528. ],
  529. [
  530. 'name' => '新增用户总消费',
  531. 'class' => 'fa-bar-chart',
  532. 'value' => '¥' . UserModel::consume($where),
  533. 'color' => 'navy'
  534. ],
  535. [
  536. 'name' => '用户活跃度',
  537. 'class' => 'fa-pie-chart',
  538. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true)) . '%',
  539. 'color' => 'yellow'
  540. ],
  541. ];
  542. $name = ['新增用户', '用户消费'];
  543. $dates = $this->get_user_index($where, $name);
  544. $user_index = ['name' => json_encode($name), 'date' => json_encode($dates['time']), 'series' => json_encode($dates['series'])];
  545. //用户浏览分析
  546. $view = StoreVisit::getVisit($where['date'], ['', 'warning', 'info', 'danger']);
  547. $view_v1 = WechatMessage::getViweList($where['date'], ['', 'warning', 'info', 'danger']);
  548. $view = array_merge($view, $view_v1);
  549. $view_v2 = [];
  550. foreach ($view as $val) {
  551. $view_v2['color'][] = '#' . rand(100000, 339899);
  552. $view_v2['name'][] = $val['name'];
  553. $view_v2['value'][] = $val['value'];
  554. }
  555. $view = $view_v2;
  556. //消费会员排行用户分析
  557. $user_null = UserModel::getUserSpend($where['date']);
  558. //消费数据
  559. $now_number = UserModel::getUserSpend($where['date'], true);
  560. list($paren_number, $title) = UserModel::getPostNumber($where['date']);
  561. if ($paren_number == 0) {
  562. $rightTitle = [
  563. 'number' => $now_number > 0 ? $now_number : 0,
  564. 'icon' => 'fa-level-up',
  565. 'title' => $title
  566. ];
  567. } else {
  568. $number = (float)bcsub($now_number, $paren_number, 4);
  569. if ($now_number == 0) {
  570. $icon = 'fa-level-down';
  571. } else {
  572. $icon = $now_number > $paren_number ? 'fa-level-up' : 'fa-level-down';
  573. }
  574. $rightTitle = ['number' => $number, 'icon' => $icon, 'title' => $title];
  575. }
  576. unset($title, $paren_number, $now_number);
  577. list($paren_user_count, $title) = UserModel::getPostNumber($where['date'], true, 'add_time', '');
  578. if ($paren_user_count == 0) {
  579. $count = $user_count == 0 ? 0 : $user_count;
  580. $icon = $user_count == 0 ? 'fa-level-down' : 'fa-level-up';
  581. } else {
  582. $count = (float)bcsub($user_count, $paren_user_count, 4);
  583. $icon = $user_count < $paren_user_count ? 'fa-level-down' : 'fa-level-up';
  584. }
  585. $leftTitle = [
  586. 'count' => $count,
  587. 'icon' => $icon,
  588. 'title' => $title
  589. ];
  590. unset($count, $icon, $title);
  591. $consume = [
  592. 'title' => '消费金额为¥' . UserModel::consume($where),
  593. 'series' => UserModel::consume($where, 'xiaofei'),
  594. 'rightTitle' => $rightTitle,
  595. 'leftTitle' => $leftTitle,
  596. ];
  597. $form = UserModel::consume($where, 'form');
  598. $grouping = UserModel::consume($where, 'grouping');
  599. $this->assign(compact('header', 'user_index', 'view', 'user_null', 'consume', 'form', 'grouping', 'where'));
  600. return $this->fetch();
  601. }
  602. public function gethreaderValue($chart, $where = [])
  603. {
  604. if ($where) {
  605. switch ($where['date']) {
  606. case null:
  607. case 'today':
  608. case 'week':
  609. case 'year':
  610. if ($where['date'] == null) {
  611. $where['date'] = 'month';
  612. }
  613. $sum_user = UserModel::whereTime('add_time', $where['date'])->count();
  614. if ($sum_user == 0) return 0;
  615. $counts = bcdiv($chart, $sum_user, 4) * 100;
  616. return $counts;
  617. break;
  618. case 'quarter':
  619. $quarter = UserModel::getMonth('n');
  620. $quarter[0] = strtotime($quarter[0]);
  621. $quarter[1] = strtotime($quarter[1]);
  622. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  623. if ($sum_user == 0) return 0;
  624. $counts = bcdiv($chart, $sum_user, 4) * 100;
  625. return $counts;
  626. default:
  627. //自定义时间
  628. $quarter = explode('-', $where['date']);
  629. $quarter[0] = strtotime($quarter[0]);
  630. $quarter[1] = strtotime($quarter[1]);
  631. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  632. if ($sum_user == 0) return 0;
  633. $counts = bcdiv($chart, $sum_user, 4) * 100;
  634. return $counts;
  635. break;
  636. }
  637. } else {
  638. $num = UserModel::count();
  639. $chart = $num != 0 ? bcdiv($chart, $num, 5) * 100 : 0;
  640. return $chart;
  641. }
  642. }
  643. public function get_user_index($where, $name)
  644. {
  645. switch ($where['date']) {
  646. case null:
  647. $days = date("t", strtotime(date('Y-m', time())));
  648. $dates = [];
  649. $series = [];
  650. $times_list = [];
  651. foreach ($name as $key => $val) {
  652. for ($i = 1; $i <= $days; $i++) {
  653. if (!in_array($i . '号', $times_list)) {
  654. array_push($times_list, $i . '号');
  655. }
  656. $time = $this->gettime(date("Y-m", time()) . '-' . $i);
  657. if ($key == 0) {
  658. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  659. } else if ($key == 1) {
  660. $dates['data'][] = UserModel::consume(true, $time);
  661. }
  662. }
  663. $dates['name'] = $val;
  664. $dates['type'] = 'line';
  665. $series[] = $dates;
  666. unset($dates);
  667. }
  668. return ['time' => $times_list, 'series' => $series];
  669. case 'today':
  670. $dates = [];
  671. $series = [];
  672. $times_list = [];
  673. foreach ($name as $key => $val) {
  674. for ($i = 0; $i <= 24; $i++) {
  675. $strtitle = $i . '点';
  676. if (!in_array($strtitle, $times_list)) {
  677. array_push($times_list, $strtitle);
  678. }
  679. $time = $this->gettime(date("Y-m-d ", time()) . $i);
  680. if ($key == 0) {
  681. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  682. } else if ($key == 1) {
  683. $dates['data'][] = UserModel::consume(true, $time);
  684. }
  685. }
  686. $dates['name'] = $val;
  687. $dates['type'] = 'line';
  688. $series[] = $dates;
  689. unset($dates);
  690. }
  691. return ['time' => $times_list, 'series' => $series];
  692. case "week":
  693. $dates = [];
  694. $series = [];
  695. $times_list = [];
  696. foreach ($name as $key => $val) {
  697. for ($i = 0; $i <= 6; $i++) {
  698. if (!in_array('星期' . ($i + 1), $times_list)) {
  699. array_push($times_list, '星期' . ($i + 1));
  700. }
  701. $time = UserModel::getMonth('h', $i);
  702. if ($key == 0) {
  703. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($time[0]), strtotime($time[1])])->count();
  704. } else if ($key == 1) {
  705. $dates['data'][] = UserModel::consume(true, [strtotime($time[0]), strtotime($time[1])]);
  706. }
  707. }
  708. $dates['name'] = $val;
  709. $dates['type'] = 'line';
  710. $series[] = $dates;
  711. unset($dates);
  712. }
  713. return ['time' => $times_list, 'series' => $series];
  714. case 'year':
  715. $dates = [];
  716. $series = [];
  717. $times_list = [];
  718. $year = date('Y');
  719. foreach ($name as $key => $val) {
  720. for ($i = 1; $i <= 12; $i++) {
  721. if (!in_array($i . '月', $times_list)) {
  722. array_push($times_list, $i . '月');
  723. }
  724. $t = strtotime($year . '-' . $i . '-01');
  725. $arr = explode('/', date('Y-m-01', $t) . '/' . date('Y-m-', $t) . date('t', $t));
  726. if ($key == 0) {
  727. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  728. } else if ($key == 1) {
  729. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  730. }
  731. }
  732. $dates['name'] = $val;
  733. $dates['type'] = 'line';
  734. $series[] = $dates;
  735. unset($dates);
  736. }
  737. return ['time' => $times_list, 'series' => $series];
  738. case 'quarter':
  739. $dates = [];
  740. $series = [];
  741. $times_list = [];
  742. foreach ($name as $key => $val) {
  743. for ($i = 1; $i <= 4; $i++) {
  744. $arr = $this->gettime('quarter', $i);
  745. if (!in_array(implode('--', $arr) . '季度', $times_list)) {
  746. array_push($times_list, implode('--', $arr) . '季度');
  747. }
  748. if ($key == 0) {
  749. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  750. } else if ($key == 1) {
  751. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  752. }
  753. }
  754. $dates['name'] = $val;
  755. $dates['type'] = 'line';
  756. $series[] = $dates;
  757. unset($dates);
  758. }
  759. return ['time' => $times_list, 'series' => $series];
  760. default:
  761. $list = UserModel::consume($where, 'default');
  762. $dates = [];
  763. $series = [];
  764. $times_list = [];
  765. foreach ($name as $k => $v) {
  766. foreach ($list as $val) {
  767. $date = $val['add_time'];
  768. if (!in_array($date, $times_list)) {
  769. array_push($times_list, $date);
  770. }
  771. if ($k == 0) {
  772. $dates['data'][] = $val['num'];
  773. } else if ($k == 1) {
  774. $dates['data'][] = UserBillAdmin::where(['uid' => $val['uid'], 'type' => 'pay_product'])->sum('number');
  775. }
  776. }
  777. $dates['name'] = $v;
  778. $dates['type'] = 'line';
  779. $series[] = $dates;
  780. unset($dates);
  781. }
  782. return ['time' => $times_list, 'series' => $series];
  783. }
  784. }
  785. public function gettime($time = '', $season = '')
  786. {
  787. if (!empty($time) && empty($season)) {
  788. $timestamp0 = strtotime($time);
  789. $timestamp24 = strtotime($time) + 86400;
  790. return [$timestamp0, $timestamp24];
  791. } else if (!empty($time) && !empty($season)) {
  792. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  793. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  794. return [$firstday, $lastday];
  795. }
  796. }
  797. /**
  798. * 会员等级首页
  799. */
  800. public function group()
  801. {
  802. return $this->fetch();
  803. }
  804. /**
  805. * 会员详情
  806. */
  807. public function see($uid = '')
  808. {
  809. $this->assign([
  810. 'uid' => $uid,
  811. 'userinfo' => UserModel::getUserDetailed($uid),
  812. 'is_layui' => true,
  813. 'headerList' => UserModel::getHeaderList($uid),
  814. 'count' => UserModel::getCountInfo($uid),
  815. ]);
  816. return $this->fetch();
  817. }
  818. /*
  819. * 获取某个用户的推广下线
  820. * */
  821. public function getSpreadList($uid, $page = 1, $limit = 20)
  822. {
  823. return Json::successful(UserModel::getSpreadList($uid, (int)$page, (int)$limit));
  824. }
  825. /**
  826. * 获取某用户的订单列表
  827. */
  828. public function getOneorderList($uid, $page = 1, $limit = 20)
  829. {
  830. return Json::successful(StoreOrder::getOneorderList(compact('uid', 'page', 'limit')));
  831. }
  832. /**
  833. * 获取某用户的趣豆列表
  834. */
  835. public function getOneIntegralList($uid, $page = 1, $limit = 20)
  836. {
  837. return Json::successful(UserBillAdmin::getOneIntegralList(compact('uid', 'page', 'limit')));
  838. }
  839. /**
  840. * 获取某用户的趣豆列表
  841. */
  842. public function getOneSignList($uid, $page = 1, $limit = 20)
  843. {
  844. return Json::successful(UserBillAdmin::getOneSignList(compact('uid', 'page', 'limit')));
  845. }
  846. /**
  847. * 获取某用户的持有优惠劵
  848. */
  849. public function getOneCouponsList($uid, $page = 1, $limit = 20)
  850. {
  851. return Json::successful(StoreCouponUser::getOneCouponsList(compact('uid', 'page', 'limit')));
  852. }
  853. /**
  854. * 获取某用户的余额变动记录
  855. */
  856. public function getOneBalanceChangList($uid, $page = 1, $limit = 20)
  857. {
  858. return Json::successful(UserBillAdmin::getOneBalanceChangList(compact('uid', 'page', 'limit')));
  859. }
  860. }