ProfileController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: Dean <zxxjjforever@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\user\controller;
  10. use api\portal\model\FeedbackModel;
  11. use api\user\model\JucaiMemberModel;
  12. use api\user\model\UserScoreLogModel;
  13. use cmf\controller\RestUserBaseController;
  14. use think\Db;
  15. use think\Validate;
  16. class ProfileController extends RestUserBaseController
  17. {
  18. /**
  19. * 用户密码修改
  20. * @throws \think\Exception
  21. * @throws \think\exception\PDOException
  22. */
  23. public function changePassword()
  24. {
  25. $validate = new Validate([
  26. 'old_password' => 'require',
  27. 'password' => 'require',
  28. 'confirm_password' => 'require|confirm:password',
  29. ]);
  30. $validate->message([
  31. 'old_password.require' => '请输入您的旧密码!',
  32. 'password.require' => '请输入您的新密码!',
  33. 'confirm_password.require' => '请输入确认密码!',
  34. 'confirm_password.confirm' => '两次输入的密码不一致!',
  35. ]);
  36. $data = $this->request->param();
  37. if (!$validate->check($data)) {
  38. $this->error($validate->getError());
  39. }
  40. $userId = $this->getUserId();
  41. $userPassword = Db::name("user")->where('id', $userId)->value('user_pass');
  42. if (!cmf_compare_password($data['old_password'], $userPassword)) {
  43. $this->error('旧密码不正确!');
  44. }
  45. Db::name("user")->where('id', $userId)->update(['user_pass' => cmf_password($data['password'])]);
  46. $this->success("密码修改成功!");
  47. }
  48. /**
  49. * 用户绑定邮箱
  50. * @throws \think\Exception
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. * @throws \think\exception\PDOException
  55. */
  56. public function bindingEmail()
  57. {
  58. $validate = new Validate([
  59. 'email' => 'require|email|unique:user,user_email',
  60. 'verification_code' => 'require',
  61. ]);
  62. $validate->message([
  63. 'email.require' => '请输入您的邮箱!',
  64. 'email.email' => '请输入正确的邮箱格式!',
  65. 'email.unique' => '邮箱账号已存在!',
  66. 'verification_code.require' => '请输入数字验证码!',
  67. ]);
  68. $data = $this->request->param();
  69. if (!$validate->check($data)) {
  70. $this->error($validate->getError());
  71. }
  72. $userId = $this->getUserId();
  73. $userEmail = Db::name("user")->where('id', $userId)->value('user_email');
  74. if (!empty($userEmail)) {
  75. $this->error("您已经绑定邮箱!");
  76. }
  77. $errMsg = cmf_check_verification_code($data['email'], $data['verification_code']);
  78. if (!empty($errMsg)) {
  79. $this->error($errMsg);
  80. }
  81. Db::name("user")->where('id', $userId)->update(['user_email' => $data['email']]);
  82. $this->success("绑定成功!");
  83. }
  84. /**
  85. * 用户绑定手机号
  86. * @throws \think\Exception
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. * @throws \think\exception\PDOException
  91. */
  92. public function bindingMobile()
  93. {
  94. $validate = new Validate([
  95. 'mobile' => 'require|unique:user,mobile',
  96. 'verification_code' => 'require',
  97. ]);
  98. $validate->message([
  99. 'mobile.require' => '请输入您的手机号!',
  100. 'mobile.unique' => '手机号已经存在!',
  101. 'verification_code.require' => '请输入数字验证码!',
  102. ]);
  103. $data = $this->request->param();
  104. if (!$validate->check($data)) {
  105. $this->error($validate->getError());
  106. }
  107. if (!cmf_check_mobile($data['mobile'])) {
  108. $this->error("请输入正确的手机格式!");
  109. }
  110. $userId = $this->getUserId();
  111. $mobile = Db::name("user")->where('id', $userId)->value('mobile');
  112. if (!empty($mobile)) {
  113. $this->error("您已经绑定手机!");
  114. }
  115. $errMsg = cmf_check_verification_code($data['mobile'], $data['verification_code']);
  116. if (!empty($errMsg)) {
  117. $this->error($errMsg);
  118. }
  119. Db::name("user")->where('id', $userId)->update(['mobile' => $data['mobile']]);
  120. $this->success("绑定成功!");
  121. }
  122. /**
  123. * 用户基本信息获取及修改
  124. * @param string $field 需要获取的字段名
  125. * @throws \think\Exception
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. * @throws \think\exception\PDOException
  130. */
  131. public function userInfo($field = '')
  132. {
  133. //判断请求为GET,获取信息
  134. if ($this->request->isGet()) {
  135. $userId = $this->getUserId();
  136. $fieldStr = 'id,user_name,user_idcard,user_type,user_login,mobile,user_email,user_nickname,avatar,signature,user_url,sex,birthday,score,coin,user_status,user_activation_key,create_time,last_login_time,last_login_ip,company_name,job,is_talent,talent_level,talent_card,jucai_id';
  137. if (empty($field)) {
  138. $userData = Db::name("user")->field($fieldStr)->find($userId);
  139. } else {
  140. $fieldArr = explode(',', $fieldStr);
  141. $postFieldArr = explode(',', $field);
  142. $mixedField = array_intersect($fieldArr, $postFieldArr);
  143. if (empty($mixedField)) {
  144. $this->error('您查询的信息不存在!');
  145. }
  146. if (count($mixedField) > 1) {
  147. $fieldStr = implode(',', $mixedField);
  148. $userData = Db::name("user")->field($fieldStr)->find($userId);
  149. } else {
  150. $userData = Db::name("user")->where('id', $userId)->value($mixedField);
  151. }
  152. }
  153. $this->success('获取成功!', $userData);
  154. }
  155. //判断请求为POST,修改信息
  156. if ($this->request->isPost()) {
  157. $userId = $this->getUserId();
  158. $fieldStr = 'user_login,user_name,user_idcard,mobile,user_nickname,avatar,signature,user_url,sex,birthday,company_name,job,is_talent,talent_level,talent_card';
  159. $data = $this->request->post();
  160. if (empty($data)) {
  161. $this->error('修改失败,提交表单为空!');
  162. }
  163. if (!empty($data['birthday'])) {
  164. $data['birthday'] = strtotime($data['birthday']);
  165. }
  166. $upData = Db::name("user")->where('id', $userId)->field($fieldStr)->update($data);
  167. if ($upData !== false) {
  168. $this->success('修改成功!');
  169. } else {
  170. $this->error('修改失败!');
  171. }
  172. }
  173. }
  174. // 用户头像上传
  175. public function avatarUpload()
  176. {
  177. $file = $this->request->file('file');
  178. $result = $file->validate([
  179. 'ext' => 'jpg,jpeg,png',
  180. 'size' => 1024 * 1024,
  181. ])->move(WEB_ROOT . 'upload' . DIRECTORY_SEPARATOR . 'avatar' . DIRECTORY_SEPARATOR);
  182. if ($result) {
  183. $avatarSaveName = str_replace('//', '/', str_replace('\\', '/', $result->getSaveName()));
  184. $avatar = cmf_get_user_avatar_url('avatar/' . $avatarSaveName);
  185. $userId = $this->getUserId();
  186. Db::name("user")->where('id', $userId)->update(['avatar' => $avatar]);
  187. $this->success('上传成功', $avatar);
  188. } else {
  189. $this->error($file->getError());
  190. }
  191. }
  192. /**
  193. * 意见反馈
  194. */
  195. public function feedback()
  196. {
  197. $data = $this->request->post();
  198. if (empty($data['content'])) {
  199. $this->error('请输入意见或建议!');
  200. }
  201. $data['user_id'] = $this->getUserId();
  202. $data['create_time'] = time();
  203. FeedbackModel::create($data);
  204. $this->success('反馈成功!');
  205. }
  206. /**
  207. * 聚才网用户信息
  208. */
  209. public function jucaiInfo()
  210. {
  211. $userId = $this->getUserId();
  212. $user = Db::name("user")->find($userId);
  213. if (empty($user['jucai_id'])) {
  214. $this->success('获取成功!', []);
  215. } else {
  216. $info = JucaiMemberModel::field('id,username,email,mobile,status')->where('id', $user['jucai_id'])->find();
  217. $this->success('获取成功!', $info);
  218. }
  219. }
  220. /**
  221. * 绑定聚才网帐号
  222. */
  223. public function bindJucai()
  224. {
  225. //验证手机号
  226. $validate = new Validate([
  227. 'mobile' => 'require',
  228. 'password' => 'require',
  229. ]);
  230. $validate->message([
  231. 'mobile.require' => '请输入您的手机号!',
  232. 'password.require' => '请输入数字验证码!',
  233. ]);
  234. $data = $this->request->param();
  235. if (!$validate->check($data)) {
  236. $this->error($validate->getError());
  237. }
  238. if (!cmf_check_mobile($data['mobile'])) {
  239. $this->error("请输入正确的手机格式!");
  240. }
  241. //获取聚才网信息
  242. $info = JucaiMemberModel::get(['mobile' => $data['mobile']]);
  243. if (empty($info)) {
  244. $this->error('该手机号不存在,请先去聚才网注册!');
  245. }
  246. if (!password_verify($data['password'], $info['password'])) {
  247. $this->error('密码错误!');
  248. }
  249. $userId = $this->getUserId();
  250. Db::name("user")->where('id', $userId)->update(['jucai_id' => $info['id']]);
  251. $this->success('绑定成功!', ['id' => $info['id']]);
  252. }
  253. /**
  254. * 积分列表
  255. */
  256. public function scoreList()
  257. {
  258. $userId = $this->getUserId();
  259. $page = $this->request->param('page');
  260. $size = $this->request->param('size');
  261. $list = UserScoreLogModel::where('user_id', $userId)->order('create_time desc')->page($page, $size)->select();
  262. if (!$list->isEmpty()) {
  263. foreach ($list as $v) {
  264. $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  265. }
  266. }
  267. $this->success('', $list);
  268. }
  269. }