Applet.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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['where_id'] = $uid;
  63. Dever::load('passport/user-update', $update);
  64. $state = Dever::config('base', 'project')->regSendSms;
  65. if ($state) {
  66. Dever::setInput('skin', $state);
  67. $this->send($mobile, $uid);
  68. }
  69. } else {
  70. Dever::alert('无效的用户id,请重新登录');
  71. }
  72. }
  73. $result['uid'] = $uid;
  74. $result['signature'] = Dever::login($uid);
  75. return $result;
  76. }
  77. /*
  78. public function test_sms()
  79. {
  80. $uid = Dever::input('uid');
  81. $mobile = Dever::input('mobile');
  82. $state = Dever::config('base', 'project')->regSendSms;
  83. if ($state) {
  84. Dever::setInput('skin', $state);
  85. $this->send($mobile, $uid);
  86. }
  87. }
  88. */
  89. /**
  90. * 生成用户,返回uid
  91. *
  92. * @return int
  93. */
  94. private function create($data)
  95. {
  96. $uid = false;
  97. $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
  98. if (!$info) {
  99. if (isset($data['unionid']) && $data['unionid']) {
  100. $info = Dever::load('passport/wechat-one', array('unionid' => $data['unionid']));
  101. if (!$info) {
  102. $uid = false;
  103. } else {
  104. $uid = $info['uid'];
  105. }
  106. $wechat['unionid'] = $data['unionid'];
  107. }
  108. if (!$uid) {
  109. $user['username'] = '临时用户';
  110. $uid = Dever::load('passport/user-insert', $user);
  111. }
  112. $wechat['openid'] = $data['openid'];
  113. $wechat['session_key'] = $data['session_key'];
  114. $wechat['uid'] = $uid;
  115. $id = Dever::load('passport/wechat-insert', $wechat);
  116. } else {
  117. $uid = $info['uid'];
  118. if (isset($data['unionid']) && $data['unionid']) {
  119. $wechat['unionid'] = $data['unionid'];
  120. }
  121. $wechat['session_key'] = $data['session_key'];
  122. $wechat['where_id'] = $info['id'];
  123. $id = $info['id'];
  124. Dever::load('passport/wechat-update', $wechat);
  125. }
  126. $user = Dever::load('passport/user-one', $uid);
  127. $result['vid'] = $id;
  128. $result['uid'] = $uid;
  129. $result['signature'] = Dever::login($uid);
  130. if ($user['mobile']) {
  131. $result['mobile'] = $user['mobile'];
  132. }
  133. return $result;
  134. }
  135. /**
  136. * 更新用户信息 手机号
  137. *
  138. * @return mixed
  139. */
  140. public function mobile()
  141. {
  142. $uid = $this->check();
  143. $vid = Dever::input('vid');
  144. $result = array();
  145. $mobile = $phoneNumber = '';
  146. $iv = Dever::input('iv');
  147. $encryptedData = Dever::input('encryptedData');
  148. if ($iv && $encryptedData) {
  149. $vinfo = Dever::load('passport/wechat-one', $vid);
  150. $data = $this->decryptData($vinfo['session_key']);
  151. if ($data && isset($data->purePhoneNumber) && isset($data->phoneNumber)) {
  152. $mobile = $data->purePhoneNumber;
  153. $phoneNumber = $data->phoneNumber;
  154. }
  155. }
  156. if ($mobile && $phoneNumber && $uid) {
  157. $info = Dever::load('passport/user-one', $uid);
  158. $result['mobile'] = $mobile;
  159. if ($info) {
  160. $update['set_mobile'] = $mobile;
  161. $update['where_id'] = $uid;
  162. Dever::load('passport/user-update', $update);
  163. $state = Dever::config('base', 'project')->regSendSms;
  164. if ($state) {
  165. Dever::setInput('skin', $state);
  166. $this->send($mobile, $uid);
  167. }
  168. } else {
  169. Dever::alert('无效的用户id,请重新登录');
  170. }
  171. }
  172. $result['vid'] = $vid;
  173. $result['uid'] = $uid;
  174. $result['signature'] = Dever::login($uid);
  175. return $result;
  176. }
  177. /**
  178. * 更新用户信息
  179. *
  180. * @return mixed
  181. */
  182. public function update()
  183. {
  184. $uid = $this->check();
  185. $name = Dever::emoji(Dever::input('nickname'));
  186. $pic = Dever::input('avatarurl');
  187. $sex = Dever::input('gender');
  188. $city = Dever::input('city');
  189. $mobile = Dever::input('mobile');
  190. $province = Dever::input('province');
  191. $country = Dever::input('country');
  192. $vid = Dever::input('vid');
  193. $update['set_temp'] = 3;
  194. if ($vid) {
  195. $vinfo = Dever::load('passport/wechat-one', $vid);
  196. $unionid = $this->unionid($vinfo['session_key']);
  197. if ($unionid) {
  198. $vinfo = Dever::load('passport/wechat-one', array('option_unionid' => $unionid));
  199. if (!$vinfo) {
  200. $wechat['set_unionid'] = $unionid;
  201. $wechat['where_id'] = $vid;
  202. Dever::load('passport/wechat-update', $wechat);
  203. } elseif ($vinfo && $vinfo['id'] != $vid) {
  204. $wechat['set_unionid'] = $unionid;
  205. $wechat['set_uid'] = $vinfo['uid'];
  206. $wechat['where_id'] = $vid;
  207. # 删除user信息
  208. if ($uid != $vinfo['uid']) {
  209. Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $uid));
  210. }
  211. $uid = $vinfo['uid'];
  212. Dever::load('passport/wechat-update', $wechat);
  213. }
  214. $update['set_temp'] = 2;
  215. }
  216. }
  217. $info = Dever::load('passport/user-one', $uid);
  218. if ($info) {
  219. if ($name) {
  220. $update['set_username'] = $name;
  221. }
  222. if ($sex || $sex == 0) {
  223. $update['set_sex'] = $this->saveSex($sex);
  224. }
  225. if ($pic) {
  226. //$update['set_avatar'] = $this->saveAvatar($pic);
  227. $update['set_avatar'] = $pic;
  228. }
  229. $update['set_city'] = $city;
  230. //$update['set_mobile'] = $mobile;
  231. $update['set_province'] = $province;
  232. $update['set_country'] = $country;
  233. $update['where_id'] = $uid;
  234. if ($country && $province && $city) {
  235. $update['set_area'] = $country . ',' . $province . ',' . $city;
  236. }
  237. if (isset($update) && $update) {
  238. Dever::load('passport/user-update', $update);
  239. }
  240. } else {
  241. Dever::alert('无效的用户id,请重新登录');
  242. }
  243. $result['vid'] = $vid;
  244. $result['uid'] = $uid;
  245. $result['signature'] = Dever::login($uid);
  246. return $result;
  247. }
  248. private function unionid($session_key)
  249. {
  250. $data = $this->decryptData($session_key);
  251. if ($data && isset($data->unionId)) {
  252. return $data->unionId;
  253. }
  254. return false;
  255. }
  256. private function decryptData($session_key)
  257. {
  258. $iv = Dever::input('iv');
  259. $encryptedData = Dever::input('encryptedData');
  260. if (!$iv || !$encryptedData) {
  261. return false;
  262. }
  263. if (strlen($session_key) != 24) {
  264. return false;
  265. }
  266. if (strlen($iv) != 24) {
  267. return false;
  268. }
  269. $aesKey = base64_decode($session_key);
  270. $aesIV = base64_decode($iv);
  271. $aesCipher = base64_decode($encryptedData);
  272. $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  273. $dataObj = json_decode($result);
  274. if ($dataObj == NULL) {
  275. return false;
  276. }
  277. $applet = Dever::config('base', 'project')->applet;
  278. $appid = $applet['appid'];
  279. if($dataObj->watermark->appid != $appid) {
  280. return false;
  281. }
  282. return $dataObj;
  283. }
  284. }