Main.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php namespace Pinterface\Api;
  2. use Dever;
  3. use Pinterface\Lib\Core;
  4. class Main extends Core
  5. {
  6. # 首页
  7. public function home()
  8. {
  9. /*
  10. $data['e'] = false;
  11. if ($this->place->info['type'] == 1) {
  12. # 公开
  13. $data['e'] = true;
  14. } elseif ($this->place->info['type'] == 2 && $this->place->user) {
  15. # 注册
  16. $data['e'] = true;
  17. } elseif ($this->place->info['type'] > 2 && $this->place->user && $this->place->user['entry_type'] > 2) {
  18. # 邀请码、购买、密码等等
  19. $data['e'] = true;
  20. } else {
  21. # 需要注册、购买、输入密码等等
  22. if ($this->place->info['type'] == 2) {
  23. $data['b'] = [
  24. 'name' => '注册',
  25. 'type' => 2,
  26. ];
  27. } elseif ($this->place->info['type'] == 3) {
  28. $data['b'] = [
  29. 'name' => '邀请码',
  30. 'type' => 3,
  31. ];
  32. } elseif ($this->place->info['type'] == 4) {
  33. $data['b'] = [
  34. 'name' => '密码',
  35. 'type' => 4,
  36. ];
  37. } elseif ($this->place->info['type'] == 5) {
  38. $data['b'] = [
  39. 'name' => '购买',
  40. 'type' => 5,
  41. ];
  42. }
  43. }
  44. */
  45. $data['n'] = $this->place->info['name'];
  46. $data['i'] = $this->place->info['info'];
  47. $data['l'] = $this->place->info['logo'];
  48. $data['u'] = $this->place->info['update'];
  49. $data['m'] = $this->place->nav();
  50. return $data;
  51. }
  52. # 获取品牌介绍
  53. public function getContent()
  54. {
  55. $data['content'] = htmlspecialchars_decode($this->place->info['content']);
  56. return $data;
  57. }
  58. # 获取个人中心首页需要的信息
  59. public function getUserHome()
  60. {
  61. # 获取所有身份
  62. $data['role'] = Dever::db('prole/info')->select(['status' => 1], ['col' => 'id,name,info']);
  63. $data['role_info'] = [];
  64. if ($data['role']) {
  65. foreach ($data['role'] as &$v) {
  66. if ($v['info'] && !$data['role_info']) {
  67. $v['subname'] = $v['info'];
  68. $v['button'] = '立即开通';
  69. $data['role_info'] = $v;
  70. }
  71. $v['level_name'] = '成为' . $v['name'];
  72. }
  73. }
  74. # 获取所有积分
  75. $data['score'] = Dever::db('pscore/info')->select(['status' => 1], ['col' => 'id,name,0 as num']);
  76. return $data;
  77. }
  78. # 验证码登录或者注册
  79. public function login_commit(){}
  80. public function login()
  81. {
  82. $mobile = Dever::input('mobile', 'is_numeric', '手机号');
  83. $pwd = Dever::input('pwd');
  84. if (!$pwd) {
  85. $code = Dever::input('code', 'is_numeric', '验证码');
  86. Dever::load(\Msg\Lib\Template::class)->check('reg_code', $mobile, $code);
  87. }
  88. $invite = Dever::input('invite');
  89. $db = Dever::db('puser/info');
  90. $user = $db->find(['mobile' => $mobile]);
  91. if ($user) {
  92. $action = '登录';
  93. if ($pwd && Dever::load(\Manage\Lib\Util::class)->hash($pwd, $user['salt']) != $user['password']) {
  94. Dever::error('登录失败,密码无效');
  95. }
  96. # 已注册,登录
  97. $uid = $user['id'];
  98. # 记录日志
  99. Dever::load(\Pscore\Lib\Log::class)->action('登录')->add($uid);
  100. # 升级初始身份等级
  101. if ($user['type'] == 1) {
  102. Dever::load(\Prole\Lib\Info::class)->up($uid, $user['sales_type'], $user['sales_id']);
  103. }
  104. } elseif (!$pwd) {
  105. $action = '注册';
  106. if ($invite) {
  107. $parent_uid = Dever::load(\Invite\Lib\Code::class)->getUid($invite);
  108. if (!$parent_uid) {
  109. Dever::error('邀请码不存在');
  110. }
  111. } else {
  112. # 如果没有上级,从访问码里找:
  113. $parent_uid = $this->place->info['parent_uid'];
  114. /* 这里暂时不做
  115. if (!$parent_uid && $this->place->info['sales_type'] && $this->place->info['sales_id']) {
  116. # 如果还没有上级,找销售渠道,销售渠道就是其的上级
  117. $sales = Dever::load(\Psales\Lib\Info::class)->get($this->place->info['sales_type'] && $this->place->info['sales_id']);
  118. if ($sales['uid']) {
  119. $parent_uid = $sales['uid'];
  120. }
  121. }*/
  122. }
  123. $uid = Dever::load(\Puser\Lib\Info::class)->createUser($mobile, $this->place->info['sales_type'], $this->place->info['sales_id'], $parent_uid);
  124. } else {
  125. Dever::error('账户不存在,请切换到验证码登录');
  126. }
  127. return $this->result($uid);
  128. }
  129. # 静默注册
  130. public function reg_commit(){}
  131. public function reg()
  132. {
  133. $value = Dever::input('uuid');
  134. if (!$value) {
  135. return [];
  136. }
  137. $platform = Dever::input('platform', 'is_string', '平台信息', 'weixin');
  138. $env = Dever::input('env', 'is_numeric', '环境信息', 3);
  139. if ($env == 5) {
  140. $value = false;
  141. # 小程序下获取openid
  142. $account = Dever::db('api/account')->find(['key' => $platform]);
  143. if ($account) {
  144. $param['js_code'] = Dever::input('applet_code', 'is_string', '登录信息');
  145. $applet = Dever::load(\Api\Lib\Account::class)->run($account, 'applet_login', $param, $env);
  146. if (isset($applet['openid'])) {
  147. $value = $applet['openid'];
  148. }
  149. }
  150. }
  151. if (!$value) {
  152. return [];
  153. }
  154. $data = ['env' => $env, 'platform' => $platform, 'value' => $value];
  155. $uuid = Dever::db('puser/uuid')->find($data);
  156. if ($uuid) {
  157. return $this->result($uuid['uid'], false);
  158. }
  159. $id = Dever::db('puser/uuid')->insert($data);
  160. if ($id) {
  161. # 生成新用户
  162. $parent_uid = $this->place->info['parent_uid'];
  163. $mobile = Dever::load(\Puser\Lib\Info::class)->createMobile();
  164. $uid = Dever::load(\Puser\Lib\Info::class)->createUser($mobile, $this->place->info['sales_type'], $this->place->info['sales_id'], $parent_uid, '', '', 2);
  165. if ($uid) {
  166. Dever::db('puser/uuid')->update($id, ['uid' => $uid]);
  167. if ($env == 5) {
  168. $update['uid'] = $uid;
  169. $update['account_id'] = $account['id'];
  170. $update['env'] = $env;
  171. $update['openid'] = $value;
  172. Dever::db('api/openid')->insert($update);
  173. }
  174. }
  175. return $this->result($uid, false);
  176. }
  177. }
  178. # env = 2 => 'h5',3 => 'jsapi',4 => 'app',5 => '小程序',6 => 'pc',
  179. public function result($uid, $openid = true)
  180. {
  181. $result['t'] = \Dever\Helper\Secure::login($uid);
  182. if ($openid) {
  183. # 获取openid
  184. $platform = Dever::input('platform', 'is_string', '平台信息', 'weixin');
  185. $env = Dever::input('env', 'is_numeric', '环境信息', 3);
  186. $result = Dever::load(\Place\Lib\Account::class)->openid(['key' => $platform], $env, $uid, $result);
  187. if (isset($result['openid'])) {
  188. unset($result['openid']);
  189. }
  190. }
  191. return $result;
  192. }
  193. # 重置密码
  194. public function reset()
  195. {
  196. # 大于8位字符,至少包含:小写字母、大写字母、数字、特殊字符
  197. //$pwd = Dever::input('pwd', '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/', '密码');
  198. $mobile = Dever::input('mobile', 'is_numeric', '手机号');
  199. $pwd = Dever::input('pwd', 'is_string', '密码');
  200. $code = Dever::input('code', 'is_string', '验证码');
  201. Dever::load(\Msg\Lib\Template::class)->check('reg_code', $mobile, $code);
  202. $update = Dever::load(\Manage\Lib\Util::class)->createPwd($pwd);
  203. Dever::db('puser/info')->update(['mobile' => $mobile], $update);
  204. }
  205. # 发送短信验证码
  206. public function sms()
  207. {
  208. $mobile = Dever::input('mobile', 'is_numeric', '手机号');
  209. $data = Dever::load(\Msg\Lib\Template::class)->send('reg_code', ['mobile' => $mobile], [], 'place');
  210. return $data;
  211. }
  212. # 获取单页信息
  213. public function page()
  214. {
  215. $name = Dever::input('name', 'is_string', '单页名称');
  216. $info = Dever::db('sector/page')->find(['name' => $name]);
  217. if ($info) {
  218. $info['content'] = htmlspecialchars_decode($info['content']);
  219. }
  220. return $info;
  221. }
  222. # 获取分享信息
  223. public function share()
  224. {
  225. $sales_type = Dever::input('sales_type');
  226. $sales_id = Dever::input('sales_id');
  227. $type = Dever::input('type');
  228. if (!$type) {
  229. $type = 0;
  230. }
  231. if (!$sales_type || !$sales_id) {
  232. $sales_type = $this->place->info['sales_type'];
  233. $sales_id = $this->place->info['sales_id'];
  234. }
  235. if (!$this->place->uid) {
  236. $this->place->uid = 0;
  237. }
  238. $result = Dever::load(\Place\Lib\Info::class)->share($this->place->info['id'], $sales_type, $sales_id, $this->place->uid, false, $type, false);
  239. $share['path'] = $result['path'];
  240. if ($type > 0) {
  241. # 资源信息
  242. $source = Dever::db('source/info')->find(['id' => $type]);
  243. if (!$source['share_pic'] && $source['pic']) {
  244. $source['pic'] = explode(',', $source['pic']);
  245. $share['pic'] = $source['pic'][0];
  246. } else {
  247. $share['pic'] = $source['share_pic'];
  248. }
  249. $share['name'] = $source['share_name'] ?: $source['name'];
  250. $share['desc'] = $source['share_desc'] ?: $source['info'];
  251. } else {
  252. $share['name'] = $this->place->info['share_name'] ?: $this->place->info['name'];
  253. $share['pic'] = $this->place->info['share_pic'] ?: $this->place->info['logo'];
  254. $share['desc'] = $this->place->info['share_desc'] ?: $this->place->info['info'];
  255. }
  256. $data['share'] = $share;
  257. return $data;
  258. }
  259. }