UserModel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 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\model;
  12. use think\Db;
  13. use think\Model;
  14. class UserModel extends Model
  15. {
  16. protected $type = [
  17. 'more' => 'array',
  18. ];
  19. public function doMobile($user)
  20. {
  21. $result = $this->where('mobile', $user['mobile'])->find();
  22. if (!empty($result)) {
  23. $comparePasswordResult = cmf_compare_password($user['user_pass'], $result['user_pass']);
  24. $hookParam = [
  25. 'user' => $user,
  26. 'compare_password_result' => $comparePasswordResult
  27. ];
  28. hook_one("user_login_start", $hookParam);
  29. if ($comparePasswordResult) {
  30. //禁用判断。
  31. if ($result['user_status'] == 0) {
  32. return 3;
  33. }
  34. session('user', $result->toArray());
  35. $data = [
  36. 'last_login_time' => time(),
  37. 'last_login_ip' => get_client_ip(0, true),
  38. ];
  39. $this->where('id', $result["id"])->update($data);
  40. $token = cmf_generate_user_token($result["id"], 'web');
  41. if (!empty($token)) {
  42. session('token', $token);
  43. }
  44. return 0;
  45. }
  46. return 1;
  47. }
  48. $hookParam = [
  49. 'user' => $user,
  50. 'compare_password_result' => false
  51. ];
  52. hook_one("user_login_start", $hookParam);
  53. return 2;
  54. }
  55. public function doName($user)
  56. {
  57. $result = $this->where('user_login', $user['user_login'])->find();
  58. if (!empty($result)) {
  59. $comparePasswordResult = cmf_compare_password($user['user_pass'], $result['user_pass']);
  60. $hookParam = [
  61. 'user' => $user,
  62. 'compare_password_result' => $comparePasswordResult
  63. ];
  64. hook_one("user_login_start", $hookParam);
  65. if ($comparePasswordResult) {
  66. //禁用判断。
  67. if ($result['user_status'] == 0) {
  68. return 3;
  69. }
  70. session('user', $result->toArray());
  71. $data = [
  72. 'last_login_time' => time(),
  73. 'last_login_ip' => get_client_ip(0, true),
  74. ];
  75. $result->where('id', $result["id"])->update($data);
  76. $token = cmf_generate_user_token($result["id"], 'web');
  77. if (!empty($token)) {
  78. session('token', $token);
  79. }
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. $hookParam = [
  85. 'user' => $user,
  86. 'compare_password_result' => false
  87. ];
  88. hook_one("user_login_start", $hookParam);
  89. return 2;
  90. }
  91. public function doEmail($user)
  92. {
  93. $result = $this->where('user_email', $user['user_email'])->find();
  94. if (!empty($result)) {
  95. $comparePasswordResult = cmf_compare_password($user['user_pass'], $result['user_pass']);
  96. $hookParam = [
  97. 'user' => $user,
  98. 'compare_password_result' => $comparePasswordResult
  99. ];
  100. hook_one("user_login_start", $hookParam);
  101. if ($comparePasswordResult) {
  102. //禁用判断。
  103. if ($result['user_status'] == 0) {
  104. return 3;
  105. }
  106. session('user', $result->toArray());
  107. $data = [
  108. 'last_login_time' => time(),
  109. 'last_login_ip' => get_client_ip(0, true),
  110. ];
  111. $this->where('id', $result["id"])->update($data);
  112. $token = cmf_generate_user_token($result["id"], 'web');
  113. if (!empty($token)) {
  114. session('token', $token);
  115. }
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. $hookParam = [
  121. 'user' => $user,
  122. 'compare_password_result' => false
  123. ];
  124. hook_one("user_login_start", $hookParam);
  125. return 2;
  126. }
  127. public function register($user, $type)
  128. {
  129. switch ($type) {
  130. case 1:
  131. $result = Db::name("user")->where('user_login', $user['user_login'])->find();
  132. break;
  133. case 2:
  134. $result = Db::name("user")->where('mobile', $user['mobile'])->find();
  135. break;
  136. case 3:
  137. $result = Db::name("user")->where('user_email', $user['user_email'])->find();
  138. break;
  139. default:
  140. $result = 0;
  141. }
  142. $userStatus = 1;
  143. if (cmf_is_open_registration()) {
  144. $userStatus = 2;
  145. }
  146. if (empty($result)) {
  147. $data = [
  148. 'user_login' => empty($user['user_login']) ? '' : $user['user_login'],
  149. 'user_email' => empty($user['user_email']) ? '' : $user['user_email'],
  150. 'mobile' => empty($user['mobile']) ? '' : $user['mobile'],
  151. 'user_nickname' => '',
  152. 'user_pass' => cmf_password($user['user_pass']),
  153. 'last_login_ip' => get_client_ip(0, true),
  154. 'create_time' => time(),
  155. 'last_login_time' => time(),
  156. 'user_status' => $userStatus,
  157. "user_type" => 2,//会员
  158. ];
  159. $userId = Db::name("user")->insertGetId($data);
  160. $data = Db::name("user")->where('id', $userId)->find();
  161. cmf_update_current_user($data);
  162. $token = cmf_generate_user_token($userId, 'web');
  163. if (!empty($token)) {
  164. session('token', $token);
  165. }
  166. return 0;
  167. }
  168. return 1;
  169. }
  170. /**
  171. * 通过邮箱重置密码
  172. * @param $email
  173. * @param $password
  174. * @return int
  175. */
  176. public function emailPasswordReset($email, $password)
  177. {
  178. $result = $this->where('user_email', $email)->find();
  179. if (!empty($result)) {
  180. $data = [
  181. 'user_pass' => cmf_password($password),
  182. ];
  183. $this->where('user_email', $email)->update($data);
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. /**
  189. * 通过手机重置密码
  190. * @param $mobile
  191. * @param $password
  192. * @return int
  193. */
  194. public function mobilePasswordReset($mobile, $password)
  195. {
  196. $result = Db::name("user")->where('mobile', $mobile)->find();
  197. if (!empty($result)) {
  198. $data = [
  199. 'user_pass' => cmf_password($password),
  200. ];
  201. Db::name("user")->where('mobile', $mobile)->update($data);
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. public function editData($user)
  207. {
  208. $userId = cmf_get_current_user_id();
  209. if (isset($user['birthday'])) {
  210. $user['birthday'] = strtotime($user['birthday']);
  211. }
  212. $field = 'user_nickname,sex,birthday,user_url,signature,more';
  213. if ($this->allowField($field)->save($user, ['id' => $userId])) {
  214. $userInfo = $this->where('id', $userId)->find();
  215. cmf_update_current_user($userInfo->toArray());
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * 用户密码修改
  222. * @param $user
  223. * @return int
  224. */
  225. public function editPassword($user)
  226. {
  227. $userId = cmf_get_current_user_id();
  228. $userQuery = Db::name("user");
  229. if ($user['password'] != $user['repassword']) {
  230. return 1;
  231. }
  232. $pass = $userQuery->where('id', $userId)->find();
  233. if (!cmf_compare_password($user['old_password'], $pass['user_pass'])) {
  234. return 2;
  235. }
  236. $data['user_pass'] = cmf_password($user['password']);
  237. Db::name("user")->where('id', $userId)->update($data);
  238. return 0;
  239. }
  240. public function comments()
  241. {
  242. $userId = cmf_get_current_user_id();
  243. $userQuery = Db::name("Comment");
  244. $where['user_id'] = $userId;
  245. $where['delete_time'] = 0;
  246. $favorites = $userQuery->where($where)->order('id desc')->paginate(10);
  247. $data['page'] = $favorites->render();
  248. $data['lists'] = $favorites->items();
  249. return $data;
  250. }
  251. public function deleteComment($id)
  252. {
  253. $userId = cmf_get_current_user_id();
  254. $userQuery = Db::name("Comment");
  255. $where['id'] = $id;
  256. $where['user_id'] = $userId;
  257. $data['delete_time'] = time();
  258. $userQuery->where($where)->update($data);
  259. return $data;
  260. }
  261. /**
  262. * 绑定用户手机号
  263. */
  264. public function bindingMobile($user)
  265. {
  266. $userId = cmf_get_current_user_id();
  267. $data ['mobile'] = $user['username'];
  268. Db::name("user")->where('id', $userId)->update($data);
  269. $userInfo = Db::name("user")->where('id', $userId)->find();
  270. cmf_update_current_user($userInfo);
  271. return 0;
  272. }
  273. /**
  274. * 绑定用户邮箱
  275. */
  276. public function bindingEmail($user)
  277. {
  278. $userId = cmf_get_current_user_id();
  279. $data ['user_email'] = $user['username'];
  280. Db::name("user")->where('id', $userId)->update($data);
  281. $userInfo = Db::name("user")->where('id', $userId)->find();
  282. cmf_update_current_user($userInfo);
  283. return 0;
  284. }
  285. }