Member.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Member extends BaseModel
  12. {
  13. public $page_info;
  14. /**
  15. * 会员详细信息(查库)
  16. * @access public
  17. * @author csdeshang
  18. * @param array $condition 条件
  19. * @param string $field 字段
  20. * @return array
  21. */
  22. public function getMemberInfo($condition, $field = '*')
  23. {
  24. $res = Db::name('member')->field($field)->where($condition)->find();
  25. return $res;
  26. }
  27. /**
  28. * 取得会员详细信息(优先查询缓存)
  29. * 如果未找到,则缓存所有字段
  30. * @access public
  31. * @author csdeshang
  32. * @param int $member_id 会员ID
  33. * @return array
  34. */
  35. public function getMemberInfoByID($member_id)
  36. {
  37. $member_info = rcache($member_id, 'member');
  38. if (empty($member_info)) {
  39. $member_info = $this->getMemberInfo(array('member_id' => $member_id), '*');
  40. wcache($member_id, $member_info, 'member');
  41. }
  42. return $member_info;
  43. }
  44. /**
  45. * 会员列表
  46. * @access public
  47. * @author csdeshang
  48. * @param array $condition 条件
  49. * @param string $field 字段
  50. * @param number $pagesize 分页
  51. * @param string $order 排序
  52. * @return array
  53. */
  54. public function getMemberList($condition = array(), $field = '*', $pagesize = 0, $order = 'member_id desc')
  55. {
  56. if ($pagesize) {
  57. $member_list = Db::name('member')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  58. $this->page_info = $member_list;
  59. return $member_list->items();
  60. } else {
  61. return Db::name('member')->where($condition)->order($order)->select()->toArray();
  62. }
  63. }
  64. /**
  65. * 会员数量
  66. * @access public
  67. * @author csdeshang
  68. * @param array $condition 查询条件
  69. * @return int
  70. */
  71. public function getMemberCount($condition)
  72. {
  73. return Db::name('member')->where($condition)->count();
  74. }
  75. /**
  76. * 编辑会员
  77. * @access public
  78. * @author csdeshang
  79. * @param array $condition 检索条件
  80. * @param array $data 数据
  81. * @return bool
  82. */
  83. public function editMember($condition, $data, $member_id = 0)
  84. {
  85. $update = Db::name('member')->where($condition)->update($data);
  86. if ($update && $member_id) {
  87. dcache($member_id, 'member');
  88. }
  89. return $update;
  90. }
  91. /**
  92. * 登录时创建会话SESSION
  93. * @access public
  94. * @author csdeshang
  95. * @param type $member_info 会员信息
  96. * @param type $reg 规则
  97. * @return type
  98. */
  99. public function createSession($member_info = array(), $reg = false)
  100. {
  101. if (empty($member_info) || !is_array($member_info)) {
  102. return;
  103. }
  104. $member_gradeinfo = model('member')->getOneMemberGrade(intval($member_info['member_exppoints']));
  105. $member_info = array_merge($member_info, $member_gradeinfo);
  106. session('is_login', '1');
  107. session('member_id', $member_info['member_id']);
  108. session('member_name', $member_info['member_name']);
  109. session('member_nickname', $member_info['member_nickname']);
  110. session('member_email', $member_info['member_email']);
  111. session('is_buy', isset($member_info['is_buylimit']) ? $member_info['is_buylimit'] : 1);
  112. session('avatar', $member_info['member_avatar']);
  113. session('level', isset($member_info['level']) ? $member_info['level'] : '');
  114. session('level_name', isset($member_info['level_name']) ? $member_info['level_name'] : '');
  115. session('member_exppoints', $member_info['member_exppoints']); //经验值
  116. session('member_points', $member_info['member_points']); //积分值
  117. // 头衔COOKIE
  118. $this->set_avatar_cookie();
  119. $seller_info = model('seller')->getSellerInfo(array('member_id' => session('member_id')));
  120. if ($seller_info) {
  121. session('store_id', $seller_info['store_id']);
  122. } else {
  123. session('store_id', NULL);
  124. }
  125. if (trim($member_info['member_qqopenid'])) {
  126. session('openid', $member_info['member_qqopenid']);
  127. }
  128. if (trim($member_info['member_sinaopenid'])) {
  129. session('slast_key.uid', $member_info['member_sinaopenid']);
  130. }
  131. if (trim($member_info['member_wxopenid'])) {
  132. session('wxopenid', $member_info['member_wxopenid']);
  133. }
  134. if (trim($member_info['member_wxunionid'])) {
  135. session('wxunionid', $member_info['member_wxunionid']);
  136. }
  137. if (!$reg) {
  138. //添加会员积分
  139. $this->addPoint($member_info);
  140. //添加会员经验值
  141. $this->addExppoint($member_info);
  142. }
  143. if (!empty($member_info['member_logintime'])) {
  144. $update_info = array(
  145. 'member_loginnum' => ($member_info['member_loginnum'] + 1),
  146. 'member_logintime' => TIMESTAMP,
  147. 'member_old_logintime' => $member_info['member_logintime'],
  148. 'member_login_ip' => request()->ip(),
  149. 'member_old_login_ip' => $member_info['member_login_ip']
  150. );
  151. $this->editMember(array('member_id' => $member_info['member_id']), $update_info, $member_info['member_id']);
  152. }
  153. cookie('cart_goods_num', '', -3600);
  154. // cookie中的浏览记录存入数据库
  155. model('goodsbrowse')->mergeGoodsbrowse(session('member_id'), session('store_id'));
  156. if (isset($member_info['auto_login']) && ($member_info['auto_login'] == 1)) {
  157. $this->auto_login();
  158. }
  159. }
  160. /**
  161. * 7天内自动登录
  162. * @access public
  163. * @author csdeshang
  164. */
  165. public function auto_login()
  166. {
  167. // 自动登录标记 保存7天
  168. cookie('auto_login', ds_encrypt(session('member_id'), MD5_KEY), 7 * 24 * 60 * 60);
  169. }
  170. /**
  171. * 设置cookie
  172. * @access public
  173. * @author csdeshang
  174. */
  175. public function set_avatar_cookie()
  176. {
  177. cookie('member_avatar', session('avatar'), 365 * 24 * 60 * 60);
  178. }
  179. /**
  180. * 获取会员信息
  181. * @access public
  182. * @author csdeshang
  183. * @param array $condition 会员条件
  184. * @param string $field 显示字段
  185. * @return array 数组格式的返回结果
  186. */
  187. public function infoMember($condition, $field = '*')
  188. {
  189. if (empty($condition))
  190. return false;
  191. $member_info = Db::name('member')->where($condition)->field($field)->find();
  192. return $member_info;
  193. }
  194. /**
  195. * 注册
  196. * @access public
  197. * @author csdeshang
  198. * @param type $register_info
  199. * @return type
  200. */
  201. public function register($register_info)
  202. {
  203. // 验证用户名是否重复
  204. $check_member_name = $this->getMemberInfo(array('member_name' => $register_info['member_name']));
  205. if (is_array($check_member_name) and count($check_member_name) > 0) {
  206. return array('error' => '用户名已存在');
  207. }
  208. $insert_id = $this->addMember($register_info);
  209. if ($insert_id) {
  210. $this->addMemberAfter($insert_id, $register_info);
  211. $member_info = Db::name('member')->where('member_id', $insert_id)->find();
  212. return $member_info;
  213. } else {
  214. return array('error' => '注册失败');
  215. }
  216. }
  217. /**
  218. * 新增用户后,赠送积分,添加相册等其他操作,主要是针对于 新增用户注册获得积分,等奖励信息的处理
  219. * @access public
  220. * @author csdeshang
  221. * @param type $member_id 会员ID
  222. * @param type $member_info 会员信息
  223. * @return type
  224. */
  225. public function addMemberAfter($member_id, $member_info)
  226. {
  227. //添加会员积分
  228. if (config('ds_config.points_isuse')) {
  229. model('points')->savePointslog('regist', array('pl_memberid' => $member_id, 'pl_membername' => $member_info['member_name']), false);
  230. }
  231. if (isset($member_info['inviter_id'])) {
  232. //向上查询3级更新分销成员数
  233. Db::name('inviter')->where('inviter_id=' . $member_info['inviter_id'])->inc('inviter_1_quantity')->update();
  234. $inviter_2 = Db::name('member')->where('member_id=' . $member_info['inviter_id'])->value('inviter_id');
  235. if ($inviter_2) {
  236. Db::name('inviter')->where('inviter_id=' . $inviter_2)->inc('inviter_2_quantity')->update();
  237. $inviter_3 = Db::name('member')->where('member_id=' . $inviter_2)->value('inviter_id');
  238. if ($inviter_3) {
  239. Db::name('inviter')->where('inviter_id=' . $inviter_3)->inc('inviter_3_quantity')->update();
  240. }
  241. }
  242. //添加邀请人(推荐人)会员积分
  243. $inviter_name = ds_getvalue_byname('member', 'member_id', $member_info['inviter_id'], 'member_name');
  244. if ($inviter_name) {
  245. model('points')->savePointslog('inviter', array(
  246. 'pl_memberid' => $member_info['inviter_id'], 'pl_membername' => $inviter_name,
  247. 'invited' => $member_info['member_name']
  248. ));
  249. }
  250. }
  251. }
  252. /**
  253. * 注册商城会员
  254. * @access public
  255. * @author csdeshang
  256. * @param array $data 会员信息
  257. * @return array 数组格式的返回结果
  258. */
  259. public function addMember($data)
  260. {
  261. if (empty($data)) {
  262. return false;
  263. }
  264. try {
  265. Db::startTrans();
  266. $member_info = array();
  267. $member_info['member_name'] = $data['member_name'];
  268. $member_info['member_password'] = md5(trim($data['member_password']));
  269. if (isset($data['member_email'])) {
  270. $member_info['member_email'] = $data['member_email'];
  271. }
  272. $member_info['member_addtime'] = TIMESTAMP;
  273. $member_info['member_logintime'] = TIMESTAMP;
  274. $member_info['member_old_logintime'] = TIMESTAMP;
  275. $member_info['member_login_ip'] = request()->ip();
  276. $member_info['member_old_login_ip'] = $member_info['member_login_ip'];
  277. $member_info['member_paypwd'] = md5('123456'); //注册会员默认支付密码为123456
  278. if (isset($data['member_truename'])) {
  279. $member_info['member_truename'] = $data['member_truename'];
  280. }
  281. if (isset($data['member_nickname'])) {
  282. $member_info['member_nickname'] = $data['member_nickname'];
  283. } else {
  284. $member_info['member_nickname'] = config('ds_config.site_name') . '_' . rand(100000, 999999);
  285. }
  286. if (isset($data['member_qq'])) {
  287. $member_info['member_qq'] = $data['member_qq'];
  288. }
  289. if (isset($data['member_sex'])) {
  290. $member_info['member_sex'] = $data['member_sex'];
  291. }
  292. if (isset($data['member_avatar'])) {
  293. $member_info['member_avatar'] = $data['member_avatar'];
  294. }
  295. if (isset($data['member_qqopenid'])) {
  296. $member_info['member_qqopenid'] = $data['member_qqopenid'];
  297. }
  298. if (isset($data['member_qqinfo'])) {
  299. $member_info['member_qqinfo'] = $data['member_qqinfo'];
  300. }
  301. if (isset($data['member_sinaopenid'])) {
  302. $member_info['member_sinaopenid'] = $data['member_sinaopenid'];
  303. }
  304. if (isset($data['member_sinainfo'])) {
  305. $member_info['member_sinainfo'] = $data['member_sinainfo'];
  306. }
  307. //添加邀请人(推荐人)会员积分
  308. if (isset($data['inviter_id']) && intval($data['inviter_id']) > 0) {
  309. $member_info['inviter_id'] = intval($data['inviter_id']);
  310. }
  311. // 手机注册登录绑定
  312. if (isset($data['member_mobilebind'])) {
  313. $member_info['member_mobile'] = $data['member_mobile'];
  314. $member_info['member_mobilebind'] = $data['member_mobilebind'];
  315. }
  316. if (isset($data['member_wxunionid'])) {
  317. $member_info['member_wxunionid'] = $data['member_wxunionid'];
  318. $member_info['member_wxinfo'] = $data['member_wxinfo'];
  319. $member_info['member_wxopenid'] = $data['member_wxopenid'];
  320. }
  321. $insert_id = Db::name('member')->insertGetId($member_info);
  322. if (!$insert_id) {
  323. throw new \think\Exception('', 10006);
  324. } else {
  325. //是否有注册红包
  326. $member_info['member_id'] = $insert_id;
  327. $bonus_model = model('bonus');
  328. $bonus = Db::name('bonus')->where('bonus_type', 2)->where('bonus_state', 1)->where('bonus_begintime', '<', TIMESTAMP)->where('bonus_endtime', '>', TIMESTAMP)->find();
  329. if ($bonus) {
  330. //获取未领取单个红包
  331. $bonusreceive = Db::name('bonusreceive')->where('bonus_id', $bonus['bonus_id'])->where('member_id', 0)->find();
  332. if (!empty($bonusreceive)) {
  333. $res = $bonus_model->receiveBonus($member_info, $bonus, $bonusreceive, '领取注册红包');
  334. if (!$res['code']) {
  335. return array('error' => $res['msg']);
  336. }
  337. }
  338. }
  339. }
  340. // 添加默认相册
  341. $insert = array();
  342. $insert['ac_name'] = '买家秀';
  343. $insert['member_id'] = $insert_id;
  344. $insert['ac_des'] = '买家秀默认相册';
  345. $insert['ac_sort'] = 255;
  346. $insert['ac_isdefault'] = 1;
  347. $insert['ac_uploadtime'] = TIMESTAMP;
  348. $result = Db::name('snsalbumclass')->insertGetId($insert);
  349. Db::commit();
  350. return $insert_id;
  351. } catch (Exception $e) {
  352. Db::rollback();
  353. return false;
  354. }
  355. }
  356. /**
  357. * 会员登录检查
  358. * @access public
  359. * @author csdeshang
  360. * @return bool
  361. */
  362. public function checkloginMember()
  363. {
  364. if (session('is_login') == '1') {
  365. @header("Location: " . (string)url('home/Member/index'));
  366. exit();
  367. }
  368. }
  369. /**
  370. * 检查会员是否允许举报商品
  371. * @access public
  372. * @author csdeshang
  373. * @param type $member_id 会员id
  374. * @return boolean
  375. */
  376. public function isMemberAllowInform($member_id)
  377. {
  378. $condition = array();
  379. $condition[] = array('member_id', '=', $member_id);
  380. $member_info = $this->getMemberInfo($condition, 'inform_allow');
  381. if (intval($member_info['inform_allow']) === 1) {
  382. return true;
  383. } else {
  384. return false;
  385. }
  386. }
  387. /**
  388. * 添加会员积分
  389. * @access public
  390. * @author csdeshang
  391. * @param type $member_info 会员信息
  392. * @return type
  393. */
  394. public function addPoint($member_info)
  395. {
  396. if (!config('ds_config.points_isuse') || empty($member_info))
  397. return;
  398. //一天内只有第一次登录赠送积分
  399. if (trim(@date('Y-m-d', $member_info['member_logintime'])) == trim(date('Y-m-d')))
  400. return;
  401. //加入队列
  402. $queue_content = array();
  403. $queue_content['member_id'] = $member_info['member_id'];
  404. $queue_content['member_name'] = $member_info['member_name'];
  405. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'addPoint', 'cron_value' => serialize($queue_content)));
  406. }
  407. /**
  408. * 添加会员经验值
  409. * @access public
  410. * @author csdeshang
  411. * @param unknown $member_info 会员信息
  412. */
  413. public function addExppoint($member_info)
  414. {
  415. if (empty($member_info))
  416. return;
  417. //一天内只有第一次登录赠送经验值
  418. if (trim(@date('Y-m-d', $member_info['member_logintime'])) == trim(date('Y-m-d')))
  419. return;
  420. //加入队列
  421. $queue_content = array();
  422. $queue_content['member_id'] = $member_info['member_id'];
  423. $queue_content['member_name'] = $member_info['member_name'];
  424. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'addExppoint', 'cron_value' => serialize($queue_content)));
  425. }
  426. /**
  427. * 取得会员安全级别
  428. * @access public
  429. * @author csdeshang
  430. * @param array $member_info 会员信息
  431. */
  432. public function getMemberSecurityLevel($member_info = array())
  433. {
  434. $tmp_level = 0;
  435. if ($member_info['member_emailbind'] == '1') {
  436. $tmp_level += 1;
  437. }
  438. if ($member_info['member_mobilebind'] == '1') {
  439. $tmp_level += 1;
  440. }
  441. if ($member_info['member_paypwd'] != '') {
  442. $tmp_level += 1;
  443. }
  444. return $tmp_level;
  445. }
  446. /**
  447. * 获得会员等级
  448. * @access public
  449. * @author csdeshang
  450. * @param bool $show_progress 是否计算其当前等级进度
  451. * @param int $exppoints 会员经验值
  452. * @param array $cur_level 会员当前等级
  453. * @return type
  454. */
  455. public function getMemberGradeArr($show_progress = false, $exppoints = 0, $cur_level = '')
  456. {
  457. $member_grade = config('ds_config.member_grade') ? unserialize(config('ds_config.member_grade')) : array();
  458. //处理会员等级进度
  459. if ($member_grade && $show_progress) {
  460. $is_max = false;
  461. if ($cur_level === '') {
  462. $cur_gradearr = $this->getOneMemberGrade($exppoints, false, $member_grade);
  463. $cur_level = $cur_gradearr['level'];
  464. }
  465. foreach ($member_grade as $k => $v) {
  466. if ($cur_level == $v['level']) {
  467. $v['is_cur'] = true;
  468. }
  469. $member_grade[$k] = $v;
  470. }
  471. }
  472. return $member_grade;
  473. }
  474. /**
  475. * 获得某一会员等级
  476. * @access public
  477. * @author csdeshang
  478. * @param int $exppoints 会员经验值
  479. * @param bool $show_progress 是否计算其当前等级进度
  480. * @param array $member_grade 会员等级
  481. * @return type
  482. */
  483. public function getOneMemberGrade($exppoints, $show_progress = false, $member_grade = array())
  484. {
  485. if (!$member_grade) {
  486. $member_grade = config('ds_config.member_grade') ? unserialize(config('ds_config.member_grade')) : array();
  487. }
  488. if (empty($member_grade)) { //如果会员等级设置为空
  489. $grade_arr['level'] = -1;
  490. $grade_arr['level_name'] = '暂无等级';
  491. return $grade_arr;
  492. }
  493. $exppoints = intval($exppoints);
  494. $grade_arr = array();
  495. if ($member_grade) {
  496. foreach ($member_grade as $k => $v) {
  497. if ($exppoints >= $v['exppoints']) {
  498. $grade_arr = $v;
  499. }
  500. }
  501. }
  502. //计算提升进度
  503. if ($show_progress == true) {
  504. if (intval($grade_arr['level']) >= (count($member_grade) - 1)) { //如果已达到顶级会员
  505. $grade_arr['downgrade'] = $grade_arr['level'] - 1; //下一级会员等级
  506. $grade_arr['downgrade_name'] = isset($member_grade[$grade_arr['downgrade']]) ? $member_grade[$grade_arr['downgrade']]['level_name'] : '';
  507. $grade_arr['downgrade_exppoints'] = isset($member_grade[$grade_arr['downgrade']]) ? $member_grade[$grade_arr['downgrade']]['exppoints'] : '';
  508. $grade_arr['upgrade'] = $grade_arr['level']; //上一级会员等级
  509. $grade_arr['upgrade_name'] = isset($member_grade[$grade_arr['upgrade']]) ? $member_grade[$grade_arr['upgrade']]['level_name'] : '';
  510. $grade_arr['upgrade_exppoints'] = isset($member_grade[$grade_arr['upgrade']]) ? $member_grade[$grade_arr['upgrade']]['exppoints'] : '';
  511. $grade_arr['less_exppoints'] = 0;
  512. $grade_arr['exppoints_rate'] = 100;
  513. } else {
  514. $grade_arr['downgrade'] = $grade_arr['level']; //下一级会员等级
  515. $grade_arr['downgrade_name'] = $member_grade[$grade_arr['downgrade']]['level_name'];
  516. $grade_arr['downgrade_exppoints'] = $member_grade[$grade_arr['downgrade']]['exppoints'];
  517. $grade_arr['upgrade'] = $member_grade[$grade_arr['level'] + 1]['level']; //上一级会员等级
  518. $grade_arr['upgrade_name'] = $member_grade[$grade_arr['upgrade']]['level_name'];
  519. $grade_arr['upgrade_exppoints'] = $member_grade[$grade_arr['upgrade']]['exppoints'];
  520. $grade_arr['less_exppoints'] = $grade_arr['upgrade_exppoints'] - $exppoints;
  521. $grade_arr['exppoints_rate'] = round(($exppoints - $member_grade[$grade_arr['level']]['exppoints']) / ($grade_arr['upgrade_exppoints'] - $member_grade[$grade_arr['level']]['exppoints']) * 100, 2);
  522. }
  523. }
  524. return $grade_arr;
  525. }
  526. /**
  527. * 登录生成token
  528. * @access public
  529. * @author csdeshang
  530. * @param type $member_id 会员id
  531. * @param type $member_name 会员名字
  532. * @param type $client 客户端
  533. * @return type
  534. */
  535. public function getBuyerToken($member_id, $member_name, $client, $openid = '')
  536. {
  537. $mbusertoken_model = model('mbusertoken');
  538. //重新登录后以前的令牌失效
  539. $condition = array();
  540. $condition[] = array('member_id', '=', $member_id);
  541. $condition[] = array('member_clienttype', '=', $client);
  542. $mbusertoken_model->delMbusertoken($condition);
  543. //生成新的token
  544. $mb_user_token_info = array();
  545. $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0, 999999)));
  546. $mb_user_token_info['member_id'] = $member_id;
  547. $mb_user_token_info['member_name'] = $member_name;
  548. $mb_user_token_info['member_token'] = $token;
  549. $mb_user_token_info['member_logintime'] = TIMESTAMP;
  550. $mb_user_token_info['member_clienttype'] = $client;
  551. if (!empty($openid)) {
  552. $mb_user_token_info['member_openid'] = $openid;
  553. }
  554. $result = $mbusertoken_model->addMbusertoken($mb_user_token_info);
  555. if ($result) {
  556. return $token;
  557. } else {
  558. return null;
  559. }
  560. }
  561. }