User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 平台用户接口
  7. */
  8. namespace app\system\controller\api\v1;
  9. use app\system\controller\api\Base;
  10. use app\common\model\SystemUserAddress;
  11. use app\common\model\SystemUserLevel;
  12. use app\common\model\SystemUser;
  13. use app\common\facade\Alisms;
  14. use think\facade\Request;
  15. class User extends Base{
  16. public function initialize() {
  17. parent::initialize();
  18. $this->isUserAuth();
  19. }
  20. /**
  21. * 获取默认地址
  22. * @return void
  23. */
  24. public function getAddress(){
  25. $param['signkey'] = Request::param('signkey');
  26. $param['sign'] = Request::param('sign');
  27. $rel = $this->apiSign($param);
  28. if($rel['code'] != 200){
  29. return enjson(204,'签名失败');
  30. }
  31. $result = SystemUserAddress::field('name,telphone,is_first,address,id')->where(['user_id'=>$this->user->id,'is_first'=>1])->find();
  32. if(empty($result)){
  33. return enjson(204);
  34. }else{
  35. return enjson(200,$result);
  36. }
  37. }
  38. /**
  39. * 保存或修改我的地址
  40. */
  41. public function createAddress(){
  42. if(request()->isPost()){
  43. $param = [
  44. 'name' => Request::param('name/s'),
  45. 'telphone' => Request::param('telphone/s'),
  46. 'city' => Request::param('city/s'),
  47. 'address' => Request::param('address/s'),
  48. 'sign' => Request::param('sign/s'),
  49. ];
  50. $validate = $this->validate($param,'Address.add');
  51. if(true !== $validate){
  52. return json(['code'=>403,'msg'=>$validate]);
  53. }
  54. if(!empty($param['sign'])){
  55. $rel = $this->apiSign($param);
  56. if($rel['code'] != 200){
  57. return enjson(403,'签名失败');
  58. }
  59. }
  60. //把所有地址重置非默认
  61. $data['name'] = $param['name'];
  62. $data['telphone'] = $param['telphone'];
  63. $data['is_first'] = 1;
  64. $data['address'] = $param['city'].$param['address'];
  65. $data['user_id'] = $this->user->id;
  66. $data['member_miniapp_id'] = $this->miniapp_id;
  67. $data['create_time'] = time();
  68. $data['update_time'] = time();
  69. SystemUserAddress::where(['user_id' => $this->user->id])->update(['is_first' => 0]);
  70. $result = SystemUserAddress::insertGetId($data);
  71. if($result){
  72. $data['id'] = $result;
  73. return json(['code'=>200,'data'=>$data,'msg'=>'获取成功']);
  74. }else{
  75. return json(['code'=>403,'msg'=>'获取失败']);
  76. }
  77. }else{
  78. return json(['code'=>401,'msg'=>'用户认证失败']);
  79. }
  80. }
  81. /**
  82. * 绑定手机号验证码
  83. * @param int $user_id 用户ID
  84. * @param string $safepassword 验证的安全密码
  85. */
  86. public function bindPhoneNumber(){
  87. if (request()->isPost()) {
  88. $data = [
  89. 'phone' => Request::param('phone/s'),
  90. 'code' => Request::param('code/d'),
  91. ];
  92. $validate = $this->validate($data, 'User.bindphone');
  93. if (true !== $validate) {
  94. return json(['code'=>403,'msg'=>$validate]);
  95. }
  96. //判断验证码
  97. $is_sms = Alisms::isSms($data['phone'],$data['code']);
  98. if (!$is_sms) {
  99. return json(['code'=>403,'msg'=>"验证码错误"]);
  100. }
  101. //验证码通过
  102. $result = SystemUser::where(['id' =>$this->user->id])->update(['phone_uid' => $data['phone']]);
  103. if ($result) {
  104. return json(['code'=>200,'msg'=>'绑定成功']);
  105. }
  106. return json(['code'=>403,'msg'=>'绑定失败']);
  107. }
  108. }
  109. /**
  110. * 获取绑定手机号验证码
  111. * @param int $user_id 用户ID
  112. * @param string $safepassword 验证的安全密码
  113. */
  114. public function getPhoneCode(){
  115. if (request()->isPost()) {
  116. $data = [
  117. 'phone' => Request::param('phone/s'),
  118. 'types' => Request::param('types/d'),
  119. ];
  120. $validate = $this->validate($data, 'User.getphone');
  121. if (true !== $validate) {
  122. return json(['code'=>403,'msg'=>$validate]);
  123. }
  124. if ($data['types']) { //验证自己手机号
  125. if ($this->user->phone_uid != $data['phone']) {
  126. return json(['code'=>403,'msg'=>'不是您绑定的手机号']);
  127. }
  128. } else {
  129. //新绑定
  130. $rel = SystemUser::where(['member_miniapp_id'=>$this->miniapp_id,'phone_uid' => $data['phone']])->field('phone_uid')->find();
  131. if(!empty($rel)){
  132. return json(['code'=>403,'msg'=>'手机号已绑定']);
  133. }
  134. }
  135. $rel = Alisms::putSms($data['phone'],$this->miniapp->member_id);
  136. if ($rel['code'] == 200) {
  137. return json(['code'=>200,'msg'=>$rel['message'],'data'=>session_id()]);
  138. } else {
  139. return json(['code'=>403,'msg'=>$rel['message']]);
  140. }
  141. }
  142. }
  143. /**
  144. * 获取已绑定手机号验证码
  145. * @param int $user_id 用户ID
  146. * @param string $safepassword 验证的安全密码
  147. */
  148. public function getUserPhoneCode(){
  149. if(empty($this->user->phone_uid)){
  150. return json(['code'=>403,'msg'=>'你没有绑定手机号']);
  151. }
  152. $rel = Alisms::putSms($this->user->phone_uid,$this->miniapp->member_id);
  153. if($rel['code'] == 200){
  154. return json(['code'=>200,'msg'=>$rel['message'],'data'=>session_id()]);
  155. }else{
  156. return json(['code'=>403,'msg'=>$rel['message']]);
  157. }
  158. }
  159. /**
  160. * 获取已绑定手机号验证码
  161. * @param int $user_id 用户ID
  162. * @param string $safepassword 验证的安全密码
  163. */
  164. public function getFriendPhoneCode(){
  165. if (request()->isPost()) {
  166. $data['phone'] = Request::param('phone/s');
  167. $validate = $this->validate($data, 'User.getphone');
  168. if (true !== $validate) {
  169. return json(['code'=>403,'msg'=>$validate]);
  170. }
  171. if($this->user->phone_uid == $data['phone']){
  172. return json(['code'=>403,'msg'=>'非好友手机号']);
  173. }
  174. //查找所属用户
  175. $user = SystemUser::field('phone_uid')->where(['member_miniapp_id'=>$this->miniapp_id,'phone_uid' => $data['phone']])->find();
  176. if(empty($user)){
  177. return json(['code'=>403,'msg'=>'未找到当前用户']);
  178. }
  179. $rel = Alisms::putSms($user->phone_uid,$this->miniapp->member_id);
  180. if ($rel['code'] == 200) {
  181. return json(['code'=>200,'msg'=>$rel['message'],'data'=>session_id()]);
  182. } else {
  183. return json(['code'=>403,'msg'=>$rel['message']]);
  184. }
  185. }
  186. }
  187. /**
  188. * 验证是否绑定手机号
  189. * @param int $user_id 用户ID
  190. * @param string $safepassword 验证的安全密码
  191. */
  192. public function isBandPhone(){
  193. if(empty($this->user->phone_uid)){
  194. return json(['code'=>204,'msg'=>'未绑']);
  195. }
  196. return json(['code'=>200,'msg'=>'绑定']);
  197. }
  198. /**
  199. * 验证是否设置安全密码
  200. * @param int $user_id 用户ID
  201. * @param string $safepassword 验证的安全密码
  202. */
  203. public function isSafePassword(int $types = 0){
  204. if($types){
  205. if(!$this->user->phone_uid){
  206. return json(['code'=>302,'msg'=>'请先认证手机号','url'=>'/pages/helper/bindphone']);
  207. }
  208. }
  209. if($this->user->safe_password){
  210. return json(['code'=>200,'msg'=>'已设置安全密码']);
  211. }else{
  212. return json(['code'=>204,'msg'=>'未设置安全密码']);
  213. }
  214. }
  215. /**
  216. * 检查旧的安全密码
  217. */
  218. public function checkSafePassword(){
  219. if(request()->isPost()){
  220. $data = [
  221. 'safepassword' => Request::param('safepassword/s'),
  222. ];
  223. $validate = $this->validate($data,'User.safepassword');
  224. if(true !== $validate){
  225. return json(['code'=>403,'msg'=>$validate]);
  226. }
  227. if(password_verify(md5($data['safepassword']),$this->user->safe_password)) {
  228. return json(['code'=>200,'msg'=>'验证通过']);
  229. }else{
  230. return json(['code'=>403,'msg'=>'安全密码不正确']);
  231. }
  232. }
  233. }
  234. /**
  235. * 设置安全密码
  236. *
  237. * @return void
  238. */
  239. public function setSafePassword(){
  240. if(request()->isPost()){
  241. $data = [
  242. 'safepassword' => Request::param('safepassword/s'),
  243. 'password_confirm' => Request::param('resafepassword/s'),
  244. 'code' => Request::param('code/s'),
  245. ];
  246. $validate = $this->validate($data,'User.setSafePassword');
  247. if(true !== $validate){
  248. return json(['code'=>403,'msg'=>$validate]);
  249. }
  250. //判断安全密码是否正确
  251. $is_sms = Alisms::isSms($this->user->phone_uid,$data['code']);
  252. if (!$is_sms) {
  253. return json(['code'=>403,'msg'=>"验证码错误"]);
  254. }
  255. $result = SystemUser::updateSafePasspord($this->user->id,$data['safepassword']);
  256. if($result){
  257. return json(['code'=>200,'msg'=>'修改成功']);
  258. }else{
  259. return json(['code'=>403,'msg'=>'修改失败']);
  260. }
  261. }
  262. }
  263. /**
  264. * 读取我的推荐用户(只显示两层关系)
  265. * @return void
  266. */
  267. public function levelUser(){
  268. $info = SystemUserLevel::levelUser($this->user->id,1);
  269. if($info){
  270. return json(['code'=>200,'msg'=>'成功','data' => $info]);
  271. }
  272. return json(['code'=>204,'msg'=>'空内容']);
  273. }
  274. /**
  275. * 获取邀请码的用户信息
  276. * @return void
  277. */
  278. public function getCodeUser(){
  279. return $this->getUCodeUser();
  280. }
  281. }