Account.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. namespace We7\Table\Account;
  7. class Account extends \We7Table {
  8. protected $tableName = 'account';
  9. protected $primaryKey = 'acid';
  10. protected $field = array(
  11. 'uniacid',
  12. 'hash',
  13. 'type',
  14. 'isconnect',
  15. 'isdeleted',
  16. 'endtime',
  17. 'send_account_expire_status',
  18. 'send_api_expire_status'
  19. );
  20. protected $default = array(
  21. 'uniacid' => '0',
  22. 'hash' => '',
  23. 'type' => '1',
  24. 'isconnect' => '',
  25. 'isdeleted' => '0',
  26. 'endtime' => '0',
  27. 'send_account_expire_status' => 0,
  28. 'send_api_expire_status' => 0
  29. );
  30. public function getByUniacid($uniacid) {
  31. return $this->where('uniacid', $uniacid)->get();
  32. }
  33. public function getUniAccountByAcid($acid) {
  34. return $this->query
  35. ->from($this->tableName, 'a')
  36. ->leftjoin('uni_account', 'u')
  37. ->on('a.uniacid', 'u.uniacid')
  38. ->where('a.acid', intval($acid))
  39. ->get();
  40. }
  41. public function getUniAccountByUniacid($uniacid) {
  42. return $this->query
  43. ->from($this->tableName, 'a')
  44. ->leftjoin('uni_account', 'u')
  45. ->on('a.uniacid', 'u.uniacid')
  46. ->where('a.uniacid', intval($uniacid))
  47. ->get();
  48. }
  49. public function getUserAccountInfo($uid, $uniacid, $account_type) {
  50. $type_info = uni_account_type($account_type);
  51. return $this->query->from('uni_account', 'a')
  52. ->leftjoin($type_info['table_name'], 'w')
  53. ->on(array('w.uniacid' => 'a.uniacid'))
  54. ->leftjoin('uni_account_users', 'au')
  55. ->on(array('a.uniacid' => 'au.uniacid'))
  56. ->where(array('a.uniacid' => $uniacid))
  57. ->where(array('au.uid' => $uid))
  58. ->orderby('a.uniacid', 'asc')
  59. ->getall('uniacid');
  60. }
  61. public function searchAccount($expire_type, $fields, $isdeleted = 1, $uid = 0) {
  62. global $_W;
  63. $uid = empty($uid) ? $_W['uid'] : $uid;
  64. $valid_account_type = array(
  65. ACCOUNT_TYPE_OFFCIAL_NORMAL,
  66. ACCOUNT_TYPE_OFFCIAL_AUTH,
  67. ACCOUNT_TYPE_APP_NORMAL,
  68. ACCOUNT_TYPE_WEBAPP_NORMAL,
  69. ACCOUNT_TYPE_PHONEAPP_NORMAL,
  70. ACCOUNT_TYPE_APP_AUTH,
  71. ACCOUNT_TYPE_ALIAPP_NORMAL,
  72. ACCOUNT_TYPE_BAIDUAPP_NORMAL,
  73. ACCOUNT_TYPE_TOUTIAOAPP_NORMAL
  74. );
  75. $this->query->from('uni_account', 'a')
  76. ->select($fields)
  77. ->leftjoin('account', 'b')
  78. ->on(array('a.uniacid' => 'b.uniacid', 'a.default_acid' => 'b.acid'))
  79. ->where('b.isdeleted !=', $isdeleted)
  80. ->where('a.default_acid !=', '0')
  81. ->where('b.type IN ', $valid_account_type)
  82. ->leftjoin('uni_account_users', 'c')
  83. ->on(array('a.uniacid' => 'c.uniacid'))
  84. ->leftjoin('users', 'u')
  85. ->on(array('u.uid' => 'c.uid'));
  86. if (!user_is_founder($uid, true)) {
  87. if (user_is_vice_founder($uid)) {
  88. $users_uids = table('users_founder_own_users')->getFounderOwnUsersList($uid);
  89. $users_uids = array_keys($users_uids);
  90. $users_uids[] = $uid;
  91. $this->query->where('c.uid', $users_uids)->where('c.role', array('clerk', 'operator', 'manager', 'owner', 'vice_founder'));
  92. } else {
  93. $this->query->where('c.uid', $uid)->where('c.role <>', 'clerk');
  94. }
  95. }
  96. if ($expire_type == 'expire') {
  97. $this->searchWithExprie();
  98. } elseif ($expire_type == 'unexpire') {
  99. $this->searchWithUnExprie();
  100. }
  101. return $this;
  102. }
  103. public function searchWithRole($role) {
  104. global $_W;
  105. return $this->query->where('c.role', $role)->where('c.uid', $_W['uid']);
  106. }
  107. public function searchWithExprie() {
  108. $this->query->where(array(
  109. 'b.endtime <' => TIMESTAMP,
  110. 'b.endtime >' => USER_ENDTIME_GROUP_UNLIMIT_TYPE
  111. ));
  112. return $this;
  113. }
  114. public function searchWithUnExprie() {
  115. $this->query->where(function ($query) {
  116. $query->where(array('b.endtime' => 0))
  117. ->whereor(array('b.endtime' => USER_ENDTIME_GROUP_UNLIMIT_TYPE))
  118. ->whereor(array('b.endtime >' => TIMESTAMP));
  119. });
  120. return $this;
  121. }
  122. public function searchAccountList($expire = false, $isdeleted = 1, $fields = 'a.uniacid', $uid = 0) {
  123. $this->searchAccount($expire, $fields, $isdeleted, $uid);
  124. $this->query->groupby('a.uniacid');
  125. $list = $this->query->getall('uniacid');
  126. return $list;
  127. }
  128. public function searchAccounTotal($expire = false) {
  129. $this->searchAccount($expire, 'count(*) as total, b.type');
  130. $this->query->groupby('b.type');
  131. $list = $this->query->getall();
  132. return $list;
  133. }
  134. public function searchWithKeyword($title) {
  135. global $_W;
  136. if (empty($title)) {
  137. return $this;
  138. }
  139. if ($title == 'admin' && $_W['isadmin']) {
  140. $this->query->where('ISNULL(c.uid)', true);
  141. } else {
  142. if ($_W['isfounder']) {
  143. $user = table('uni_account_users')
  144. ->searchWithUsers()
  145. ->where('a.role', 'owner')
  146. ->where('b.username', $title);
  147. if (user_is_vice_founder($_W['uid'])) {
  148. $uids = table('users_founder_own_users')->where('founder_uid', $_W['uid'])->getall('uid');
  149. if (!empty($uid)) {
  150. $user->where('a.uid', array_keys($uids));
  151. }
  152. }
  153. $user = $user->get();
  154. }
  155. !empty($user) && user_is_founder($user['uid'], true) ? $user = array() : $user;
  156. !empty($user) ? $this->query->where('CONCAT(a.name,u.username) LIKE', "%{$title}%")->where('c.role', 'owner') : $this->query->where('a.name LIKE', "%{$title}%");
  157. }
  158. return $this;
  159. }
  160. public function searchWithTitle($title) {
  161. $this->query->where('a.name', $title);
  162. return $this;
  163. }
  164. public function searchWithType($types = array()) {
  165. $this->query->where(array('b.type' => $types));
  166. return $this;
  167. }
  168. public function searchWithLetter($letter) {
  169. if (!empty($letter) && strlen($letter) == 1) {
  170. $this->query->where('a.title_initial', $letter);
  171. }
  172. return $this;
  173. }
  174. public function accountRankOrder() {
  175. global $_W;
  176. if (!$_W['isadmin']) {
  177. $this->query->orderby('c.rank', 'desc');
  178. } else {
  179. $this->query->orderby('a.rank', 'desc');
  180. }
  181. return $this;
  182. }
  183. public function accountUniacidOrder($order = 'desc') {
  184. $order = !empty($order) ? $order : 'desc';
  185. $this->query->orderby('a.uniacid', $order);
  186. return $this;
  187. }
  188. public function accountEndtimeOrder($order) {
  189. $order = $order == 'endtime_asc' ? 'asc' : 'desc';
  190. return $this->query->orderby('b.endtime', $order)
  191. ->where('b.endtime >', 2);
  192. }
  193. public function accountInitialsOrder($order = 'asc') {
  194. $order = !empty($order) ? $order : 'asc';
  195. $this->query->orderby('a.title_initial', $order);
  196. return $this;
  197. }
  198. public function searchWithViceFounder($vice_founder_id) {
  199. $this->query
  200. ->leftjoin('uni_account_users', 'c')
  201. ->on(array('a.uniacid' => 'c.uniacid'))
  202. ->where('c.role', 'vice_founder')
  203. ->where('c.uid', $vice_founder_id);
  204. return $this;
  205. }
  206. public function accountGroupModules($uniacid, $type = '') {
  207. $packageids = $this->query->from('uni_account_group')->where('uniacid', $uniacid)->select('groupid')->getall('groupid');
  208. $packageids = empty($packageids) ? array() : array_keys($packageids);
  209. if (in_array('-1', $packageids)) {
  210. $modules = $this->query->from('modules')->select('name')->getall('name');
  211. return array_keys($modules);
  212. }
  213. $uni_modules = array();
  214. $uni_groups = $this->query->from('uni_group')->where('id', $packageids)->getall();
  215. $uni_account_extra_modules = table('uni_account_extra_modules')->where('uniacid', $uniacid)->getall();
  216. $acount_modules = array_merge($uni_groups, $uni_account_extra_modules);
  217. if (!empty($acount_modules)) {
  218. if (empty($type)) {
  219. $account = table('account')->getByUniacid($uniacid);
  220. $type = $account['type'];
  221. }
  222. foreach ($acount_modules as $group) {
  223. $group_module = (array)iunserializer($group['modules']);
  224. if (empty($group_module)) {
  225. continue;
  226. }
  227. switch ($type) {
  228. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  229. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  230. $uni_modules = is_array($group_module['modules']) ? array_merge($group_module['modules'], $uni_modules) : $uni_modules;
  231. break;
  232. case ACCOUNT_TYPE_APP_NORMAL:
  233. case ACCOUNT_TYPE_APP_AUTH:
  234. case ACCOUNT_TYPE_WXAPP_WORK:
  235. $uni_modules = is_array($group_module['wxapp']) ? array_merge($group_module['wxapp'], $uni_modules) : $uni_modules;
  236. break;
  237. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  238. $uni_modules = is_array($group_module['webapp']) ? array_merge($group_module['webapp'], $uni_modules) : $uni_modules;
  239. break;
  240. case ACCOUNT_TYPE_PHONEAPP_NORMAL:
  241. $uni_modules = is_array($group_module['phoneapp']) ? array_merge($group_module['phoneapp'], $uni_modules) : $uni_modules;
  242. break;
  243. case ACCOUNT_TYPE_ALIAPP_NORMAL:
  244. $uni_modules = is_array($group_module['aliapp']) ? array_merge($group_module['aliapp'], $uni_modules) : $uni_modules;
  245. break;
  246. }
  247. }
  248. $uni_modules = array_unique($uni_modules);
  249. }
  250. return $uni_modules;
  251. }
  252. public function userOwnedAccount($uid = 0) {
  253. global $_W;
  254. $uid = intval($uid) > 0 ? intval($uid) : $_W['uid'];
  255. if (!user_is_founder($uid, true)) {
  256. $uniacid_list = table('uni_account_users')->getUsableAccountsByUid($uid);
  257. if (empty($uniacid_list)) {
  258. return array();
  259. }
  260. $this->query->where('u.uniacid', array_keys($uniacid_list));
  261. }
  262. return $this->query->from('uni_account', 'u')
  263. ->leftjoin('account', 'a')
  264. ->on(array('u.default_acid' => 'a.acid'))
  265. ->where('a.isdeleted', 0)
  266. ->orderby('a.uniacid', 'DESC')
  267. ->getall('uniacid');
  268. }
  269. public function searchWithuniAccountUsers() {
  270. return $this->query->from('account', 'a')
  271. ->leftjoin('uni_account_users', 'b')
  272. ->on(array('b.uniacid' => 'a.uniacid'));
  273. }
  274. public function searchWithUniAccount() {
  275. return $this->query->from($this->tableName, 'a')
  276. ->leftjoin('uni_account', 'b')
  277. ->on('b.uniacid', 'a.uniacid');
  278. }
  279. public function searchWithUsersOperateStar($uid) {
  280. $this->query
  281. ->leftjoin('users_operate_star', 's')
  282. ->on(array('a.uniacid' => 's.uniacid', 's.type' => 1, 's.uid' => $uid));
  283. return $this;
  284. }
  285. public function accountStarOrder($order = 'desc') {
  286. $order = !empty($order) ? $order : 'desc';
  287. $this->query->orderby('s.rank', 'desc');
  288. return $this;
  289. }
  290. }