User.php 44 KB

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