Email.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * 账户绑定
  4. */
  5. namespace app\api\controller\v1;
  6. use app\common\library\Mail;
  7. use app\common\model\Members;
  8. use think\Validate;
  9. use think\Session;
  10. use think\Db;
  11. class Email extends UserCenter
  12. {
  13. protected $memberModel;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->memberModel = new Members;
  18. }
  19. /**
  20. * 邮箱绑定页面
  21. * @return mixed
  22. * @throws \app\api\controller\HttpResponseException
  23. */
  24. public function index()
  25. {
  26. //判断是否已经绑定邮箱
  27. if (!empty($this->userInfo['email'])) {
  28. $this->error('您已经绑定邮箱', url('v1.userCenter/account'));
  29. }
  30. return $this->fetch();
  31. }
  32. /**
  33. * 解绑邮箱页面
  34. * @return mixed
  35. * @throws \app\api\controller\HttpResponseException
  36. */
  37. public function resetEmail()
  38. {
  39. if (empty($this->userInfo['email'])) {
  40. $this->error('您还未绑定邮箱', url('v1.userCenter/account'));
  41. }
  42. //Session::set('reset_email', true);
  43. $this->assign('email', ($this->userInfo['email']));
  44. return $this->fetch();
  45. }
  46. /**
  47. * 发送邮箱验证邮件
  48. * @throws \app\api\controller\HttpResponseException
  49. */
  50. public function sendEmail()
  51. {
  52. //未绑定 接受数据 验证邮箱合法性
  53. if ($this->request->isPut()) {
  54. $email = $this->request->put('email', '', 'trim');
  55. if (!Validate::is($email, 'email')) {
  56. $this->jsonResult('', 0, '邮箱格式不正确');
  57. }
  58. $userinfo = Members::where(['username' => $this->username])->field('email')->find();
  59. $url = !empty($userinfo['email']) ? url('v1.Checkemail/unBindEmail', ['username' => $this->username]) :
  60. url('v1.Checkemail/checkEmail', ['username' => $this->username]);
  61. $result = (new Mail())->sendCheckMail($this->username, $email, $url);
  62. return $this->json('', (int)$result['status'], ($result['status'] ? '发送成功' : $result['msg']));
  63. }
  64. }
  65. /**
  66. * 发送邮箱验证邮件
  67. * @throws \app\api\controller\HttpResponseException
  68. */
  69. public function sendEmailCode()
  70. {
  71. if ($this->request->isPost()) { // 手机号绑定页面,发送验证码
  72. $data = $this->request->put();
  73. $runnable = true;
  74. $message = '';
  75. $code = 0;
  76. //1.判断验证码防止被刷
  77. if (true !== ($res = $this->validate($data,
  78. [
  79. ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'],
  80. ['email', 'require|email', '请输入邮箱|邮箱格式不正确'],
  81. ]
  82. ))) {
  83. $runnable = false;
  84. $message = $res;
  85. }
  86. //3.发送短信验证码
  87. if ($runnable) {
  88. $userinfo = Members::where(['username' => $this->username])->field('email')->find();
  89. $temple = !empty($userinfo['email']) ? 'bindEmailVLEmail' : 'bindEmail';
  90. $result = (new Mail())->sendCodeMailByType($this->username, $data['email'],$temple);
  91. if (!$result['status']) {
  92. $message = $result['msg'];
  93. }
  94. $code = $result['status'];
  95. }
  96. return $this->json('', $code, $message);
  97. }
  98. }
  99. /**
  100. * 邮箱换绑原邮箱验证
  101. * @throws \app\api\controller\HttpResponseException
  102. */
  103. public function velidateCode()
  104. {
  105. if ($this->request->isPost()) {
  106. $data = $this->request->post();
  107. //1.验证数据合法性
  108. if (true !== ($res = $this->validate($data, [
  109. 'sms_code|验证码' => 'require|isEmpty',
  110. 'email|邮箱地址' => 'require|isEmpty|email',
  111. ]))) {
  112. return $this->json([], 0, $res);
  113. }
  114. //2.验证对应的邮箱和验证码是否匹配
  115. $res = (new Mail())->checkCode($this->username , $data['email'] , $data['sms_code']);
  116. if ($res['status'] == false) {
  117. return $this->json([], 0, $res['msg']);
  118. }
  119. else{
  120. Session::set('emailVelidate', 1);
  121. return $this->json(['url' => url('v1.email/changeEmail')], 1, '原邮箱验证成功');
  122. }
  123. }
  124. }
  125. /**
  126. * 解绑邮箱页面
  127. * @return mixed
  128. * @throws \app\api\controller\HttpResponseException
  129. */
  130. public function changeEmail()
  131. {
  132. if (empty($this->userInfo['email'])) {
  133. $this->error('您还未绑定邮箱', url('v1.userCenter/account'));
  134. }
  135. if (!Session::get('emailVelidate')) {
  136. $this->error('请先进行原邮箱验证', url('v1.email/resetEmail'));
  137. }
  138. //Session::set('reset_email', true);
  139. $this->assign('email', ($this->userInfo['email']));
  140. return $this->fetch();
  141. }
  142. /**
  143. * 发送邮箱验证邮件
  144. * @throws \app\api\controller\HttpResponseException
  145. */
  146. public function sendNewEmailCode()
  147. {
  148. if ($this->request->isPost()) { // 邮箱绑定页面,发送验证码
  149. $data = $this->request->put();
  150. $runnable = true;
  151. $message = '';
  152. $code = 0;
  153. //1.判断验证码防止被刷
  154. if (true !== ($res = $this->validate($data,
  155. [
  156. ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'],
  157. ['email', 'require|email', '请输入邮箱|邮箱格式不正确'],
  158. ]
  159. ))) {
  160. $runnable = false;
  161. $message = $res;
  162. }
  163. //2.检查换绑手机是否跟原绑定手机一致
  164. if (isset($this->userInfo['email']) && ($data['email'] == $this->userInfo['email'])) {
  165. $runnable = false;
  166. $message = '邮箱跟原绑定邮箱一样,请重新填写';
  167. }
  168. //3.发送验证码
  169. if ($runnable) {
  170. $userinfo = Members::where(['username' => $this->username])->field('email')->find();
  171. $temple = !empty($userinfo['email']) ? 'bindEmailVLEmail' : 'bindEmail';
  172. $result = (new Mail())->sendCodeMailByType($this->username, $data['email'],$temple);
  173. if (!$result['status']) {
  174. $message = $result['msg'];
  175. }
  176. $code = $result['status'];
  177. }
  178. return $this->json('', $code, $message);
  179. }
  180. }
  181. /**
  182. * 邮箱换绑操作
  183. * @throws \app\api\controller\HttpResponseException
  184. */
  185. public function chgBindEmail()
  186. {
  187. if ($this->request->isPost()) {
  188. $data = $this->request->post();
  189. //1.验证数据合法性
  190. if (true !== ($res = $this->validate($data, [
  191. 'sms_code|短信验证码' => 'require|isEmpty',
  192. 'email|邮箱' => 'require|isEmpty|email',
  193. ]))) {
  194. return $this->json([], 0, $res);
  195. }
  196. //2.验证对应的邮箱和验证码是否匹配
  197. $res = (new Mail())->checkCode($this->username , $data['email'] , $data['sms_code']);
  198. if ($res['status'] == false) {
  199. return $this->json([], 0, $res['msg']);
  200. }
  201. //3.验证是否重复绑定
  202. if (isset($this->userInfo['email']) && ($data['email'] == $this->userInfo['email'])) {
  203. return $this->json([], 0, '邮箱跟原绑定邮箱一样,请重新填写');
  204. }
  205. //4.更新数据 记录日志
  206. if ($res = Members::where(['username' => $this->username])->update(['email' => $data['email'],'update_time'=>time()])) { // 插入玩家历史记录
  207. Db::table('cy_member_history')->insert([
  208. 'userid' => $this->userInfo['id'], //todo
  209. 'password' => '',
  210. 'email' => $data['email'],
  211. 'ip' => request()->ip(),
  212. 'create_time' => time(),
  213. ]);
  214. Session::delete('emailVelidate');
  215. return $this->json(['url' => url('v1.UserCenter/account')], 1, '邮箱换绑成功');
  216. //TODO 手动失效短信验证码
  217. }
  218. return $this->json([], 0, '邮箱换绑失败');
  219. }
  220. }
  221. /**
  222. * 邮箱绑定操作
  223. */
  224. public function bindEmail()
  225. {
  226. if ($this->request->isPost()) {
  227. $data = $this->request->post();
  228. //1.验证数据合法性
  229. if (true !== ($res = $this->validate($data, [
  230. 'sms_code|验证码' => 'require|isEmpty',
  231. 'email|邮箱' => 'require|isEmpty|email',
  232. ]))) {
  233. return $this->json([], 0, $res);
  234. }
  235. //2.验证对应的手机号和验证码是否匹配
  236. $res = (new Mail())->checkCode($this->username , $data['email'] , $data['sms_code']);
  237. if ($res['status'] == false) {
  238. return $this->json([], 0, $res['msg']);
  239. }
  240. //4.更新数据 记录日志
  241. if ($res = Members::where(['username' => $this->username])
  242. ->update(['email' => $data['email'],'update_time'=>time()])) { // 插入玩家历史记录
  243. Db::table('cy_member_history')->insert([
  244. 'userid' => $this->userInfo['id'], //todo
  245. 'password' => '',
  246. 'email' => $data['email'],
  247. 'ip' => request()->ip(),
  248. 'create_time' => time(),
  249. ]);
  250. return $this->json([], 1, '手机号绑定成功');
  251. //TODO 手动失效短信验证码
  252. }
  253. return $this->json([], 0, '手机号绑定失败');
  254. }
  255. }
  256. // /**
  257. // * 验证邮箱链接地址
  258. // */
  259. // public function checkEmail()
  260. // {
  261. // $token = input('t', '', 'trim');
  262. // if (empty($token)) {
  263. // $this->error('邮箱验证地址错误');
  264. // }
  265. // //验证token
  266. // $res = (new Mail())->checkMailToken($this->username, $token);
  267. //
  268. // if (!$res['status']) {
  269. // $this->error($res['msg']);
  270. // }
  271. // $data = $res['data'];
  272. // //3.验证是否重复绑定
  273. // if (isset($this->userInfo['email']) && ($data['mail'] == $this->userInfo['email'])) {
  274. // $this->error('邮箱地址跟原来邮箱地址一样,不要重复绑定', url('v1.userCenter/account'));
  275. // }
  276. // //4.更新数据 记录日志
  277. // if ($res = Members::where(['username' => $this->username])->update(['email' => $data['mail']])) {
  278. // Db::table('cy_member_history')->insert([
  279. // 'userid' => $this->userInfo['id'], //todo
  280. // 'password' => '',
  281. // 'mobile' => $data['mail'],
  282. // 'ip' => request()->ip(),
  283. // 'create_time' => time(),
  284. // ]);
  285. //
  286. // //删除解绑密码状态
  287. // if (Session::has('reset_email')) {
  288. // Session::delete('reset_email');
  289. // }
  290. // (new Mail)->clearKeyByUsername($this->username);
  291. // return $this->success('邮箱绑定成功');
  292. // }
  293. // return $this->error('邮箱绑定失败');
  294. // }
  295. //
  296. //
  297. // /**
  298. // * 解绑邮箱
  299. // * @throws \app\api\controller\HttpResponseException
  300. // */
  301. // public function unBindEmail()
  302. // {
  303. // //删除解绑密码状态
  304. // if (!Session::has('reset_email')) {
  305. // return $this->error('参数错误', url('v1.userCenter/account'));
  306. // }
  307. // $token = input('t', '', 'trim');
  308. // if (empty($token)) {
  309. // $this->error('邮箱验证地址错误');
  310. // }
  311. // //验证token
  312. // $res = (new Mail())->checkMailToken($this->username, $token);
  313. //
  314. // if (!$res['status']) {
  315. // $this->error($res['msg']);
  316. // }
  317. // //4.更新数据 记录日志
  318. // if ($res = Members::where(['username' => $this->username])->update(['email' => ''])) {
  319. // Db::table('cy_member_history')->insert([
  320. // 'userid' => $this->userInfo['id'], //todo
  321. // 'password' => '',
  322. // 'email' => 'unbind',
  323. // 'ip' => request()->ip(),
  324. // 'create_time' => time(),
  325. // ]);
  326. // //删除解绑密码状态
  327. // if (Session::has('reset_email')) {
  328. // Session::delete('reset_email');
  329. // }
  330. // (new Mail)->clearKeyByUsername($this->username);
  331. // return $this->success('邮箱解绑成功');
  332. // }
  333. // return $this->error('邮箱解绑失败');
  334. // }
  335. }