mem_relation.php 14 KB

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