Register.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\UsersModel;
  4. use app\common\controller\Api;
  5. use think\Cache;
  6. /**
  7. * 注册接口
  8. */
  9. class Register extends Api
  10. {
  11. protected $noNeedRight = '*';
  12. protected $noNeedLogin = '*';
  13. /**
  14. * 手机号注册
  15. *
  16. * @param string $user_tel 账号
  17. * @param string $code 验证码
  18. * @param string $user_tjtel 推荐人手机号
  19. * @param string $user_pwd 密码
  20. * @param string $type 注册方式0手机号1QQ或微信
  21. * @param string $user_avatar 头像
  22. * @param string $user_openID QQ或微信开放id
  23. * @param string $user_nickname QQ或微信开放姓名
  24. */
  25. public function register()
  26. {
  27. $params = $this->request->post();
  28. if (isset($params['user_tel'])) { //验证手机号是否合法
  29. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  30. if (preg_match($check, $params['user_tel'])) {
  31. if (isset($params['user_tjtel'])) { //验证推荐人手机号
  32. $issetTjTel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
  33. if (!$issetTjTel) {
  34. return $this->result('未找到推荐人手机号', '', 100);
  35. }
  36. } else {
  37. return $this->result('请填写推荐人手机号', [], 100);
  38. }
  39. if (isset($params['user_tel'])) { //验证用户手机号
  40. $issetTjTel = UsersModel::where('user_tel', $params['user_tel'])->find();
  41. if ($issetTjTel) {
  42. return $this->result('此手机号已被注册', '', 100);
  43. }
  44. } else {
  45. return $this->result('请填写手机号', [], 100);
  46. }
  47. if (!isset($params['code']) || !Cache::get($params['code'])) { //验证验证码是否错误
  48. return $this->result('验证码错误', '', 100);
  49. }
  50. $userModel = new UsersModel(); //实例化usermodel
  51. if ($params['type'] == 0) { //使用手机号注册
  52. $rules = [
  53. 'code' => 'require|number',
  54. 'user_pwd' => 'require|max:18|min:6'
  55. ];
  56. $msg = [
  57. 'code.require' => "验证码不能为空",
  58. 'code.number' => "验证码必须为数字",
  59. 'user_pwd.require' => '密码不能为空',
  60. 'user_pwd.max' => '密码长度过长',
  61. 'user_pwd.min' => '密码最少六位',
  62. ];
  63. $validata = $this->validate($params, $rules, $msg);
  64. if (is_string($validata)) {
  65. return $this->result($validata, [], 100);
  66. }
  67. $data = array(
  68. 'user_nickname' => '优-' . rand(10000, 99999),
  69. 'user_tel' => $params['user_tel'],
  70. 'user_pwd' => sha1(md5($params['user_pwd'])),
  71. 'user_avatar' => config('site.httpurl') . '/uploads/logo.img',
  72. 'create_time' => date('Y-m-d H:i:s', time()),
  73. 'user_tjtel' => $params['user_tjtel'],
  74. );
  75. $addUser = $userModel->allowField(true)->save($data);
  76. if ($addUser) {
  77. Cache::rm($params['code']);//删除验证码缓存
  78. return $this->result('注册成功', [], 200);
  79. } else {
  80. return $this->result('注册失败', [], 100);
  81. }
  82. }
  83. if ($params['type'] == 1) {
  84. $rules = [
  85. 'user_nickname' => 'require',
  86. 'user_avatar' => 'require',
  87. 'user_openid' => 'require',
  88. ];
  89. $msg = [
  90. 'user_nickname.require' => '昵称未获取',
  91. 'user_avatar.require' => '头像未获取',
  92. 'user_openid.require' => '开放id未获取',
  93. ];
  94. $validata = $this->validate($params, $rules, $msg);
  95. if (is_string($validata)) {
  96. return $this->result($validata, [], 100);
  97. }
  98. $data = array(
  99. 'user_nickname' => $params['user_nickname'],
  100. 'user_tel' => $params['user_tel'],
  101. 'user_pwd' => '无',
  102. 'user_avatar' => $params['user_avatar'],
  103. 'create_time' => date('Y-m-d H:i:s', time()),
  104. 'user_tjtel' => $params['user_tjtel'],
  105. 'user_openid' => $params['user_openid'],
  106. );
  107. $addUser = $userModel->allowField(true)->save($data);
  108. if ($addUser) {
  109. Cache::rm($params['code']);//删除验证码缓存
  110. return $this->result('注册成功', [], 200);
  111. } else {
  112. return $this->result('注册失败', [], 100);
  113. }
  114. }
  115. } else {
  116. return $this->result('手机号不合法', '', 100);
  117. }
  118. } else {
  119. return $this->result('手机号不存在', '', 100);
  120. }
  121. }
  122. /**
  123. * 手机号短信发送
  124. *
  125. * @param string $user_tel 账号
  126. */
  127. public function registerTel()
  128. {
  129. $sendUrl = config('site.sendurl'); //短信接口的URL
  130. $params = $this->request->post();
  131. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  132. if (isset($params['user_tel'])) {
  133. if (preg_match($check, $params['user_tel'])) {
  134. $issettel = UsersModel::where('user_tel', $params['user_tel'])->find(); //判断手机号是否存在
  135. if ($issettel) {
  136. return $this->result('该手机号已存在', [], 100);
  137. }
  138. $code = $this->setCode();
  139. $tpl_value = '#code#=' . $code . '&#company#=优享街';
  140. $smsConf = array(
  141. 'key' => config('site.key'), //您申请的APPKEY
  142. 'mobile' => $params['user_tel'], //接受短信的用户手机号码
  143. 'tpl_id' => '203667', //您申请的短信模板ID,根据实际情况修改
  144. 'tpl_value' => $tpl_value //您设置的模板变量,根据实际情况修改
  145. );
  146. $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
  147. if ($content) {
  148. $result = json_decode($content, true);
  149. $error_code = $result['error_code'];
  150. if ($error_code == 0) {
  151. return $this->result('发送成功', $code, '200');
  152. } else {
  153. return $this->result('请求失败', [], '100');
  154. }
  155. }
  156. } else {
  157. return $this->result('手机号不合法', [], 100);
  158. }
  159. } else {
  160. return $this->result('手机号不能为空', [], 100);
  161. }
  162. }
  163. //发送短信
  164. /**
  165. * 请求接口返回内容
  166. * @param string $url [请求的URL地址]
  167. * @param string $params [请求的参数]
  168. * @param int $ipost [是否采用POST形式]
  169. * @return string
  170. */
  171. function juhecurl($url, $params = false, $ispost = 0)
  172. {
  173. $httpInfo = array();
  174. $ch = curl_init();
  175. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  176. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
  177. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  178. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  179. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  180. if ($ispost) {
  181. curl_setopt($ch, CURLOPT_POST, true);
  182. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  183. curl_setopt($ch, CURLOPT_URL, $url);
  184. } else {
  185. if ($params) {
  186. curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
  187. } else {
  188. curl_setopt($ch, CURLOPT_URL, $url);
  189. }
  190. }
  191. $response = curl_exec($ch);
  192. if ($response === FALSE) {
  193. //echo "cURL Error: " . curl_error($ch);
  194. return false;
  195. }
  196. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  197. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  198. curl_close($ch);
  199. return $response;
  200. }
  201. //生成唯一六位随机数
  202. public function setCode()
  203. {
  204. $code = rand('1000', '9999');
  205. if (Cache::get($code)) {
  206. $code = self::setCode();
  207. }
  208. Cache::set($code, $code, 600);
  209. return $code;
  210. }
  211. }