User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Message;
  4. use app\admin\model\UserPower;
  5. use app\common\controller\Backend;
  6. use app\common\library\Auth;
  7. use app\common\service\TenimService;
  8. use fast\Random;
  9. use think\Db;
  10. use think\Exception;
  11. use think\exception\PDOException;
  12. use think\exception\ValidateException;
  13. /**
  14. * 会员管理
  15. *
  16. * @icon fa fa-user
  17. */
  18. class User extends Backend
  19. {
  20. protected $relationSearch = true;
  21. protected $searchFields = 'u_id,username,nickname';
  22. /**
  23. * @var \app\admin\model\User
  24. */
  25. protected $model = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = model('User');
  30. $typeList = [
  31. 'genderList' => $this->model->getGenderList(),
  32. 'isCoolList' => $this->model->getIsCoolList(),
  33. 'isManagerList' => $this->model->getIsManagerList(),
  34. 'isStealthList' => $this->model->getIsStealthList(),
  35. 'statusList' => $this->model->getStatusList(),
  36. 'needCheckList' => $this->model->getNeedCheckList(),
  37. ];
  38. $this->view->assign($typeList);
  39. }
  40. /**
  41. * 查看
  42. */
  43. public function index()
  44. {
  45. $this->relationSearch = true;
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags', 'trim']);
  48. if ($this->request->isAjax()) {
  49. //如果发送的来源是Selectpage,则转发到Selectpage
  50. if ($this->request->request('keyField')) {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $list = $this->model
  55. ->with(['noble','preuser','auth','age','userwallet'])
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. foreach ($list as $k => $v) {
  60. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  61. $v->hidden(['password', 'salt']);
  62. $v->getRelation('age')->visible(['name']);
  63. $v->getRelation('userwallet')->visible(['money','jewel']);
  64. }
  65. $result = array("total" => $list->total(), "rows" => $list->items());
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 添加
  72. */
  73. /**
  74. * 添加
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isPost()) {
  79. $params = $this->request->post("row/a");
  80. $params = $this->preExcludeFields($params);
  81. if (!$params) {
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. //是否采用模型验证
  88. if ($this->modelValidate) {
  89. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  90. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  91. $this->model->validateFailException(true)->validate($validate);
  92. }
  93. if (empty($params['avatar'])) {
  94. $params['avatar'] = '/assets/img/default_avatar.png';
  95. }
  96. $ids = $this->model->column("u_id");
  97. $invite_no = $this->model->column("invite_no");
  98. $params['u_id'] = $this->model->getUinqueId(8, [$ids]);
  99. $params['invite_no'] = $this->model->getUinqueNo(8, $invite_no);
  100. if (empty($params['nickname'])) {
  101. $params['nickname'] = 'gg_'.$params['u_id'];
  102. }
  103. $params['image'] = '/assets/img/default_avatar.png';
  104. $params['username'] = $params['mobile'];
  105. $params['status'] = 'normal';
  106. $params['salt'] = Random::alnum();
  107. $params['has_info'] = 1;
  108. $result = $this->model->allowField(true)->save($params);
  109. $userId = $this->model->id;
  110. //权限
  111. $userPower = new UserPower();
  112. $userPowerData['user_id'] = $userId;
  113. $userPowerRes = $userPower->insertGetId($userPowerData);
  114. if (!$userPowerRes) {
  115. throw new Exception('创建用户权限失败');
  116. }
  117. //钱包
  118. $userwallet = [
  119. 'user_id' => $userId,
  120. ];
  121. $userWalletRes = Db::name('user_wallet')->insertGetId($userwallet);
  122. if(!$userWalletRes){
  123. throw new Exception('创建用户钱包失败');
  124. }
  125. //创建IM用户
  126. $tenimService = new TenimService();
  127. $imParams['user_id'] = $userPowerRes;
  128. $imParams['nickname'] = $params['nickname'];
  129. $imParams['avatar'] = cdnurl($params['avatar']);
  130. $tenimRes = $tenimService->accountImport($imParams);
  131. if (!$tenimRes['status']) {
  132. throw new Exception($tenimRes['msg']);
  133. }
  134. } catch (ValidateException|PDOException|Exception $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. }
  138. if ($result == false) {
  139. $this->error(__('No rows were inserted'));
  140. }
  141. Db::commit();
  142. $this->success();
  143. }
  144. return $this->view->fetch();
  145. }
  146. /**
  147. * 编辑
  148. */
  149. public function edit($ids = null)
  150. {
  151. $row = $this->model->get($ids);
  152. if (!$row) {
  153. $this->error(__('No Results were found'));
  154. }
  155. $adminIds = $this->getDataLimitAdminIds();
  156. if (is_array($adminIds)) {
  157. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  158. $this->error(__('You have no permission'));
  159. }
  160. }
  161. if ($this->request->isPost()) {
  162. $params = $this->request->post("row/a");
  163. if (!$params) {
  164. $this->error(__('Parameter %s can not be empty', ''));
  165. }
  166. $params = $this->preExcludeFields($params);
  167. $result = false;
  168. try {
  169. //是否采用模型验证
  170. if ($this->modelValidate) {
  171. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  172. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  173. $row->validateFailException(true)->validate($validate);
  174. }
  175. if (!empty($params['u_id'])) {
  176. $userWhere['u_id'] = $params['u_id'];
  177. $userWhere['id'] = ['neq',$ids];
  178. $user = $this->model->where($userWhere)->find();
  179. if (!empty($user)) {
  180. throw new Exception('前端用户ID已存在');
  181. }
  182. }
  183. if (!empty($params['mobile'])) {
  184. $userWhere['mobile'] = $params['mobile'];
  185. $user = $this->model->where($userWhere)->find();
  186. if (!empty($user)) {
  187. throw new Exception('手机号已存在');
  188. }
  189. }
  190. $result = $row->allowField(true)->save($params);
  191. } catch (ValidateException|PDOException|Exception $e) {
  192. $this->error($e->getMessage());
  193. }
  194. if ($result == false) {
  195. $this->error(__('No rows were updated'));
  196. }
  197. $this->success();
  198. }
  199. $this->view->assign("row", $row);
  200. return $this->view->fetch();
  201. }
  202. /**
  203. * 删除
  204. */
  205. public function del($ids = "")
  206. {
  207. if (!$this->request->isPost()) {
  208. $this->error(__("Invalid parameters"));
  209. }
  210. $ids = $ids ? $ids : $this->request->post("ids");
  211. $row = $this->model->get($ids);
  212. $this->modelValidate = true;
  213. if (!$row) {
  214. $this->error(__('No Results were found'));
  215. }
  216. Auth::instance()->delete($row['id']);
  217. $this->success();
  218. }
  219. /**
  220. * 详情
  221. * @param null $ids
  222. * @return string
  223. * @throws \think\Exception
  224. * @throws \think\exception\DbException
  225. */
  226. public function detail($ids = null)
  227. {
  228. /* 判断数据是否存在*/
  229. $row = $this->model->get($ids);
  230. if (!$row) {
  231. $this->error(__('No Results were found'));
  232. }
  233. /* 判断是否有权限访问*/
  234. $adminIds = $this->getDataLimitAdminIds();
  235. if (is_array($adminIds)) {
  236. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  237. $this->error(__('You have no permission'));
  238. }
  239. }
  240. $this->view->assign("row", $row);
  241. return $this->view->fetch();
  242. }
  243. /**
  244. * 编辑
  245. */
  246. public function infocheck($ids = null)
  247. {
  248. $row = $this->model->get($ids);
  249. if (!$row) {
  250. $this->error(__('No Results were found'));
  251. }
  252. $adminIds = $this->getDataLimitAdminIds();
  253. if (is_array($adminIds)) {
  254. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  255. $this->error(__('You have no permission'));
  256. }
  257. }
  258. if ($this->request->isPost()) {
  259. $params = $this->request->post("row/a");
  260. if (!$params) {
  261. $this->error(__('Parameter %s can not be empty', ''));
  262. }
  263. $params = $this->preExcludeFields($params);
  264. $result = false;
  265. try {
  266. //是否采用模型验证
  267. if ($this->modelValidate) {
  268. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  269. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  270. $row->validateFailException(true)->validate($validate);
  271. }
  272. $nickStatusStr = $statusStr = '';
  273. if (isset($params['check_nick_status'])) {
  274. if ($params['check_nick_status'] == 1) {//审核通过 更新昵称
  275. !empty($params['pre_nickname']) && $params['nickname'] = $params['pre_nickname'];
  276. $params['pre_nickname'] = '';
  277. $nickStatusStr = '昵称通过';
  278. } else {//审核拒绝 清空新昵称
  279. $params['pre_nickname'] = '';
  280. $nickStatusStr = '昵称拒绝';
  281. }
  282. }
  283. if (isset($params['check_status'])) {
  284. if ($params['check_status'] == 1) {//审核通过 更新头像
  285. !empty($params['pre_avatar']) && $params['avatar'] = $params['pre_avatar'];
  286. $params['pre_avatar'] = '';
  287. $statusStr = '头像通过';
  288. } else {//审核拒绝 清空新头像
  289. $params['pre_avatar'] = '';
  290. $statusStr = '头像拒绝';
  291. }
  292. }
  293. $params['need_check'] = 0;
  294. $result = $row->allowField(true)->save($params);
  295. } catch (ValidateException|PDOException|Exception $e) {
  296. $this->error($e->getMessage());
  297. }
  298. if ($result == false) {
  299. $this->error(__('No rows were updated'));
  300. }
  301. if (!empty($nickStatusStr) || !empty($statusStr)) {
  302. //通过发消息
  303. $title = '用户信息变更审核通知';
  304. $content = '您申请的'.$nickStatusStr.$statusStr;
  305. Message::addMessage($row['id'],$title,$content);
  306. }
  307. $this->success();
  308. }
  309. $checkStatusList = [1=>'通过', 0=>'拒绝'];
  310. $showNickname = $showAvatar = 0;
  311. if (!empty($row['pre_nickname']) && $row['pre_nickname'] != $row['nickname']) {
  312. $showNickname = 1;
  313. }
  314. if (!empty($row['pre_avatar']) && $row['pre_avatar'] != $row['avatar']) {
  315. $showAvatar = 1;
  316. }
  317. $this->view->assign([
  318. 'row' => $row,
  319. 'checkStatusList' => $checkStatusList,
  320. 'showNickname' => $showNickname,
  321. 'showAvatar' => $showAvatar,
  322. ]);
  323. return $this->view->fetch();
  324. }
  325. /**
  326. * 钻石充值
  327. * @param null $ids
  328. * @return string
  329. */
  330. public function addJewel($ids=null)
  331. {
  332. /* 判断数据是否存在*/
  333. $row = $this->model->get($ids);
  334. if (!$row) {
  335. $this->error(__('No Results were found'));
  336. }
  337. /* 判断是否有权限访问*/
  338. $adminIds = $this->getDataLimitAdminIds();
  339. if (is_array($adminIds)) {
  340. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  341. $this->error(__('You have no permission'));
  342. }
  343. }
  344. if ($this->request->isPost()) {
  345. $params = $this->request->post("row/a");
  346. if (!$params) {
  347. $this->error(__('Parameter %s can not be empty', ''));
  348. }
  349. $params = $this->preExcludeFields($params);
  350. Db::startTrans();
  351. try {
  352. //是否采用模型验证
  353. if ($this->modelValidate) {
  354. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  355. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  356. $row->validateFailException(true)->validate($validate);
  357. }
  358. if (!empty($params['jewel_add'])) {//钻石充值
  359. $userWhere['id'] = $row['id'];
  360. $user = Db::name('user')->where($userWhere)->find();
  361. $userwallet = Db::name('user_wallet')->where('user_id',$row['id'])->find();
  362. $jewelRes = model('Wallet')->lockChangeAccountRemain($row['id'],$params['jewel_add'],'+',0,'钻石充值',17,'jewel');
  363. if ($jewelRes['status'] == false) {
  364. Db::rollback();
  365. $this->error($jewelRes['msg']);
  366. }
  367. $params['jewel'] = bcadd($userwallet['jewel'],$params['jewel_add']);
  368. //充值日志记录
  369. //判断是否首充
  370. $jewellogWhere['user_id'] = $row['id'];
  371. $jewellogWhere['type'] = 1;
  372. $userJewelLog = model('UserJewelLog')->where($jewellogWhere)->find();
  373. $isFirst = 1;
  374. if (!empty($userJewelLog)) {
  375. $isFirst = 0;
  376. }
  377. $preUserId = $user['pre_userid'];
  378. $userRechargeLogRes = model('UserRechargeLog')->addRecord($row['id'], $params['jewel_add'], $userwallet['money'], $params['jewel'], $userwallet['money'], 4, 4,$isFirst,$preUserId);
  379. if (!$userRechargeLogRes) {
  380. throw new Exception('充值记录生成失败');
  381. }
  382. }
  383. $result = $row->allowField(true)->save($params);
  384. if ($result == false) {
  385. throw new Exception(__('No rows were updated'));
  386. }
  387. Db::commit();
  388. $this->success();
  389. } catch (ValidateException|PDOException|Exception $e) {
  390. Db::rollback();
  391. $this->error($e->getMessage());
  392. }
  393. }
  394. $this->view->assign("row", $row);
  395. return $this->view->fetch();
  396. }
  397. }