User.php 15 KB

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