mem_relation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. } catch (Exception $ex) {
  257. Log::record($ex->getMessage(),Log::ERR);
  258. }
  259. }
  260. $this->new_subscriber = array();
  261. $this->save();
  262. return true;
  263. }
  264. public function is_follower($other_id)
  265. {
  266. if($this->member_id == $other_id) {
  267. return false;
  268. } else {
  269. return algorithm::binary_search($this->follower,$other_id);
  270. }
  271. }
  272. private function subscribed($mobile)
  273. {
  274. if(algorithm::binary_search($this->build_mobiles,$mobile)) {
  275. return true;
  276. }
  277. if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
  278. return true;
  279. }
  280. return false;
  281. }
  282. private function add_fllower($someone_id)
  283. {
  284. if(algorithm::binary_search($this->follower,$someone_id) == false) {
  285. $pos = algorithm::lower_bonud($this->follower,$someone_id);
  286. algorithm::array_insert($this->follower,$pos,$someone_id);
  287. $this->dirty = true;
  288. }
  289. }
  290. private function remove_fllower($someone_id)
  291. {
  292. if(algorithm::binary_search($this->follower,$someone_id)) {
  293. $pos = algorithm::lower_bonud($this->follower,$someone_id);
  294. algorithm::array_erase($this->follower,$pos);
  295. $this->dirty = true;
  296. }
  297. }
  298. private function add_unbuild($mobile)
  299. {
  300. if(algorithm::binary_search($this->unbuild_mobiles,$mobile) == false) {
  301. $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
  302. algorithm::array_insert($this->unbuild_mobiles,$pos,$mobile);
  303. $this->dirty = true;
  304. }
  305. }
  306. private function remove_unbuild($mobile)
  307. {
  308. if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
  309. $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
  310. algorithm::array_erase($this->unbuild_mobiles,$pos);
  311. $this->dirty = true;
  312. }
  313. }
  314. private function add_build($mobile)
  315. {
  316. if(algorithm::binary_search($this->build_mobiles,$mobile) == false) {
  317. $pos = algorithm::lower_bonud($this->build_mobiles,$mobile);
  318. algorithm::array_insert($this->build_mobiles,$pos,$mobile);
  319. $this->dirty = true;
  320. }
  321. }
  322. public function subscribe_contacts($contacts)
  323. {
  324. foreach ($contacts as $mobile)
  325. {
  326. $mobile = intval($mobile);
  327. if($this->member_mobile == $mobile || $mobile < 10000000000) { //自己不能订阅自己
  328. continue;
  329. }
  330. if($this->subscribed($mobile)) {
  331. continue;
  332. }
  333. $someone_id = $this->doSubscribe(0,$mobile);
  334. if($someone_id > 0) {
  335. $this->add_fllower($someone_id);
  336. $this->remove_unbuild($mobile);
  337. $this->add_build($mobile);
  338. } else {
  339. $this->add_unbuild($mobile);
  340. }
  341. }
  342. $this->save();
  343. }
  344. private function init($info)
  345. {
  346. $this->member_id = intval($info['member_id']);
  347. $this->member_mobile = $info['member_mobile'];
  348. $this->contact_name = $info['contact_name'];
  349. $this->subscriber = unserialize($info['subscriber']);
  350. $this->new_subscriber = unserialize($info['new_subscriber']);
  351. $this->follower = unserialize($info['follower']);
  352. $this->build_mobiles = unserialize($info['build_mobiles']);
  353. $this->unbuild_mobiles = unserialize($info['unbuild_mobiles']);
  354. if(empty($info['invited_user'])) {
  355. $this->invited_user = [];
  356. } else {
  357. $this->invited_user = unserialize($info['invited_user']);
  358. }
  359. }
  360. private function mobile_int($mobiles)
  361. {
  362. foreach ($mobiles as $mobile)
  363. {
  364. $val = intval($mobile);
  365. if($val > 10000000000) {
  366. $ret[] = $val;
  367. }
  368. }
  369. if(isset($ret)) {
  370. return $ret;
  371. } else {
  372. return array();
  373. }
  374. }
  375. public function sort_mobile()
  376. {
  377. $this->mobile_int($this->build_mobiles);
  378. sort($this->build_mobiles);
  379. $this->mobile_int($this->unbuild_mobiles);
  380. sort($this->unbuild_mobiles);
  381. $this->dirty = true;
  382. }
  383. private function save()
  384. {
  385. if($this->dirty) {
  386. $data = array();
  387. $data['member_mobile'] = $this->member_mobile;
  388. $data['member_id'] = $this->member_id;
  389. $data['contact_name'] = $this->contact_name;
  390. $data['subscriber'] = serialize($this->subscriber);
  391. $data['new_subscriber '] = serialize($this->new_subscriber);
  392. $data['follower'] = serialize($this->follower);
  393. $data['build_mobiles'] = serialize($this->build_mobiles);
  394. $data['unbuild_mobiles'] = serialize($this->unbuild_mobiles);
  395. $data['invited_user'] = serialize($this->invited_user);
  396. $this->mod_relation->replace($data);
  397. $this->dirty = false;
  398. }
  399. }
  400. }