Register.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\UsersModel;
  4. use app\common\controller\Api;
  5. use fast\Random;
  6. use think\Cache;
  7. use think\Controller;
  8. use think\Db;
  9. /**
  10. * 注册接口
  11. */
  12. class Register extends Controller
  13. {
  14. protected $noNeedRight = '*';
  15. protected $noNeedLogin = '*';
  16. /**
  17. * 手机号注册
  18. * @ApiMethod (POST)
  19. * @param string $user_tel 账号
  20. * @param string $code 验证码
  21. * @param string $user_tjtel 推荐人手机号
  22. * @param string $user_pwd 密码
  23. * @param string $type 默认0
  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 json(['code' => 100, 'msg' => '未找到推荐人手机号','data' => []]);
  35. }
  36. $addUserMember = Db::name('Users')->where('user_tel', $params['user_tjtel'])->setInc('user_member_count', 1); // 给上机的下级成员总数加1
  37. } else {
  38. return json(['code' => 100, 'msg' => '请填写推荐人手机号','data' => []]);
  39. }
  40. if (isset($params['user_tel'])) { //验证用户手机号
  41. $issetTjTel = UsersModel::where('user_tel', $params['user_tel'])->find();
  42. if ($issetTjTel) {
  43. return json(['code' => 100, 'msg' => '此手机号已被注册','data' => []]);
  44. }
  45. } else {
  46. return json(['code' => 100, 'msg' => '请填写手机号','data' => []]);
  47. }
  48. if (!isset($params['code']) || !Cache::get($params['code'])) { //验证验证码是否错误
  49. return json(['code' => 100, 'msg' => '验证码错误','data' => []]);
  50. }
  51. $userModel = new UsersModel(); //实例化usermodel
  52. if ($params['type'] == 0) { //使用手机号注册
  53. $rules = [
  54. 'code' => 'require|number',
  55. 'user_pwd' => 'require|max:18|min:6'
  56. ];
  57. $msg = [
  58. 'code.require' => "验证码不能为空",
  59. 'code.number' => "验证码必须为数字",
  60. 'user_pwd.require' => '密码不能为空',
  61. 'user_pwd.max' => '密码长度过长',
  62. 'user_pwd.min' => '密码最少六位',
  63. ];
  64. $validata = $this->validate($params, $rules, $msg);
  65. if (is_string($validata)) {
  66. return json(['code' => 100, 'msg' => $validata,'data' => []]);
  67. }
  68. $user_avatar = config('site.httpurl') . '/uploads/logo.png';
  69. $data = array(
  70. 'user_nickname' => '优-' . rand(10000, 99999),
  71. 'user_tel' => $params['user_tel'],
  72. 'user_pwd' => sha1(md5($params['user_pwd'])),
  73. 'user_avatar' => $user_avatar,
  74. 'create_time' => date('Y-m-d H:i:s', time()),
  75. 'user_tjtel' => $params['user_tjtel'],
  76. 'type' => 0,
  77. );
  78. $addUser = $userModel->allowField(true)->save($data);
  79. if ($addUser) {
  80. Cache::rm($params['code']);//删除验证码缓存
  81. return json(['code' => 200, 'msg' => '注册成功','data' => []]);
  82. } else {
  83. return json(['code' => 100, 'msg' => '注册失败','data' => []]);
  84. }
  85. }
  86. } else {
  87. return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
  88. }
  89. } else {
  90. return json(['code' => 100, 'msg' => '手机号不存在','data' => []]);
  91. }
  92. }
  93. /**
  94. * QQ微信注册
  95. * @ApiMethod (POST)
  96. * @param string $type 默认1
  97. * @param string $user_avatar 头像
  98. * @param string $user_uniodid QQ或微信开放id
  99. * @param string $user_nickname QQ或微信开放姓名
  100. */
  101. public function wechatRegister()
  102. {
  103. $params = $this->request->post();
  104. $rules = [
  105. 'user_nickname' => 'require',
  106. 'user_avatar' => 'require',
  107. 'user_unionid' => 'require',
  108. 'type' => 'require',
  109. ];
  110. $msg = [
  111. 'user_nickname.require' => '昵称未获取',
  112. 'user_avatar.require' => '头像未获取',
  113. 'user_unionid.require' => '开放id未获取',
  114. 'type.require' => '开放id未获取',
  115. ];
  116. $validata = $this->validate($params, $rules, $msg);
  117. if (is_string($validata)) {
  118. return json(['code' => 200, 'msg' => '','data' => $validata]);
  119. }
  120. $data = array(
  121. 'user_nickname' => $params['user_nickname'],
  122. 'user_avatar' => $params['user_avatar'],
  123. 'create_time' => date('Y-m-d H:i:s', time()),
  124. 'user_unionid' => $params['user_unionid'],
  125. 'type' => $params['type'],
  126. );
  127. $addUser = Db::name('Users')->insertGetId($data);
  128. $token = self::settoken($addUser);
  129. $res = array(
  130. 'user_id' => $addUser,
  131. 'token' => $token,
  132. );
  133. if ($addUser) {
  134. return json(['code' => 10, 'msg' => '请绑定手机号','data' => []]);
  135. } else {
  136. return json(['code' => 100, 'msg' => '注册失败','data' => []]);
  137. }
  138. }
  139. /**
  140. * 注册添加手机号
  141. * @ApiMethod (POST)
  142. * @param string $user_id 用户ID
  143. * @param string $user_tel 用户手机号
  144. * @param string $code 验证码
  145. * @param string $user_tjtel 用户手机号
  146. */
  147. public function addTel()
  148. {
  149. $params = $this->request->post();
  150. $rules = [
  151. 'user_id' => 'require|number',
  152. 'code' => 'require|number',
  153. 'user_tel' => 'require|number',
  154. 'user_tjtel' => 'require|number',
  155. ];
  156. $msg = [
  157. 'user_id.require' => '网络错误1',
  158. 'code .require' => '网络错误2',
  159. 'user_tel.require' => '手机号不能为空',
  160. 'user_tjtel.require' => '推荐人手机号不能为空',
  161. 'user_id.number' => '网络错误4',
  162. 'code.number' => '网络错误5',
  163. 'user_tel.number' => '网络错误6',
  164. 'user_tjtel.number' => '网络错误7',
  165. ];
  166. $validate = $this->validate($params, $rules, $msg);
  167. if (is_string($validate)) {
  168. return json(['code' => 100, 'msg' => $validate,'data' => []]);
  169. }
  170. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  171. if (!preg_match($check, $params['user_tel'])) {
  172. return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
  173. }
  174. //检查数据库是否有此推荐人手机号
  175. $user_tjtel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
  176. if (!$user_tjtel) {
  177. return json(['code' => 100, 'msg' => '无此推荐人','data' => []]);
  178. }
  179. //判断验证码是否正确
  180. if (!Cache::get($params['code'])) {
  181. return json(['code' => 100, 'msg' => '验证码不正确','data' => []]);
  182. }
  183. Cache::rm($params['code']);
  184. $upddata = array(
  185. 'user_tel' => $params['user_tel'],
  186. 'user_tjtel' => $params['user_tjtel'],
  187. 'type' => 1,
  188. );
  189. $updUser_tel = Db::name('users')->where('user_id', $params['user_id'])->update($upddata);
  190. if ($updUser_tel) {
  191. $token = self::settoken($params['user_id']);
  192. $res = array(
  193. "user_id" => $params['user_id'],
  194. "token" => $token,
  195. );
  196. return json(['code' => 200, 'msg' => '绑定成功','data' => []]);
  197. } else {
  198. Db::name('users')->where('user_id', $params['user_id'])->delete(); // 绑定失败删除此用户
  199. return json(['code' => 100, 'msg' => '绑定失败','data' => []]);
  200. }
  201. }
  202. /**
  203. * 手机号短信发送
  204. * @ApiMethod (POST)
  205. * @param string $user_tel 账号
  206. */
  207. public function registerTel()
  208. {
  209. $sendUrl = config('site.sendurl'); //短信接口的URL
  210. $params = $this->request->post();
  211. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  212. if (isset($params['user_tel'])) {
  213. if (preg_match($check, $params['user_tel'])) {
  214. $issettel = UsersModel::where('user_tel', $params['user_tel'])->find(); //判断手机号是否存在
  215. $code = $this->setCode();
  216. $tpl_value = '#code#=' . $code . '&#company#=优享街';
  217. $smsConf = array(
  218. 'key' => config('site.key'), //您申请的APPKEY
  219. 'mobile' => $params['user_tel'], //接受短信的用户手机号码
  220. 'tpl_id' => '203667', //您申请的短信模板ID,根据实际情况修改
  221. 'tpl_value' => $tpl_value //您设置的模板变量,根据实际情况修改
  222. );
  223. $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
  224. if ($content) {
  225. $result = json_decode($content, true);
  226. $error_code = $result['error_code'];
  227. if ($error_code == 0) {
  228. return json(['code' => 200, 'msg' => '发送成功','data' => $code]);
  229. } else {
  230. return json(['code' => 100, 'msg' => '请求失败','data' => []]);
  231. }
  232. }
  233. } else {
  234. return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
  235. }
  236. } else {
  237. return json(['code' => 100, 'msg' => '手机号不能为空','data' => []]);
  238. }
  239. }
  240. /**
  241. * 请求接口返回内容
  242. * @ApiInternal
  243. * @param string $url [请求的URL地址]
  244. * @param string $params [请求的参数]
  245. * @param int $ipost [是否采用POST形式]
  246. * @return string
  247. */
  248. function juhecurl($url, $params = false, $ispost = 0)
  249. {
  250. $httpInfo = array();
  251. $ch = curl_init();
  252. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  253. 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');
  254. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  255. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  256. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  257. if ($ispost) {
  258. curl_setopt($ch, CURLOPT_POST, true);
  259. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  260. curl_setopt($ch, CURLOPT_URL, $url);
  261. } else {
  262. if ($params) {
  263. curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
  264. } else {
  265. curl_setopt($ch, CURLOPT_URL, $url);
  266. }
  267. }
  268. $response = curl_exec($ch);
  269. if ($response === FALSE) {
  270. //echo "cURL Error: " . curl_error($ch);
  271. return false;
  272. }
  273. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  274. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  275. curl_close($ch);
  276. return $response;
  277. }
  278. //生成唯一六位随机数
  279. /**
  280. * 生成唯一六位随机数
  281. *
  282. * 可以通过@ApiInternal忽略请求的方法
  283. * @ApiInternal
  284. */
  285. public function setCode()
  286. {
  287. $code = rand('1000', '9999');
  288. if (Cache::get($code)) {
  289. $code = self::setCode();
  290. }
  291. Cache::set($code, $code, 600);
  292. return $code;
  293. }
  294. /**
  295. * 生成token
  296. *
  297. * 可以通过@ApiInternal忽略请求的方法
  298. * @ApiInternal
  299. */
  300. public function settoken($user_id)
  301. {
  302. $token = Random::uuid();
  303. \app\common\library\Token::set($token, $user_id, 0);
  304. return $tokenInfo = \app\common\library\Token::get($token);
  305. }
  306. }