User.php 44 KB

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