ProfileController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use cmf\lib\Storage;
  13. use think\Validate;
  14. use think\Image;
  15. use cmf\controller\UserBaseController;
  16. use app\user\model\UserModel;
  17. class ProfileController extends UserBaseController
  18. {
  19. /**
  20. * 会员中心首页
  21. */
  22. public function center()
  23. {
  24. $user = cmf_get_current_user();
  25. $this->assign($user);
  26. $userId = cmf_get_current_user_id();
  27. $userModel = new UserModel();
  28. $user = $userModel->where('id', $userId)->find();
  29. $this->assign('user', $user);
  30. return $this->fetch();
  31. }
  32. /**
  33. * 编辑用户资料
  34. */
  35. public function edit()
  36. {
  37. $user = cmf_get_current_user();
  38. $this->assign($user);
  39. return $this->fetch('edit');
  40. }
  41. /**
  42. * 编辑用户资料提交
  43. */
  44. public function editPost()
  45. {
  46. if ($this->request->isPost()) {
  47. $validate = new Validate();
  48. $validate->rule([
  49. 'user_nickname' => 'max:32',
  50. 'sex' => 'between:0,2',
  51. 'birthday' => 'dateFormat:Y-m-d|after:-88 year|before:-1 day',
  52. 'user_url' => 'url|max:64',
  53. 'signature' => 'max:128',
  54. ]);
  55. $validate->message([
  56. 'user_nickname.max' => lang('NICKNAME_IS_TO0_LONG'),
  57. 'sex.between' => lang('SEX_IS_INVALID'),
  58. 'birthday.dateFormat' => lang('BIRTHDAY_IS_INVALID'),
  59. 'birthday.after' => lang('BIRTHDAY_IS_TOO_EARLY'),
  60. 'birthday.before' => lang('BIRTHDAY_IS_TOO_LATE'),
  61. 'user_url.url' => lang('URL_FORMAT_IS_WRONG'),
  62. 'user_url.max' => lang('URL_IS_TO0_LONG'),
  63. 'signature.max' => lang('SIGNATURE_IS_TO0_LONG'),
  64. ]);
  65. $data = $this->request->post();
  66. if (!$validate->check($data)) {
  67. $this->error($validate->getError());
  68. }
  69. $editData = new UserModel();
  70. if ($editData->editData($data,[
  71. 'user_nickname',
  72. 'sex',
  73. 'birthday',
  74. 'user_url',
  75. 'signature',
  76. 'more'
  77. ])) {
  78. $this->success(lang('EDIT_SUCCESS'), "user/profile/center");
  79. } else {
  80. $this->error(lang('NO_NEW_INFORMATION'));
  81. }
  82. } else {
  83. $this->error(lang('ERROR'));
  84. }
  85. }
  86. /**
  87. * 个人中心修改密码
  88. */
  89. public function password()
  90. {
  91. $user = cmf_get_current_user();
  92. $this->assign($user);
  93. return $this->fetch();
  94. }
  95. /**
  96. * 个人中心修改密码提交
  97. */
  98. public function passwordPost()
  99. {
  100. if ($this->request->isPost()) {
  101. $validate = new Validate();
  102. $validate->rule([
  103. 'old_password' => 'require|min:6|max:32',
  104. 'password' => 'require|min:6|max:32',
  105. 'repassword' => 'require|min:6|max:32',
  106. ]);
  107. $validate->message([
  108. 'old_password.require' => lang('old_password_is_required'),
  109. 'old_password.max' => lang('old_password_is_too_long'),
  110. 'old_password.min' => lang('old_password_is_too_short'),
  111. 'password.require' => lang('password_is_required'),
  112. 'password.max' => lang('password_is_too_long'),
  113. 'password.min' => lang('password_is_too_short'),
  114. 'repassword.require' => lang('repeat_password_is_required'),
  115. 'repassword.max' => lang('repeat_password_is_too_long'),
  116. 'repassword.min' => lang('repeat_password_is_too_short'),
  117. ]);
  118. $data = $this->request->post();
  119. if (!$validate->check($data)) {
  120. $this->error($validate->getError());
  121. }
  122. $login = new UserModel();
  123. $log = $login->editPassword($data);
  124. switch ($log) {
  125. case 0:
  126. $this->success(lang('change_success'));
  127. break;
  128. case 1:
  129. $this->error(lang('password_repeat_wrong'));
  130. break;
  131. case 2:
  132. $this->error(lang('old_password_is_wrong'));
  133. break;
  134. default :
  135. $this->error(lang('ERROR'));
  136. }
  137. } else {
  138. $this->error(lang('ERROR'));
  139. }
  140. }
  141. // 用户头像编辑
  142. public function avatar()
  143. {
  144. $user = cmf_get_current_user();
  145. $this->assign($user);
  146. return $this->fetch('avatar');
  147. }
  148. // 用户头像上传
  149. public function avatarUpload()
  150. {
  151. $file = $this->request->file('file');
  152. $validator = validate(['file' => 'fileExt:jpg,jpeg,png']);
  153. if (!$validator->check(['file' => $file])) {
  154. if ($this->request->isAjax()) {
  155. $this->error($validator->getError());
  156. } else {
  157. return json_encode([
  158. 'code' => 0,
  159. "msg" => $validator->getError(),
  160. "data" => "",
  161. "url" => ''
  162. ]);
  163. }
  164. }
  165. $fileMd5 = $file->md5();
  166. $fileExt = $file->getOriginalExtension();
  167. $fileName = $fileMd5 . '.' . $fileExt;
  168. $date = date('Ymd');
  169. $avatarDir = WEB_ROOT . 'upload' . DIRECTORY_SEPARATOR . 'avatar' . DIRECTORY_SEPARATOR . $date . DIRECTORY_SEPARATOR;
  170. $file->move($avatarDir, $fileMd5 . '.' . $fileExt);
  171. $avatar = 'avatar/' . $date . '/' . $fileName;
  172. session('avatar', $avatar);
  173. if ($this->request->isAjax()) {
  174. $avatarPath = $avatarDir . $fileName;
  175. $storage = new Storage();
  176. $result = $storage->upload($avatar, $avatarPath, 'image');
  177. $userId = cmf_get_current_user_id();
  178. UserModel::where("id", $userId)->update(["avatar" => $avatar]);
  179. session('user.avatar', $avatar);
  180. $this->success("上传成功", null, ['file' => $avatar]);
  181. } else {
  182. return json_encode([
  183. 'code' => 1,
  184. "msg" => "上传成功",
  185. "data" => ['file' => $avatar],
  186. "url" => ''
  187. ]);
  188. }
  189. }
  190. // 用户头像裁剪
  191. public function avatarUpdate()
  192. {
  193. $avatar = session('avatar');
  194. if (!empty($avatar)) {
  195. $w = $this->request->param('w', 0, 'intval');
  196. $h = $this->request->param('h', 0, 'intval');
  197. $x = $this->request->param('x', 0, 'intval');
  198. $y = $this->request->param('y', 0, 'intval');
  199. $avatarPath = WEB_ROOT . "upload/" . $avatar;
  200. $avatarImg = Image::open($avatarPath);
  201. $avatarImg->crop($w, $h, $x, $y)->save($avatarPath);
  202. $result = true;
  203. if ($result === true) {
  204. $storage = new Storage();
  205. $result = $storage->upload($avatar, $avatarPath, 'image');
  206. $userId = cmf_get_current_user_id();
  207. UserModel::where("id", $userId)->update(["avatar" => $avatar]);
  208. session('user.avatar', $avatar);
  209. $this->success("头像更新成功!");
  210. } else {
  211. $this->error("头像保存失败!");
  212. }
  213. }
  214. }
  215. /**
  216. * 绑定手机号或邮箱
  217. */
  218. public function binding()
  219. {
  220. $user = cmf_get_current_user();
  221. $this->assign($user);
  222. return $this->fetch();
  223. }
  224. /**
  225. * 绑定手机号
  226. */
  227. public function bindingMobile()
  228. {
  229. if ($this->request->isPost()) {
  230. $validate = new Validate();
  231. $validate->rule([
  232. 'username' => 'require|number|unique:user,mobile',
  233. 'verification_code' => 'require',
  234. ]);
  235. $validate->message([
  236. 'username.require' => '手机号不能为空',
  237. 'username.number' => '手机号只能为数字',
  238. 'username.unique' => '手机号已存在',
  239. 'verification_code.require' => '验证码不能为空',
  240. ]);
  241. $data = $this->request->post();
  242. if (!$validate->check($data)) {
  243. $this->error($validate->getError());
  244. }
  245. $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
  246. if (!empty($errMsg)) {
  247. $this->error($errMsg);
  248. }
  249. $userModel = new UserModel();
  250. $log = $userModel->bindingMobile($data);
  251. switch ($log) {
  252. case 0:
  253. $this->success('手机号绑定成功');
  254. break;
  255. default :
  256. $this->error('未受理的请求');
  257. }
  258. } else {
  259. $this->error("请求错误");
  260. }
  261. }
  262. /**
  263. * 绑定邮箱
  264. */
  265. public function bindingEmail()
  266. {
  267. if ($this->request->isPost()) {
  268. $validate = new Validate();
  269. $validate->rule([
  270. 'username' => 'require|email|unique:user,user_email',
  271. 'verification_code' => 'require',
  272. ]);
  273. $validate->message([
  274. 'username.require' => '邮箱地址不能为空',
  275. 'username.email' => '邮箱地址不正确',
  276. 'username.unique' => '邮箱地址已存在',
  277. 'verification_code.require' => '验证码不能为空',
  278. ]);
  279. $data = $this->request->post();
  280. if (!$validate->check($data)) {
  281. $this->error($validate->getError());
  282. }
  283. $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
  284. if (!empty($errMsg)) {
  285. $this->error($errMsg);
  286. }
  287. $userModel = new UserModel();
  288. $log = $userModel->bindingEmail($data);
  289. switch ($log) {
  290. case 0:
  291. $this->success('邮箱绑定成功');
  292. break;
  293. default :
  294. $this->error('未受理的请求');
  295. }
  296. } else {
  297. $this->error("请求错误");
  298. }
  299. }
  300. }