Common.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\api\controller;
  9. use app\api\controller\base\Base;
  10. use app\common\model\User;
  11. class Common extends Base
  12. {
  13. //注册
  14. public function register()
  15. {
  16. $post = $this->request->param();
  17. $validate = new \think\Validate([
  18. ['openid', 'max:50'],
  19. ['unionid', 'max:50'],
  20. ['nickname|昵称', 'require|max:50'],
  21. ['head_pic|头像', 'require|max:255'],
  22. ['sex|性别', 'in:1,2'],
  23. ['country|国家', 'max:50'],
  24. ['province|省份', 'max:50'],
  25. ['city|城市', 'max:50'],
  26. ]);
  27. if (!$validate->check($post)) {
  28. $this->json_error('提交失败:' . $validate->getError());
  29. }
  30. $user = new User();
  31. $data = [
  32. 'openid' => $post['openid']??'',
  33. 'unionid' => $post['unionid']??'',
  34. 'passport' => $post['openid']??'',
  35. 'nickname' => $post['nickname'],
  36. 'user_type' => User::TYPE_WECHAT,
  37. 'user_cate' => User::CATE_USER,
  38. 'head_pic' => $post['head_pic'],
  39. 'status' => User::STATUS_PASS,
  40. 'ip' => $this->request->ip(),
  41. 'sex' => $post['sex']??0,
  42. 'country' => $post['country']??'',
  43. 'province' => $post['province']??'',
  44. 'city' => $post['city']??'',
  45. ];
  46. if (false == $user->allowField(true)->save($data)) {
  47. $this->json_error('添加失败');
  48. } else {
  49. $this->json_success('添加成功', 'index');
  50. }
  51. }
  52. }