User.php 35 KB

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