FakeUser.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\controller\shopro\data;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. /**
  6. * 虚拟用户
  7. */
  8. class FakeUser extends Common
  9. {
  10. protected $noNeedRight = ['select', 'getRandom'];
  11. /**
  12. * Faq模型对象
  13. * @var \app\admin\model\shopro\data\Express
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\shopro\data\FakeUser;
  20. }
  21. /**
  22. * 查看
  23. *
  24. * @return string|Json
  25. * @throws \think\Exception
  26. * @throws DbException
  27. */
  28. public function index()
  29. {
  30. if (!$this->request->isAjax()) {
  31. return $this->view->fetch();
  32. }
  33. $list = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  34. $this->success('', null, $list);
  35. }
  36. /**
  37. * 随机获取一个虚拟用户
  38. *
  39. * @return void
  40. */
  41. public function getRandom()
  42. {
  43. $userFake = $this->model->orderRaw('rand()')->find();
  44. $userFake ? $this->success('获取成功', null, $userFake) : $this->error('请在数据维护中添加虚拟用户');
  45. }
  46. public function select()
  47. {
  48. if (!$this->request->isAjax()) {
  49. return $this->view->fetch();
  50. }
  51. $list = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  52. $this->success('', null, $list);
  53. }
  54. /**
  55. * 添加
  56. */
  57. public function add()
  58. {
  59. if (!$this->request->isAjax()) {
  60. return $this->view->fetch();
  61. }
  62. $params = $this->request->only(['username', 'nickname', 'mobile', 'password', 'avatar', 'gender', 'email']);
  63. $this->svalidate($params, '.add');
  64. $this->model->save($params);
  65. $this->success('保存成功', null, $this->model);
  66. }
  67. /**
  68. * 随机生成用户
  69. */
  70. public function random()
  71. {
  72. if (!$this->request->isAjax()) {
  73. return $this->view->fetch();
  74. }
  75. set_time_limit(0);
  76. $num = $this->request->param('num', 1);
  77. for ($i = 0; $i < $num; $i++) {
  78. $style = [
  79. 'adventurer',
  80. 'adventurer-neutral',
  81. 'big-smile',
  82. 'big-ears-neutral',
  83. 'bottts',
  84. 'croodles',
  85. 'croodles-neutral',
  86. 'fun-emoji',
  87. 'icons',
  88. 'identicon',
  89. 'lorelei',
  90. 'lorelei-neutral',
  91. 'micah',
  92. 'notionists'
  93. ];
  94. $username = gen_random_str(mt_rand(6, 15), mt_rand(0, 1));
  95. $avatarSources = [
  96. // "https://joeschmoe.io/api/v1/random", // 生成的是 svg ,无法使用
  97. "https://api.dicebear.com/7.x/%s/png" . "?seed=" . $username . '&size=256',
  98. "https://api.multiavatar.com/" . $username . ".png"
  99. ];
  100. $avatar_url = $avatarSources[array_rand($avatarSources)];
  101. $avatar_url = sprintf($avatar_url, $style[array_rand($style)]);
  102. $store_path = '/uploads/' . date('Ymd') . '/' . md5(time() . mt_rand(1000, 9999)) . '.png'; // 存数据库路径
  103. $save_path = ROOT_PATH . 'public' . $store_path; // 服务器绝对路径
  104. image_resize_save($avatar_url, $save_path);
  105. $fakeUser = new \app\admin\model\shopro\data\FakeUser();
  106. $fakeUser->username = $username;
  107. $fakeUser->nickname = $username;
  108. $fakeUser->mobile = random_mobile();
  109. $fakeUser->password = gen_random_str();
  110. $fakeUser->avatar = cdnurl($store_path, true); // 这里存了完整路径
  111. $fakeUser->gender = mt_rand(0, 1);
  112. $fakeUser->email = random_email($fakeUser->mobile);
  113. $fakeUser->save();
  114. }
  115. $this->success('生成成功');
  116. }
  117. /**
  118. * 详情
  119. *
  120. * @param $id
  121. * @return \think\Response
  122. */
  123. public function detail($id)
  124. {
  125. $detail = $this->model->where('id', $id)->find();
  126. if (!$detail) {
  127. $this->error(__('No Results were found'));
  128. }
  129. $this->success('获取成功', null, $detail);
  130. }
  131. /**
  132. * 编辑(支持批量)
  133. */
  134. public function edit($id = null)
  135. {
  136. if (!$this->request->isAjax()) {
  137. return $this->view->fetch('add');
  138. }
  139. $params = $this->request->only(['username', 'nickname', 'mobile', 'password', 'avatar', 'gender', 'email']);
  140. $list = $this->model->where('id', 'in', $id)->select();
  141. $result = Db::transaction(function () use ($list, $params) {
  142. $count = 0;
  143. foreach ($list as $item) {
  144. $params['id'] = $item->id;
  145. $this->svalidate($params);
  146. $count += $item->save($params);
  147. }
  148. return $count;
  149. });
  150. if ($result) {
  151. $this->success('更新成功', null, $result);
  152. } else {
  153. $this->error('更新失败');
  154. }
  155. }
  156. /**
  157. * 删除(支持批量)
  158. *
  159. * @param $id
  160. * @return \think\Response
  161. */
  162. public function delete($id)
  163. {
  164. if (empty($id)) {
  165. $this->error(__('Parameter %s can not be empty', 'id'));
  166. }
  167. $list = $this->model->where('id', 'in', $id)->select();
  168. $result = Db::transaction(function () use ($list) {
  169. $count = 0;
  170. foreach ($list as $item) {
  171. $count += $item->delete();
  172. }
  173. return $count;
  174. });
  175. if ($result) {
  176. $this->success('删除成功', null, $result);
  177. } else {
  178. $this->error(__('No rows were deleted'));
  179. }
  180. }
  181. }