mem_relation.php 13 KB

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