Applet.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace Passport\Src;
  3. use Dever;
  4. use Passport\Src\Lib\Base;
  5. # 小程序
  6. class Applet extends Base
  7. {
  8. public function init()
  9. {
  10. $uid = $this->check();
  11. $user = Dever::load('passport/user-one', $uid);
  12. return $user;
  13. }
  14. /**
  15. * 用户绑定 生成用户信息
  16. *
  17. * @return mixed
  18. */
  19. public function bind()
  20. {
  21. $token = Dever::project('token');
  22. $applet = array();
  23. if ($token) {
  24. $applet = Dever::db('token/project')->one(Dever::input('appid', 1));
  25. }
  26. if (!$applet) {
  27. $applet = Dever::config('base', 'project')->applet;
  28. }
  29. $appid = $applet['appid'];
  30. $secret = $applet['secret'];
  31. $url = $applet['url'];
  32. $code = Dever::input('code');
  33. $url .= '?appid=' . $appid;
  34. $url .= '&secret=' . $secret;
  35. $url .= '&js_code=' . $code;
  36. $url .= '&grant_type=authorization_code';
  37. $data = Dever::curl($url);
  38. if (strstr($data, 'errcode')) {
  39. Dever::alert($data);
  40. }
  41. //YzJkOThpRFhwZ1lQTF9mZl9hLVZjZnFXemJVenlYcDQ3d3JWekk0b1I4NjBBQ0Naejg4a0VQa0U=
  42. //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}';
  43. $data = Dever::json_decode($data);
  44. $user = $this->create($data);
  45. return $user;
  46. }
  47. /**
  48. * 更新用户信息 绑定用户手机号
  49. *
  50. * @return mixed
  51. */
  52. public function bind_mobile()
  53. {
  54. $uid = $this->check();
  55. //$code = Dever::input('mcode');
  56. $mobile = Dever::load('passport/reg')->checkMobileExists();
  57. if ($mobile && $uid) {
  58. $info = Dever::load('passport/user-one', $uid);
  59. $result['mobile'] = $mobile;
  60. if ($info) {
  61. $update['set_mobile'] = $mobile;
  62. $update['set_bind'] = 1;
  63. $update['where_id'] = $uid;
  64. Dever::load('passport/user-update', $update);
  65. $state = Dever::config('base', 'project')->regSendSms;
  66. if ($state) {
  67. Dever::setInput('skin', $state);
  68. $this->send($mobile, $uid);
  69. }
  70. } else {
  71. Dever::alert('无效的用户id,请重新登录');
  72. }
  73. }
  74. if (!$info['mobile']) {
  75. Dever::score($uid, 'bind_mobile', '绑定手机号');
  76. }
  77. $result['uid'] = $uid;
  78. $result['signature'] = Dever::login($uid);
  79. return $result;
  80. }
  81. /*
  82. public function test_sms()
  83. {
  84. $uid = Dever::input('uid');
  85. $mobile = Dever::input('mobile');
  86. $state = Dever::config('base', 'project')->regSendSms;
  87. if ($state) {
  88. Dever::setInput('skin', $state);
  89. $this->send($mobile, $uid);
  90. }
  91. }
  92. */
  93. /**
  94. * 生成用户,返回uid
  95. *
  96. * @return int
  97. */
  98. private function create($data)
  99. {
  100. $uid = false;
  101. $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
  102. if (!$info) {
  103. if (isset($data['unionid']) && $data['unionid']) {
  104. $info = Dever::load('passport/wechat-one', array('unionid' => $data['unionid']));
  105. if (!$info) {
  106. $uid = false;
  107. } else {
  108. $uid = $info['uid'];
  109. }
  110. $wechat['unionid'] = $data['unionid'];
  111. }
  112. if (!$uid) {
  113. $user['bind'] = 2;
  114. $user['temp'] = 1;
  115. $user['username'] = '临时用户';
  116. $user['source_type'] = 'applet';
  117. $uid = Dever::load('passport/user-insert', $user);
  118. }
  119. $wechat['openid'] = $data['openid'];
  120. $wechat['session_key'] = $data['session_key'];
  121. $wechat['uid'] = $uid;
  122. $wechat['type'] = 1;
  123. $id = Dever::load('passport/wechat-insert', $wechat);
  124. } else {
  125. $uid = $info['uid'];
  126. if (isset($data['unionid']) && $data['unionid']) {
  127. $wechat['unionid'] = $data['unionid'];
  128. }
  129. $wechat['session_key'] = $data['session_key'];
  130. $wechat['where_id'] = $info['id'];
  131. $id = $info['id'];
  132. Dever::load('passport/wechat-update', $wechat);
  133. }
  134. $user = Dever::load('passport/user-one', $uid);
  135. $result['vid'] = $id;
  136. $result['uid'] = $uid;
  137. $result['signature'] = Dever::login($uid);
  138. if ($user['mobile']) {
  139. $result['mobile'] = $user['mobile'];
  140. }
  141. return $result;
  142. }
  143. /**
  144. * 更新用户信息 手机号
  145. *
  146. * @return mixed
  147. */
  148. public function mobile()
  149. {
  150. $uid = $this->check();
  151. $vid = Dever::input('vid');
  152. $result = array();
  153. $mobile = $phoneNumber = '';
  154. $iv = Dever::input('iv');
  155. $encryptedData = Dever::input('encryptedData');
  156. if ($iv && $encryptedData) {
  157. $vinfo = Dever::load('passport/wechat-one', $vid);
  158. $data = $this->decryptData($vinfo['session_key']);
  159. if ($data && isset($data->purePhoneNumber) && isset($data->phoneNumber)) {
  160. $mobile = $data->purePhoneNumber;
  161. $phoneNumber = $data->phoneNumber;
  162. }
  163. }
  164. if ($mobile && $phoneNumber && $uid) {
  165. $info = Dever::load('passport/user-one', $uid);
  166. $result['mobile'] = $mobile;
  167. if ($info) {
  168. $update['set_mobile'] = $mobile;
  169. $update['set_bind'] = 1;
  170. $update['where_id'] = $uid;
  171. Dever::load('passport/user-update', $update);
  172. $state = Dever::config('base', 'project')->regSendSms;
  173. if ($state) {
  174. Dever::setInput('skin', $state);
  175. $this->send($mobile, $uid);
  176. }
  177. } else {
  178. Dever::alert('无效的用户id,请重新登录');
  179. }
  180. }
  181. if (!$info['mobile']) {
  182. Dever::score($uid, 'bind_mobile', '绑定手机号');
  183. }
  184. $result['vid'] = $vid;
  185. $result['uid'] = $uid;
  186. $result['signature'] = Dever::login($uid);
  187. return $result;
  188. }
  189. /**
  190. * 更新用户信息
  191. *
  192. * @return mixed
  193. */
  194. public function update()
  195. {
  196. $uid = $this->check();
  197. $name = Dever::emoji(Dever::input('nickname'));
  198. $pic = Dever::input('avatarurl');
  199. $sex = Dever::input('gender');
  200. $city = Dever::input('city');
  201. $mobile = Dever::input('mobile');
  202. $province = Dever::input('province');
  203. $country = Dever::input('country');
  204. $vid = Dever::input('vid');
  205. $update['temp'] = 3;
  206. if ($vid) {
  207. $vinfo = Dever::load('passport/wechat-one', $vid);
  208. $unionid = $this->unionid($vinfo['session_key']);
  209. if ($unionid) {
  210. $vinfo = Dever::load('passport/wechat-one', array('option_unionid' => $unionid));
  211. if (!$vinfo) {
  212. $wechat['set_unionid'] = $unionid;
  213. $wechat['where_id'] = $vid;
  214. Dever::load('passport/wechat-update', $wechat);
  215. } elseif ($vinfo && $vinfo['id'] != $vid) {
  216. $wechat['set_unionid'] = $unionid;
  217. $wechat['set_uid'] = $vinfo['uid'];
  218. $wechat['where_id'] = $vid;
  219. # 删除user信息
  220. if ($uid != $vinfo['uid']) {
  221. Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $uid));
  222. }
  223. $uid = $vinfo['uid'];
  224. Dever::load('passport/wechat-update', $wechat);
  225. }
  226. $update['temp'] = 2;
  227. }
  228. }
  229. $info = Dever::load('passport/user-one', $uid);
  230. if ($info) {
  231. if ($info['temp'] == 1) {
  232. Dever::score($uid, 'bind_wechat', '用户微信授权');
  233. }
  234. $update['temp'] = 2;
  235. if ($name) {
  236. $update['set_username'] = $name;
  237. }
  238. if ($sex == 1) {
  239. $update['set_sex'] = 1;
  240. } elseif ($sex == 2) {
  241. $update['set_sex'] = 2;
  242. } else {
  243. $update['set_sex'] = 3;
  244. }
  245. /*
  246. if ($sex || $sex == 0) {
  247. $update['set_sex'] = $this->saveSex($sex);
  248. }
  249. */
  250. if ($pic) {
  251. //$update['set_avatar'] = $this->saveAvatar($pic);
  252. $update['set_avatar'] = $pic;
  253. }
  254. if ($city) {
  255. $update['set_city'] = $city;
  256. }
  257. //$update['set_mobile'] = $mobile;
  258. if ($province) {
  259. $update['set_province'] = $province;
  260. }
  261. if ($country) {
  262. $update['set_country'] = $country;
  263. }
  264. if ($country && $province && $city) {
  265. $update['set_area'] = $country . ',' . $province . ',' . $city;
  266. }
  267. if (isset($update) && $update) {
  268. $update['where_id'] = $uid;
  269. Dever::load('passport/user-update', $update);
  270. }
  271. } else {
  272. Dever::alert('无效的用户id,请重新登录');
  273. }
  274. $result['vid'] = $vid;
  275. $result['uid'] = $uid;
  276. $result['signature'] = Dever::login($uid);
  277. return $result;
  278. }
  279. private function unionid($session_key)
  280. {
  281. $data = $this->decryptData($session_key);
  282. if ($data && isset($data->unionId)) {
  283. return $data->unionId;
  284. }
  285. return false;
  286. }
  287. private function decryptData($session_key)
  288. {
  289. $iv = Dever::input('iv');
  290. $encryptedData = Dever::input('encryptedData');
  291. if (!$iv || !$encryptedData) {
  292. return false;
  293. }
  294. if (strlen($session_key) != 24) {
  295. return false;
  296. }
  297. if (strlen($iv) != 24) {
  298. return false;
  299. }
  300. $aesKey = base64_decode($session_key);
  301. $aesIV = base64_decode($iv);
  302. $aesCipher = base64_decode($encryptedData);
  303. $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  304. $dataObj = json_decode($result);
  305. if ($dataObj == NULL) {
  306. return false;
  307. }
  308. $applet = Dever::config('base', 'project')->applet;
  309. $appid = $applet['appid'];
  310. if($dataObj->watermark->appid != $appid) {
  311. return false;
  312. }
  313. return $dataObj;
  314. }
  315. }