User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use addons\service\library\WxPay;
  7. use app\api\model\service\UserInfo;
  8. use app\api\model\service\Order;
  9. use app\api\model\service\ProjectConfig;
  10. use app\api\model\service\PackageOrderDetail;
  11. use fast\Http;
  12. use think\Db;
  13. use fast\Random;
  14. use think\Config;
  15. use think\Cache;
  16. use think\Validate;
  17. class User extends Api
  18. {
  19. protected $noNeedLogin = ['login', 'logout', 'mobilelogin', 'register', 'resetpwd', 'getNearAddress','changeemail', 'changemobile', 'third', 'phone', 'perfect','cs','userLogin','getLocation','getarea'];
  20. protected $noNeedRight = ['*'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. if (!Config::get('fastadmin.usercenter')) {
  25. $this->error(__('User center already closed'));
  26. }
  27. }
  28. public function getNearAddress()
  29. {
  30. if (!$this->request->isPost()) {
  31. $this->error('请求方式异常');
  32. }
  33. $name = input('name','');
  34. $city = input('city','');
  35. (!$name || !$city) && $this->error('参数缺失');
  36. $re = \addons\service\library\Common::getAreaList(['city'=>$city,'name'=>$name]);
  37. $re['status'] == 0 && $this->error('请求失败');
  38. $this->success('信息返回',$re['pois']);
  39. }
  40. public function getArea()
  41. {
  42. if (!$this->request->isPost()) {
  43. $this->error('请求方式异常');
  44. }
  45. $data['openCity'] = \app\api\model\service\CityConfig::field('id,city')->order('weigh desc')->select();
  46. $data['areaList'] = db('area')->where(['level'=>2])->field('id,name,first,lng,lat,pinyin')->select();
  47. $this->success('信息返回成功',$data);
  48. }
  49. /**
  50. * 根据经纬度获取位置
  51. * @return void
  52. */
  53. public function getLocation()
  54. {
  55. $lng = input('lng','');
  56. $lat = input('lat','');
  57. (!$lng || !$lat) && $this->error('定位异常');
  58. $re = \addons\service\library\Map::getLocation($lng,$lat);
  59. $this->success('地址信息返回成功',$re);
  60. }
  61. /**
  62. * 会员登录
  63. *
  64. * @ApiMethod (POST)
  65. * @param string $account 账号
  66. * @param string $password 密码
  67. */
  68. public function login()
  69. {
  70. $account = $this->request->post('account');
  71. $password = $this->request->post('password');
  72. if (!$account || !$password) {
  73. $this->error(__('Invalid parameters'));
  74. }
  75. $ret = $this->auth->login($account, $password);
  76. if ($ret) {
  77. $data = ['userinfo' => $this->auth->getUserinfo()];
  78. $this->success(__('Logged in successful'), $data);
  79. } else {
  80. $this->error($this->auth->getError());
  81. }
  82. }
  83. /**
  84. * 用户登录
  85. * @return void
  86. * @throws \think\Exception
  87. * @throws \think\exception\DbException
  88. * @throws \think\exception\PDOException
  89. */
  90. public function userLogin()
  91. {
  92. $config = \app\api\model\service\ProjectConfigure::getProjectConfig();
  93. $type = input('type', '');
  94. if ($this->request->isPost()) {
  95. $post = $this->request->post();
  96. if (!isset($post['iv'])) {
  97. $this->error('参数获取异常');
  98. }
  99. if (!is_numeric($type)) {
  100. $this->error('参数缺失');
  101. }
  102. switch ($type) {
  103. case 0:
  104. $appid = $config['userappid'];
  105. $secret = $config['usersecret'];
  106. break;
  107. case 1:
  108. $appid = $config['skillappid'];
  109. $secret = $config['skillsecret'];
  110. break;
  111. case 2:
  112. $appid = $config['shopappid'];
  113. $secret = $config['shopsecret'];
  114. break;
  115. default:
  116. $appid = $config['userappid'];
  117. $secret = $config['usersecret'];
  118. }
  119. $params = [
  120. 'appid' => $appid,
  121. 'secret' => $secret,
  122. 'js_code' => $post['code'],
  123. 'grant_type' => 'authorization_code'
  124. ];
  125. $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
  126. $json = (array)json_decode($result['msg'], true);
  127. !array_key_exists('openid',$json) && $this->error('登录信息异常');
  128. if (array_key_exists('unionid',$json)) {
  129. $userInfo = \app\api\model\service\UserInfo::get(['unionid' => $json['unionid']]);
  130. }else{
  131. switch ($type)
  132. {
  133. case 0:
  134. $userInfo = \app\api\model\service\UserInfo::get(['user_openid' => $json['openid']]);
  135. break;
  136. case 1:
  137. $userInfo = \app\api\model\service\UserInfo::get(['skill_openid' => $json['openid']]);
  138. break;
  139. case 2:
  140. $userInfo = \app\api\model\service\UserInfo::get(['shop_openid' => $json['openid']]);
  141. break;
  142. }
  143. }
  144. if ($userInfo) {
  145. $userInfo->updatetime = time();
  146. if(!$userInfo->user_openid && $type == 0)
  147. {
  148. $userInfo->user_openid = $json['openid'];
  149. }elseif (!$userInfo->skill_openid && $type == 1)
  150. {
  151. $userInfo->skill_openid = $json['openid'];
  152. }elseif (!$userInfo->shop_openid && $type == 2){
  153. $userInfo->shop_openid = $json['openid'];
  154. }
  155. $userInfo->save();
  156. $ret = $this->auth->direct($userInfo->user_id);
  157. } else {
  158. $errCode = $this->decryptData($appid, $json['session_key'], $post['encryptedData'], urldecode($this->define_str_replace($post['iv'])), $data);
  159. if ($errCode == 0) {
  160. $data = (array)json_decode($data, true);
  161. $mobile = $data['phoneNumber'];
  162. $user = \app\common\model\User::getByMobile($mobile);
  163. if ($user) {
  164. if ($user->status != 'normal') {
  165. $this->error(__('Account is locked'));
  166. }
  167. $userInfo = \app\api\model\service\UserInfo::get(['user_id' => $user->id]);
  168. if($userInfo)
  169. {
  170. $userInfo = new \app\api\model\service\UserInfo();
  171. if(array_key_exists('unionid',$json))
  172. {
  173. $updateData['unionid'] = $json['unionid'];
  174. }
  175. if($userInfo->user_openid != $json['openid'] && $type == 0)
  176. {
  177. $updateData['user_openid'] = $json['openid'];
  178. }elseif ($userInfo->skill_openid != $json['openid'] && $type == 1)
  179. {
  180. $updateData['skill_openid'] = $json['openid'];
  181. }elseif ($userInfo->shop_openid != $json['openid'] && $type == 2){
  182. $updateData['shop_openid'] = $json['openid'];
  183. }
  184. $userInfo->save($updateData,['user_id'=> $user->id]);
  185. }
  186. $ret = $this->auth->direct($user->id);
  187. } else {
  188. $ret = $this->auth->register($mobile, $mobile, '', $mobile, ['avatar'=>\app\api\model\service\ProjectConfig::getProjectConfig('head_image')]);
  189. $uid = $this->auth->id;
  190. $newUser = ['user_id'=>$uid,'mobile'=>$mobile];
  191. if(array_key_exists('unionid',$json))
  192. {
  193. $newUser['unionid'] = $json['unionid'];
  194. }
  195. switch ($type)
  196. {
  197. case 0:
  198. $newUser['user_openid'] = $json['openid'];
  199. break;
  200. case 1:
  201. $newUser['skill_openid'] = $json['openid'];
  202. break;
  203. case 2:
  204. $newUser['shop_openid'] = $json['openid'];
  205. break;
  206. default:
  207. $newUser['user_openid'] = $json['openid'];
  208. }
  209. $userInfo = new UserInfo($newUser);
  210. $userInfo->allowField(true)->save();
  211. }
  212. } else {
  213. $this->error('获取信息失败');
  214. }
  215. }
  216. if ($ret) {
  217. $userInfo = ['user' => $this->auth->getUserinfo(),'userInfo'=>UserInfo::getInfo($this->auth->id)];
  218. $this->success(__('Logged in successful'), $userInfo);
  219. } else {
  220. $this->error($this->auth->getError());
  221. }
  222. }
  223. $this->error(__('非法请求'));
  224. }
  225. //避免把iv里面的空格转换为 +
  226. public function define_str_replace($data)
  227. {
  228. return str_replace(' ','+',$data);
  229. }
  230. /**
  231. * 微信信息解密
  232. * @param $appid
  233. * @param $sessionKey
  234. * @param $encryptedData
  235. * @param $iv
  236. * @param $data
  237. * @return int
  238. */
  239. public function decryptData($appid,$sessionKey, $encryptedData, $iv, &$data )
  240. {
  241. if (strlen($sessionKey) != 24) {
  242. return -41001;
  243. }
  244. $aesKey=base64_decode($sessionKey);
  245. if (strlen($iv) != 24) {
  246. return -41002;
  247. }
  248. $aesIV=base64_decode($iv);
  249. $aesCipher=base64_decode($encryptedData);
  250. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  251. $dataObj=json_decode( $result );
  252. if( $dataObj == NULL )
  253. {
  254. return -41003;
  255. }
  256. if( $dataObj->watermark->appid != $appid )
  257. {
  258. return -41004;
  259. }
  260. $data = $result;
  261. return 0;
  262. }
  263. }