User.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\View;
  5. use app\common\validate\User as UserValidate;
  6. class User extends Base
  7. {
  8. // 设置数据表(不含前缀)
  9. protected $name = 'user';
  10. // 定义时间戳字段名
  11. protected $createTime = '';
  12. protected $updateTime = '';
  13. // 自动完成
  14. protected $auto = [];
  15. protected $insert = [];
  16. protected $update = [];
  17. public $_guest_group = 1;
  18. public $_def_group = 2;
  19. public function countData($where)
  20. {
  21. $total = $this->where($where)->count();
  22. return $total;
  23. }
  24. public function listData($where, $order, $page = 1, $limit = 20, $start = 0)
  25. {
  26. $page = $page > 0 ? (int)$page : 1;
  27. $limit = $limit ? (int)$limit : 20;
  28. $start = $start ? (int)$start : 0;
  29. $total = $this->where($where)->count();
  30. $list = Db::name('User')->where($where)->order($order)->page($page)->limit($limit)->select();
  31. return ['code' => 1, 'msg' => lang('data_list'), 'page' => $page, 'pagecount' => ceil($total / $limit), 'limit' => $limit, 'total' => $total, 'list' => $list];
  32. }
  33. public function infoData($where, $field='*')
  34. {
  35. if (empty($where) || !is_array($where)) {
  36. return ['code' => 1001, 'msg'=>lang('param_err')];
  37. }
  38. $info = $this->field($field)->where($where)->find();
  39. if (empty($info)) {
  40. return ['code' => 1002, 'msg' => lang('obtain_err')];
  41. }
  42. $info = $info->toArray();
  43. //用户组
  44. $group_list = model('Group')->getCache('group_list');
  45. $info['group'] = $group_list[$info['group_id']];
  46. $info['user_pwd'] = '';
  47. return ['code' => 1, 'msg' =>lang('obtain_ok'), 'info' => $info];
  48. }
  49. public function saveData($data)
  50. {
  51. $validate = \think\Loader::validate('User');
  52. if (isset($data['user_start_time']) && !is_numeric($data['user_start_time'])) {
  53. $data['user_start_time'] = strtotime($data['user_start_time']);
  54. }
  55. if (isset($data['user_end_time']) && !is_numeric($data['user_end_time'])) {
  56. $data['user_end_time'] = strtotime($data['user_end_time']);
  57. }
  58. if (!empty($data['user_id'])) {
  59. if (!$validate->scene('edit')->check($data)) {
  60. return ['code' => 1001, 'msg' => lang('param_err').':' . $validate->getError()];
  61. }
  62. if (empty($data['user_pwd'])) {
  63. unset($data['user_pwd']);
  64. } else {
  65. $data['user_pwd'] = md5($data['user_pwd']);
  66. }
  67. $where = [];
  68. $where['user_id'] = ['eq', $data['user_id']];
  69. $res = $this->where($where)->update($data);
  70. } else {
  71. if (!$validate->scene('edit')->check($data)) {
  72. return ['code' => 1002, 'msg' => lang('param_err').':' . $validate->getError()];
  73. }
  74. $data['user_pwd'] = md5($data['user_pwd']);
  75. $res = $this->insert($data);
  76. }
  77. if (false === $res) {
  78. return ['code' => 1003, 'msg' => '' . $this->getError()];
  79. }
  80. return ['code' => 1, 'msg' =>lang('save_ok')];
  81. }
  82. public function delData($where)
  83. {
  84. $res = $this->where($where)->delete();
  85. if ($res === false) {
  86. return ['code' => 1001, 'msg' => lang('del_err').':' . $this->getError()];
  87. }
  88. return ['code' => 1, 'msg'=>lang('del_ok')];
  89. }
  90. public function fieldData($where, $col, $val)
  91. {
  92. if (!isset($col) || !isset($val)) {
  93. return ['code' => 1001, 'msg'=>lang('param_err')];
  94. }
  95. $data = [];
  96. $data[$col] = $val;
  97. $res = $this->where($where)->update($data);
  98. if ($res === false) {
  99. return ['code' => 1002, 'msg' => lang('set_err').':' . $this->getError()];
  100. }
  101. return ['code' => 1, 'msg' =>lang('set_ok')];
  102. }
  103. public function register($param)
  104. {
  105. $config = config('maccms');
  106. $data = [];
  107. $password_raw = trim($param['user_pwd']);
  108. $data['user_name'] = htmlspecialchars(urldecode(trim($param['user_name'])));
  109. $data['user_pwd'] = htmlspecialchars(urldecode(trim($param['user_pwd'])));
  110. $data['user_pwd2'] = htmlspecialchars(urldecode(trim($param['user_pwd2'])));
  111. $data['verify'] = $param['verify'];
  112. $uid = $param['uid'];
  113. $is_from_3rdparty = !empty($param['user_openid_qq']) || !empty($param['user_openid_weixin']);
  114. if ($config['user']['status'] == 0 || $config['user']['reg_open'] == 0) {
  115. return ['code' => 1001, 'msg' => lang('model/user/not_open_reg')];
  116. }
  117. if (empty($data['user_name']) || empty($data['user_pwd']) || empty($data['user_pwd2'])) {
  118. return ['code' => 1002, 'msg' => lang('model/user/input_require')];
  119. }
  120. if (!$is_from_3rdparty && !captcha_check($data['verify']) && $config['user']['reg_verify'] == 1) {
  121. return ['code' => 1003, 'msg' => lang('verify_err')];
  122. }
  123. if ($data['user_pwd'] != $data['user_pwd2']) {
  124. return ['code' => 1004, 'msg' => lang('model/user/pass_not_pass2')];
  125. }
  126. $row = $this->where('user_name', $data['user_name'])->find();
  127. if (!empty($row)) {
  128. return ['code' => 1005, 'msg' => lang('model/user/haved_reg')];
  129. }
  130. if (!preg_match("/^[a-zA-Z\d]*$/i", $data['user_name'])) {
  131. return ['code' => 1006, 'msg' => lang('model/user/name_contain')];
  132. }
  133. $validate = \think\Loader::validate('User');
  134. if (!$validate->scene('add')->check($data)) {
  135. return ['code' => 1007, 'msg' => lang('param_err').':' . $validate->getError()];
  136. }
  137. $filter = $GLOBALS['config']['user']['filter_words'];
  138. if(!empty($filter)) {
  139. $filter_arr = explode(',', $filter);
  140. $f_name = str_replace($filter_arr, '', $data['user_name']);
  141. if ($f_name != $data['user_name']) {
  142. return ['code' => 1008, 'msg' =>lang('model/user/name_filter',[$filter])];
  143. }
  144. }
  145. $ip = mac_get_ip_long();
  146. if( $GLOBALS['config']['user']['reg_num'] > 0){
  147. $where2=[];
  148. $where2['user_reg_ip'] = ['eq', $ip];
  149. $where2['user_reg_time'] = ['gt', strtotime('today')];
  150. $cc = $this->where($where2)->count();
  151. if($cc >= $GLOBALS['config']['user']['reg_num']){
  152. return ['code' => 1009, 'msg' => lang('model/user/ip_limit',[$GLOBALS['config']['user']['reg_num']])];
  153. }
  154. }
  155. $fields = [];
  156. $fields['user_name'] = $data['user_name'];
  157. $fields['user_pwd'] = md5($password_raw);
  158. $fields['group_id'] = $this->_def_group;
  159. $fields['user_points'] = intval($config['user']['reg_points']);
  160. $fields['user_status'] = intval($config['user']['reg_status']);
  161. $fields['user_reg_time'] = time();
  162. $fields['user_reg_ip'] = $ip;
  163. $fields['user_openid_qq'] = (string)$param['user_openid_qq'];
  164. $fields['user_openid_weixin'] = (string)$param['user_openid_weixin'];
  165. if (!$is_from_3rdparty) {
  166. // https://github.com/magicblack/maccms10/issues/418
  167. if($config['user']['reg_phone_sms'] == '1'){
  168. $param['type'] = 3;
  169. $res = $this->check_msg($param);
  170. if($res['code'] >1){
  171. return $res;
  172. }
  173. $fields['user_phone'] = $param['to'];
  174. $update=[];
  175. $update['user_phone'] = '';
  176. $where2=[];
  177. $where2['user_phone'] = $param['to'];
  178. $row = $this->where($where2)->find();
  179. if (!empty($row)) {
  180. return ['code' => 1011, 'msg' =>lang('model/user/phone_haved')];
  181. }
  182. //$this->where($where2)->update($update);
  183. }
  184. elseif($config['user']['reg_email_sms'] == '1'){
  185. $param['type'] = 3;
  186. $res = $this->check_msg($param);
  187. if($res['code'] >1){
  188. return $res;
  189. }
  190. $fields['user_email'] = $param['to'];
  191. $update=[];
  192. $update['user_email'] = '';
  193. $where2=[];
  194. $where2['user_email'] = $param['to'];
  195. $row = $this->where($where2)->find();
  196. if (!empty($row)) {
  197. return ['code' => 1012, 'msg' => lang('model/user/email_haved')];
  198. }
  199. //$this->where($where2)->update($update);
  200. }
  201. }
  202. $res = $this->insert($fields);
  203. if ($res === false) {
  204. return ['code' => 1010, 'msg' => lang('model/user/reg_err')];
  205. }
  206. $nid = $this->getLastInsID();
  207. $uid = intval($uid);
  208. if($uid > 0) {
  209. $where2 = [];
  210. $where2['user_id'] = $uid;
  211. $invite = $this->where($where2)->find();
  212. if ($invite) {
  213. $where=[];
  214. $where['user_id'] = $nid;
  215. $update=[];
  216. $update['user_pid'] = $invite['user_id'];
  217. $update['user_pid_2'] = $invite['user_pid'];
  218. $update['user_pid_3'] = $invite['user_pid_2'];
  219. $r1 = $this->where($where)->update($update);
  220. $r2 = false;
  221. $config['user']['invite_reg_num'] = intval($config['user']['invite_reg_num']);
  222. if($config['user']['invite_reg_points']>0){
  223. $r2 = $this->where($where2)->setInc('user_points', $config['user']['invite_reg_points']);
  224. }
  225. if($r2!==false) {
  226. //积分日志
  227. $data = [];
  228. $data['user_id'] = $uid;
  229. $data['plog_type'] = 2;
  230. $data['plog_points'] = $config['user']['invite_reg_points'];
  231. model('Plog')->saveData($data);
  232. }
  233. }
  234. }
  235. return ['code' => 1, 'msg' => lang('model/user/reg_ok')];
  236. }
  237. public function regcheck($t, $str)
  238. {
  239. $where = [];
  240. if ($t == 'user_name') {
  241. $where['user_name'] = $str;
  242. $row = $this->where($where)->find();
  243. if (!empty($row)) {
  244. return ['code' => 1001, 'msg' => lang('registered')];
  245. }
  246. } elseif ($t == 'user_email') {
  247. $where['user_email'] = $str;
  248. $row = $this->where($where)->find();
  249. if (!empty($row)) {
  250. return ['code' => 1001, 'msg' => lang('registered')];
  251. }
  252. } elseif ($t == 'verify') {
  253. if (!captcha_check($str)) {
  254. return ['code' => 1002, 'msg' => lang('verify_err')];
  255. }
  256. }
  257. return ['code' => 1, 'msg' => 'ok'];
  258. }
  259. public function info($param)
  260. {
  261. if (empty($param['user_pwd'])) {
  262. return ['code' => 1001, 'msg' => lang('model/user/input_old_pass')];
  263. }
  264. $password_raw = trim($param['user_pwd']);
  265. $password_formatted = htmlspecialchars(urldecode(trim($param['user_pwd'])));
  266. if (!in_array($GLOBALS['user']['user_pwd'], [md5($password_raw), md5($password_formatted)])) {
  267. return ['code' => 1002, 'msg' => lang('model/user/old_pass_err')];
  268. }
  269. if ($param['user_pwd1'] != $param['user_pwd2']) {
  270. return ['code' => 1003, 'msg' => lang('model/user/pass_not_same_pass2')];
  271. }
  272. $data = [];
  273. $data['user_id'] = $GLOBALS['user']['user_id'];
  274. $data['user_name'] = $GLOBALS['user']['user_name'];
  275. if(!empty($param['user_nick_name'])){
  276. $data['user_nick_name'] = htmlspecialchars(urldecode(trim($param['user_nick_name'])));
  277. }
  278. $data['user_qq'] = htmlspecialchars(urldecode(trim($param['user_qq'])));
  279. $data['user_question'] = htmlspecialchars(urldecode(trim($param['user_question'])));
  280. $data['user_answer'] = htmlspecialchars(urldecode(trim($param['user_answer'])));
  281. if (!empty($param['user_pwd2'])) {
  282. $data['user_pwd'] = trim($param['user_pwd2']);
  283. }
  284. return $this->saveData($data);
  285. }
  286. public function login($param)
  287. {
  288. $data = [];
  289. $password_raw = trim($param['user_pwd']);
  290. $data['user_name'] = htmlspecialchars(urldecode(trim($param['user_name'])));
  291. $data['user_pwd'] = htmlspecialchars(urldecode(trim($param['user_pwd'])));
  292. $data['verify'] = $param['verify'];
  293. $data['openid'] = htmlspecialchars(urldecode(trim($param['openid'])));
  294. $data['col'] = htmlspecialchars(urldecode(trim($param['col'])));
  295. if (empty($data['openid'])) {
  296. if (empty($data['user_name']) || empty($data['user_pwd'])) {
  297. return ['code' => 1001, 'msg' => lang('model/user/input_require')];
  298. }
  299. if ($GLOBALS['config']['user']['login_verify'] ==1 && !captcha_check($data['verify'])) {
  300. return ['code' => 1002, 'msg' => lang('verify_err')];
  301. }
  302. $where = [];
  303. $pattern = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';
  304. if (!preg_match($pattern, $data['user_name'])) {
  305. $where['user_name'] = ['eq', $data['user_name']];
  306. } else {
  307. $where['user_email'] = ['eq', $data['user_name']];
  308. }
  309. // https://github.com/magicblack/maccms10/issues/781 兼容密码
  310. $where['user_pwd'] = [['eq', md5($password_raw)], ['eq', $data['user_pwd']], 'or'];
  311. } else {
  312. if (empty($data['openid']) || empty($data['col'])) {
  313. return ['code' => 1001, 'msg' => lang('model/user/input_require')];
  314. }
  315. if (!in_array($data['col'], ['user_openid_qq', 'user_openid_weixin'])) {
  316. return ['code' => 1002, 'msg' => lang('param_err') . ': col'];
  317. }
  318. $where[$data['col']] = $data['openid'];
  319. }
  320. $where['user_status'] = ['eq', 1];
  321. $row = $this->where($where)->find();
  322. if(empty($row)) {
  323. return ['code' => 1003, 'msg' => lang('model/user/not_found')];
  324. }
  325. if($row['group_id'] > 2 && $row['user_end_time'] < time()) {
  326. $row['group_id'] = 2;
  327. $update['group_id'] = 2;
  328. }
  329. $random = md5(rand(10000000, 99999999));
  330. $update['user_random'] = $random;
  331. $update['user_login_ip'] = mac_get_ip_long();
  332. $update['user_login_time'] = time();
  333. $update['user_login_num'] = $row['user_login_num'] + 1;
  334. $update['user_last_login_time'] = $row['user_login_time'];
  335. $update['user_last_login_ip'] = $row['user_login_ip'];
  336. $res = $this->where($where)->update($update);
  337. if ($res === false) {
  338. return ['code' => 1004, 'msg' => lang('model/user/update_login_err')];
  339. }
  340. //用户组
  341. $group_list = model('Group')->getCache('group_list');
  342. $group = $group_list[$row['group_id']];
  343. cookie('user_id', $row['user_id'],['expire'=>2592000] );
  344. cookie('user_name', $row['user_name'],['expire'=>2592000] );
  345. cookie('group_id', $group['group_id'],['expire'=>2592000] );
  346. cookie('group_name', $group['group_name'],['expire'=>2592000] );
  347. cookie('user_check', md5($random . '-' .$row['user_name'] . '-' . $row['user_id'] .'-' ),['expire'=>2592000] );
  348. cookie('user_portrait', mac_get_user_portrait($row['user_id']),['expire'=>2592000] );
  349. return ['code' => 1, 'msg' => lang('model/user/login_ok')];
  350. }
  351. public function expire()
  352. {
  353. $where=[];
  354. $where['group_id'] = ['gt',2];
  355. $where['user_end_time'] = ['elt',time()];
  356. $update=[];
  357. $update['group_id'] = 2;
  358. $res = $this->where($where)->update($update);
  359. if ($res === false) {
  360. return ['code' => 101, 'msg' => lang('model/user/update_expire_err')];
  361. }
  362. return ['code' => 1, 'msg' => lang('model/user/update_expire_ok')];
  363. }
  364. public function logout()
  365. {
  366. cookie('user_id', null);
  367. cookie('user_name', null);
  368. cookie('group_id', null);
  369. cookie('group_name', null);
  370. cookie('user_check', null);
  371. cookie('user_portrait', null);
  372. return ['code' => 1, 'msg' =>lang('model/user/logout_ok')];
  373. }
  374. public function checkLogin()
  375. {
  376. $user_id = cookie('user_id');
  377. $user_name = cookie('user_name');
  378. $user_check = cookie('user_check');
  379. $user_id = htmlspecialchars(urldecode(trim($user_id)));
  380. $user_name = htmlspecialchars(urldecode(trim($user_name)));
  381. $user_check = htmlspecialchars(urldecode(trim($user_check)));
  382. if (empty($user_id) || empty($user_name) || empty($user_check)) {
  383. return ['code' => 1001, 'msg' => lang('model/user/not_login')];
  384. }
  385. $where = [];
  386. $where['user_id'] = $user_id;
  387. $where['user_name'] = $user_name;
  388. $where['user_status'] = 1;
  389. $info = $this->field('*')->where($where)->find();
  390. if(empty($info)) {
  391. return ['code' => 1002, 'msg' => lang('model/user/not_login')];
  392. }
  393. $info = $info->toArray();
  394. $login_check = md5($info['user_random'] . '-' . $info['user_name']. '-' . $info['user_id'] .'-' );
  395. if($login_check != $user_check) {
  396. return ['code' => 1003, 'msg' => lang('model/user/not_login')];
  397. }
  398. $group_list = model('Group')->getCache('group_list');
  399. $info['group'] = $group_list[$info['group_id']];
  400. //会员截止日期
  401. if ($info['group_id'] > 2 && $info['user_end_time'] < time()) {
  402. //用户组
  403. $info['group'] = $group_list[2];
  404. $update = [];
  405. $update['group_id'] = 2;
  406. $res = $this->where($where)->update($update);
  407. if($res === false){
  408. return ['code' => 1004, 'msg' => lang('model/user/update_expire_err')];
  409. }
  410. cookie('group_id', $info['group']['group_id'], ['expire'=>2592000] );
  411. cookie('group_name', $info['group']['group_name'],['expire'=>2592000] );
  412. }
  413. return ['code' => 1, 'msg' => lang('model/user/haved_login'), 'info' => $info];
  414. }
  415. public function resetPwd()
  416. {
  417. }
  418. public function findpass($param)
  419. {
  420. $data = [];
  421. $password_raw = trim($param['user_pwd']);
  422. $data['user_name'] = htmlspecialchars(urldecode(trim($param['user_name'])));
  423. $data['user_question'] = htmlspecialchars(urldecode(trim($param['user_question'])));
  424. $data['user_answer'] = htmlspecialchars(urldecode(trim($param['user_answer'])));
  425. $data['user_pwd'] = htmlspecialchars(urldecode(trim($param['user_pwd'])));
  426. $data['user_pwd2'] = htmlspecialchars(urldecode(trim($param['user_pwd2'])));
  427. $data['verify'] = $param['verify'];
  428. if (empty($data['user_name']) || empty($data['user_question']) || empty($data['user_answer']) || empty($data['user_pwd']) || empty($data['user_pwd2']) || empty($data['verify'])) {
  429. return ['code' => 1001, 'msg' => lang('param_err')];
  430. }
  431. if (!captcha_check($data['verify'])) {
  432. return ['code' => 1002, 'msg' => lang('verify_err')];
  433. }
  434. if ($data['user_pwd'] != $data['user_pwd2']) {
  435. return ['code' => 1003, 'msg' => lang('model/user/pass_not_same_pass2')];
  436. }
  437. $where = [];
  438. $where['user_name'] = $data['user_name'];
  439. $where['user_question'] = $data['user_question'];
  440. $where['user_answer'] = $data['user_answer'];
  441. $info = $this->where($where)->find();
  442. if (empty($info)) {
  443. return ['code' => 1004, 'msg' => lang('model/user/findpass_not_found')];
  444. }
  445. $update = [];
  446. $update['user_pwd'] = md5($password_raw);
  447. $where = [];
  448. $where['user_id'] = $info['user_id'];
  449. $res = $this->where($where)->update($update);
  450. if (false === $res) {
  451. return ['code' => 1005, 'msg' => '' . $this->getError()];
  452. }
  453. return ['code' => 1, 'msg' => lang('model/user/findpass_ok')];
  454. }
  455. public function popedom($type_id, $popedom, $group_id = 1)
  456. {
  457. $group_list = model('Group')->getCache();
  458. $group_info = $group_list[$group_id];
  459. if (strpos(',' . $group_info['group_type'], ',' . $type_id . ',') !== false && !empty($group_info['group_popedom'][$type_id][$popedom]) !== false) {
  460. return true;
  461. }
  462. return false;
  463. }
  464. public function upgrade($param)
  465. {
  466. $group_id = intval($param['group_id']);
  467. $long = $param['long'];
  468. $points_long = ['day'=>86400,'week'=>86400*7,'month'=>86400*30,'year'=>86400*365];
  469. if (!array_key_exists($long, $points_long)) {
  470. return ['code'=>1001,'msg'=>'非法操作'];
  471. }
  472. if($group_id <3){
  473. return ['code'=>1002,'msg'=>lang('model/user/select_diy_group_err')];
  474. }
  475. $group_list = model('Group')->getCache();
  476. $group_info = $group_list[$group_id];
  477. if(empty($group_info)){
  478. return ['code'=>1003,'msg'=>lang('model/user/group_not_found')];
  479. }
  480. if($group_info['group_status'] == 0){
  481. return ['code'=>1004,'msg'=>lang('model/user/group_is_close')];
  482. }
  483. $point = $group_info['group_points_'.$long];
  484. if($GLOBALS['user']['user_points'] < $point){
  485. return ['code'=>1005,'msg'=>lang('model/user/potins_not_enough')];
  486. }
  487. $sj = $points_long[$long];
  488. $end_time = time() + $sj;
  489. if($GLOBALS['user']['user_end_time'] > time() ){
  490. $end_time = $GLOBALS['user']['user_end_time'] + $sj;
  491. }
  492. $where = [];
  493. $where['user_id'] = $GLOBALS['user']['user_id'];
  494. $data = [];
  495. $data['user_points'] = $GLOBALS['user']['user_points'] - $point;
  496. $data['user_end_time'] = $end_time;
  497. $data['group_id'] = $group_id;
  498. $res = $this->where($where)->update($data);
  499. if($res===false){
  500. return ['code'=>1009,'msg'=>lang('model/user/update_group_err')];
  501. }
  502. //积分日志
  503. $data = [];
  504. $data['user_id'] = $GLOBALS['user']['user_id'];
  505. $data['plog_type'] = 7;
  506. $data['plog_points'] = $point;
  507. model('Plog')->saveData($data);
  508. //分销日志
  509. $this->reward($point);
  510. cookie('group_id', $group_info['group_id'],['expire'=>2592000] );
  511. cookie('group_name', $group_info['group_name'],['expire'=>2592000] );
  512. return ['code'=>1,'msg'=>lang('model/user/update_group_ok')];
  513. }
  514. public function check_msg($param)
  515. {
  516. $param['to'] = htmlspecialchars(urldecode(trim($param['to'])));
  517. $param['code'] = htmlspecialchars(urldecode(trim($param['code'])));
  518. if(!in_array($param['ac'],['email','phone']) || empty($param['to']) || empty($param['code']) || empty($param['type'])){
  519. return ['code'=>9001,'msg'=>lang('param_err')];
  520. }
  521. // https://github.com/magicblack/maccms10/issues/792 邮箱增加黑白名单校验
  522. if ($param['ac'] == 'email' && in_array($param['type'], [1, 3])) {
  523. $result = UserValidate::validateEmail($param['to']);
  524. if ($result['code'] > 1) {
  525. return $result;
  526. }
  527. }
  528. //msg_type 1绑定2找回3注册
  529. $stime = strtotime('-5 min');
  530. if($param['ac']=='email' && intval($GLOBALS['config']['email']['time'])>0){
  531. $stime = strtotime('-'.$GLOBALS['config']['email']['time'].' min');
  532. }
  533. $where=[];
  534. $where['user_id'] = intval($GLOBALS['user']['user_id']);
  535. $where['msg_time'] = ['gt',$stime];
  536. $where['msg_code'] = ['eq',$param['code']];
  537. $where['msg_type'] = ['eq', $param['type'] ];
  538. $res = model('msg')->infoData($where);
  539. if($res['code'] >1){
  540. return ['code'=>9002,'msg'=>lang('model/user/msg_not_found')];
  541. }
  542. return ['code'=>1,'msg'=>'ok'];
  543. }
  544. public function send_msg($param)
  545. {
  546. $param['to'] = htmlspecialchars(urldecode(trim($param['to'])));
  547. $param['code'] = htmlspecialchars(urldecode(trim($param['code'])));
  548. $type_arr = [
  549. 1=>['des'=>lang('bind'),'flag'=>'bind'],
  550. 2=>['des'=>lang('findpass'),'flag'=>'findpass'],
  551. 3=>['des'=>lang('register'),'flag'=>'reg'],
  552. ];
  553. if(!in_array($param['ac'],['email','phone']) || !isset($type_arr[$param['type']]) || empty($param['to']) || empty($param['type'])){
  554. return ['code'=>9001,'msg'=>lang('param_err')];
  555. }
  556. // https://github.com/magicblack/maccms10/issues/792 邮箱增加黑白名单校验
  557. if ($param['ac'] == 'email' && in_array($param['type'], [1, 3])) {
  558. $result = UserValidate::validateEmail($param['to']);
  559. if ($result['code'] > 1) {
  560. return $result;
  561. }
  562. }
  563. $type_des = $type_arr[$param['type']]['des'];
  564. $type_flag = $type_arr[$param['type']]['flag'];
  565. $to = $param['to'];
  566. $code = mac_get_rndstr(6,'num');
  567. $r=0;
  568. $stime = strtotime('-5 min');
  569. if($param['ac']=='email' && intval($GLOBALS['config']['email']['time'])>0){
  570. $stime = strtotime('-'.$GLOBALS['config']['email']['time'].' min');
  571. }
  572. $where=[];
  573. $where['user_id'] = intval($GLOBALS['user']['user_id']);
  574. $where['msg_time'] = ['gt',$stime];
  575. $where['msg_type'] = ['eq', $param['type'] ];
  576. $where['msg_to'] = ['eq', $param['to'] ];
  577. $res = model('msg')->infoData($where);
  578. if($res['code'] ==1){
  579. return ['code'=>9002,'msg'=>lang('model/user/do_not_send_frequently')];
  580. }
  581. $res_msg= ','.lang('please_try_again');
  582. if($param['ac']=='email'){
  583. $title = $GLOBALS['config']['email']['tpl']['user_'.$type_flag.'_title'];
  584. $msg = $GLOBALS['config']['email']['tpl']['user_'.$type_flag.'_body'];
  585. View::instance()->assign(['code'=>$code,'time'=>$GLOBALS['config']['email']['time']]);
  586. $title = View::instance()->display($title);
  587. $msg = View::instance()->display($msg);
  588. $msg = htmlspecialchars_decode($msg);
  589. $res_send = mac_send_mail($to, $title, $msg);
  590. $res_code = $res_send['code'];
  591. $res_msg = $res_send['msg'];
  592. }
  593. else{
  594. $msg = $GLOBALS['config']['sms']['content'];
  595. $msg = str_replace(['[用户]','[类型]','[时长]','[验证码]'],[$GLOBALS['user']['user_name'],$type_des,'5',$code],$msg);
  596. $res_send = mac_send_sms($to,$code,$type_flag,$type_des,$msg);
  597. $res_code = $res_send['code'];
  598. $res_msg = $res_send['msg'];
  599. }
  600. if($res_code==1){
  601. $data=[];
  602. $data['user_id'] = intval($GLOBALS['user']['user_id']);
  603. $data['msg_type'] = $param['type'];
  604. $data['msg_status'] = 0;
  605. $data['msg_to'] = $to;
  606. $data['msg_code'] = $code;
  607. $data['msg_content'] = $msg;
  608. $data['msg_time'] = time();
  609. $res = model('msg')->saveData($data);
  610. return ['code'=>1,'msg'=>lang('model/user/msg_send_ok')];
  611. }
  612. else{
  613. return ['code'=>9009,'msg'=>lang('model/user/msg_send_err').':'.$res_msg];
  614. }
  615. }
  616. public function bind($param)
  617. {
  618. $param['type'] = 1;
  619. $res = $this->check_msg($param);
  620. if($res['code'] >1){
  621. return ['code'=>$res['code'],'msg'=>$res['msg']];
  622. }
  623. $update=[];
  624. $update2=[];
  625. $where2=[];
  626. if($param['ac']=='email') {
  627. $update['user_email'] = $param['to'];
  628. $update2['user_email'] = '';
  629. $where2['user_email'] = $param['to'];
  630. }
  631. else{
  632. $update['user_phone'] = $param['to'];
  633. $update2['user_phone'] = '';
  634. $where2['user_phone'] = $param['to'];
  635. }
  636. $this->where($where2)->update($update2);
  637. $where=[];
  638. $where['user_id'] = $GLOBALS['user']['user_id'];
  639. $res = $this->where($where)->update($update);
  640. if($res===false){
  641. return ['code'=>2003,'msg'=>lang('model/user/update_bind_err')];
  642. }
  643. return ['code'=>1,'msg'=>lang('model/user/update_bind_ok')];
  644. }
  645. public function unbind($param)
  646. {
  647. if(!in_array($param['ac'],['email','phone']) ){
  648. return ['code'=>2001,'msg'=>lang('param_err')];
  649. }
  650. $col = 'user_email';
  651. if($param['ac']=='phone'){
  652. $col = 'user_phone';
  653. }
  654. $update=[];
  655. $update[$col] = '';
  656. $where=[];
  657. $where['user_id'] = $GLOBALS['user']['user_id'];
  658. $res = $this->where($where)->update($update);
  659. if($res===false){
  660. return ['code'=>2002,'msg'=>lang('model/user/update_bind_err')];
  661. }
  662. return ['code'=>1,'msg'=>lang('model/user/update_unbind_ok')];
  663. }
  664. public function bindmsg($param)
  665. {
  666. $param['type'] = 1;
  667. return $this->send_msg($param);
  668. }
  669. public function findpass_msg($param)
  670. {
  671. $param['type'] = 2;
  672. return $this->send_msg($param);
  673. }
  674. public function reg_msg($param)
  675. {
  676. $param['type'] = 3;
  677. return $this->send_msg($param);
  678. }
  679. public function findpass_reset($param)
  680. {
  681. $to = htmlspecialchars(urldecode(trim($param['user_email'])));
  682. if(empty($to)){
  683. $to = htmlspecialchars(urldecode(trim($param['to'])));
  684. }
  685. $password_raw = trim($param['user_pwd']);
  686. $param['code'] = htmlspecialchars(urldecode(trim($param['code'])));
  687. $param['user_pwd'] = htmlspecialchars(urldecode(trim($param['user_pwd'])));
  688. $param['user_pwd2'] = htmlspecialchars(urldecode(trim($param['user_pwd2'])));
  689. if (strlen($param['user_pwd']) < 6) {
  690. return ['code' => 2002, 'msg' => lang('model/user/pass_length_err')];
  691. }
  692. if ($param['user_pwd'] != $param['user_pwd2']) {
  693. return ['code' => 2003, 'msg' => lang('model/user/pass_not_same_pass2')];
  694. }
  695. $param['type'] = 2;
  696. $res = $this->check_msg($param);
  697. if($res['code'] >1){
  698. return ['code'=>$res['code'],'msg'=>$res['msg']];
  699. }
  700. if($param['ac']=='email') {
  701. $pattern = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';
  702. if(!preg_match( $pattern, $to)){
  703. return ['code'=>2005,'msg'=>lang('model/user/email_format_err')];
  704. }
  705. $where = [];
  706. $where['user_email'] = $to;
  707. $user = $this->where($where)->find();
  708. if (!$user) {
  709. return ['code' => 2006, 'msg' => lang('model/user/email_err')];
  710. }
  711. }
  712. else{
  713. $pattern = "/^1{1}\d{10}$/";
  714. if(!preg_match($pattern,$to)){
  715. return ['code'=>2007,'msg'=>lang('model/user/phone_format_err')];
  716. }
  717. $where = [];
  718. $where['user_phone'] = $to;
  719. $user = $this->where($where)->find();
  720. if (!$user) {
  721. return ['code' => 2008, 'msg' =>lang('model/user/phone_err')];
  722. }
  723. }
  724. $update = [];
  725. $update['user_pwd'] = md5($password_raw);
  726. $res = $this->where($where)->update($update);
  727. if($res===false){
  728. return ['code'=>2009,'msg'=>lang('model/user/pass_reset_err')];
  729. }
  730. return ['code'=>1,'msg'=>lang('model/user/pass_reset_ok')];
  731. }
  732. public function visit($param)
  733. {
  734. $param['uid'] = abs(intval($param['uid']));
  735. if ($param['uid'] == 0) {
  736. return ['code' => 101, 'msg' =>lang('model/user/id_err')];
  737. }
  738. $ip = mac_get_ip_long();
  739. $max_cc = $GLOBALS['config']['user']['invite_visit_num'];
  740. if(empty($max_cc)){
  741. $max_cc=1;
  742. }
  743. $todayunix = strtotime("today");
  744. $where = [];
  745. $where['user_id'] = $param['uid'];
  746. $where['visit_ip'] = $ip;
  747. $where['visit_time'] = ['gt', $todayunix];
  748. $cc = model('visit')->where($where)->count();
  749. if ($cc>= $max_cc){
  750. return ['code' => 102, 'msg' => lang('model/user/visit_tip')];
  751. }
  752. $data = [];
  753. $data['user_id'] = $param['uid'];
  754. $data['visit_ip'] = $ip;
  755. $data['visit_time'] = time();
  756. $data['visit_ly'] = htmlspecialchars(mac_get_refer());
  757. $res = model('visit')->saveData($data);
  758. if ($res['code'] > 1) {
  759. return ['code' => 103, 'msg' => lang('model/user/visit_err')];
  760. }
  761. $res = $this->where('user_id', $param['uid'])->setInc('user_points', intval($GLOBALS['config']['user']['invite_visit_points']));
  762. if($res) {
  763. //积分日志
  764. $data = [];
  765. $data['user_id'] = $param['uid'];
  766. $data['plog_type'] = 3;
  767. $data['plog_points'] = intval($GLOBALS['config']['user']['invite_visit_points']);
  768. model('Plog')->saveData($data);
  769. }
  770. return ['code'=>1,'msg'=>lang('model/user/visit_ok')];
  771. }
  772. public function reward($fee_points=0)
  773. {
  774. //三级分销
  775. if($fee_points>0 && $GLOBALS['config']['user']['reward_status'] == '1'){
  776. if(!empty($GLOBALS['config']['user']['reward_ratio']) && !empty($GLOBALS['user']['user_pid'])){
  777. $points = floor($fee_points / 100 * $GLOBALS['config']['user']['reward_ratio']);
  778. if($points>0){
  779. $where=[];
  780. $where['user_id'] = $GLOBALS['user']['user_pid'];
  781. $r = model('User')->where($where)->setInc('user_points',$points);
  782. if($r){
  783. $data = [];
  784. $data['user_id'] = $GLOBALS['user']['user_pid'];
  785. $data['plog_type'] = 4;
  786. $data['plog_points'] = $points;
  787. $data['plog_remarks'] = lang('model/user/reward_tip',[$GLOBALS['user']['user_id'],$GLOBALS['user']['user_name'],$fee_points,$points]);
  788. model('Plog')->saveData($data);
  789. }
  790. }
  791. }
  792. if(!empty($GLOBALS['config']['user']['reward_ratio_2']) && !empty($GLOBALS['user']['user_pid_2'])){
  793. $points = floor($fee_points / 100 * $GLOBALS['config']['user']['reward_ratio_2']);
  794. if($points>0){
  795. $where=[];
  796. $where['user_id'] = $GLOBALS['user']['user_pid_2'];
  797. $r = model('User')->where($where)->setInc('user_points',$points);
  798. if($r){
  799. $data = [];
  800. $data['user_id'] = $GLOBALS['user']['user_pid_2'];
  801. $data['plog_type'] = 5;
  802. $data['plog_points'] = $points;
  803. $data['plog_remarks'] =lang('model/user/reward_tip',[$GLOBALS['user']['user_id'],$GLOBALS['user']['user_name'],$fee_points,$points]);
  804. model('Plog')->saveData($data);
  805. }
  806. }
  807. }
  808. if(!empty($GLOBALS['config']['user']['reward_ratio_3']) && !empty($GLOBALS['user']['user_pid_3'])){
  809. $points = floor($fee_points / 100 * $GLOBALS['config']['user']['reward_ratio_3']);
  810. if($points>0){
  811. $where=[];
  812. $where['user_id'] = $GLOBALS['user']['user_pid_3'];
  813. $r = model('User')->where($where)->setInc('user_points',$points);
  814. if($r){
  815. $data = [];
  816. $data['user_id'] = $GLOBALS['user']['user_pid_3'];
  817. $data['plog_type'] = 6;
  818. $data['plog_points'] = $points;
  819. $data['plog_remarks'] = lang('model/user/reward_tip',[$GLOBALS['user']['user_id'],$GLOBALS['user']['user_name'],$fee_points,$points]);
  820. model('Plog')->saveData($data);
  821. }
  822. }
  823. }
  824. }
  825. return ['code'=>1,'msg'=>lang('model/user/reward_ok')];
  826. }
  827. }