Applet.php 10 KB

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