Member.php 23 KB

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