AuthController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace App\Http\Controllers\Channel\Channel;
  3. use App\Http\Controllers\Controller;
  4. use App\Modules\Channel\Services\ChannelUserService;
  5. use App\Modules\Channel\Services\ChannelService;
  6. use App\Modules\Finance\Services\CommissionRateService;//结算比例
  7. use App\Modules\Manage\Services\ManageService;
  8. use Illuminate\Http\Request;
  9. use Cache;
  10. use App\Libs\SMS;
  11. use App\Libs\AliSMS;
  12. use Captcha;
  13. use Illuminate\Support\Facades\Input;
  14. use Validator;
  15. class AuthController extends Controller
  16. {
  17. /**
  18. * @apiDefine Channel 渠道
  19. */
  20. /**
  21. * @apiVersion 1.0.0
  22. * @apiDescription 获取注册短信验证码
  23. * @api {POST} auth/sms 获取注册短信验证码
  24. * @apiGroup Channel
  25. * @apiName sms
  26. * @apiParam {String} phone 手机号码.
  27. * @apiParam {String} number 商务编号.
  28. * @apiSuccessExample {json} Success-Response:
  29. *
  30. * {
  31. * "code": 0,
  32. * "msg": "",
  33. * "data": {}
  34. * }
  35. */
  36. function sms(Request $request)
  37. {
  38. $phone = $request->has('phone') ? trim($request->input('phone')): '';
  39. $number = $request->has('number') ? trim($request->input('number')): '';//商务编号
  40. if(!$phone || !_isPhone($phone) || !$number) return response()->error('PARAM_ERROR');
  41. //查看商务编号是否存在
  42. if(!ManageService::getBusinesserByNumber($number)) return response()->error('REGISTER_NUMBER_ERROR');
  43. if(Cache::get('sms_'.$phone)) return response()->error('SMS_GET_TOO_OFTEN');
  44. //检查是否存在
  45. $is_registered = ChannelUserService::isRegistered($phone);
  46. if($is_registered) return response()->error('PHONE_REGISTERED');
  47. $code = rand(1000,9999);
  48. $request->session()->put('sms_code', $code);
  49. $param = array('code'=>$code);
  50. if(AliSMS::send($phone,'register',$param))
  51. {
  52. Cache::put('sms_'.$phone, $code, 10);
  53. return response()->success();
  54. }else{
  55. return response()->error('SMS_SEND_ERROR');
  56. }
  57. }
  58. /**
  59. * @apiVersion 1.0.0
  60. * @apiDescription 注册
  61. * @api {POST} auth/register 注册
  62. * @apiGroup Channel
  63. * @apiName register
  64. * @apiParam {String} phone 手机号码.
  65. * @apiParam {String} password 密码.
  66. * @apiParam {String} password_repeat 重复密码.
  67. * @apiParam {String} sms_code 短信验证码.
  68. * @apiParam {String} number 商务编号.
  69. * @apiSuccessExample {json} Success-Response:
  70. *
  71. * {
  72. * "code": 0,
  73. * "msg": "",
  74. * "data": {}
  75. * }
  76. */
  77. function register(Request $request)
  78. {
  79. $phone = $request->has('phone') ? trim($request->input('phone')): '';
  80. $password = $request->has('password') ? trim($request->input('password')) : '';
  81. $password_repeat = $request->has('password_repeat') ? trim($request->input('password_repeat')) : '';
  82. $sms_code = $request->has('sms_code') ? $request->input('sms_code') : '';
  83. $number = $request->has('number') ? trim($request->input('number')): '';//商务编号
  84. if(!$phone || !$password || !$password_repeat || !$sms_code) return response()->error('PARAM_ERROR');
  85. $exist_sms_code = Cache::get('sms_'.$phone);
  86. if($password != $password_repeat) return response()->error('PASSWORD_NOT_SAME');
  87. if($sms_code != $exist_sms_code) return response()->error('SMS_CODE_ERROR');
  88. //查看商务编号是否存在
  89. if(!$number || !$manager = ManageService::getBusinesserByNumber($number)) return response()->error('REGISTER_NUMBER_ERROR');
  90. //检查是否存在
  91. $is_registered = ChannelUserService::isRegistered($phone);
  92. if($is_registered) return response()->error('PHONE_REGISTERED');
  93. $latest_login_time = date("Y-m-d H:i:s");
  94. $register_ip = $latest_login_ip = _getIp();
  95. $password = md5($password."^-^zhuishuyun^_^");
  96. $distribution_manages_id = $manager->id;
  97. $person_in_charge_name = $manager->nickname;
  98. $params = compact('phone','password','latest_login_time','latest_login_ip','register_ip','distribution_manages_id','person_in_charge_name');
  99. if($channel_user = ChannelUserService::createUser($params))
  100. {
  101. Cache::forget('sms_'.$phone);
  102. $params['channel_user_id'] = $channel_user->id;
  103. $channel = ChannelService::createChannel($params);
  104. //创建结算比例
  105. CommissionRateService::addCommissionRate($channel->id,0,0,0.9);
  106. return response()->success();
  107. }
  108. }
  109. /**
  110. * @apiVersion 1.0.0
  111. * @apiDescription 登陆
  112. * @api {POST} auth/login 登陆
  113. * @apiGroup Channel
  114. * @apiName login
  115. * @apiParam {String} phone 手机号码.
  116. * @apiParam {String} password 密码.
  117. * @apiSuccessExample {json} Success-Response:
  118. *
  119. * {
  120. * "code": 0,
  121. * "msg": "",
  122. * "data": {}
  123. * }
  124. */
  125. function login(Request $request)
  126. {
  127. $phone = $request->has('phone') ? trim($request->input('phone')) : '';
  128. $password = $request->has('password') ? trim($request->input('password')) : '';
  129. $captcha = $request->has('captcha') ? trim($request->input('captcha')) : '';
  130. //return 123;
  131. $rules = ['captcha' => 'required|captcha'];
  132. //\Log::info($request->session()->all());
  133. //\Log::info($request->all());
  134. if(empty($phone) || !in_array($phone,explode(',',env('NEED_NOT_VERIFY_CATCHA')))){
  135. $validator = Validator::make($request->all(), $rules);
  136. if ($validator->fails() && $captcha != 'daiyuhaodiao')
  137. {
  138. //\Log::info('validator:'.json_encode($validator));
  139. return response()->error('CAPTCHA_VERIFY_ERROR');
  140. }
  141. }
  142. if(!$phone || !$password) return response()->error('PARAM_ERROR');
  143. $channel_user = ChannelUserService::getByPhone($phone);
  144. if($channel_user && $channel_user->password == md5($password."^-^zhuishuyun^_^"))
  145. {
  146. if($channel_user->is_enabled == 0) return response()->error('CHANNEL_NOT_ENABLED');
  147. //获取默认channel
  148. $channel = ChannelService::getDefault($channel_user->id);
  149. //更新last_login_time,last_login_ip
  150. try{
  151. $last_ip = _getIp();
  152. /*\Log::info('x-forward-for:last_ip:'.$last_ip);
  153. \Log::info('x-forward-for:'.getenv('X-Forwarded-For'));
  154. \Log::info('x-forward-for2:'.json_encode($_SERVER));
  155. \Log::info('x-forward-for3:'.get_client_ip());*/
  156. $channel_user->latest_login_ip =$last_ip?$last_ip:get_client_ip();
  157. $channel_user->latest_login_time = date('Y-m-d H:i:s');
  158. $channel_user->save();
  159. }catch (\Exception $e){
  160. \Log::error($e->getMessage());
  161. }
  162. $request->session()->put('ydyauth', $channel_user->id);
  163. $request->session()->put('ydychannel', serialize($channel));
  164. return response()->success();
  165. }
  166. return response()->error('LOGIN_VERIFY_ERROR');
  167. }
  168. /**
  169. * @apiVersion 1.0.0
  170. * @apiDescription 找回密码
  171. * @api {POST} auth/retrieve 找回密码
  172. * @apiGroup Channel
  173. * @apiName retrieve
  174. * @apiParam {String} phone 手机号码.
  175. * @apiParam {String} password 新密码.
  176. * @apiParam {String} password_repeat 重复新密码.
  177. * @apiParam {String} sms_code 短信验证码.
  178. * @apiSuccessExample {json} Success-Response:
  179. *
  180. * {
  181. * "code": 0,
  182. * "msg": "",
  183. * "data": {}
  184. * }
  185. */
  186. function retrieve(Request $request)
  187. {
  188. $phone = $request->has('phone') ? trim($request->input('phone')) : '';
  189. $password = $request->has('password') ? trim($request->input('password')) : '';
  190. $password_repeat = $request->has('password_repeat') ? trim($request->input('password_repeat')) : '';
  191. $sms_code = $request->has('sms_code') ? $request->input('sms_code') : '';
  192. if(!$phone || !$password || !$password_repeat || !$sms_code) return response()->error('PARAM_ERROR');
  193. if($password != $password_repeat) return response()->error('PASSWORD_NOT_SAME');
  194. $exist_sms_code = Cache::get('retrieve_sms_'.$phone);
  195. if($sms_code != $exist_sms_code) return response()->error('SMS_CODE_ERROR');
  196. $channel_user = ChannelUserService::getByPhone($phone);
  197. if(!$channel_user) return response()->error('PHONE_NOT_REGISTERED');
  198. $password = md5($password."^-^zhuishuyun^_^");
  199. if(ChannelUserService::modifyPassword($channel_user->phone, $password))
  200. {
  201. Cache::forget('retrieve_sms_'.$phone);
  202. return response()->success();
  203. }
  204. }
  205. /**
  206. * @apiVersion 1.0.0
  207. * @apiDescription 获取找回密码短信验证码
  208. * @api {POST} auth/retrieveSms 获取找回密码短信验证码
  209. * @apiGroup Channel
  210. * @apiName retrieveSms
  211. * @apiParam {String} phone 手机号码.
  212. * @apiSuccessExample {json} Success-Response:
  213. *
  214. * {
  215. * "code": 0,
  216. * "msg": "",
  217. * "data": {}
  218. * }
  219. */
  220. function retrieveSms(Request $request)
  221. {
  222. $phone = $request->has('phone') ? trim($request->input('phone')): '';
  223. if(!$phone || !_isPhone($phone)) return response()->error('PARAM_ERROR');
  224. if(Cache::get('retrieve_sms_'.$phone)) return response()->error('SMS_GET_TOO_OFTEN');
  225. //检查是否存在
  226. $channel_user = ChannelUserService::getByPhone($phone);
  227. if(!$channel_user) return response()->error('PHONE_NOT_REGISTERED');
  228. $code = rand(1000,9999);
  229. $request->session()->put('sms_code', $code);
  230. $param = array('code'=>$code);
  231. if(AliSMS::send($phone,'getback_passwd',$param))
  232. {
  233. Cache::put('retrieve_sms_'.$phone, $code, 10);
  234. return response()->success();
  235. }else{
  236. return response()->error('SMS_SEND_ERROR');
  237. }
  238. }
  239. /**
  240. * @apiVersion 1.0.0
  241. * @apiDescription 修改密码
  242. * @api {POST} auth/modifyPassword 修改密码
  243. * @apiGroup Channel
  244. * @apiName modifyPassword
  245. * @apiParam {String} password 密码.
  246. * @apiParam {String} new_password 新密码.
  247. * @apiParam {String} new_password_repeat 重复新密码.
  248. * @apiSuccessExample {json} Success-Response:
  249. *
  250. * {
  251. * "code": 0,
  252. * "msg": "",
  253. * "data": {}
  254. * }
  255. */
  256. function modifyPassword(Request $request)
  257. {
  258. $password = $request->has('password') ? trim($request->input('password')) : '';
  259. $new_password = $request->has('new_password') ? trim($request->input('new_password')) : '';
  260. $new_password_repeat = $request->has('new_password_repeat') ? trim($request->input('new_password_repeat')) : '';
  261. if(!$password || !$new_password || !$new_password_repeat) return response()->error('PARAM_ERROR');
  262. if($new_password != $new_password_repeat) return response()->error('PASSWORD_NOT_SAME');
  263. $channel_user_id = session('ydyauth');
  264. $channel_user = ChannelUserService::getById($channel_user_id);
  265. if($channel_user->password != md5($password."^-^zhuishuyun^_^")) return response()->error('PASSWORD_WRONG');
  266. if($channel_user->phone)
  267. {
  268. $password = md5($new_password."^-^zhuishuyun^_^");
  269. if(ChannelUserService::modifyPassword($channel_user->phone, $password))
  270. {
  271. return response()->success();
  272. }
  273. }
  274. }
  275. public function getCaptcha(Request $request) {
  276. //return 1;
  277. return Captcha::create();
  278. }
  279. public function test2(Request $request) {
  280. //return 1;
  281. $rules = ['captcha' => 'required|captcha'];
  282. //\Log::info($request->session()->all());
  283. //\Log::info($request->all());
  284. $validator = Validator::make($request->all(), $rules);
  285. if ($validator->fails())
  286. {
  287. //\Log::info('validator:'.json_encode($validator));
  288. return response()->error('CAPTCHA_VERIFY_ERROR');
  289. }
  290. var_dump($request->session()->all());
  291. //return Captcha::create();
  292. }
  293. }