ResetpwdController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace app\platform\controllers;
  3. use app\common\exceptions\TokenHasExpiredException;
  4. use app\common\services\txyunsms\SmsSingleSender;
  5. use app\frontend\modules\member\services\MemberService;
  6. use app\platform\modules\user\models\AdminUser;
  7. use app\platform\modules\user\models\YzUserProfile;
  8. use app\common\helpers\Cache;
  9. use iscms\Alisms\SendsmsPusher as Sms;
  10. use app\frontend\modules\member\models\smsSendLimitModel;
  11. use app\platform\modules\system\models\SystemSetting;
  12. use app\common\services\aliyun\AliyunSMS;
  13. use Mews\Captcha\Captcha;
  14. use Gregwar\Captcha\PhraseBuilder;
  15. use Gregwar\Captcha\CaptchaBuilder;
  16. use app\common\helpers\Url;
  17. class ResetpwdController extends BaseController
  18. {
  19. public function SendCode()
  20. {
  21. $mobile = request()->mobile;
  22. $username = request()->username; //账号
  23. $state = \YunShop::request()->state ?: '86';
  24. if (empty($mobile)) {
  25. return $this->errorJson('请填入手机号');
  26. }
  27. if ($username) {
  28. $user = AdminUser::where('username', $username)->with('hasOneProfile')->first();
  29. if (!$user) {
  30. return $this->errorJson('账号不存在');
  31. }
  32. $user = $user->toArray();
  33. //判断账号
  34. if ($user['has_one_profile']['mobile'] == $mobile) { //管理员
  35. return $this->send($mobile, $state);
  36. }
  37. if (\Schema::hasTable('yz_store')) //门店
  38. {
  39. $plugins_mobile = \DB::table('yz_store')->where('user_uid', $user['uid'])->value('mobile'); //门店
  40. // $plugins_mobile = \DB::table('yz_store_apply')->where('uid',$member_id)->value('mobile'); //门店
  41. if ($plugins_mobile == $mobile) {
  42. return $this->send($mobile, $state);
  43. }
  44. }
  45. if (\Schema::hasTable('yz_hotel')) {
  46. $plugins_mobile = \DB::table('yz_hotel')->where('user_uid', $user['uid'])->value('mobile'); //酒店
  47. if ($plugins_mobile == $mobile) {
  48. return $this->send($mobile, $state);
  49. }
  50. }
  51. if (\Schema::hasTable('yz_area_dividend_agent')) {//区域分红
  52. $plugins_mobile = \DB::table('yz_area_dividend_agent')->where('user_id', $user['uid'])->value('mobile');
  53. if ($plugins_mobile == $mobile) {
  54. return $this->send($mobile, $state);
  55. }
  56. }
  57. if (\Schema::hasTable('yz_supplier')) {//供应商
  58. $plugins_mobile = \DB::table('yz_supplier')->where('uid', $user['uid'])->value('mobile');
  59. if ($plugins_mobile == $mobile) {
  60. return $this->send($mobile, $state);
  61. }
  62. }
  63. if (\Schema::hasTable('yz_package_deliver')) {//自提点
  64. $plugins_mobile = \DB::table('yz_package_deliver')->where('user_uid', $user['uid'])->value('deliver_mobile');
  65. if ($plugins_mobile == $mobile) {
  66. return $this->send($mobile, $state);
  67. }
  68. }
  69. if (\Schema::hasTable('yz_subsidiary')) { //分公司
  70. $plugins_mobile = \DB::table('yz_subsidiary')->where('user_uid', $user['uid'])->value('mobile');
  71. if ($plugins_mobile == $mobile) {
  72. return $this->send($mobile, $state);
  73. }
  74. }
  75. } else {
  76. $uid = $this->checkUserOnMobile($mobile);
  77. if (!$uid) {
  78. return $this->errorJson('该手机号不存在');
  79. }
  80. }
  81. return $this->errorJson('该手机号不存在');
  82. }
  83. public function send($mobile, $state)
  84. {
  85. $code = rand(1000, 9999);
  86. //检查次数及是否正确
  87. if (!MemberService::smsSendLimit(\YunShop::app()->uniacid ?: 0, $mobile)) {
  88. return $this->errorJson('发送短信数量达到今日上限');
  89. } else {
  90. return $this->sendSmsV2($mobile, $code, $state);
  91. }
  92. }
  93. public function checkCode()
  94. {
  95. $mobile = request()->mobile;
  96. $code = request()->code;
  97. //检查验证码是否正确
  98. $check_code = app('sms')->checkAppCode($mobile, $code);
  99. if ($check_code['status'] != 1) {
  100. return $this->errorJson($check_code['json']);
  101. }
  102. return $this->successJson('验证成功');
  103. }
  104. public function detail()
  105. {
  106. $setting = SystemSetting::settingLoad('sms', 'system_sms');
  107. if (!$setting) {
  108. return $this->errorJson('暂无数据');
  109. }
  110. return $this->successJson('获取成功', $setting);
  111. }
  112. public function getCaptcha()
  113. {
  114. $setting = SystemSetting::settingLoad('sms');
  115. if ($setting['status'] != 1) {
  116. return $this->errorJson('请开启图形验证码验证');
  117. }
  118. $phrase = new PhraseBuilder();
  119. $code = $phrase->build(4);
  120. $builder = new CaptchaBuilder($code, $phrase);
  121. $builder->setBackgroundColor(150, 150, 150);
  122. $builder->setMaxAngle(25);
  123. $builder->setMaxBehindLines(0);
  124. $builder->setMaxFrontLines(0);
  125. $builder->build($width = 100, $height = 40, $font = null);
  126. $phrase = $builder->getPhrase();
  127. \Session::flash('code', $phrase);
  128. // header('Cache-Control: no-cache, must-revalidate');
  129. header('Content-Type: image/jpeg');
  130. $builder->output();
  131. }
  132. public function changePwd()
  133. {
  134. $username = request()->username;
  135. $pwd = request()->pwd;
  136. $mobile = request()->mobile;
  137. $confirm_password = \YunShop::request()->confirm_password;
  138. $loginset = SystemSetting::settingLoad('loginset', 'system_loginset');
  139. if ($loginset['password_verify'] == 1) {
  140. $validatePassword = validatePassword($pwd);
  141. if ($validatePassword !== true) {
  142. return $this->errorJson($validatePassword);
  143. }
  144. }
  145. $msg = $this->validate($mobile, $pwd, $confirm_password);
  146. if ($msg != 1) {
  147. if (isset($msg['json'])) {
  148. return $this->errorJson($msg['json']);
  149. } elseif ($msg) {
  150. return $this->errorJson($msg);
  151. }
  152. return $this->errorJson('未知道错误');
  153. // return $this->errorJson($msg['json']);
  154. }
  155. if ($username) {
  156. $user = AdminUser::where('username', $username)->with('hasOneProfile')->first();
  157. if (!$user->uid || (in_array($user->type,[0,1]) && $user->hasOneProfile->mobile != $mobile)) {
  158. return $this->errorJson('该用户不存在');
  159. }
  160. $uid = $user->uid;
  161. $res = $this->modify($pwd, $uid);
  162. if ($res) {
  163. return $this->successJson('密码修改成功');
  164. }
  165. }
  166. return $this->errorJson('修改密码失败');
  167. }
  168. private function checkUserOnMobile($mobile)
  169. {
  170. $member_info = YzUserProfile::where('mobile', $mobile)->first();
  171. if ($member_info) {
  172. return $member_info['uid'];
  173. }
  174. return false;
  175. }
  176. public function validate($mobile, $pwd, $confirm_password)
  177. {
  178. if ($confirm_password == '') {
  179. $data = array(
  180. 'mobile' => $mobile,
  181. 'password' => $pwd,
  182. );
  183. $rules = array(
  184. 'mobile' => 'regex:/^1\d{10}$/',
  185. 'password' => 'required|min:8|regex:/^[A-Za-z0-9.@!~#\$%\^&\*+_-]+$/',
  186. );
  187. $message = array(
  188. 'regex' => ':attribute 格式错误',
  189. 'required' => ':attribute 不能为空',
  190. 'min' => ':attribute 最少8位'
  191. );
  192. $attributes = array(
  193. "mobile" => '手机号',
  194. 'password' => '密码',
  195. );
  196. } else {
  197. $data = array(
  198. 'mobile' => $mobile,
  199. 'password' => $pwd,
  200. 'confirm_password' => $confirm_password,
  201. );
  202. $rules = array(
  203. 'mobile' => 'regex:/^1\d{10}$/',
  204. 'password' => 'required|min:8|regex:/^[A-Za-z0-9.@!~#\$%\^&\*+_-]+$/',
  205. 'confirm_password' => 'same:password',
  206. );
  207. $message = array(
  208. 'regex' => ':attribute 格式错误',
  209. 'required' => ':attribute 不能为空',
  210. 'min' => ':attribute 最少8位',
  211. 'same' => ':attribute 不匹配'
  212. );
  213. $attributes = array(
  214. "mobile" => '手机号',
  215. 'password' => '密码',
  216. 'confirm_password' => '密码',
  217. );
  218. }
  219. $validate = \Validator::make($data, $rules, $message, $attributes);
  220. if ($validate->fails()) {
  221. $warnings = $validate->messages();
  222. $show_warning = $warnings->first();
  223. return $show_warning;
  224. } else {
  225. return 1;
  226. }
  227. }
  228. public function sendSmsV2($mobile, $code, $state, $templateType = 'reg', $sms_type = 2)
  229. {
  230. if (2 == $sms_type) {
  231. $sms = app('sms')->sendPwd($mobile, $state, 1);
  232. } elseif (3 == $sms_type) {
  233. $sms = app('sms')->sendLog($mobile, $state, 1);
  234. } else {
  235. $sms = app('sms')->sendCode($mobile, $state, 1);
  236. }
  237. if (0 == $sms['status']) {
  238. return $this->errorJson($sms['json']);
  239. }
  240. return $this->successJson();
  241. }
  242. public function sendSmsV3($mobile, $state, $sms_type = 2)
  243. {
  244. $sms = app('sms')->sendCode($mobile, $state, 1);
  245. if (0 == $sms['status']) {
  246. return $this->errorJson('短信发送失败,可能是您短时间内多次发送导致,请一小时之后再试');
  247. }
  248. return $this->successJson();
  249. }
  250. /**
  251. * 管理员修改密码
  252. */
  253. public function authPassword()
  254. {
  255. $auth = config('app.AUTH_PASSWORD');
  256. $auth_request = request()->auth;
  257. $is_ok = false;
  258. if ($auth_request == $auth && $auth != '') {
  259. $is_ok = true;
  260. $user_request = request()->user;
  261. if (!empty($user_request['username']) && !empty($user_request['password'])) {
  262. $user = $this->getUser($user_request['username']);
  263. if (!$user) {
  264. return $this->message('用户名不存在', '/index.php/admin/auth');
  265. }
  266. $res = $this->modify($user_request['password'], $user->uid);
  267. if ($res) {
  268. (new LoginController)->logout();
  269. return $this->message('密码修改成功', '/');
  270. }
  271. return $this->error('修改密码失败', '/index.php/admin/auth');
  272. }
  273. }
  274. return view('platform.auth', [
  275. 'is_ok' => $is_ok,
  276. 'auth' => $auth
  277. ])->render();
  278. }
  279. public function getUser($username)
  280. {
  281. return AdminUser::where('username', $username)->first();
  282. }
  283. public function modify($pwd, $uid)
  284. {
  285. $data['password'] = bcrypt($pwd);
  286. $res = AdminUser::where('uid', $uid)->update($data);
  287. return $res;
  288. }
  289. }