| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- /**
- * User: nickbai
- * Date: 2017/10/24 10:46
- * Email: 1902822973@qq.com
- */
- namespace app\index\controller;
- use think\Controller;
- use think\Db;
- class Register extends Controller
- {
- // 注册首页
- public function index()
- {
- $this->assign([
- 'version' => config('version')
- ]);
- return $this->fetch();
- }
- // 处理注册
- public function doRegister()
- {
- if (request()->isPost()) {
- $userName = input("param.user_name/s");
- $userEmail = input("param.user_email/s");
- $password = input("param.password/s");
- $phone = input("param.phone/s");
- $emailCode = input("param.code/s");
- if (empty($userName)) {
- return json(['code' => -1, 'data' => '', 'msg' => '用户名不能为空']);
- }
- if (empty($userEmail)) {
- return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
- }
- if (empty($password)) {
- return json(['code' => -3, 'data' => '', 'msg' => '密码不能为空']);
- }
- if (empty($phone)) {
- return json(['code' => -4, 'data' => '', 'msg' => '密码不能为空']);
- }
- if (empty($emailCode)) {
- return json(['code' => -5, 'data' => '', 'msg' => '验证码不能为空']);
- }
- if ($emailCode != session('code')) {
- return json(['code' => -6, 'data' => '', 'msg' => '验证码不正确']);
- }
- $name = db('accounts')->where('account_name', $userName)->find();
- if (!empty($name)) {
- return json(['code' => -7, 'data' => '', 'msg' => '用户名已存在']);
- }
- $email = db('accounts')->where('account_email', $userEmail)->find();
- if (!empty($email)) {
- return json(['code' => -8, 'data' => '', 'msg' => '邮箱已存在']);
- }
- // 添加用户信息
- $userInfo = [
- 'account_name' => $userName,
- 'account_email' => $userEmail,
- 'password' => md5($password . config('salt')),
- 'account_phone' => $phone,
- 'status' => 1,
- 'add_time' => time(),
- 'last_login_time' => time()
- ];
- $user_id = db('accounts')->insertGetId($userInfo);
- // 注册成功 生成token
- $module = mt_rand(100000, 999999);
- $token = base64_encode($module . '#$@%!^*/' . time() . '/' . $userInfo['id']);
- // 更新用户状态
- $param = [
- 'token' => $token,
- 'expire_time' => time(),
- ];
- db('accounts')->where('id', $user_id)->update($param);
- return json(['code' => 1, 'data' => url('user/index'), 'msg' => '注册成功']);
- }
- }
- //自动注册
- public function autoReg()
- {
- $appid = input("post.appid/s");
- $appuid = input("post.appuid/s");
- $token = trim(input("post.token/s"));
- $nowuid = (empty($appuid) || empty($appuid)) ? uniqid('anon_') : $appid . '_' . $appuid;
- $avatar = "http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg"; //默认头像
- if ($token) {
- $old = db('accounts')->where(['token' => $token])->find();
- if ($old) {
- if ($old['status'] != 1) {
- return json(['code' => 0, 'data' => [], 'msg' => '禁止登陆']);
- }
- return json(['code' => 1, 'data' => ['id' => $old['id'], 'name' => $old['account_name'], 'token' => $old['token'], 'avatar' => $avatar], 'msg' => '注册成功']);
- }
- }
- $now = time();
- $token = md5(uniqid() . rand(10000, 50000));
- $newdata = [
- 'account_name' => $nowuid,
- 'password' => md5(time() . rand(1, 5000)),
- 'status' => 1,
- 'add_time' => $now,
- 'last_login_time' => $now,
- 'token' => $token,
- 'expire_time' => $now,
- ];
- $retid = Db::name('accounts')->insertGetId($newdata);
- if ($retid) {
- return json(['code' => 1, 'data' => ['id' => $retid, 'name' => $nowuid, 'token' => $token, 'avatar' => $avatar], 'msg' => '注册成功']);
- } else {
- return json(['code' => 0, 'data' => [], 'msg' => '注册用户失败']);
- }
- }
- }
|