account.table.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. defined('IN_IA') or exit('Access Denied');
  7. class AccountTable extends We7Table {
  8. protected $tableName = 'uni_account';
  9. protected $primaryKey = 'acid';
  10. protected $uni_verifycode = 'uni_verifycode';
  11. protected $uniSettings = 'uni_settings';
  12. protected $uniAccountUsers = 'uni_account_users';
  13. public function baseaccount() {
  14. return $this->hasOne('baseaccount', 'acid', 'default_acid');
  15. }
  16. public function menus() {
  17. return $this->hasMany('menu', 'uniacid', 'uniacid');
  18. }
  19. public function unigroup() {
  20. return $this->belongsMany('unigroup', 'id', 'uniacid', 'uni_account_group', 'groupid' ,'uniacid');
  21. }
  22. public function searchAccount($expire_type, $fields, $isdeleted = 1) {
  23. global $_W;
  24. $this->query->from('uni_account', 'a')
  25. ->select($fields)
  26. ->leftjoin('account', 'b')
  27. ->on(array('a.uniacid' => 'b.uniacid', 'a.default_acid' => 'b.acid'))
  28. ->where('b.isdeleted !=', $isdeleted)
  29. ->where('a.default_acid !=', '0');
  30. if (!user_is_founder($_W['uid'], true)) {
  31. if (empty($expire_type)) {
  32. $this->query->leftjoin('uni_account_users', 'c')->on(array('a.uniacid' => 'c.uniacid'));
  33. }
  34. if (user_is_vice_founder($_W['uid'])) {
  35. $users_uids = table('users_founder_own_users')->getFounderOwnUsersList($_W['uid']);
  36. $users_uids = array_keys($users_uids);
  37. $users_uids[] = $_W['uid'];
  38. $this->query->where('c.uid', $users_uids)->where('c.role', array('manager', 'owner', 'vice_founder'));
  39. } else {
  40. $this->query->where('c.uid', $_W['uid']);
  41. }
  42. }
  43. if (!empty($expire_type) && $expire_type == 'expire') {
  44. $this->searchWithExprie();
  45. }
  46. if (!empty($expire_type) && $expire_type == 'unexpire') {
  47. $this->searchWithUnExprie();
  48. }
  49. return $this;
  50. }
  51. public function searchWithExprie() {
  52. $this->query
  53. ->leftjoin('uni_account_users', 'c')
  54. ->on(array('a.uniacid' => 'c.uniacid'))
  55. ->leftjoin('users', 'u')
  56. ->on(array('c.uid' => 'u.uid'))
  57. ->where(function ($query) {
  58. $query->where(array('c.role' => 'owner', 'u.endtime !=' => 0, 'u.endtime <' => TIMESTAMP))
  59. ->whereor(array('b.endtime <' => TIMESTAMP, 'b.endtime !=' => 0));
  60. });
  61. return $this;
  62. }
  63. public function searchWithUnExprie() {
  64. $this->query
  65. ->leftjoin('uni_account_users', 'c')
  66. ->on(array('a.uniacid' => 'c.uniacid'))
  67. ->leftjoin('users', 'u')
  68. ->on(array('c.uid' => 'u.uid'))
  69. ->where(function ($query) {
  70. $query->where(array('c.role' => 'owner', 'u.endtime >' => TIMESTAMP, 'b.endtime >' => TIMESTAMP))
  71. ->whereor(array('c.role' => 'owner', 'u.endtime ==' => 0, 'b.endtime >' => TIMESTAMP))
  72. ->whereor(array('b.endtime' => 0))
  73. ->whereor(array('b.endtime >' => TIMESTAMP));
  74. });
  75. return $this;
  76. }
  77. public function searchAccountList($expire = false, $isdeleted = 1, $fields = 'a.uniacid') {
  78. $this->searchAccount($expire, $fields, $isdeleted);
  79. $this->query->groupby('a.uniacid');
  80. $list = $this->query->getall('uniacid');
  81. return $list;
  82. }
  83. public function searchAccountListFields($fields = 'a.uniacid', $expire = false) {
  84. $this->searchAccount($expire, $fields);
  85. $this->accountUniacidOrder();
  86. $list = $this->query->getall('uniacid');
  87. return $list;
  88. }
  89. public function searchAccounTotal($expire = false) {
  90. $this->searchAccount($expire, 'count(*) as total, b.type');
  91. $this->query->groupby('b.type');
  92. $list = $this->query->getall();
  93. return $list;
  94. }
  95. public function userOwnedAccount($uid = 0) {
  96. global $_W;
  97. $uid = intval($uid) > 0 ? intval($uid) : $_W['uid'];
  98. $is_founder = user_is_founder($uid, true);
  99. if (empty($is_founder)) {
  100. $uniacid_list = table('uni_account_users')->getUsableAccountsByUid($uid);
  101. $uniacid_list = array_keys($uniacid_list);
  102. if (empty($uniacid_list)) {
  103. return array();
  104. }
  105. $this->query->where('u.uniacid', $uniacid_list);
  106. }
  107. return $this->query->from('uni_account', 'u')->leftjoin('account', 'a')->on(array('u.default_acid' => 'a.acid'))->where('a.isdeleted', 0)->getall('uniacid');
  108. }
  109. public function accountWechatsInfo($uniacids, $uid) {
  110. return $this->query->from('uni_account', 'a')
  111. ->leftjoin('account_wechats', 'w')
  112. ->on(array('w.uniacid' => 'a.uniacid'))
  113. ->leftjoin('uni_account_users', 'au')
  114. ->on(array('a.uniacid' => 'au.uniacid'))
  115. ->where(array('a.uniacid' => $uniacids))
  116. ->where(array('au.uid' => $uid))
  117. ->orderby('a.uniacid', 'asc')
  118. ->getall('acid');
  119. }
  120. public function accountWxappInfo($uniacids, $uid) {
  121. return $this->query->from('uni_account', 'a')
  122. ->leftjoin('account_wxapp', 'w')
  123. ->on(array('w.uniacid' => 'a.uniacid'))
  124. ->leftjoin('uni_account_users', 'au')
  125. ->on(array('a.uniacid' => 'au.uniacid'))
  126. ->where(array('a.uniacid' => $uniacids))
  127. ->where(array('au.uid' => $uid))
  128. ->orderby('a.uniacid', 'asc')
  129. ->getall('acid');
  130. }
  131. public function accountWebappInfo($uniacids, $uid) {
  132. return $this->query->from('uni_account', 'a')
  133. ->leftjoin('account_webapp', 'w')
  134. ->on(array('w.uniacid' => 'a.uniacid'))
  135. ->leftjoin('uni_account_users', 'au')
  136. ->on(array('a.uniacid' => 'au.uniacid'))
  137. ->where(array('a.uniacid' => $uniacids))
  138. ->where(array('au.uid' => $uid))
  139. ->orderby('a.uniacid', 'asc')
  140. ->getall('acid');
  141. }
  142. public function accountPhoneappInfo($uniacids, $uid) {
  143. return $this->query->from('uni_account', 'a')
  144. ->leftjoin('account_phoneapp', 'w')
  145. ->on(array('w.uniacid' => 'a.uniacid'))
  146. ->leftjoin('uni_account_users', 'au')
  147. ->on(array('a.uniacid' => 'au.uniacid'))
  148. ->where(array('a.uniacid' => $uniacids))
  149. ->where(array('au.uid' => $uid))
  150. ->orderby('a.uniacid', 'asc')
  151. ->getall('acid');
  152. }
  153. public function accountXzappInfo($uniacids, $uid) {
  154. return $this->query->from('uni_account', 'a')
  155. ->leftjoin('account_xzapp', 'w')
  156. ->on(array('w.uniacid' => 'a.uniacid'))
  157. ->leftjoin('uni_account_users', 'au')
  158. ->on(array('a.uniacid' => 'au.uniacid'))
  159. ->where(array('a.uniacid' => $uniacids))
  160. ->where(array('au.uid' => $uid))
  161. ->orderby('a.uniacid', 'asc')
  162. ->getall('acid');
  163. }
  164. public function accountAliappInfo($uniacids, $uid) {
  165. return $this->query->from('uni_account', 'a')
  166. ->leftjoin('account_aliapp', 'w')
  167. ->on(array('w.uniacid' => 'a.uniacid'))
  168. ->leftjoin('uni_account_users', 'au')
  169. ->on(array('a.uniacid' => 'au.uniacid'))
  170. ->where(array('a.uniacid' => $uniacids))
  171. ->where(array('au.uid' => $uid))
  172. ->orderby('a.uniacid', 'asc')
  173. ->getall('acid');
  174. }
  175. public function accountToutiaoappInfo($uniacids, $uid) {
  176. return $this->query->from('uni_account', 'a')
  177. ->leftjoin('account_toutiaoapp', 'w')
  178. ->on(array('w.uniacid' => 'a.uniacid'))
  179. ->leftjoin('uni_account_users', 'au')
  180. ->on(array('a.uniacid' => 'au.uniacid'))
  181. ->where(array('a.uniacid' => $uniacids))
  182. ->where(array('au.uid' => $uid))
  183. ->orderby('a.uniacid', 'asc')
  184. ->getall('acid');
  185. }
  186. public function accountBaiduappInfo($uniacids, $uid) {
  187. return $this->query->from('uni_account', 'a')
  188. ->leftjoin('account_baiduapp', 'w')
  189. ->on(array('w.uniacid' => 'a.uniacid'))
  190. ->leftjoin('uni_account_users', 'au')
  191. ->on(array('a.uniacid' => 'au.uniacid'))
  192. ->where(array('a.uniacid' => $uniacids))
  193. ->where(array('au.uid' => $uid))
  194. ->orderby('a.uniacid', 'asc')
  195. ->getall('acid');
  196. }
  197. public function searchWithKeyword($title) {
  198. if (empty($title)) {
  199. return $this;
  200. }
  201. $this->query->where('a.name LIKE', "%{$title}%");
  202. return $this;
  203. }
  204. public function searchWithTitle($title) {
  205. $this->query->where('a.name', $title);
  206. return $this;
  207. }
  208. public function searchWithType($types = array()) {
  209. $this->query->where(array('b.type' => $types));
  210. return $this;
  211. }
  212. public function searchWithLetter($letter) {
  213. if (!empty($letter) && strlen($letter) == 1) {
  214. $this->query->where('a.title_initial', $letter);
  215. }
  216. return $this;
  217. }
  218. public function accountRankOrder() {
  219. global $_W;
  220. if (!user_is_founder($_W['uid'], true)) {
  221. $this->query->orderby('c.rank', 'desc');
  222. } else {
  223. $this->query->orderby('a.rank', 'desc');
  224. }
  225. return $this;
  226. }
  227. public function accountUniacidOrder($order = 'desc') {
  228. $order = !empty($order) ? $order : 'desc';
  229. $this->query->orderby('a.uniacid', $order);
  230. return $this;
  231. }
  232. public function searchWithNoconnect() {
  233. $this->query->where('b.isconnect =', '0');
  234. return $this;
  235. }
  236. public function searchWithViceFounder($vice_founder_id) {
  237. $this->query
  238. ->leftjoin('uni_account_users', 'c')
  239. ->on(array('a.uniacid' => 'c.uniacid'))
  240. ->where('c.role', 'vice_founder')
  241. ->where('c.uid', $vice_founder_id);
  242. return $this;
  243. }
  244. public function getWechatappAccount($acid) {
  245. return $this->query->from('account_wechats')->where('acid', $acid)->get();
  246. }
  247. public function getUniAccountByAcid($acid) {
  248. $account = $this->query->from('account')->where('acid', $acid)->get();
  249. $uniaccount = array();
  250. if (!empty($account)) {
  251. $uniaccount = $this->query->from('uni_account')->where('uniacid', $account['uniacid'])->get();
  252. }
  253. if (empty($account)) {
  254. return array();
  255. } else {
  256. return array_merge($account, $uniaccount);
  257. }
  258. }
  259. public function getUniAccountByUniacid($uniacid) {
  260. $account = $this->getAccountByUniacid($uniacid);
  261. $uniaccount = array();
  262. if (!empty($account)) {
  263. $uniaccount = $this->query->from('uni_account')->where('uniacid', $account['uniacid'])->get();
  264. }
  265. if (empty($account)) {
  266. return array();
  267. } else {
  268. return !empty($uniaccount) && is_array($uniaccount) ? array_merge($account, $uniaccount) : $account;
  269. }
  270. }
  271. public function accountGroupModules($uniacid, $type = '') {
  272. $packageids = $this->query->from('uni_account_group')->where('uniacid', $uniacid)->select('groupid')->getall('groupid');
  273. $packageids = empty($packageids) ? array() : array_keys($packageids);
  274. if (in_array('-1', $packageids)) {
  275. $modules = $this->query->from('modules')->select('name')->getall('name');
  276. return array_keys($modules);
  277. }
  278. $uni_modules = array();
  279. $site_store_buy_package = table('store')->searchUserBuyPackage($uniacid);
  280. $packageids = array_merge($packageids, array_keys($site_store_buy_package));
  281. $uni_groups = $this->query->from('uni_group')->where('uniacid', $uniacid)->whereor('id', $packageids)->getall('modules');
  282. if (!empty($uni_groups)) {
  283. if (empty($type)) {
  284. $account = $this->getAccountByUniacid($uniacid);
  285. $type = $account['type'];
  286. }
  287. foreach ($uni_groups as $group) {
  288. $group_module = (array)iunserializer($group['modules']);
  289. if (empty($group_module)) {
  290. continue;
  291. }
  292. switch ($type) {
  293. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  294. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  295. $uni_modules = is_array($group_module['modules']) ? array_merge($group_module['modules'], $uni_modules) : $uni_modules;
  296. break;
  297. case ACCOUNT_TYPE_APP_NORMAL:
  298. case ACCOUNT_TYPE_APP_AUTH:
  299. case ACCOUNT_TYPE_WXAPP_WORK:
  300. $uni_modules = is_array($group_module['wxapp']) ? array_merge($group_module['wxapp'], $uni_modules) : $uni_modules;
  301. break;
  302. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  303. $uni_modules = is_array($group_module['webapp']) ? array_merge($group_module['webapp'], $uni_modules) : $uni_modules;
  304. break;
  305. case ACCOUNT_TYPE_XZAPP_NORMAL:
  306. case ACCOUNT_TYPE_XZAPP_AUTH:
  307. $uni_modules = is_array($group_module['xzapp']) ? array_merge($group_module['xzapp'], $uni_modules) : $uni_modules;
  308. break;
  309. case ACCOUNT_TYPE_PHONEAPP_NORMAL:
  310. $uni_modules = is_array($group_module['phoneapp']) ? array_merge($group_module['phoneapp'], $uni_modules) : $uni_modules;
  311. break;
  312. case ACCOUNT_TYPE_ALIAPP_NORMAL:
  313. $uni_modules = is_array($group_module['aliapp']) ? array_merge($group_module['aliapp'], $uni_modules) : $uni_modules;
  314. break;
  315. }
  316. }
  317. $uni_modules = array_unique($uni_modules);
  318. }
  319. return $uni_modules;
  320. }
  321. public function getAccountByUniacid($uniacid) {
  322. return $this->query->from('account')->where('uniacid', $uniacid)->get();
  323. }
  324. public function getAccountExtraPermission($uniacid) {
  325. if (empty($uniacid)) {
  326. return array();
  327. }
  328. $result = $this->query->from('uni_group')->where('uniacid', $uniacid)->get();
  329. if (!empty($result)) {
  330. $result['templates'] = iunserializer($result['templates']);
  331. $group_module = (array)iunserializer($result['modules']);
  332. if (empty($group_module)) {
  333. $result['modules'] = array();
  334. } else {
  335. $account = $this->getAccountByUniacid($uniacid);
  336. switch ($account['type']) {
  337. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  338. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  339. $result['modules'] = $group_module['modules'];
  340. break;
  341. case ACCOUNT_TYPE_APP_NORMAL:
  342. case ACCOUNT_TYPE_APP_AUTH:
  343. case ACCOUNT_TYPE_WXAPP_WORK:
  344. $result['modules'] = $group_module['wxapp'];
  345. break;
  346. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  347. $result['modules'] = $group_module['webapp'];
  348. break;
  349. case ACCOUNT_TYPE_XZAPP_NORMAL:
  350. case ACCOUNT_TYPE_XZAPP_AUTH:
  351. $result['modules'] = $group_module['xzapp'];
  352. break;
  353. case ACCOUNT_TYPE_PHONEAPP_NORMAL:
  354. $result['modules'] = $group_module['phoneapp'];
  355. break;
  356. default:
  357. $result['modules'] = array();
  358. }
  359. }
  360. } else {
  361. $result = array();
  362. }
  363. return $result;
  364. }
  365. public function getUniVerifycode($params) {
  366. global $_W;
  367. $this->query->from($this->uni_verifycode);
  368. if (!empty($params['uniacid'])) {
  369. $this->query->where('uniacid', $params['uniacid']);
  370. }
  371. if (!empty($params['receiver'])) {
  372. $this->query->where('receiver', $params['receiver']);
  373. }
  374. if (!empty($params['createtime >'])) {
  375. $this->query->where('createtime >', $params['createtime >']);
  376. }
  377. if (!empty($params['verifycode'])) {
  378. $this->query->where('verifycode', $params['verifycode']);
  379. }
  380. return $this->query->get();
  381. }
  382. public function getUniSetting() {
  383. return $this->query->from($this->uniSettings)->get();
  384. }
  385. public function getUniAccountList() {
  386. return $this->query->select('uniacid')->from($this->tableName)->getall();
  387. }
  388. public function getOwnerUid() {
  389. return $this->query->from($this->uniAccountUsers)->getcolumn('uid');
  390. }
  391. public function getOwnedAccountCount($uid) {
  392. return $this->query->from($this->uniAccountUsers, 'u')->select('d.type, count(*) as count')->leftjoin($this->tableName, 'a')
  393. ->on(array('u.uniacid' => 'a.uniacid'))->leftjoin('account', 'd')->on(array('a.default_acid' => 'd.acid'))
  394. ->where('u.uid', $uid)->where('u.role', 'owner')->where('d.isdeleted', 0)->groupby('d.type')->getall();
  395. }
  396. public function getAccountNameByUniacid($uniacid) {
  397. return $this->query
  398. ->from('uni_account', 'u')
  399. ->select('u.uniacid', 'u.name', 'a.type')
  400. ->leftjoin('account', 'a')
  401. ->on(array('u.uniacid' => 'a.uniacid'))
  402. ->where('u.uniacid', $uniacid)->get();
  403. }
  404. }