mem_relation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 == 0 && $member_id > 0) {
  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. public function subscriber() {
  180. return $this->subscriber;
  181. }
  182. public function subscriber_count() {
  183. return count($this->subscriber);
  184. }
  185. public function follower() {
  186. return $this->follower;
  187. }
  188. public function follower_count() {
  189. return count($this->follower);
  190. }
  191. public function unfollower() {
  192. return $this->unbuild_mobiles;
  193. }
  194. //关注某人
  195. public function subscribe($member_id)
  196. {
  197. if(!isset($member_id) || intval($member_id) == 0 || $this->member_id == $member_id) {
  198. return false;
  199. }
  200. try
  201. {
  202. $member_id = intval($member_id);
  203. $someone_id = $this->doSubscribe($member_id,$some_mobile);
  204. if($someone_id > 0) {
  205. $this->add_fllower($someone_id);
  206. $this->remove_unbuild($some_mobile);
  207. $this->add_build($some_mobile);
  208. } else {
  209. $this->add_unbuild($some_mobile);
  210. }
  211. $this->save();
  212. return true;
  213. }
  214. catch (Exception $ex) {
  215. Log::record($ex->getMessage(),Log::ERR);
  216. return false;
  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. public function pass_subscribe()
  237. {
  238. if(count($this->new_subscriber) > 0) {
  239. $this->dirty = true;
  240. }
  241. else {
  242. return true;
  243. }
  244. foreach ($this->new_subscriber as $someone_id)
  245. {
  246. try
  247. {
  248. $someone = new mem_relation($someone_id);
  249. $someone->add_fllower($this->member_id);
  250. if(!empty($this->member_mobile)) {
  251. $mobile = intval($this->member_mobile);
  252. $someone->remove_unbuild($mobile);
  253. $someone->add_build($mobile);
  254. }
  255. $someone->save();
  256. if(algorithm::binary_search($this->subscriber,$someone_id) == false) {
  257. $pos = algorithm::lower_bonud($this->subscriber,$someone_id);
  258. algorithm::array_insert($this->subscriber,$pos,$someone_id);
  259. $this->dirty = true;
  260. }
  261. } catch (Exception $ex) {
  262. Log::record($ex->getMessage(),Log::ERR);
  263. }
  264. }
  265. $this->new_subscriber = array();
  266. $this->save();
  267. return true;
  268. }
  269. public function is_follower($other_id)
  270. {
  271. if($this->member_id == $other_id) {
  272. return false;
  273. } else {
  274. return algorithm::binary_search($this->follower,$other_id);
  275. }
  276. }
  277. private function subscribed($mobile)
  278. {
  279. if(algorithm::binary_search($this->build_mobiles,$mobile)) {
  280. return true;
  281. }
  282. if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
  283. return true;
  284. }
  285. return false;
  286. }
  287. private function add_fllower($someone_id)
  288. {
  289. if(algorithm::binary_search($this->follower,$someone_id) == false) {
  290. $pos = algorithm::lower_bonud($this->follower,$someone_id);
  291. algorithm::array_insert($this->follower,$pos,$someone_id);
  292. $this->dirty = true;
  293. }
  294. }
  295. private function remove_fllower($someone_id)
  296. {
  297. if(algorithm::binary_search($this->follower,$someone_id)) {
  298. $pos = algorithm::lower_bonud($this->follower,$someone_id);
  299. algorithm::array_erase($this->follower,$pos);
  300. $this->dirty = true;
  301. }
  302. }
  303. private function add_unbuild($mobile)
  304. {
  305. if(algorithm::binary_search($this->unbuild_mobiles,$mobile) == false) {
  306. $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
  307. algorithm::array_insert($this->unbuild_mobiles,$pos,$mobile);
  308. $this->dirty = true;
  309. }
  310. }
  311. private function remove_unbuild($mobile)
  312. {
  313. if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
  314. $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
  315. algorithm::array_erase($this->unbuild_mobiles,$pos);
  316. $this->dirty = true;
  317. }
  318. }
  319. private function add_build($mobile)
  320. {
  321. if(algorithm::binary_search($this->build_mobiles,$mobile) == false) {
  322. $pos = algorithm::lower_bonud($this->build_mobiles,$mobile);
  323. algorithm::array_insert($this->build_mobiles,$pos,$mobile);
  324. $this->dirty = true;
  325. }
  326. }
  327. public function subscribe_contacts($contacts)
  328. {
  329. foreach ($contacts as $mobile)
  330. {
  331. $mobile = intval($mobile);
  332. if($this->member_mobile == $mobile || $mobile < 10000000000) { //自己不能订阅自己
  333. continue;
  334. }
  335. if($this->subscribed($mobile)) {
  336. continue;
  337. }
  338. $someone_id = $this->doSubscribe(0,$mobile);
  339. if($someone_id > 0) {
  340. $this->add_fllower($someone_id);
  341. $this->remove_unbuild($mobile);
  342. $this->add_build($mobile);
  343. } else {
  344. $this->add_unbuild($mobile);
  345. }
  346. }
  347. $this->save();
  348. }
  349. private function init($info)
  350. {
  351. $this->member_id = intval($info['member_id']);
  352. $this->member_mobile = $info['member_mobile'];
  353. $this->contact_name = $info['contact_name'];
  354. $this->subscriber = unserialize($info['subscriber']);
  355. $this->new_subscriber = unserialize($info['new_subscriber']);
  356. $this->follower = unserialize($info['follower']);
  357. $this->build_mobiles = unserialize($info['build_mobiles']);
  358. $this->unbuild_mobiles = unserialize($info['unbuild_mobiles']);
  359. if(empty($info['invited_user'])) {
  360. $this->invited_user = [];
  361. } else {
  362. $this->invited_user = unserialize($info['invited_user']);
  363. }
  364. }
  365. private function mobile_int($mobiles)
  366. {
  367. foreach ($mobiles as $mobile)
  368. {
  369. $val = intval($mobile);
  370. if($val > 10000000000) {
  371. $ret[] = $val;
  372. }
  373. }
  374. if(isset($ret)) {
  375. return $ret;
  376. } else {
  377. return array();
  378. }
  379. }
  380. public function sort_mobile()
  381. {
  382. $this->mobile_int($this->build_mobiles);
  383. sort($this->build_mobiles);
  384. $this->mobile_int($this->unbuild_mobiles);
  385. sort($this->unbuild_mobiles);
  386. $this->dirty = true;
  387. }
  388. private function save()
  389. {
  390. if($this->dirty) {
  391. $data = array();
  392. $data['member_mobile'] = $this->member_mobile;
  393. $data['member_id'] = $this->member_id;
  394. $data['contact_name'] = $this->contact_name;
  395. $data['subscriber'] = serialize($this->subscriber);
  396. $data['new_subscriber '] = serialize($this->new_subscriber);
  397. $data['follower'] = serialize($this->follower);
  398. $data['build_mobiles'] = serialize($this->build_mobiles);
  399. $data['unbuild_mobiles'] = serialize($this->unbuild_mobiles);
  400. $data['invited_user'] = serialize($this->invited_user);
  401. $this->mod_relation->replace($data);
  402. $this->dirty = false;
  403. }
  404. }
  405. }