Member.php 23 KB

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