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