User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php namespace Puser\Manage\Lib;
  2. use Dever;
  3. class User
  4. {
  5. # 后台创建用户
  6. public function createStart($db, $data)
  7. {
  8. if (isset($data['import'])) {
  9. if (!$data['import']) {
  10. Dever::error('请选择导入文件');
  11. }
  12. Dever::load(\Puser\Manage\Lib\Excel::class)->import($data['import']);
  13. return 'end';
  14. }
  15. if (isset($data['mobile']) && $data['mobile']) {
  16. $data['type'] = 1;
  17. if (isset($data['id']) && $data['id'] > 0) {
  18. $info = Dever::db('puser/info')->find(['mobile' => $data['mobile']]);
  19. if ($info) {
  20. Dever::error('手机号'.$data['mobile'].'已存在');
  21. }
  22. } else {
  23. $info = Dever::db('puser/info')->find(['mobile' => $data['mobile']]);
  24. if ($info) {
  25. Dever::error('手机号'.$data['mobile'].'已存在');
  26. }
  27. if (empty($data['name'])) {
  28. $data['name'] = Dever::load(\Puser\Lib\Info::class)->createName($data['mobile']);
  29. }
  30. if (empty($data['avatar'])) {
  31. $data['avatar'] = Dever::load(\Puser\Lib\Info::class)->createAvatar($data['name']);
  32. }
  33. }
  34. }
  35. if (isset($data['sales']) && $data['sales']) {
  36. $data['sales'] = explode(',', $data['sales']);
  37. $data['sales_type'] = $data['sales'][0];
  38. $data['sales_id'] = $data['sales'][1];
  39. unset($data['sales']);
  40. }
  41. if (isset($data['parent_uid']) && $data['parent_uid'] > 0) {
  42. $length = strlen($data['parent_uid']);
  43. if ($length == 11) {
  44. $parent = Dever::db('puser/info')->find(['mobile' => $data['parent_uid'], 'status' => 1]);
  45. } else {
  46. $parent = Dever::db('puser/info')->find(['id' => $data['parent_uid'], 'status' => 1]);
  47. }
  48. if (!$parent) {
  49. Dever::error('推荐人' . $data['parent_uid'] . '不存在');
  50. }
  51. if (isset($data['id']) && $data['id']) {
  52. if ($data['id'] == $parent['id']) {
  53. Dever::error('推荐人' . $data['parent_uid'] . '不能是当前用户');
  54. }
  55. $info = Dever::db('puser/info')->find($data['id']);
  56. if ($info['parent_uid'] > 0) {
  57. Dever::error('当前用户已有推荐人,添加失败');
  58. }
  59. $childInfo = Dever::db('invite/relation')->find(['uid' => $data['id'], 'to_uid' => $parent['id']]);
  60. if ($childInfo) {
  61. Dever::error('推荐人' . $data['parent_uid'] . '是当前用户的下'.$childInfo['level'].'级,添加失败');
  62. }
  63. $parentInfo = Dever::db('invite/relation')->find(['uid' => $parent['id'], 'to_uid' => $data['id']]);
  64. if ($parentInfo) {
  65. Dever::error('推荐人' . $data['parent_uid'] . '是当前用户的上'.$parentInfo['level'].'级,添加失败');
  66. }
  67. }
  68. $data['parent_uid'] = $parent['id'];
  69. }
  70. return $data;
  71. }
  72. public function createEnd($db, $data)
  73. {
  74. if (isset($data['parent_uid']) && $data['parent_uid'] > 0) {
  75. Dever::load(\Invite\Lib\Relation::class)->set($data['parent_uid'], $data['id']);
  76. }
  77. Dever::load(\Pscore\Lib\Log::class)->action('注册')->add($data['id']);
  78. # 升级初始身份等级
  79. Dever::load(\Prole\Lib\Info::class)->up($data['id'], $data['sales_type'] ?? 0, $data['sales_id'] ?? 0);
  80. }
  81. # 获取用户名
  82. public function getName($uid)
  83. {
  84. $user = Dever::db('puser/info')->find($uid);
  85. return $user['name'] . ' | ' . $user['id'];
  86. }
  87. # 统一展示用户信息 'uid' => Dever::call('Puser/Manage/Lib/User.getInfo', ['{name}', '{mobile}']),
  88. public function getListInfo($title = '', $name = '', $mobile = '', $uid = '{uid}')
  89. {
  90. return [
  91. 'name' => $title ?: '用户信息',
  92. 'type' => 'popover',
  93. 'location' => 'right',
  94. 'align' => 'center',
  95. //'tag' => true,
  96. 'show' => 'Dever::call("Puser/Manage/Lib/User.getDesc", ["'.$uid.'", "'.$name.'", "'.$mobile.'", "'.$title.'"])',
  97. ];
  98. }
  99. # 统一做用户搜索信息
  100. public function getListSearch()
  101. {
  102. return [
  103. 'name' => '用户搜索',
  104. 'type' => 'select_text',
  105. 'option' => [
  106. ['id' => 1, 'name' => '用户ID', 'value' => 'uid'],
  107. ['id' => 2, 'name' => '用户名', 'value' => 'Puser/Manage/Lib/User.getUidByName'],
  108. ['id' => 3, 'name' => '用户手机号', 'value' => 'Puser/Manage/Lib/User.getUidByMobile'],
  109. ],
  110. ];
  111. }
  112. # 统一做用户搜索信息
  113. public function getListSearchParent()
  114. {
  115. return [
  116. 'name' => '推荐人搜索',
  117. 'type' => 'select_text',
  118. 'option' => [
  119. ['id' => 1, 'name' => '推荐人ID', 'value' => 'parent_uid'],
  120. ['id' => 2, 'name' => '推荐人用户名', 'value' => 'Puser/Manage/Lib/User.getParentUidByName'],
  121. ['id' => 3, 'name' => '推荐人手机号', 'value' => 'Puser/Manage/Lib/User.getParentUidByMobile'],
  122. ],
  123. ];
  124. }
  125. # 获取用户的身份列表
  126. public function getRole($uid)
  127. {
  128. $info = Dever::db('prole/info')->select(['status' => 1]);
  129. return Dever::load(\Manage\Lib\Util::class)->createTip(function(&$name, &$content) use ($uid, $info) {
  130. $time = time();
  131. foreach ($info as $v) {
  132. $user = Dever::db('prole/user')->find(['uid' => $uid, 'info_id' => $v['id'], 'status' => 1]);
  133. if ($user) {
  134. if ($user['edate'] < $time) {
  135. Dever::db('prole/user')->update($v['id'], ['status' => 2]);
  136. } else {
  137. $role = Dever::db('prole/info')->find($user['info_id']);
  138. $level = Dever::db('prole/level')->find($user['level_id']);
  139. $content[$v['id']] = ['name' => $role['name'], 'content' => $level['name'], 'role_id' => $role['id'], 'level_id' => $level['id']];
  140. if ($name = '-') {
  141. $name = $level['name'];
  142. }
  143. }
  144. }
  145. }
  146. });
  147. }
  148. # 获取用户的积分列表
  149. public function getScore($uid)
  150. {
  151. $info = Dever::db('pscore/info')->select(['status' => 1]);
  152. return Dever::load(\Manage\Lib\Util::class)->createTip(function(&$name, &$content) use ($uid, $info) {
  153. foreach ($info as $v) {
  154. $user = Dever::db('pscore/user')->find(['uid' => $uid, 'info_id' => $v['id'], 'status' => 1]);
  155. if ($user) {
  156. $content[$v['id']] = ['name' => $v['name'], 'content' => $user['balance'], 'id' => $v['id']];
  157. if ($name = '-') {
  158. $score = Dever::load(\Pscore\Lib\Info::class)->getText($user['balance'], $v);
  159. $name = $score;
  160. }
  161. }
  162. }
  163. });
  164. }
  165. # 获取团队人数
  166. public function getGroup($id)
  167. {
  168. return Dever::load(\Invite\Lib\Relation::class)->getChildNum($id);
  169. }
  170. public function getDesc($uid, $username = '', $mobile = '', $title = '')
  171. {
  172. return Dever::load(\Manage\Lib\Util::class)->createTip(function(&$name, &$content) use ($uid, $username, $mobile, $title) {
  173. $user = Dever::db('puser/info')->find($uid);
  174. if ($user) {
  175. if (!$username) {
  176. $username = $user['name'];
  177. }
  178. if ($mobile) {
  179. $user['mobile'] = $mobile;
  180. }
  181. $content = [
  182. ['name' => '用户ID', 'content' => $user['id']],
  183. ['name' => '用户名', 'content' => $user['name']],
  184. ];
  185. if ($title) {
  186. $content[] = ['name' => $title, 'content' => $username];
  187. }
  188. $content[] = ['name' => '手机号', 'content' => $user['mobile']];
  189. $name = $username . ' | ' . $user['id'];
  190. }
  191. });
  192. }
  193. # 获取用户信息
  194. public function getMobile($mobile)
  195. {
  196. return $mobile;
  197. return \Dever\Helper\Str::hide($mobile);
  198. }
  199. # 后台为用户充值钱包
  200. public function wallet($db, $data)
  201. {
  202. if ($data['amount']) {
  203. Dever::load(\Puser\Lib\Wallet::class)->sync($data['uid']);
  204. }
  205. }
  206. # 后台为用户充值积分
  207. public function score($db, $data)
  208. {
  209. if ($data['amount']) {
  210. Dever::load(\Puser\Lib\Score::class)->sync($data['uid']);
  211. }
  212. }
  213. # 根据用户名称获取用户
  214. public function getUidByName($value)
  215. {
  216. # 直接查出数据
  217. //$user = Dever::db('puser/info')->columns(['name' => $value]);
  218. # 返回sql
  219. $user = Dever::db('puser/info')->sql(['name' => $value], ['col' => 'id']);
  220. return ['uid', 'in', $user];
  221. }
  222. # 根据用户手机号获取用户
  223. public function getUidByMobile($value)
  224. {
  225. $user = Dever::db('puser/info')->sql(['mobile' => $value], ['col' => 'id']);
  226. return ['uid', 'in', $user];
  227. }
  228. # 根据上级用户名称获取用户
  229. public function getParentUidByName($value)
  230. {
  231. # 返回sql
  232. $user = Dever::db('puser/info')->sql(['name' => $value], ['col' => 'id']);
  233. return ['parent_uid', 'in', $user];
  234. }
  235. # 根据上级用户手机号获取用户
  236. public function getParentUidByMobile($value)
  237. {
  238. $user = Dever::db('puser/info')->sql(['mobile' => $value], ['col' => 'id']);
  239. return ['parent_uid', 'in', $user];
  240. }
  241. # 根据用户收件姓名获取用户
  242. public function getUidByAddName($value)
  243. {
  244. $user = Dever::db('puser/address')->sql(['name' => $value], ['col' => 'uid']);
  245. return ['uid', 'in', $user];
  246. }
  247. # 根据用户收件手机号获取用户
  248. public function getUidByAddMobile($value)
  249. {
  250. $user = Dever::db('puser/address')->sql(['phone' => $value], ['col' => 'uid']);
  251. return ['uid', 'in', $user];
  252. }
  253. # 获取用户手机号验证规则
  254. public function getMobileRules()
  255. {
  256. return [
  257. # 规则1
  258. [
  259. # 必填
  260. 'required' => true,
  261. # 输入后触发
  262. 'trigger' => 'blur',
  263. # 提示信息
  264. 'message' => '请输入手机号',
  265. ],
  266. # 规则2
  267. [
  268. # 长度
  269. 'len' => 11,
  270. # 正则
  271. 'pattern' => Dever::rule('mobile', ''),
  272. 'trigger' => 'blur',
  273. # 提示信息
  274. 'message' => '手机号错误',
  275. # 验证类型 date,array,number,boolean,integer,float,url,email,enum,string
  276. 'type' => 'string',
  277. ],
  278. ];
  279. }
  280. # 创建用户
  281. public function createUser($sales_type, $sales_id, $name, $mobile, $uid = false)
  282. {
  283. if ($uid) {
  284. $info = Dever::db('puser/info')->find($uid);
  285. if ($info) {
  286. if ($info['mobile'] == $mobile) {
  287. return false;
  288. } else {
  289. $info = Dever::db('puser/info')->find(['mobile' => $mobile, 'id' => ['!=', $info['id']]]);
  290. if ($info) {
  291. return $info['id'];
  292. }
  293. Dever::db('puser/info')->update($uid, ['mobile' => $mobile]);
  294. return false;
  295. }
  296. }
  297. }
  298. # 创建新账号
  299. $info = Dever::db('puser/info')->find(['mobile' => $mobile]);
  300. if ($info) {
  301. return $info['id'];
  302. } else {
  303. return Dever::load(\Puser\Lib\Info::class)->createUser($mobile, $sales_type, $sales_id, 0, $name);
  304. }
  305. }
  306. }