mem_relation.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/23
  6. * Time: 下午10:28
  7. */
  8. namespace relation;
  9. use \Exception;
  10. use \session_helper;
  11. use \algorithm;
  12. use \errcode;
  13. use \member_info;
  14. use \Log;
  15. //member_mobile VARCHAR(11) PRIMARY KEY NOT NULL COMMENT '用户手机号',
  16. //member_id INT(11) DEFAULT '0' COMMENT '已经注册用户id',
  17. //contact_name VARCHAR(50) DEFAULT '',
  18. //subscriber TEXT COMMENT '订阅我为好友的人。',
  19. //follower TEXT COMMENT '我订阅别人成为好友通过的人。',
  20. //build_mobiles TEXT COMMENT '已经与我建立好友关系的手机号',
  21. //unbuild_mobiles TEXT COMMENT '还未与我建立好友关系的手机号'
  22. class mem_relation
  23. {
  24. const use_frineds_relation = true;
  25. //对应数据库表中字段
  26. private $member_id;
  27. private $member_mobile;
  28. private $contact_name;
  29. private $subscriber; //关注我的
  30. private $new_subscriber;
  31. private $follower; //我关注的
  32. private $build_mobiles;
  33. private $unbuild_mobiles;
  34. private $invited_user;
  35. private $mod_relation;
  36. private $dirty;
  37. public function __construct($member_id,$mobile = null)
  38. {
  39. $this->dirty = false;
  40. if(is_array($member_id)) {
  41. $info = $member_id; //直接传信息过来
  42. $this->init($info);
  43. return;
  44. }
  45. $this->mod_relation = Model('member_relation');
  46. $member_id = intval($member_id);
  47. if($member_id > 0)
  48. {
  49. $member_info = new member_info($member_id);
  50. if(!empty($member_info))
  51. {
  52. $mobile = $member_info->mobile();
  53. if(!empty($mobile)) {
  54. $info = $this->mod_relation->findByMobile($mobile);
  55. }
  56. if(!empty($info))
  57. {
  58. $this->init($info);
  59. if($this->member_id != $member_id) {
  60. $this->member_id = $member_info->member_id();
  61. $this->dirty = true;
  62. }
  63. if($this->contact_name != $member_info->nickname()) {
  64. $this->contact_name = $member_info->nickname();
  65. $this->dirty = true;
  66. }
  67. return;
  68. }
  69. else
  70. {
  71. if(!empty($member_info)) {
  72. $this->member_id = $member_info->member_id();
  73. $this->member_mobile = $member_info->mobile();
  74. $this->contact_name = $member_info->nickname();
  75. $this->subscriber = array();
  76. $this->new_subscriber = array();
  77. $this->follower = array();
  78. $this->build_mobiles = array();
  79. $this->unbuild_mobiles = array();
  80. $this->invited_user = array();
  81. $this->dirty = true;
  82. return;
  83. }
  84. }
  85. }
  86. }
  87. else
  88. {
  89. if(isset($mobile) && !empty($mobile))
  90. {
  91. $mobile = session_helper::mobile_valid($mobile);
  92. if($mobile == false) {
  93. throw new Exception("member_relation __construct error",errcode::ErrParamter);
  94. }
  95. $mobile = intval($mobile);
  96. $info = $this->mod_relation->findByMobile($mobile);
  97. if(!empty($info)) {
  98. $this->init($info);
  99. return;
  100. }
  101. else
  102. {
  103. $this->member_mobile = $mobile;
  104. $this->member_id = $member_id;
  105. $this->contact_name = '';
  106. $this->subscriber = array();
  107. $this->new_subscriber = array();
  108. $this->follower = array();
  109. $this->build_mobiles = array();
  110. $this->unbuild_mobiles = array();
  111. $this->invited_user = array();
  112. $this->dirty = true;
  113. }
  114. }
  115. else
  116. {
  117. throw new Exception("member_relation __construct error",errcode::ErrParamter);
  118. }
  119. }
  120. }
  121. public function __destruct()
  122. {
  123. if($this->dirty) {
  124. $this->save();
  125. }
  126. }
  127. public function invite($user_id)
  128. {
  129. if(algorithm::binary_search($this->invited_user,$user_id)) {
  130. return true;
  131. }
  132. if(algorithm::binary_search($this->invited_user,$user_id) == false) {
  133. $pos = algorithm::lower_bonud($this->invited_user,$user_id);
  134. algorithm::array_insert($this->invited_user,$pos,$user_id);
  135. $this->dirty = true;
  136. }
  137. return true;
  138. }
  139. //用户($member_id) 关注 我($this)
  140. private function subscribe_me($member_id)
  141. {
  142. if(algorithm::binary_search($this->subscriber,$member_id)) {
  143. return;
  144. }
  145. if(algorithm::binary_search($this->new_subscriber,$member_id) == false) {
  146. $pos = algorithm::lower_bonud($this->new_subscriber,$member_id);
  147. algorithm::array_insert($this->new_subscriber,$pos,$member_id);
  148. $this->dirty = true;
  149. }
  150. }
  151. //用户($member_id) 取消关注 我($this)
  152. private function unsubscribe_me($member_id)
  153. {
  154. if(algorithm::binary_search($this->subscriber,$member_id)) {
  155. $pos = algorithm::lower_bonud($this->subscriber,$member_id);
  156. algorithm::array_erase($this->subscriber,$pos);
  157. $this->dirty = true;
  158. return;
  159. }
  160. if(algorithm::binary_search($this->new_subscriber,$member_id)) {
  161. $pos = algorithm::lower_bonud($this->new_subscriber,$member_id);
  162. algorithm::array_erase($this->new_subscriber,$pos);
  163. $this->dirty = true;
  164. }
  165. }
  166. private function doUnSubscribe($member_id)
  167. {
  168. $someone = new mem_relation($member_id, null);
  169. $someone->unsubscribe_me($this->member_id);
  170. $someone->save();
  171. return $someone->member_id;
  172. }
  173. private function doSubscribe($member_id,&$mobile)
  174. {
  175. $someone = new mem_relation($member_id, $mobile);
  176. $someone->subscribe_me($this->member_id);
  177. $someone->save();
  178. $mobile = intval($someone->member_mobile);
  179. return $someone->member_id;
  180. }
  181. public function member_id()
  182. {
  183. return $this->member_id;
  184. }
  185. //关注我的
  186. public function subscriber() {
  187. return $this->subscriber;
  188. }
  189. public function subscriber_count() {
  190. return count($this->subscriber);
  191. }
  192. //我关注的
  193. public function follower() {
  194. return $this->follower;
  195. }
  196. public function follower_count() {
  197. return count($this->follower);
  198. }
  199. //"我"关注某人
  200. public function subscribe($user)
  201. {
  202. if(!isset($user) || intval($user) == 0 || $this->member_id == $user) {
  203. return false;
  204. }
  205. try
  206. {
  207. $user = intval($user);
  208. $someone_id = $this->doSubscribe($user,$some_mobile);
  209. if($someone_id > 0)
  210. {
  211. if(!self::use_frineds_relation) {
  212. $this->add_fllower($someone_id);
  213. $this->remove_unbuild($some_mobile);
  214. $this->add_build($some_mobile);
  215. }
  216. }
  217. else {
  218. $this->add_unbuild($some_mobile);
  219. }
  220. $this->save();
  221. return true;
  222. }
  223. catch (Exception $ex) {
  224. Log::record($ex->getMessage(),Log::ERR);
  225. return false;
  226. }
  227. }
  228. //"我"取消关注某人
  229. public function unsubscribe($member_id)
  230. {
  231. if(!isset($member_id) || intval($member_id) == 0 || $this->member_id == $member_id) {
  232. return false;
  233. }
  234. try
  235. {
  236. $member_id = intval($member_id);
  237. $this->doUnSubscribe($member_id);
  238. $this->remove_fllower($member_id);
  239. $this->save();
  240. return true;
  241. } catch (Exception $ex) {
  242. Log::record($ex->getMessage(),Log::ERR);
  243. return false;
  244. }
  245. }
  246. //我通过所有关注
  247. public function pass_subscribe()
  248. {
  249. if(count($this->new_subscriber) > 0) {
  250. $this->dirty = true;
  251. }
  252. else {
  253. return true;
  254. }
  255. foreach ($this->new_subscriber as $someone_id) {
  256. $this->pass_onesub($someone_id);
  257. }
  258. $this->new_subscriber = array();
  259. $this->save();
  260. return true;
  261. }
  262. public function pass_onesub($someone_id)
  263. {
  264. try
  265. {
  266. $someone = new mem_relation($someone_id);
  267. $someone->add_fllower($this->member_id);
  268. if(!empty($this->member_mobile)) {
  269. $mobile = intval($this->member_mobile);
  270. $someone->remove_unbuild($mobile);
  271. $someone->add_build($mobile);
  272. }
  273. $someone->save();
  274. if(algorithm::binary_search($this->subscriber,$someone_id) == false) {
  275. $pos = algorithm::lower_bonud($this->subscriber,$someone_id);
  276. algorithm::array_insert($this->subscriber,$pos,$someone_id);
  277. $this->dirty = true;
  278. }
  279. return true;
  280. }
  281. catch (Exception $ex) {
  282. Log::record($ex->getMessage(),Log::ERR);
  283. return false;
  284. }
  285. }
  286. //是否是我的粉丝
  287. public function is_follower($other_id)
  288. {
  289. if($this->member_id == $other_id) {
  290. return false;
  291. } else {
  292. return algorithm::binary_search($this->follower,$other_id);
  293. }
  294. }
  295. public function is_friends($other_id)
  296. {
  297. if($this->member_id == $other_id) {
  298. return true;
  299. }
  300. return (algorithm::binary_search($this->follower,$other_id)) || (algorithm::binary_search($this->subscriber,$other_id));
  301. }
  302. private function subscribed($mobile)
  303. {
  304. if(algorithm::binary_search($this->build_mobiles,$mobile)) {
  305. return true;
  306. }
  307. if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
  308. return true;
  309. }
  310. return false;
  311. }
  312. private function add_fllower($someone_id)
  313. {
  314. if(algorithm::binary_search($this->follower,$someone_id) == false) {
  315. $pos = algorithm::lower_bonud($this->follower,$someone_id);
  316. algorithm::array_insert($this->follower,$pos,$someone_id);
  317. $this->dirty = true;
  318. }
  319. }
  320. private function remove_fllower($someone_id)
  321. {
  322. if(algorithm::binary_search($this->follower,$someone_id)) {
  323. $pos = algorithm::lower_bonud($this->follower,$someone_id);
  324. algorithm::array_erase($this->follower,$pos);
  325. $this->dirty = true;
  326. }
  327. }
  328. private function add_unbuild($mobile)
  329. {
  330. if(algorithm::binary_search($this->unbuild_mobiles,$mobile) == false) {
  331. $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
  332. algorithm::array_insert($this->unbuild_mobiles,$pos,$mobile);
  333. $this->dirty = true;
  334. }
  335. }
  336. private function remove_unbuild($mobile)
  337. {
  338. if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
  339. $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
  340. algorithm::array_erase($this->unbuild_mobiles,$pos);
  341. $this->dirty = true;
  342. }
  343. }
  344. private function add_build($mobile)
  345. {
  346. if(algorithm::binary_search($this->build_mobiles,$mobile) == false) {
  347. $pos = algorithm::lower_bonud($this->build_mobiles,$mobile);
  348. algorithm::array_insert($this->build_mobiles,$pos,$mobile);
  349. $this->dirty = true;
  350. }
  351. }
  352. //我关注我通讯录中所有人
  353. public function subscribe_contacts($contacts)
  354. {
  355. foreach ($contacts as $mobile)
  356. {
  357. $mobile = intval($mobile);
  358. if($this->member_mobile == $mobile || $mobile < 10000000000) { //自己不能订阅自己
  359. continue;
  360. }
  361. if($this->subscribed($mobile)) {
  362. continue;
  363. }
  364. $someone_id = $this->doSubscribe(0,$mobile);
  365. if($someone_id > 0) {
  366. $this->add_fllower($someone_id);
  367. $this->remove_unbuild($mobile);
  368. $this->add_build($mobile);
  369. } else {
  370. $this->add_unbuild($mobile);
  371. }
  372. }
  373. $this->save();
  374. }
  375. private function init($info)
  376. {
  377. $this->member_id = intval($info['member_id']);
  378. $this->member_mobile = $info['member_mobile'];
  379. $this->contact_name = $info['contact_name'];
  380. $this->subscriber = unserialize($info['subscriber']);
  381. $this->new_subscriber = unserialize($info['new_subscriber']);
  382. $this->follower = unserialize($info['follower']);
  383. $this->build_mobiles = unserialize($info['build_mobiles']);
  384. $this->unbuild_mobiles = unserialize($info['unbuild_mobiles']);
  385. if(empty($info['invited_user'])) {
  386. $this->invited_user = [];
  387. } else {
  388. $this->invited_user = unserialize($info['invited_user']);
  389. }
  390. }
  391. private function mobile_int($mobiles)
  392. {
  393. foreach ($mobiles as $mobile)
  394. {
  395. $val = intval($mobile);
  396. if($val > 10000000000) {
  397. $ret[] = $val;
  398. }
  399. }
  400. if(isset($ret)) {
  401. return $ret;
  402. } else {
  403. return array();
  404. }
  405. }
  406. public function sort_mobile()
  407. {
  408. $this->mobile_int($this->build_mobiles);
  409. sort($this->build_mobiles);
  410. $this->mobile_int($this->unbuild_mobiles);
  411. sort($this->unbuild_mobiles);
  412. $this->dirty = true;
  413. }
  414. private function save()
  415. {
  416. if($this->dirty && !empty($this->member_mobile)) {
  417. $data = array();
  418. $data['member_mobile'] = $this->member_mobile;
  419. $data['member_id'] = $this->member_id;
  420. $data['contact_name'] = $this->contact_name;
  421. $data['subscriber'] = serialize($this->subscriber);
  422. $data['new_subscriber '] = serialize($this->new_subscriber);
  423. $data['follower'] = serialize($this->follower);
  424. $data['build_mobiles'] = serialize($this->build_mobiles);
  425. $data['unbuild_mobiles'] = serialize($this->unbuild_mobiles);
  426. $data['invited_user'] = serialize($this->invited_user);
  427. $this->mod_relation->replace($data);
  428. }
  429. $this->dirty = false;
  430. }
  431. }